Beispiel #1
0
        /// <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();
            }
        }