Beispiel #1
0
        public PacketReader(JSocket socket, byte[] buffer)
        {
            m_socket = socket;
            m_buffer = buffer;

            ConfigPkg = m_socket.UsedPacket;
            m_cryptor = m_socket.ReceiveCryptor;
            if (m_cryptor == null) m_cryptor = new NoneCryptor();

            m_receiveAsyncEvent = new SocketAsyncEventArgs();

            m_receiveAsyncEvent.Completed += new EventHandler<SocketAsyncEventArgs>(receiveAsyncEvent_Completed);
        }
Beispiel #2
0
        public PacketSender(JSocket socket, byte[] buffer)
        {
            m_socket = socket;
            m_buffer = buffer;
            m_pkgQueue = new Queue();
            m_cryptorBuffer = new byte[m_buffer.Length];

            m_cryptor = m_socket.SendCryptor;
            if (m_cryptor == null) m_cryptor = new NoneCryptor();

            m_sendAsyncEvent = new SocketAsyncEventArgs();
            m_sendAsyncEvent.UserToken = this;
            m_sendAsyncEvent.SetBuffer(m_buffer, 0, 0);
            m_sendAsyncEvent.Completed += new EventHandler<SocketAsyncEventArgs>(sendAsyncEvent_Completed);
        }
Beispiel #3
0
        private static void sendAsyncImp(object state)
        {
            PacketSender ps     = state as PacketSender;
            JSocket      socket = ps.m_socket;

            try
            {
                sendAsyncEvent_Completed(socket.Socket, ps.m_sendAsyncEvent);
            }
            catch (Exception ex)
            {
                log.Error("Async send package error.", ex);
                socket.Disconnect();
            }
        }
Beispiel #4
0
        public PacketReader(JSocket socket, byte[] buffer)
        {
            m_socket = socket;
            m_buffer = buffer;

            ConfigPkg = m_socket.UsedPacket;
            m_cryptor = m_socket.ReceiveCryptor;
            if (m_cryptor == null)
            {
                m_cryptor = new NoneCryptor();
            }

            m_receiveAsyncEvent = new SocketAsyncEventArgs();

            m_receiveAsyncEvent.Completed += new EventHandler <SocketAsyncEventArgs>(receiveAsyncEvent_Completed);
        }
Beispiel #5
0
        public PacketSender(JSocket socket, byte[] buffer)
        {
            m_socket        = socket;
            m_buffer        = buffer;
            m_pkgQueue      = new Queue();
            m_cryptorBuffer = new byte[m_buffer.Length];

            m_cryptor = m_socket.SendCryptor;
            if (m_cryptor == null)
            {
                m_cryptor = new NoneCryptor();
            }

            m_sendAsyncEvent           = new SocketAsyncEventArgs();
            m_sendAsyncEvent.UserToken = this;
            m_sendAsyncEvent.SetBuffer(m_buffer, 0, 0);
            m_sendAsyncEvent.Completed += new EventHandler <SocketAsyncEventArgs>(sendAsyncEvent_Completed);
        }
Beispiel #6
0
 public SocketAccepter(JSocket socket)
 {
     m_socket = socket;
 }
Beispiel #7
0
 public SocketAccepter(JSocket socket)
 {
     m_socket = socket;
 }
Beispiel #8
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();
            }
        }
Beispiel #9
0
 void m_socket_ConnectedSuccess(JSocket socket)
 {
     OnConnected(true);
 }
Beispiel #10
0
 void m_socket_ConnectedFaild(JSocket socket)
 {
     OnConnected(false);
 }
Beispiel #11
0
 public SocketConnecter(JSocket socket)
 {
     m_socket = socket;
 }