private ITcpConnection CreateTcpConnection(IPEndPoint endPoint) { Log.Info("TcpBusClientSide.CreateTcpConnection(" + endPoint.Address + ":" + endPoint.Port + ") entered."); var clientTcpConnection = TcpConnection.CreateConnectingTcpConnection(Guid.NewGuid(), endPoint, new TcpClientConnector(), TimeSpan.FromSeconds(120), conn => { Log.Info("TcpBusClientSide.CreateTcpConnection(" + endPoint.Address + ":" + endPoint.Port + ") successfully constructed TcpConnection."); ConfigureTcpListener(); }, (conn, err) => { HandleError(conn, err); }, verbose: true); return(clientTcpConnection); }
private void ConfigureTcpListener() { Framer.RegisterMessageArrivedCallback(TcpMessageArrived); Action <ITcpConnection, IEnumerable <ArraySegment <byte> > > callback = null; callback = (x, data) => { try { Framer.UnFrameData(data); } catch (PackageFramingException exc) { Log.ErrorException(exc, "LengthPrefixMessageFramer.UnFrameData() threw an exception:"); // SendBadRequestAndClose(Guid.Empty, string.Format("Invalid TCP frame received. Error: {0}.", exc.Message)); return; } TcpConnection.ReceiveAsync(callback); }; TcpConnection.ReceiveAsync(callback); }
public TcpBusServerSide( IPAddress hostIp, int commandPort, IDispatcher messageBus, IMessageSerializer messageSerializer = null) : base(hostIp, commandPort, messageBus, messageSerializer) { Log.Info("ConfigureTcpListener(" + CommandEndpoint.AddressFamily + ", " + CommandEndpoint + ") entered."); var listener = new TcpServerListener(CommandEndpoint); listener.StartListening((endPoint, socket) => { var conn = Transport.TcpConnection.CreateAcceptedTcpConnection(Guid.NewGuid(), endPoint, socket, verbose: true); LengthPrefixMessageFramer framer = new LengthPrefixMessageFramer(); framer.RegisterMessageArrivedCallback(TcpMessageArrived); Action <ITcpConnection, IEnumerable <ArraySegment <byte> > > callback = null; callback = (x, data) => { try { framer.UnFrameData(data); } catch (PackageFramingException exc) { Log.ErrorException(exc, "LengthPrefixMessageFramer.UnFrameData() threw an exception:"); // SendBadRequestAndClose(Guid.Empty, string.Format("Invalid TCP frame received. Error: {0}.", exc.Message)); return; } conn.ReceiveAsync(callback); }; conn.ReceiveAsync(callback); TcpConnection.Add(conn); }, "Standard"); Log.Info("ConfigureTcpListener(" + CommandEndpoint.AddressFamily + ", " + CommandEndpoint + ") successfully constructed TcpServerListener."); _commandPortListener = listener; }
private ITcpConnection CreateTcpConnection(EndPoint endPoint) { Log.Info($"TcpBusClientSide.CreateTcpConnection({endPoint}) entered."); var clientTcpConnection = TcpConnection.CreateConnectingTcpConnection( Guid.NewGuid(), endPoint, new TcpClientConnector(), TimeSpan.FromSeconds(120), conn => { Log.Debug($"TcpBusClientSide.CreateTcpConnection({endPoint}) successfully constructed TcpConnection."); IsConnected = true; ConfigureTcpListener(conn); }, (conn, err) => { IsConnected = false; HandleError(conn, err); }, verbose: true); return(clientTcpConnection); }