private void OnSocketAccepted(Socket socket) { IPEndPoint socketEndPoint; try { socketEndPoint = (IPEndPoint)socket.RemoteEndPoint; } catch (Exception ex) { _logger.Error("TCP connection accepted, but get remote endpoint of this connection failed.", ex); return; } var connection = TcpConnection.CreateAcceptedTcpConnection(Guid.NewGuid(), socketEndPoint, socket); new AcceptedConnection(connection, _eventListener, _messageHandler).StartReceiving(); _logger.InfoFormat("TCP connection accepted: [remoteEndPoint:{0}, localEndPoint:{1}, connectionId:{2:B}].", connection.RemoteEndPoint, connection.LocalEndPoint, connection.ConnectionId); if (_eventListener != null) { _eventListener.OnConnectionAccepted(connection); } }
public static ITcpConnection CreateConnectingTcpConnection(Guid connectionId, IPEndPoint remoteEndPoint, TcpClientConnector connector, TimeSpan connectionTimeout, Action<ITcpConnection> onConnectionEstablished, Action<ITcpConnection, SocketError> onConnectionFailed, bool verbose) { var connection = new TcpConnection(connectionId, remoteEndPoint, verbose); // ReSharper disable ImplicitlyCapturedClosure connector.InitConnect(remoteEndPoint, (_, socket) => { connection.InitSocket(socket); if (onConnectionEstablished != null) onConnectionEstablished(connection); }, (_, socketError) => { if (onConnectionFailed != null) onConnectionFailed(connection, socketError); }, connection, connectionTimeout); // ReSharper restore ImplicitlyCapturedClosure return connection; }
public static ITcpConnection CreateAcceptedTcpConnection(Guid connectionId, IPEndPoint remoteEndPoint, Socket socket, bool verbose) { var connection = new TcpConnection(connectionId, remoteEndPoint, verbose); connection.InitSocket(socket); return connection; }
public static ITcpConnection CreateConnectingTcpConnection(Guid connectionId, IPEndPoint localEndPoint, IPEndPoint remoteEndPoint, TcpClientConnector connector, Action<ITcpConnection> onConnectionEstablished, Action<ITcpConnection, SocketError> onConnectionFailed) { var connection = new TcpConnection(connectionId, remoteEndPoint); connector.InitConnect(localEndPoint, remoteEndPoint, (_, socket) => { connection.InitSocket(socket); if (onConnectionEstablished != null) onConnectionEstablished(connection); }, (_, socketError) => { if (onConnectionFailed != null) onConnectionFailed(connection, socketError); }, connection); return connection; }