Exemplo n.º 1
0
        public JSocket(byte[] sBuffer, byte[] rBuffer)
        {
            EnableAsyncSend = true;

            HadTryTimes = 0;
            CanTryTimes = 3;

            SendBuffer = sBuffer;
            ReceiveBuffer = rBuffer;

            m_sender = new PacketSender(this, SendBuffer);
            m_reader = new PacketReader(this, ReceiveBuffer);
        }
Exemplo n.º 2
0
        private static void sendAsyncEvent_Completed(object sender, SocketAsyncEventArgs e)
        {
            PacketSender ps     = (PacketSender)e.UserToken;
            JSocket      socket = ps.m_socket;
            Queue        q      = ps.m_pkgQueue;

            byte[]         cryptorBuffer = ps.m_cryptorBuffer;
            IPacketCryptor cryptor       = ps.m_cryptor;
            int            sended        = e.BytesTransferred;

            byte[] data  = ps.m_buffer;
            int    count = ps.m_sendingLength - sended;

            if (count > 0)
            {
                Array.Copy(data, sended, data, 0, count);
            }
            else
            {
                count = 0;
            }

            e.SetBuffer(0, 0);

            int dataOffset = ps.m_lastPkgReadOffset;

            try
            {
                lock (q.SyncRoot)
                {
                    while (q.Count > 0)
                    {
                        Packet pkg = (Packet)q.Peek();

                        int len = 0;

                        if (dataOffset == 0)
                        {
                            System.Buffer.BlockCopy(pkg.Buffer, 0, cryptorBuffer, 0, pkg.Length);
                            cryptor.Encrypt(cryptorBuffer, pkg.Length);
                        }

                        len = ByteArray.CopyTo(cryptorBuffer, dataOffset, data, count, pkg.Length);

                        dataOffset += len;
                        count      += len;

                        if (pkg.Length <= dataOffset)
                        {
                            q.Dequeue();
                            dataOffset = 0;
                            //cryptor.Decrypt(pkg.Buffer, pkg.Length);
                            cryptor.Update();
                        }

                        if (data.Length == count)
                        {
                            break;
                        }
                    }

                    ps.m_lastPkgReadOffset = dataOffset;

                    if (count <= 0)
                    {
                        ps.m_sendingTcp = false;
                        return;
                    }
                }

                ps.m_sendingLength = count;

                e.SetBuffer(0, count);

                if (socket.Socket.SendAsync(e) == false)
                {
                    sendAsyncEvent_Completed(sender, e);
                }
            }
            catch (Exception ex)
            {
                log.Error("Async sending package error.", ex);
                socket.Disconnect();
            }
        }