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; } }
private void CheckConnection() { var observerList = _observerDictionary.Keys.ToList(); foreach (var observer in observerList) { try { observer.CheckConnection(); } catch (Exception ex) { LogAgent.Error(ex.Message, ex); } } }
public void Send(byte[] sendBuffer, int messageByteCount) { if (_state != 1) { throw new Exception("tcpTransport send,state error"); } try { _socket.Send(sendBuffer, messageByteCount, SocketFlags.None); } catch (Exception ex) { Close(); LogAgent.Error("tcpTransport Send,socket Send error", ex); throw; } }
public void SendAsync(byte[] sendBuffer, int messageByteCount) { if (_state != 1) { throw new Exception("tcpTransport SendAsync,state error"); } try { _socket.BeginSend(sendBuffer, 0, messageByteCount, SocketFlags.None, SendCallback, this); } catch (Exception ex) { Close(); LogAgent.Error("tcpTransport SendAsync,socket BeginSend error", ex); throw; } }
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; } }
private static void ReceiveCallback(IAsyncResult ar) { var tcpTransport = (TcpTransport)ar.AsyncState; if (tcpTransport == null) { return; } if (tcpTransport._state != 1) { return; } int readCount = 0; try { readCount = tcpTransport._socket.EndReceive(ar); } catch (Exception ex) { LogAgent.Error("tcpTransport ReceiveCallback,socket EndReceive error", ex); } if (readCount <= 0) { tcpTransport.Close(); return; } var receiveProcessor = tcpTransport._receiveProcessor; try { var step = receiveProcessor.CheckCurrentStep(readCount); if (step == -1) { throw new Exception("tcpTransport ReceiveCallback,receiveProcessor CheckCurrentStep error"); } if (step == 0) { tcpTransport.BeginNewReceive(false); return; } } catch (Exception ex) { tcpTransport.Close(); LogAgent.Error("tcpTransport ReceiveCallback,tcpTransport BeginNewReceive error", ex); return; } try { FrameData receiveData = receiveProcessor.GetCurrentReceiveData(); tcpTransport.BeginNewReceive(true); tcpTransport.OnReceive(receiveData); } catch (Exception ex) { tcpTransport.Close(); LogAgent.Error("tcpTransport ReceiveCallback,tcpTransport BeginNewReceive error", ex); return; } }