public class ClientConnection : IClientConnection
{
private readonly Logger Log = Logger.Instance;
byte[] buffer = new byte[2048];
NetworkStream Stream { get; set; }
public int Id { get; set; }
public byte[] RemoteIPAddress { get; set; }
public int RemoteUdpPort { get; set; }
public ClientConnection(TcpClient client)
{
Stream = client.GetStream();
// PROTOCOL_BASE_CONNECT_ACK
SendPacket(new PROTOCOL_BASE_CONNECT_ACK());
Log.Info("New connected");
Stream.BeginRead(buffer, 0, buffer.Length, BeginRead, null);
}
public void BeginRead(IAsyncResult asyncResult)
{
int received = Stream.EndRead(asyncResult);
Log.Info("Received = {0}", received);
while (received >= 6)
{
int length = BitConverter.ToUInt16(buffer, 0) & 0x7FFF;
byte[] temp = new byte[length + 2];
Array.Copy(buffer, 2, temp, 0, temp.Length);
int bits = Id % 7 + 1;
BitShift.Unshift(temp, bits);
byte[] opcode = new byte[] { temp[0], temp[1] };
RecvOpcode packet = (RecvOpcode)BitConverter.ToUInt16(opcode, 0);
Type t = Type.GetType("PiercingBlow.Login.Network.Recv." + packet.ToString());
Log.Info("PacketId = {0} Lenght = {1}", BitConverter.ToUInt16(opcode, 0), temp.Length);
if (t != null)
{
ClientPacket clientpacket = (ClientPacket)Activator.CreateInstance(t);
clientpacket.Client = this;
clientpacket.Process(buffer);
}
else
{
Log.Trace(temp.ToHex());
}
received -= length + 4;
Array.Copy(buffer, length + 4, buffer, 0, received); // << Копируем оставшиеся данные в начало буфера
}
Stream.BeginRead(buffer, 0, buffer.Length, BeginRead, null);
}
public void SendPacket(ServerPacket packet)
{
packet.Client = this;
packet.WriteImpl();
byte[] data = packet.ToByteArray();
byte[] lenght = BitConverter.GetBytes((short)data.Length);
byte[] opcode = BitConverter.GetBytes(Convert.ToInt16((int)Enum.Parse(typeof(SendOpcode), packet.GetType().Name)));
byte[] buff = new byte[data.Length + 4];
Array.Copy(lenght, 0, buff, 0, lenght.Length);
Array.Copy(opcode, 0, buff, 2, opcode.Length);
Array.Copy(data, 0, buff, 4, data.Length);
Stream.Write(buff, 0, buff.Length);
Log.Debug("Send {0} bytes", buff.Length);
}
}
}