/// <summary> /// Constructor /// </summary> /// <param name="srvr">Pointer to the server the client is connected to</param> public BaseClient(byte[] readBuffer,byte[] sendBuffer) { m_readBuffer = readBuffer; m_sendBuffer = sendBuffer; m_readBufEnd = 0; rc_event = new SocketAsyncEventArgs(); rc_event.Completed += RecvEventCallback; m_processor = new StreamProcessor(this); m_encryted = false; m_strict = true; }
/// <summary> /// Callback method for async sends /// </summary> /// <param name="ar"></param> private static void AsyncTcpSendCallback(object sender, SocketAsyncEventArgs e) { StreamProcessor proc = (StreamProcessor)e.UserToken; BaseClient client = proc.m_client; try { Queue q = proc.m_tcpQueue; if (q == null || !client.Socket.Connected) { return; } int sent = e.BytesTransferred; byte[] data = proc.m_tcpSendBuffer; int count = 0; if (sent != e.Count) { //log.Error("Count:" + e.Count + ",sent:" + sent + ",offset:" + e.Offset + ",m_sendBufferLength:" + proc.m_sendBufferLength + ",client:" + client.TcpEndpoint); if (proc.m_sendBufferLength > sent) { count = proc.m_sendBufferLength - sent; Array.Copy(data, sent, data, 0, count); } } e.SetBuffer(0, 0); int firstOffset = proc.m_firstPkgOffset; lock (q.SyncRoot) { if (q.Count > 0) { do { PacketIn pak = (PacketIn)q.Peek(); int len = 0; if (client.Encryted) { int key = proc.send_fsm.getState(); //len = pak.CopyTo(data, count, firstOffset,key); len = pak.CopyTo3(data, count, firstOffset, client.SEND_KEY, ref client.numPacketProcces); //if (pak.m_sended == 0) //{ // log.Info("KeySendKey" + PrintArray(client.SEND_KEY)); // log.Info("Packet" + PrintArray(data, count, 8)); // log.Info(""); //} } else { len = pak.CopyTo(data, count, firstOffset); } firstOffset += len; count += len; if (pak.Length <= firstOffset) { q.Dequeue(); firstOffset = 0; if (client.Encryted) { proc.send_fsm.UpdateState(); // log.Info("Update KEy"); //client.numPacketProcces+=1; pak.isSended = true; } } if (data.Length == count) { //pak.isSended = true; break; } } while (q.Count > 0); } proc.m_firstPkgOffset = firstOffset; if (count <= 0) { proc.m_sendingTcp = false; return; } } proc.m_sendBufferLength = count; e.SetBuffer(0, count); if (client.SendAsync(e) == false) { AsyncTcpSendCallback(sender, e); } } catch (Exception ex) { log.Error("AsyncTcpSendCallback", ex); log.WarnFormat("It seems <{0}> went linkdead. Closing connection. (SendTCP, {1}: {2})", client, ex.GetType(), ex.Message); client.Disconnect(); } }
private static void AsyncTcpSendCallback(object sender, SocketAsyncEventArgs e) { StreamProcessor streamProcessor = (StreamProcessor)e.UserToken; BaseClient client = streamProcessor.m_client; try { Queue tcpQueue = streamProcessor.m_tcpQueue; if (tcpQueue != null && client.Socket.Connected) { int bytesTransferred = e.BytesTransferred; byte[] tcpSendBuffer = streamProcessor.m_tcpSendBuffer; int num = 0; if (bytesTransferred != e.Count) { if (streamProcessor.m_sendBufferLength > bytesTransferred) { num = streamProcessor.m_sendBufferLength - bytesTransferred; Array.Copy(tcpSendBuffer, bytesTransferred, tcpSendBuffer, 0, num); } } e.SetBuffer(0, 0); int num2 = streamProcessor.m_firstPkgOffset; object syncRoot; Monitor.Enter(syncRoot = tcpQueue.SyncRoot); try { if (tcpQueue.Count > 0) { do { PacketIn packetIn = (PacketIn)tcpQueue.Peek(); byte[] array = null; int num3; if (client.Encryted) { array = StreamProcessor.cloneArrary(client.SEND_KEY, 8); num3 = packetIn.CopyTo(tcpSendBuffer, num, num2, ref array); } else { num3 = packetIn.CopyTo(tcpSendBuffer, num, num2); } num2 += num3; num += num3; if (packetIn.Length <= num2) { if (array != null && array != client.SEND_KEY) { client.SEND_KEY = StreamProcessor.cloneArrary(array, 8); } tcpQueue.Dequeue(); num2 = 0; if (client.Encryted) { streamProcessor.send_fsm.UpdateState(); packetIn.isSended = true; } } if (tcpSendBuffer.Length == num) { break; } }while (tcpQueue.Count > 0); } streamProcessor.m_firstPkgOffset = num2; if (num <= 0) { streamProcessor.m_sendingTcp = false; return; } } finally { Monitor.Exit(syncRoot); } streamProcessor.m_sendBufferLength = num; e.SetBuffer(0, num); if (!client.SendAsync(e)) { StreamProcessor.AsyncTcpSendCallback(sender, e); } } } catch (Exception ex) { StreamProcessor.log.Error("AsyncTcpSendCallback", ex); StreamProcessor.log.WarnFormat("It seems <{0}> went linkdead. Closing connection. (SendTCP, {1}: {2})", client, ex.GetType(), ex.Message); client.Disconnect(); } }
public void ReceiveBytes(int numBytes) { Monitor.Enter(this); try { byte[] packetBuf = this.m_client.PacketBuf; int num = this.m_client.PacketBufSize + numBytes; if (num < 20) { this.m_client.PacketBufSize = num; } else { this.m_client.PacketBufSize = 0; int num2 = 0; int num3; int num5; while (true) { num3 = 0; if (this.m_client.Encryted) { int count = this.receive_fsm.count; byte[] param = StreamProcessor.cloneArrary(this.m_client.RECEIVE_KEY, 8); while (num2 + 4 < num) { byte[] array = StreamProcessor.decryptBytes(packetBuf, num2, 8, param); int num4 = ((int)array[0] << 8) + (int)array[1]; if (num4 == 29099) { num3 = ((int)array[2] << 8) + (int)array[3]; break; } num2++; } } else { while (num2 + 4 < num) { int num4 = ((int)packetBuf[num2] << 8) + (int)packetBuf[num2 + 1]; if (num4 == 29099) { num3 = ((int)packetBuf[num2 + 2] << 8) + (int)packetBuf[num2 + 3]; break; } num2++; } } if ((num3 != 0 && num3 < 20) || num3 > 16869) { break; } num5 = num - num2; if (num5 < num3 || num3 == 0) { goto Block_11; } GSPacketIn gSPacketIn = new GSPacketIn(new byte[16869], 16869); if (this.m_client.Encryted) { gSPacketIn.CopyFrom3(packetBuf, num2, 0, num3, this.m_client.RECEIVE_KEY); } else { gSPacketIn.CopyFrom(packetBuf, num2, 0, num3); } gSPacketIn.ReadHeader(); StreamProcessor.log.Debug(Marshal.ToHexDump("Recieve Packet:", gSPacketIn.Buffer, 0, num3)); try { this.m_client.OnRecvPacket(gSPacketIn); } catch (Exception exception) { if (StreamProcessor.log.IsErrorEnabled) { StreamProcessor.log.Error("HandlePacket(pak)", exception); } } num2 += num3; if (num - 1 <= num2) { goto IL_332; } } StreamProcessor.log.Error(string.Concat(new object[] { "packetLength:", num3, ",GSPacketIn.HDR_SIZE:", 20, ",offset:", num2, ",bufferSize:", num, ",numBytes:", numBytes })); StreamProcessor.log.ErrorFormat("Err pkg from {0}:", this.m_client.TcpEndpoint); StreamProcessor.log.Error(Marshal.ToHexDump("===> error buffer", packetBuf)); this.m_client.PacketBufSize = 0; if (this.m_client.Strict) { this.m_client.Disconnect(); goto IL_228; } goto IL_228; Block_11: Array.Copy(packetBuf, num2, packetBuf, 0, num5); this.m_client.PacketBufSize = num5; IL_332: if (num - 1 == num2) { packetBuf[0] = packetBuf[num2]; this.m_client.PacketBufSize = 1; } IL_228 :; } } finally { Monitor.Exit(this); } }
private static void AsyncTcpSendCallback(object sender, SocketAsyncEventArgs e) { StreamProcessor proc = (StreamProcessor)e.UserToken; BaseClient client = proc.m_client; Queue q = proc.m_tcpQueue; if (q != null && client.Socket.Connected) { int plen = 0; int blen = 0; int count = 0; int firstOffset = 0; int sent = e.BytesTransferred; byte[] data = proc.m_tcpSendBuffer; try { if (e.Count != 0 && e.Count != sent && e.SocketError == SocketError.Success) { if (e.Count > sent) { count = e.Count - sent; Array.Copy(data, sent, data, 0, count); } } proc.m_lastSent = e.BytesTransferred; e.SetBuffer(0, 0); object syncRoot; Monitor.Enter(syncRoot = q.SyncRoot); try { firstOffset = proc.m_firstPkgOffset; if (q.Count > 0) { do { PacketIn pak = (PacketIn)q.Peek(); plen = pak.Length; blen = pak.Buffer.Length; int len; if (client.Encryted) { int key = proc.send_fsm.getState(); len = pak.CopyTo(data, count, firstOffset, key + firstOffset); } else { len = pak.CopyTo(data, count, firstOffset); } firstOffset += len; count += len; if (pak.Length <= firstOffset) { q.Dequeue(); firstOffset = 0; if (client.Encryted) { proc.send_fsm.UpdateState(); } } if (data.Length == count) { break; } }while (q.Count > 0); } proc.m_firstPkgOffset = firstOffset; if (count <= 0) { proc.m_sendingTcp = false; return; } } finally { Monitor.Exit(syncRoot); } e.SetBuffer(0, count); e.SocketError = SocketError.SocketError; int start = Environment.TickCount; //if (StreamProcessor.log.IsDebugEnabled) //{ //StreamProcessor.log.Debug(string.Format("Send To ({0}) {1} bytes", client.TcpEndpoint, e.Count)); //} if (!client.Socket.SendAsync(e)) { e.SetBuffer(0, 0); StreamProcessor.AsyncTcpSendCallback(sender, e); } int took = Environment.TickCount - start; if (took > 100) { StreamProcessor.log.WarnFormat("AsyncTcpSendCallback.BeginSend took {0}ms! (TCP to client: {1})", took, client.TcpEndpoint); } } catch (Exception ex) { StreamProcessor.log.Error("AsyncTcpSendCallback", ex); StreamProcessor.log.ErrorFormat("First_offset:{0},Count:{1},data:{2},package len:{3} buffer len:{4}", new object[] { firstOffset, count, data.Length, plen, blen }); StreamProcessor.log.WarnFormat("It seems <{0}> went linkdead. Closing connection. (SendTCP, {1}: {2})", client, ex.GetType(), ex.Message); client.Disconnect(); } } }