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(); } }
/// <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 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(); } } }