well, i just checked it using notepad since i don't have my laptop rigth now(hard to look in details without an IDE :c) but the first thing i saw was that you are creating an infinite loop in a thread from the ThreadPool, try to avoid this since that pool is supposed to be used by other .net applications too, not that -1 thread could cause a big impact, but it's a bad practice.
you are also using a thread-safe wrapper for the Dictionary class, when u could use a ConcurrentDictionary, i haven't tested yet but i suppose that ConcurrentDictionary only need to 'lock' when a remove or a loop is being executed, while the wrapper of the dictionary needs to lock when you are adding a pair too.
I saw that u are using a Stopwatch to do some checks, it's something i have thougth a lot too since DateTime lacks of precision, have not found anything against it :p
I kinda dislike too(just personal opinion)the way that u have splitted the packets in ToReceive and ToSend when could have only one class with the methods needed, it looks a bit disorganized(if exists that word O.o), for example this is how I LIKE, emphasis in the I xd to have the packets
Code:
class AuthorizationResponse : IServerPacket
{
private static readonly NetPacketHeader Header = new NetPacketHeader()
{
Length = 32,
ID = 1055
};
public uint UID;
public AccountPermission Authorization;
public string MsgServerIP = "SERVER_IP";//configuration
public int MsgServerPort = SERVER_PORT;//configuration
public NetPacket Serialize()
{
NetPacket packet = new NetPacket(Header, false);
packet.WriteUInt32(UID);
packet.WriteUInt32((uint)Authorization);
packet.WriteString(MsgServerIP, 16);
packet.WriteInt32(MsgServerPort);
return packet;
}
}
IServerPacket is an interface that make the packet's class have a Serialize method, or what is your ToSend folder, or something like that xd.
i also have an interface called IClientPacket, that will be the ToReceive folder idea too, with the methods Deserialize and Process like this
Code:
public class AuthorizationRequest : IClientPacket
{
public string Account;
public string Password;
public string ServerName;
public void Deserialize(NetPacket packet)
{
packet.Seek(4, SeekOrigin.Begin);
Account = packet.ReadString(16).TrimEnd('\0', ' ');
Password = packet.ReadString(16).TrimEnd('\0', ' ');
ServerName = packet.ReadString(16).TrimEnd('\0', ' ');
packet.Dispose();
}
public void Process(object sender)
{
AuthClient client = sender as AuthClient;
IServerPacket response = new AuthorizationResponse();
try
{
using (MySqlReader reader = new MySqlReader())
{
reader.Select("accounts");
reader.WhereMatch("account", Account);
reader.Execute();
string realPassword = PasswordCryptography.EncryptPassword(reader.ReadString("password"));
client.UID = reader.ReadUInt32("uid");
response.UID = client.UID;
if (LoginExploits.AccountClients.ContainsKey(response.UID))
client.Disconnect();
AccountPermission permission = (AccountPermission)reader.ReadByte("state");
switch (permission)
{
case AccountPermission.Moderator:
case AccountPermission.ProjectManager:
case AccountPermission.Normal:
response.Authorization = AccountPermission.Normal;
break;
case AccountPermission.Banned:
case AccountPermission.Unactivated:
response.Authorization = permission;
break;
default:
response.Authorization = AccountPermission.Error;
break;
}
if (response.Authorization == AccountPermission.Normal)
{
if(realPassword != Password)
response.Authorization = AccountPermission.Invalid;
}
LoginExploits.AccountClients.Add(client.UID, client);
}
}
catch(Exception e)
{
Console.WriteLine(e);
response.Authorization = AccountPermission.Invalid;
}
client.Send(response);
}
}
maybe not the best code, but an example of the 'concept' behind how i structure my packets.
hope helped a little bit :)