Beispiel #1
0
        void acceptAsyncEvent_Completed(object sender, SocketAsyncEventArgs e)
        {
            Socket socket = null;

            try
            {
                socket = e.AcceptSocket;
            }
            catch (Exception ex)
            {
                if (socket != null)
                {
                    try
                    {
                        socket.Close();
                    }
                    catch
                    { }
                }

                log.Error("获取客户端Socket连接错误.", ex);
                m_socket.Disconnect();
                return;
            }

            m_socket.ListenAcceptSocket(socket);

            acceptAsync(m_socket.Socket, m_acceptAsyncEvent);
        }
Beispiel #2
0
        public void SendTcp(Packet pkg)
        {
            if (pkg == null)
            {
                return;
            }

            if (!pkg.Writed)
            {
                pkg.WriteHeader();
                pkg.WriteData();
                pkg.Pack();
            }

            if (pkg.Length <= 0)
            {
                return;
            }

            if (m_socket.Socket.Connected == false)
            {
                return;
            }

            try
            {
                lock (m_pkgQueue.SyncRoot)
                {
                    m_pkgQueue.Enqueue(pkg);

                    if (m_sendingTcp)
                    {
                        return;
                    }

                    m_sendingTcp = true;
                }

                if (m_socket.EnableAsyncSend)
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(sendAsyncImp), this);
                }
                else
                {
                    sendAsyncEvent_Completed(this, m_sendAsyncEvent);
                }
            }
            catch (Exception ex)
            {
                log.Error("数据包发送失败.", ex);
                m_socket.Disconnect();
            }
        }
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
        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 #5
0
        void receiveAsyncEvent_Completed(object sender, SocketAsyncEventArgs e)
        {
            try
            {
                lock (this)
                {
                    //if (m_policy == false && m_buffer[0] == '<')
                    //{
                    //    //m_policy = true;
                    //    m_socket.Socket.Send(POLICY);
                    //    log.Info(Marshal.ToHexDump("===============跨域包数据===============", m_buffer, 0, 23));
                    //    return;
                    //}

                    if (e.BytesTransferred > 0)
                    {
                        //m_policy = true;

                        #region 读取数据包

                        int bufferSize = m_dataRemain + e.BytesTransferred;

                        if (bufferSize < ConfigPkg.HeaderSize)
                        {
                            m_dataRemain = bufferSize;
                            return;
                        }

                        m_dataRemain = 0;

                        int curOffset = 0;
                        int dataLen   = bufferSize;

                        int pkgLen = 0;
                        //int header = 0;

                        while (dataLen >= ConfigPkg.HeaderSize)
                        {
                            //pkgLen = 0;

                            //while ((curOffset + ConfigPkg.HeaderSize) <= bufferSize)
                            //{
                            //    bytes[0] = m_buffer[curOffset];
                            //    bytes[1] = m_buffer[curOffset + 1];

                            //    m_cryptor.Decrypt(bytes);

                            //    header = bytes[0] << 8;
                            //    header += bytes[1];

                            //    if ((short)header == ConfigPkg.Header)
                            //    {
                            //        bytes[0] = m_buffer[curOffset + ConfigPkg.PkgLenOffset];
                            //        bytes[1] = m_buffer[curOffset + ConfigPkg.PkgLenOffset + 1];

                            //        m_cryptor.Decrypt(bytes);

                            //        pkgLen = bytes[0] << 8;
                            //        pkgLen += bytes[1];
                            //        break;
                            //    }
                            //    else
                            //    {
                            //        curOffset++;
                            //    }
                            //}

                            bytes[0] = m_buffer[curOffset + ConfigPkg.PkgLenOffset];
                            bytes[1] = m_buffer[curOffset + ConfigPkg.PkgLenOffset + 1];

                            m_cryptor.Decrypt(bytes);

                            pkgLen  = bytes[0] << 8;
                            pkgLen += bytes[1];

                            if (pkgLen <= 0 || pkgLen > ConfigPkg.BufferSize || pkgLen < ConfigPkg.HeaderSize)
                            {
                                log.ErrorFormat("读取的数据包长:{0}  包头长:{1}  读取时的当前游标:{2}  当前总数据流长度:{3}  当前收到数据流长度:{4}  来自:{5}", pkgLen, ConfigPkg.HeaderSize, curOffset, bufferSize, e.BytesTransferred, m_socket.RemoteEndPoint);
                                log.Error(Marshal.ToHexDump("=======================包数据==========================", m_buffer));

                                m_socket.Disconnect();
                                return;
                            }

                            dataLen = bufferSize - curOffset;

                            if (dataLen >= pkgLen)
                            {
                                Packet pkg = m_socket.UsedPacket;
                                pkg.CopyFrom(m_buffer, curOffset, 0, pkgLen);
                                m_cryptor.Decrypt(pkg.Buffer, pkgLen);
                                m_cryptor.Update();
                                pkg.ReadHeader();

                                if (log.IsInfoEnabled)
                                {
                                    log.Info(Marshal.ToHexDump("Recieve Package:", pkg.Buffer, 0, pkgLen));
                                }

                                curOffset += pkgLen;
                                dataLen    = bufferSize - curOffset;

                                m_socket.ReceivePkg(pkg);
                            }
                            else
                            {
                                break;
                            }
                        }

                        if (dataLen > 0)
                        {
                            Array.Copy(m_buffer, curOffset, m_buffer, 0, dataLen);
                            m_dataRemain = dataLen;
                        }

                        #endregion
                    }
                    else
                    {
                        log.InfoFormat("The client of '{0}' disconnected!", m_socket.RemoteEndPoint);
                        m_socket.Disconnect();
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error("数据包解析出错.", ex);
                m_socket.Disconnect();
            }
            finally
            {
                AsyncReceiveImp();
            }
        }