示例#1
0
        /// <summary>
        /// 接收数据通知函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private bool SocketReceived(object sender, SocketAsyncEventArgs e)
        {
            SocketListener sl          = sender as SocketListener;
            TCPInPacket    tcpInPacket = null;
            Socket         s           = (e.UserToken as AsyncUserToken).CurrentSocket;

            lock (dictInPackets) //锁定接收包队列
            {
                if (!dictInPackets.TryGetValue(s, out tcpInPacket))
                {
                    tcpInPacket      = tcpInPacketPool.Pop(s, TCPCmdPacketEvent);
                    dictInPackets[s] = tcpInPacket;
                }
            }

            //处理收到的包
            if (!tcpInPacket.WriteData(e.Buffer, e.Offset, e.BytesTransferred))
            {
                //LogManager.WriteLog(LogTypes.Error, string.Format("接收到非法数据长度的tcp命令, 需要立即端断开!"));
                //socketListener.CloseSocket(tcpInPacket.CurrentSocket);
                return(false);
            }

            return(true);
        }
示例#2
0
        private void SocketReceived(IAsyncResult iar)
        {
            if (null == _Socket)
            {
                return;
            }
            try
            {
                SocketError socketError = SocketError.Success;
                int         recvLength  = _Socket.EndReceive(iar, out socketError);
                byte[]      bytesData   = iar.AsyncState as byte[];

                if (recvLength <= 0)
                {
                    return;
                }
                //处理收到的包
                if (!_MyTCPInPacket.WriteData(bytesData, 0, recvLength))
                {
                    //TODO 处理包异常通知
                    return;
                }

                if (_Socket.Connected)
                {
                    _Socket.BeginReceive(bytesData, 0, bytesData.Length, SocketFlags.None, new AsyncCallback(SocketReceived), bytesData);
                }
            }
            catch (Exception e)
            {
                MainWindow.GetInstance().ShowText(e.ToString());
            }
        }
示例#3
0
        /// <summary>
        /// 接收数据通知函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private bool SocketReceived(object sender, SocketAsyncEventArgs e)
        {
            try
            {
                SocketListener sl          = sender as SocketListener;
                TCPInPacket    tcpInPacket = null;
                AsyncUserToken userToken   = (e.UserToken as AsyncUserToken);
                TMSKSocket     s           = userToken.CurrentSocket;
                if (GameManager.FlagOptimizeLock)
                {
                    tcpInPacket = s._TcpInPacket;
                    if (null == tcpInPacket)
                    {
                        lock (dictInPackets) //锁定接收包队列
                        {
                            if (!dictInPackets.TryGetValue(s, out tcpInPacket))
                            {
                                tcpInPacket      = tcpInPacketPool.Pop(s, TCPCmdPacketEvent);
                                dictInPackets[s] = tcpInPacket;
                            }
                        }
                        s._TcpInPacket = tcpInPacket;
                    }
                }
                else
                {
                    lock (dictInPackets) //锁定接收包队列
                    {
                        if (!dictInPackets.TryGetValue(s, out tcpInPacket))
                        {
                            tcpInPacket      = tcpInPacketPool.Pop(s, TCPCmdPacketEvent);
                            dictInPackets[s] = tcpInPacket;
                        }
                    }
                }
                //处理收到的包
                if (!tcpInPacket.WriteData(e.Buffer, e.Offset, e.BytesTransferred))
                {
                    SysConOut.WriteLine(string.Format("接收到非法数据长度的tcp命令, 需要立即断开! {0} {1}", e.Offset, e.BytesTransferred));
                    //LogManager.WriteLog(LogTypes.Error, string.Format("接收到非法数据长度的tcp命令, 需要立即断开!"));
                    return(false);
                }
            }
            catch (Exception ex)
            {
                SysConOut.WriteLine("SocketReceived 异常-------------------" + ex.Message);
            }



            return(true);
        }
示例#4
0
        private bool SocketReceived(object sender, SocketAsyncEventArgs e)
        {
            SocketListener sl          = sender as SocketListener;
            TCPInPacket    tcpInPacket = null;
            Socket         s           = (e.UserToken as AsyncUserToken).CurrentSocket;

            lock (this.dictInPackets)
            {
                if (!this.dictInPackets.TryGetValue(s, out tcpInPacket))
                {
                    tcpInPacket           = this.tcpInPacketPool.Pop(s, new TCPCmdPacketEventHandler(this.TCPCmdPacketEvent));
                    this.dictInPackets[s] = tcpInPacket;
                }
            }
            return(tcpInPacket.WriteData(e.Buffer, e.Offset, e.BytesTransferred));
        }