示例#1
0
        private static void SendCallback(IAsyncResult ar)
        {
            var serverTransport = (TcpTransport)ar.AsyncState;

            if (serverTransport == null)
            {
                return;
            }

            if (serverTransport._state != 1)
            {
                return;
            }

            int sendCount = 0;

            try
            {
                sendCount = serverTransport._socket.EndSend(ar);
            }
            catch (Exception ex)
            {
                LogAgent.Warn("tcpTransport SendCallback,socket EndSend error", ex);
            }

            if (sendCount <= 0)
            {
                serverTransport.Close();
                return;
            }
        }
示例#2
0
        public void Send(byte[] sendBuffer, int messageByteCount)
        {
            if (_state != 2)
            {
                throw new RpcException("tcpTransport Send failed,state error");
            }

            try
            {
                _socket.Send(sendBuffer, messageByteCount, SocketFlags.None);
            }
            catch (Exception ex)
            {
                Close();
                LogAgent.Warn("close network in tcpTransport Send,socket Send error", ex);
                throw;
            }
        }
示例#3
0
        public void SendAsync(byte[] sendBuffer, int messageByteCount)
        {
            if (_state != 2)
            {
                throw new RpcException("tcpTransport SendAsync failed,state error");
            }

            try
            {
                _socket.BeginSend(sendBuffer, 0, messageByteCount, SocketFlags.None, SendCallback, this);
            }
            catch (Exception ex)
            {
                Close();
                LogAgent.Warn("close network in tcpTransport SendAsync,socket BeginSend error", ex);
                throw;
            }
        }
示例#4
0
 private void BeginNewReceive(bool isReset = false)
 {
     try
     {
         if (isReset)
         {
             _receiveProcessor.Reset();
         }
         _receiveProcessor.GetNextReceiveCallbackData(ref _tempReceiveCallbackData);
         _socket.BeginReceive(_tempReceiveCallbackData.Buffer, _tempReceiveCallbackData.Offset, _tempReceiveCallbackData.Size, SocketFlags.None, ReceiveCallback, this);
     }
     catch (Exception ex)
     {
         LogAgent.Warn("tcpTransport BeginNewReceive,socket BeginReceive error", ex);
         Close();
         throw;
     }
 }
示例#5
0
        private static void ReceiveCallback(IAsyncResult ar)
        {
            var tcpTransport = (TcpTransport)ar.AsyncState;

            if (tcpTransport == null)
            {
                return;
            }

            if (tcpTransport._state != 2)
            {
                return;
            }

            int readCount;

            try
            {
                readCount = tcpTransport._socket.EndReceive(ar);
            }
            catch (Exception ex)
            {
                tcpTransport.Close();
                LogAgent.Warn("close network in tcpTransport ReceiveCallback,socket EndReceive error", ex);
                return;
            }

            if (readCount <= 0)
            {
                tcpTransport.Close(false);
                LogAgent.Debug("close network in tcpTransport ReceiveCallback,socket receive size:0,remote socket is closed");
                return;
            }

            var receiveProcessor = tcpTransport._receiveProcessor;

            try
            {
                var step = receiveProcessor.CheckCurrentStep(readCount);
                if (step == -1)
                {
                    tcpTransport.Close();
                    LogAgent.Warn("close network in tcpTransport ReceiveCallback,receiveProcessor CheckCurrentStep error");
                    return;
                }

                if (step == 0)
                {
                    tcpTransport.BeginNewReceive(false);
                    return;
                }
            }
            catch (Exception ex)
            {
                tcpTransport.Close();
                LogAgent.Warn("close network in tcpTransport ReceiveCallback,tcpTransport BeginNewReceive error", ex);
                return;
            }

            try
            {
                FrameData receiveData = receiveProcessor.GetCurrentReceiveData();
                tcpTransport.BeginNewReceive(true);
                tcpTransport.OnReceive(receiveData);
            }
            catch (Exception ex)
            {
                tcpTransport.Close();
                LogAgent.Warn("close network in tcpTransport ReceiveCallback,tcpTransport BeginNewReceive error", ex);
                return;
            }
        }