Esempio n. 1
0
        private bool ProcessPacketBody()
        {
            lock (m_HeartBeatState)
            {
                m_HeartBeatState.Reset(m_ResetHeartBeatElapseSecondsWhenReceivePacket);
            }

            try
            {
                Protocol packet = m_Helper.Decode(m_ReceiveState.Reader);
                m_ReceivePacketPool.Enqueue(packet);

                m_ReceiveState.PrepareForPacketHeader(m_Helper.HeaderLength);
            }
            catch (Exception exception)
            {
                m_Active = false;
                if (NetworkChannelError != null)
                {
                    NetworkChannelError(this, NetworkErrorCode.DeserializePacketError, exception.ToString());
                    return(false);
                }

                throw;
            }

            return(true);
        }
            private void ConnectCallback(IAsyncResult ar)
            {
                SocketUserData socketUserData;
                try
                {
                    socketUserData.Socket.EndConnect(ar);
                }
                catch (ObjectDisposedException)
                {
                    return;
                }
                catch (e)
                {
                    Error();
                    return;

                    throw;
                }

                m_Active = true;

                lock (m_HeartBeatState)
                {
                    m_HeartBeatState.Reset(true);
                }

                NetworkChannelConnected();

                Receive();
            }
            /// <summary>
            /// 关闭连接并释放所有相关资源。
            /// </summary>
            public void Close()
            {
                lock (this)
                {
                    if (m_Socket == null)
                    {
                        return;
                    }

                    m_Active = false;

                    try
                    {
                        m_Socket.Shutdown(SocketShutdown.Both);
                    }
                    catch
                    {
                    }
                    finally
                    {
                        m_Socket.Close();
                        m_Socket = null;

                        if (NetworkChannelClosed != null)
                        {
                            NetworkChannelClosed(this);
                        }
                    }

                    m_SentPacketCount     = 0;
                    m_ReceivedPacketCount = 0;

                    lock (m_SendPacketPool)
                    {
                        m_SendPacketPool.Clear();
                    }

                    m_ReceivePacketPool.Clear();

                    lock (m_HeartBeatState)
                    {
                        m_HeartBeatState.Reset(true);
                    }
                }
            }
            private bool ProcessPacket()
            {
                lock (m_HeartBeatState)
                {
                    m_HeartBeatState.Reset(m_ResetHeartBeatElapseSecondsWhenReceivePacket);
                }

                try
                {
                    object customErrorData = null;
                    Packet packet          = m_NetworkChannelHelper.DeserializePacket(m_ReceiveState.Stream, out customErrorData);

                    if (customErrorData != null)
                    {
                        if (NetworkChannelCustomError != null)
                        {
                            NetworkChannelCustomError(this, customErrorData);
                        }
                    }

                    if (packet != null)
                    {
                        m_EventPool.Fire(this, packet);
                    }

                    m_ReceiveState.PrepareForPacketHeader(m_NetworkChannelHelper.PacketHeaderLength);
                }
                catch (System.Exception exception)
                {
                    m_Active = false;
                    if (NetworkChannelError != null)
                    {
                        NetworkChannelError(this, NetworkErrorCode.DeserializePacketError, exception.ToString());
                        return(false);
                    }

                    throw;
                }

                return(true);
            }
            private void ConnectCallback(IAsyncResult ar)
            {
                SocketUserData socketUserData = (SocketUserData)ar.AsyncState;
                try
                {
                    socketUserData.Socket.EndConnect(ar);
                }
                catch (ObjectDisposedException)
                {
                    return;
                }
                catch (Exception exception)
                {
                    m_Active = false;
                    if (NetworkChannelError != null)
                    {
                        NetworkChannelError(this, NetworkErrorCode.ConnectError, exception.Message);
                        return;
                    }

                    throw;
                }

                m_Active = true;
                lock (m_HeartBeatState)
                {
                    m_HeartBeatState.Reset(true);
                }

                if (NetworkChannelConnected != null)
                {
                    NetworkChannelConnected(this, socketUserData.UserData);
                }

                Receive();
            }
            private bool Process()
            {
                if (m_ReceiveState.ReceivedLength != m_ReceiveState.Length)
                {
                    throw new GameFrameworkException(string.Format("Receive length '{0}' is not equal to length '{1}'.", m_ReceiveState.ReceivedLength.ToString(), m_ReceiveState.Length.ToString()));
                }

                if (m_ReceiveState.Length < ReceiveState.HeaderLength)
                {
                    throw new GameFrameworkException(string.Format("Length '{0}' is smaller than length header.", m_ReceiveState.Length.ToString()));
                }

                if (m_ReceiveState.Length == ReceiveState.HeaderLength)
                {
                    int packetLength = Utility.Converter.GetIntFromBytes(m_ReceiveState.GetBuffer());
                    if (packetLength <= 0)
                    {
                        string errorMessage = string.Format("Packet length '{0}' is invalid.", packetLength.ToString());
                        if (NetworkChannelError != null)
                        {
                            NetworkChannelError(this, NetworkErrorCode.HeaderError, errorMessage);
                            return(false);
                        }

                        throw new GameFrameworkException(errorMessage);
                    }

                    m_ReceiveState.Length += packetLength;
                    if (m_ReceiveState.Length > m_ReceiveState.BufferSize)
                    {
                        string errorMessage = string.Format("Length '{0}' is larger than buffer size '{1}'.", m_ReceiveState.Length.ToString(), m_ReceiveState.BufferSize.ToString());
                        if (NetworkChannelError != null)
                        {
                            NetworkChannelError(this, NetworkErrorCode.OutOfRangeError, errorMessage);
                            return(false);
                        }

                        throw new GameFrameworkException(errorMessage);
                    }

                    return(true);
                }

                lock (m_HeartBeatState)
                {
                    m_HeartBeatState.Reset(m_ResetHeartBeatElapseSecondsWhenReceivePacket);
                }

                Packet packet = null;

                try
                {
                    int    packetLength    = m_ReceiveState.Length - ReceiveState.HeaderLength;
                    object customErrorData = null;
                    using (MemoryStream memoryStream = new MemoryStream(m_ReceiveState.GetBuffer(), ReceiveState.HeaderLength, packetLength, false))
                    {
                        lock (m_NetworkHelper)
                        {
                            packet = m_NetworkHelper.Deserialize(this, memoryStream, out customErrorData);
                        }
                    }

                    m_ReceiveState.Reset();
                    if (packet == null)
                    {
                        if (NetworkChannelCustomError != null)
                        {
                            NetworkChannelCustomError(this, customErrorData);
                        }
                    }
                    else
                    {
                        if (NetworkChannelReceived != null)
                        {
                            NetworkChannelReceived(this, packet);
                        }
                    }
                }
                catch (Exception exception)
                {
                    m_Active = false;
                    if (NetworkChannelError != null)
                    {
                        NetworkChannelError(this, NetworkErrorCode.DeserializeError, exception.ToString());
                        return(false);
                    }

                    throw;
                }

                return(true);
            }