Example #1
0
        protected override bool MainLoop()
        {
            while (mSendPacketPool.Count > 0)
            {
                System.Object packet = null;
                lock (mSendPacketPool)
                {
                    packet = mSendPacketPool.Dequeue();
                }

                mStream.SetPosition(TcpNetworkChannel.PacketHeadLength);
                if (!mHandler.SerializePacket(packet, mStream))
                {
                    mHandler.OnNetworkError("Serialized packet failure.");
                    return(false);
                }
                int packetLength = (int)mStream.Position - TcpNetworkChannel.PacketHeadLength;
                mStream.SetPosition(0);
                TcpNetworkChannel.WritePacketHead(packetLength, mStream);
                try
                {
                    mNetworkStream.Write(mStream.GetBuffer(), 0, packetLength + TcpNetworkChannel.PacketHeadLength);
                }
                catch (Exception exception)
                {
                    SocketException socketException = exception as SocketException;
                    if (socketException != null)
                    {
                        mMessageHandler(string.Format("SendThread socket error code = {0} see detail from https://docs.microsoft.com/en-us/windows/win32/winsock/windows-sockets-error-codes-2"
                                                      , socketException.ErrorCode));
                        return(false);
                    }
                    throw;
                }
            }
            Pause();
            return(true);
        }
Example #2
0
        void ReceiveCallback(IAsyncResult ar)
        {
            if (mManualResetEvent != null)
            {
                Debug.Log("ReceiveCallback mManualResetEvent != null");
                mManualResetEvent.Set();

                Receiving = false;
                return;
            }
            try
            {
                SerializerStream Stream = mReceiveData.Stream;
                int bytesReceived       = TcpClient.GetStream().EndRead(ar);
                if (bytesReceived <= 0)
                {
                    //if (mChannel.Status != NetworkStatus.Disconnecting)
                    //    mChannel.Disconnect();
                    Debug.Log("ReceiveCallback bytesReceived <= 0");

                    Receiving = false;
                    Close(true);
                    return;
                }

                Stream.SetPosition(Stream.Position + bytesReceived);
                if (Stream.Position < Stream.Length)
                {
                    Receive();
                    return;
                }

                Stream.SetPosition(0);
                if (mReceiveData.ProcessPacketHeader)
                {
                    int receiveSize = TcpNetworkChannel.ReadPacketHead(Stream);
                    Stream.SetPosition(0);
                    if (receiveSize > 0)
                    {
                        Stream.SafeSetLength(receiveSize);
                        mReceiveData.ProcessPacketHeader = false;
                    }
                    else
                    {
                        OnNetworkError("ReceiveCallback packet head error,length = " + receiveSize);
                    }
                }
                else
                {
                    System.Object packet = mHandler.DeserializePacket(Stream);
                    lock (mMessageQueue)
                    {
                        mMessageQueue.Enqueue(mObjectPool.Acquire().PacketMessage(packet));
                    }
                    Stream.SetPosition(0);
                    Stream.SafeSetLength(TcpNetworkChannel.PacketHeadLength);
                    mReceiveData.ProcessPacketHeader = true;
                }
                Receive();
            }
            catch (Exception exception)
            {
                SocketException socketException = exception as SocketException;
                if (socketException != null)
                {
                    OnNetworkError(string.Format("SendThread socket error code = {0} see detail from https://docs.microsoft.com/en-us/windows/win32/winsock/windows-sockets-error-codes-2"
                                                 , socketException.ErrorCode));
                }
                else
                {
                    OnNetworkError(exception.ToString());
                }
                Receiving = false;
                Close(true);
            }
        }