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);
        }
Beispiel #2
0
        public static void Test1()
        {
            Console.WriteLine("memory cpu test.");
            IMessageFramer messageFramer   = new LengthPrefixMessageFramer(new FregataOptions());
            var            bufferPipelinse = new BufferPipeline();

            for (int i = 0; i < 500000; i++)
            {
                messageFramer.FrameData(bufferPipelinse.Writer, new byte[4] {
                    1, 2, 3, 4
                });
            }
            bufferPipelinse.Writer.Flush();
            messageFramer.RegisterMessageArrivedCallback((result) =>
            {
                _dealCount++;
                Task.Factory.StartNew(() =>
                {
                    for (int i = 0; i < result.Length; i++)
                    {
                        Console.Write(result.Buffer.Span[i]);
                    }
                    Console.WriteLine();
                });
            });
            while (messageFramer.CanUnFrameData(bufferPipelinse.Reader))
            {
                messageFramer.UnFrameData(bufferPipelinse.Reader);
            }
            Console.WriteLine("================");
            Console.WriteLine(_dealCount);
        }
        public TcpTypedConnection(TcpConnection connection)
        {
            _connection = connection;
            _framer = new LengthPrefixMessageFramer();
            EffectiveEndPoint = connection.EffectiveEndPoint;

            connection.ConnectionClosed += OnConnectionClosed;

            //Setup callback for incoming messages
            _framer.RegisterMessageArrivedCallback(IncomingMessageArrived);
        }
        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;
        }
        public void Test()
        {
            IMessageFramer messageFramer   = new LengthPrefixMessageFramer(_fregataOptions);
            var            bufferPipelinse = new BufferPipeline();

            for (int i = 0; i < 10000; i++)
            {
                messageFramer.FrameData(bufferPipelinse.Writer, new byte[4] {
                    1, 2, 3, 4
                });
            }
            bufferPipelinse.Writer.Flush();
            messageFramer.RegisterMessageArrivedCallback((result) =>
            {
                Assert.True(result.Length == 4);
                for (int i = 0; i < result.Length; i++)
                {
                    Assert.True(result.Buffer.Span[i] == i + 1);
                }
            });
            messageFramer.UnFrameData(bufferPipelinse.Reader);
        }
Beispiel #6
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;
        }