added login function

This commit is contained in:
Alexander Eckart committed 2013-05-04 20:28:32 +02:00
1 parent 7f71853672
commit 1147d612fe
13 files changed
+249 -23

No files matched your search

@@ -33,12 +33,6 @@ namespace ClientServerLib
get { return connected; }
}
public bool LoggedIn
{
get { return connection.LoggedIn; }
set { connection.LoggedIn = value; }
}
public CClient()
{
doConnect = false;
@@ -18,7 +18,6 @@ namespace ClientServerLib
private int connectionID;
public TcpClient TcpClient;
public bool LoggedIn;
public bool KeySet
{
@@ -34,7 +33,6 @@ namespace ClientServerLib
{
TcpClient = client;
keySet = false;
LoggedIn = false;
connectionID = ConnectionID;
encoder = new UTF8Encoding();
}
+24 -13
View File
@@ -73,28 +73,39 @@ namespace ClientServerLib
private void ListenForClients()
{
tcpListener.Start();
TcpClient client;
while (running)
{
//wait for new client
TcpClient client = tcpListener.AcceptTcpClient();
int id = ids.Dequeue();
CConnection connection = new CConnection(client, id);
lock (clients)
try
{
clients.Add(id, connection);
client = tcpListener.AcceptTcpClient();
}
catch
{
client = null;
}
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientCom));
if (client != null)
{
int id = ids.Dequeue();
CConnection connection = new CConnection(client, id);
NetworkStream clientStream = client.GetStream();
byte[] serverParams = connection.GetKeyParams();
clientStream.Write(serverParams, 0, serverParams.Length);
clientStream.Flush();
lock (clients)
{
clients.Add(id, connection);
}
clientThread.Start(connection);
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientCom));
NetworkStream clientStream = client.GetStream();
byte[] serverParams = connection.GetKeyParams();
clientStream.Write(serverParams, 0, serverParams.Length);
clientStream.Flush();
clientThread.Start(connection);
}
}
}
@@ -48,6 +48,8 @@
<Compile Include="DeffieHellman\StrongNumberProvider.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Structs.cs" />
<Compile Include="Vocaluxe\CCommands.cs" />
<Compile Include="Vocaluxe\VocaluxeStructs.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
@@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Security.Cryptography;
namespace Vocaluxe.Base.Server
{
public static class CCommands
{
public static SHA256Managed SHA256 = new SHA256Managed();
public const int CommandLogin = 20;
public const int ResponseLoginWrongPassword = 21;
public const int ResponseLoginFailed = 22;
public const int ResponseLoginOK = 23;
public static byte[] CreateCommandWithoutParams(int Command)
{
return BitConverter.GetBytes(Command);
}
/*
public static byte[] CreateResponseSendUserNames(SUsers Users)
{
return Serialize<SUsers>(ResponseSendUserNames, Users);
}
public static SUsers DecodeResponseSendUserNames(byte[] response)
{
SUsers users;
TryDeserialize<SUsers>(response, out users);
return users;
}
*/
public static byte[] CreateCommandLogin(string Password)
{
SLoginData data = new SLoginData();
data.SHA256 = SHA256.ComputeHash(Encoding.UTF8.GetBytes(Password));
return Serialize<SLoginData>(CommandLogin, data);
}
public static bool ResponseCommandLogin(byte[] Message, out SLoginData LoginData)
{
return TryDeserialize<SLoginData>(Message, out LoginData);
}
private static byte[] Serialize<T>(int Command, T obj)
{
byte[] command = BitConverter.GetBytes(Command);
MemoryStream stream = new MemoryStream();
stream.Write(command, 0, command.Length);
byte[] data;
using (MemoryStream ms = new MemoryStream())
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
data = ms.ToArray();
}
stream.Write(data, 0, data.Length);
return stream.ToArray();
}
private static bool TryDeserialize<T>(byte[] message, out T obj)
{
obj = default(T);
if (message == null)
return false;
if (message.Length < 5)
return false;
byte[] data = new byte[message.Length - 4];
Array.Copy(message, 4, data, 0, data.Length);
using (MemoryStream ms = new MemoryStream(data))
{
try
{
BinaryFormatter formatter = new BinaryFormatter();
obj = (T)formatter.Deserialize(ms);
return true;
}
catch (Exception)
{
return false;
}
}
}
}
}
@@ -0,0 +1,10 @@
using System;
namespace Vocaluxe.Base.Server
{
[Serializable]
public struct SLoginData
{
public byte[] SHA256;
}
}