Exemplo n.º 1
0
        static void Main(string[] args)
        {
            encoding = Encoding.ASCII;
            SocketServer server;
            server = new SocketServer(new IPEndPoint(IPAddress.Loopback, 0));

            server.Connected += c =>
                {
                    serverClient = new FramedClient(c);

                    // When received is called, bs will contain no more and no less
                    // data than whole packet as sent from client.
                    serverClient.Received += bs =>
                        {
                            var msg = encoding.GetString(bs.Array, bs.Offset, bs.Count);
                            msg = "Hello, " + msg + "!";
                            serverClient.SendPacket(encoding.GetBytes(msg));
                        };
                };

            server.Start();
            HandleClient(server.BindEndPoint.Port);
            
            Console.WriteLine("Press any key to stop...");
            Console.ReadKey();

            server.Stop();
            client.Close();
            serverClient.Close();
        }
Exemplo n.º 2
0
        public void Run()
        {
            service = new TemperatureService();
            server = new SocketServer(new IPEndPoint(IPAddress.IPv6Any, 0));

            server.Connected.Subscribe(ClientConnected);

            server.Start();
            this.ServerPort = server.BindEndPoint.Port;

            service.TemperatureChanged += TempChanged;
        }
Exemplo n.º 3
0
        public void Run()
        {
            ReactiveMessageClient<IServerPacketHandler> client = null;

            service = new MarketService();
            server = new SocketServer(new IPEndPoint(IPAddress.Loopback, 0));

            server.Connected.Subscribe(c =>
                {
                    client = new ReactiveMessageClient<IServerPacketHandler>(
                                    new FramedClient(c),
                                    new ProtoBufStacksSerializer());
                    client.PreLoadTypesFromAssemblyOfType<Price>();

                    // Here is the subscription to all the packets.
                    // `.Packets` is an implementation of interface
                    // given when message client was defined, so
                    // every packet has a strongly typed `IObservable<TPacket>`
                    // to subscribe to.
                    client.Packets.RegisterSymbol.Subscribe(req =>
                        {
                            if (req.Register)
                                service.RegisterSymbol(req.Symbol);
                            else
                                service.UnregisterSymbol(req.Symbol);
                        });
                });

            // Service will only report registered price changes.
            // In this sample, this observer should not run 
            // for 4 seconds after application start.
            service.PricesChanged.Subscribe(prices =>
                {
                    Console.WriteLine("Services reported that prices changed");

                    // Send new prices to client.
                    foreach (var p in prices)
                    {
                        if (client != null)
                            client.Send(p);
                    }
                });

            server.Start();
            this.ServerPort = server.BindEndPoint.Port;
        }
Exemplo n.º 4
0
        public static void CreateServerAndConnectedClient(out SocketServer server,
            out SocketClient client1, out SocketClient client2)
        {
            var connected1 = new ManualResetEventSlim(false);
            var connected2 = new ManualResetEventSlim(false);

            var ex = ServerHelpers.CreateExecutor();
            var s = ServerHelpers.CreateServer();

            SocketClient lClient = null;
            SocketClient sClient = null;

            s.Connected.Subscribe(c =>
            {
                sClient = c;
                connected2.Set();
            });

            s.Started.Subscribe(_ =>
            {
                var c = new SocketClient(ex);
                c.Connected.Subscribe(u =>
                {
                    lClient = c;
                    connected1.Set();
                });
                c.Connect(new IPEndPoint(IPAddress.Loopback, s.BindEndPoint.Port));
            });

            s.Start();

            connected1.AssertWaitFor(3000);
            connected2.AssertWaitFor(3000);

            server = s;
            client1 = lClient;
            client2 = sClient;
        }
Exemplo n.º 5
0
        public static void CreateServerAndConnectedClient(out SocketServer server,
          out SslClient client1, out SslClient client2)
        {
            var connected1 = new ManualResetEventSlim(false);
            var connected2 = new ManualResetEventSlim(false);

            var ex = ServerHelpers.CreateExecutor();
            var s = ServerHelpers.CreateServer();

            SslClient lClient = null;
            SslClient sClient = null;

            var certBytesStream = Assembly.GetExecutingAssembly()
                                          .GetManifestResourceStream("Stacks.Tests.StacksTest.pfx");
            var certBytes = new BinaryReader(certBytesStream).ReadBytes((int)certBytesStream.Length);

            s.Connected.Subscribe(c =>
            {
                sClient = new SslClient(c, new X509Certificate2(certBytes));

                sClient.Connected.Subscribe(_ =>
                    {
                        connected2.Set();
                    });

                sClient.EstablishSsl();
            });

            s.Started.Subscribe(_ =>
            {
                lClient = new SslClient(new SocketClient(ex), "Stacks Test", true);

                lClient.Connected.Subscribe(__ =>
                {
                    connected1.Set();
                });

                lClient.Connect(new IPEndPoint(IPAddress.Loopback, s.BindEndPoint.Port));
            });

            s.Start();

            connected1.AssertWaitFor(300000);
            connected2.AssertWaitFor(300000);

            server = s;
            client1 = lClient;
            client2 = sClient;
        }