Ejemplo n.º 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 = 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.Error(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);
        }
        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;
        }