Пример #1
0
        private TcpServerListener ConfigureTcpListener(IPEndPoint hostEndPoint, Action <ArraySegment <byte> > handler)
        {
            Log.Info("ConfigureTcpListener(" + hostEndPoint.AddressFamily + ", " + hostEndPoint.Port + ") entered.");

            LengthPrefixMessageFramer framer = new LengthPrefixMessageFramer();

            framer.RegisterMessageArrivedCallback(handler);

            var listener = new TcpServerListener(hostEndPoint);

            listener.StartListening((endPoint, socket) =>
            {
                TcpConnection = Transport.TcpConnection.CreateAcceptedTcpConnection(Guid.NewGuid(), endPoint, socket, verbose: true);

                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);
            }, "Standard");
            Log.Info("ConfigureTcpListener(" + hostEndPoint.AddressFamily + ", " + hostEndPoint.Port + ") successfully constructed TcpServerListener.");
            return(listener);
        }
Пример #2
0
 public TcpBusServerSide(
     IPAddress hostIp,
     int commandPort,
     IGeneralBus messageBus)
     : base(hostIp, commandPort, messageBus)
 {
     _commandPortListener = ConfigureTcpListener(CommandEndpoint, TcpMessageArrived);
 }
        public TcpBusServerSide(
            IPAddress hostIp,
            int commandPort,
            IEnumerable <Type> inboundDiscardingMessageTypes = null,
            QueuedHandlerDiscarding inboundDiscardingMessageQueuedHandler = null,
            IEnumerable <Type> inboundNondiscardingMessageTypes           = null,
            QueuedHandler inboundNondiscardingMessageQueuedHandler        = null,
            Dictionary <Type, IMessageSerializer> messageSerializers      = null)
            : base(
                hostIp,
                commandPort,
                inboundDiscardingMessageTypes,
                inboundDiscardingMessageQueuedHandler,
                inboundNondiscardingMessageTypes,
                inboundNondiscardingMessageQueuedHandler,
                messageSerializers)
        {
            Log.Debug($"Configuring TCP Listener at {CommandEndpoint.AddressFamily}, {CommandEndpoint}.");

            var listener = new TcpServerListener(CommandEndpoint);

            listener.StartListening(
                (endPoint, socket) =>
            {
                var connectionId = Guid.NewGuid();
                var conn         = TcpConnection.CreateAcceptedTcpConnection(connectionId, endPoint, socket, verbose: true);

                var framer = new LengthPrefixMessageFramer();
                framer.RegisterMessageArrivedCallback(TcpMessageArrived);

                Action <ITcpConnection, IEnumerable <ArraySegment <byte> > > callback = null;
                callback = (x, data) =>
                {
                    try
                    {
                        framer.UnFrameData(x.ConnectionId, data);
                    }
                    catch (PackageFramingException exc)
                    {
                        Log.ErrorException(exc, "LengthPrefixMessageFramer.UnFrameData() threw an exception:");
                        return;
                    }
                    x.ReceiveAsync(callback);
                };
                conn.ReceiveAsync(callback);
                AddConnection(conn);
            },
                "Standard");
            Log.Debug($"TCP Listener at {CommandEndpoint.AddressFamily}, {CommandEndpoint} successfully configured.");
            _commandPortListener = listener;
        }
Пример #4
0
        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;
        }