Пример #1
0
        private static void SendCallback(IAsyncResult ar)
        {
            var tcpTransport = (TcpTransport)ar.AsyncState;

            if (tcpTransport == null)
            {
                return;
            }

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

            try
            {
                var sendCount = tcpTransport._socket.EndSend(ar);
            }
            catch (Exception ex)
            {
                tcpTransport.Close();
                LogAgent.Warn("close network in tcpTransport SendCallback,socket EndSend error", ex);
                return;
            }
        }
Пример #2
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.Error("tcpTransport SendCallback,socket EndSend error", ex);
            }

            if (sendCount <= 0)
            {
                serverTransport.Close();
                return;
            }
        }
Пример #3
0
        private void CheckConnection()
        {
            var observerList = _observerDictionary.Keys.ToList();

            foreach (var observer in observerList)
            {
                try
                {
                    observer.CheckConnection();
                }
                catch (Exception ex)
                {
                    LogAgent.Error(ex.Message, ex);
                }
            }
        }
Пример #4
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;
            }
        }
Пример #5
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;
            }
        }
Пример #6
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.Error("tcpTransport BeginNewReceive,socket BeginReceive error", ex);
         Close();
         throw;
     }
 }
Пример #7
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;
            }
        }