예제 #1
0
        /// <summary>
        /// 数据接收处理函数
        /// </summary>
        /// <param name="iar">异步Socket</param>
        protected virtual void RecvData(IAsyncResult iar)
        {
            Socket remote = (Socket)iar.AsyncState;

            try
            {
                int recv = remote.EndReceive(iar);

                if (recv == 0)
                {
                    _session.TypeOfExit = Session.ExitType.NormalExit;

                    if (DisConnectedServer != null)
                    {
                        DisConnectedServer(this, new NetEventArgs(_session));
                    }

                    return;
                }

                string receivedData = _coder.GetEncodingString(_recvDataBuffer, recv);

                //通过事件发布收到的报文
                //if (ReceivedDatagram != null)
                //{
                //    if (_resolver != null)
                //    {
                //        if (_session.Datagram != null && _session.Datagram.Length != 0)
                //        {
                //            receivedData = _session.Datagram + receivedData;
                //        }

                //        string[] recvDatagrams = _resolver.Resolve(ref receivedData);
                //        foreach (string newDatagram in recvDatagrams)
                //        {
                //            ICloneable copySession = (ICloneable)_session;
                //            Session clientSession = (Session)copySession.Clone();
                //            clientSession.Datagram = newDatagram;

                //            //发布一个报文消息
                //            ReceivedDatagram(this, new NetEventArgs(clientSession));
                //        }

                //        //剩余的代码片断,下次接收的时候使用
                //        _session.Datagram = receivedData;
                //    }
                //    else
                //    {
                //        ICloneable copySession = (ICloneable)_session;
                //        Session clientSession = (Session)copySession.Clone();
                //        clientSession.Datagram = receivedData;

                //        ReceivedDatagram(this, new NetEventArgs(clientSession));
                //    }

                //}//end of if(ReceivedDatagram != null)

                {
                    //omega 添加 2013年9月13日11:20:07
                    ICloneable copySession   = (ICloneable)_session;
                    Session    clientSession = (Session)copySession.Clone();
                    clientSession.Datagram       = receivedData;
                    clientSession.RecvDataBuffer = _recvDataBuffer;

                    ReceivedDatagram(this, new NetEventArgs(clientSession));
                }


                //继续接收数据
                _session.ClientSocket.BeginReceive(_recvDataBuffer, 0, DefaultBufferSize, SocketFlags.None,
                                                   new AsyncCallback(RecvData), _session.ClientSocket);
            }
            catch (SocketException ex)
            {
                //客户端退出
                if (10054 == ex.ErrorCode)
                {
                    _session.TypeOfExit = Session.ExitType.ExceptionExit;

                    if (DisConnectedServer != null)
                    {
                        DisConnectedServer(this, new NetEventArgs(_session));
                    }
                }
                else
                {
                    throw (ex);
                }
            }
            catch (ObjectDisposedException ex)
            {
                if (ex != null)
                {
                    ex = null;
                    //DoNothing;
                }
            }
        }
예제 #2
0
        /// <summary>
        /// 接受数据完成处理函数,异步的特性就体现在这个函数中,
        /// 收到数据后,会自动解析为字符串报文
        /// </summary>
        /// <param name="iar">目标客户端Socket</param>
        protected virtual void ReceiveData(IAsyncResult iar)
        {
            Session sendDataSession = (Session)iar.AsyncState;
            Socket  client          = sendDataSession.ClientSocket;

            try
            {
                //如果两次开始了异步的接收,所以当客户端退出的时候
                //会两次执行EndReceive
                int recv = client.EndReceive(iar);

                if (recv == 0)
                {
                    CloseClient(client, Session.ExitType.NormalExit);
                    return;
                }

                string receivedData  = _coder.GetEncodingString(sendDataSession.RecvDataBuffer, recv);
                string receivedData2 = _coder.GetEncodingString(_recvDataBuffer, recv);

                //发布收到数据的事件
                //if (RecvData != null)
                //{
                //    //Session sendDataSession = FindSession(client);

                //    Debug.Assert(sendDataSession != null);

                //    if (_resolver != null)
                //    {
                //        if (sendDataSession.Datagram != null && sendDataSession.Datagram.Length != 0)
                //        {
                //            receivedData = sendDataSession.Datagram + receivedData;
                //        }

                //        string[] recvDatagrams = _resolver.Resolve(ref receivedData);

                //        foreach (string newDatagram in recvDatagrams)
                //        {
                //            ICloneable copySession = (ICloneable)sendDataSession;
                //            Session clientSession = (Session)copySession.Clone();

                //            string strDatagram = newDatagram;
                //            //clientSession.ClassName = this.GetClassFullName(ref strDatagram);
                //            clientSession.Datagram = strDatagram;

                //            //发布一个报文消息
                //            RecvData(this, new NetEventArgs(clientSession));
                //        }

                //        //剩余的代码片断,下次接收的时候使用
                //        sendDataSession.Datagram = receivedData;

                //        if (sendDataSession.Datagram.Length > MaxDatagramSize)
                //        {
                //            sendDataSession.Datagram = null;
                //        }
                //    }
                //    else
                //    {
                //        ICloneable copySession = (ICloneable)sendDataSession;
                //        Session clientSession = (Session)copySession.Clone();
                //        //clientSession.ClassName = this.GetClassFullName(ref receivedData);
                //        clientSession.Datagram = receivedData;

                //        RecvData(this, new NetEventArgs(clientSession));
                //    }
                //}

                {
                    ICloneable copySession   = (ICloneable)sendDataSession;
                    Session    clientSession = (Session)copySession.Clone();
                    //clientSession.ClassName = this.GetClassFullName(ref receivedData);
                    clientSession.Datagram       = receivedData;
                    clientSession.RecvDataBuffer = sendDataSession.RecvDataBuffer;

                    RecvData(this, new NetEventArgs(clientSession));
                }

                //继续接收来自来客户端的数据
                client.BeginReceive(sendDataSession.RecvDataBuffer, 0, sendDataSession.RecvDataBuffer.Length, SocketFlags.None,
                                    new AsyncCallback(ReceiveData), sendDataSession);
            }
            catch (SocketException ex)
            {
                if (10054 == ex.ErrorCode)
                {
                    //客户端强制关闭
                    CloseClient(client, Session.ExitType.ExceptionExit);
                }
            }
            catch (ObjectDisposedException ex)
            {
                if (ex != null)
                {
                    ex = null;
                    //DoNothing;
                }
            }
        }