Esempio n. 1
0
        private void AcceptSocket(IConnectionStubListener <SimpleMessage <int>, int> listener)
        {
            Console.WriteLine(@"Server: Listening for connection");
            var remoteConnection = listener.AcceptNext();

            Console.WriteLine(@"Server: Got connection, closing listener");
            int read;

            Console.WriteLine(@"Server: Waiting for message");
            var msg = remoteConnection.ReadNext(out read);

            if (null == msg)
            {
                Console.WriteLine(@"Received null from connection");
                remoteConnection.Dispose();
                return;
            }
            Console.WriteLine(@"Server: Read {0} bytes", read);
            Console.WriteLine(@"Server: Message received; contents: {0}", Encoding.ASCII.GetString(msg.Data));
            Console.WriteLine(@"Server: Sending response");
            remoteConnection.Write(new SimpleMessage <int>(0, Encoding.ASCII.GetBytes("Hello to you too")));
            Console.WriteLine(@"Server: Done, exiting");
            remoteConnection.Dispose();
            listener.Close();
        }
Esempio n. 2
0
        private void RunListenerSubscriber(IConnectionStubListener <SimpleMessage <int>, int> listener)
        {
            var stub = listener.AcceptNext();

            Console.WriteLine(@"Server: Got Connection");
            var connection = new StubConnection <SimpleMessage <int>, int>(
                new ThreadManager(), stub);
            var messages = 0;

            connection.Subscribe(1, (message, size) =>
            {
                TextMessageHandler("Server", message, size);
                messages++;
            });
            var exit = false;

            connection.Subscribe(2, (message, size) =>
            {
                Console.WriteLine(@"Received exit Message");
                Console.WriteLine(@"Total messages received: {0}", messages);
                exit = true;
            });
            connection.Send(new SimpleMessage <int>(0));
            while (!exit)
            {
                Thread.CurrentThread.Join(10);
            }
            connection.Disconnect();
            listener.Close();
        }
Esempio n. 3
0
 private void RunListenerSubscriber(IConnectionStubListener<SimpleMessage<int>, int> listener)
 {
     var stub = listener.AcceptNext();
     Console.WriteLine(@"Server: Got Connection");
     var connection = new StubConnection<SimpleMessage<int>, int>(
         new ThreadManager(), stub);
     var messages = 0;
     connection.Subscribe(1, (message, size) =>
     {
         TextMessageHandler("Server", message, size);
         messages++;
     });
     var exit = false;
     connection.Subscribe(2, (message, size) =>
     {
         Console.WriteLine(@"Received exit Message");
         Console.WriteLine(@"Total messages received: {0}", messages);
         exit = true;
     });
     connection.Send(new SimpleMessage<int>(0));
     while (!exit)
     {
         Thread.CurrentThread.Join(10);
     }
     connection.Disconnect();
     listener.Close();
 }
Esempio n. 4
0
        private void RunClientSubscriber(IConnection<SimpleMessage<int>, int> connection, IConnectionStubListener<SimpleMessage<int>, int> listener, ushort port)
        {
            listener.BindTo(new IPEndPoint(IPAddress.Any, port));
            var listenerTask = Task.Run(() => RunListenerSubscriber(listener));
            var ready = false;
            connection.Subscribe(0, (message, size) => ready = true);
            connection.Subscribe(1, (message, size) => TextMessageHandler("Client", message, size));
            connection.Connect(new IPEndPoint(IPAddress.Loopback, port));
            while (!ready)
                Thread.CurrentThread.Join(10);

            const int messages = 500;
            Console.WriteLine(@"Sending {0} messages with random data", 5);
            var rnd = new Random();
            for (var i = 0; i < messages; ++i)
            {
                var data = new byte[rnd.Next(16, 256)];
                rnd.NextBytes(data);
                connection.Send(new SimpleMessage<int>(1, Encoding.ASCII.GetBytes(Convert.ToBase64String(data))));
            }
            Console.WriteLine(@"Client: Done, sending exit message");
            connection.Send(new SimpleMessage<int>(2));
            connection.Disconnect();
            Console.WriteLine(@"Waiting for listener thread to exit");
            listenerTask.Wait();
            Console.WriteLine(@"Listener thread has exited");
            Console.WriteLine();
            Console.WriteLine(@"Press any key to exit");
            Console.ReadKey();
        }
Esempio n. 5
0
 public MessageServer(IConnectionStubListener <TMessage, TKey> listener,
                      IClientBuilder <TMessage, TKey> clientBuilder,
                      IThreadManager threadManager,
                      IBus <MessageEnvelope <TMessage, TKey> > messageBus,
                      IBus <IServerCommand> serverCommandBus)
 {
     this.serverCommandBus = serverCommandBus;
     this.messageBus       = Guard.IsNull(() => messageBus);
     this.clientBuilder    = Guard.IsNull(() => clientBuilder);
     this.threadManager    = Guard.IsNull(() => threadManager);
     this.listener         = Guard.IsNull(() => listener);
     this.messageBus.Subscribe <MessageEnvelope <TMessage, TKey> >(HandleMessage);
     this.serverCommandBus.Subscribe <DisconnectCommand>(OnDisconnect);
 }
Esempio n. 6
0
        private void RunClientSubscriber(IConnection <SimpleMessage <int>, int> connection, IConnectionStubListener <SimpleMessage <int>, int> listener, ushort port)
        {
            listener.BindTo(new IPEndPoint(IPAddress.Any, port));
            var listenerTask = Task.Run(() => RunListenerSubscriber(listener));
            var ready        = false;

            connection.Subscribe(0, (message, size) => ready = true);
            connection.Subscribe(1, (message, size) => TextMessageHandler("Client", message, size));
            connection.Connect(new IPEndPoint(IPAddress.Loopback, port));
            while (!ready)
            {
                Thread.CurrentThread.Join(10);
            }

            const int messages = 500;

            Console.WriteLine(@"Sending {0} messages with random data", 5);
            var rnd = new Random();

            for (var i = 0; i < messages; ++i)
            {
                var data = new byte[rnd.Next(16, 256)];
                rnd.NextBytes(data);
                connection.Send(new SimpleMessage <int>(1, Encoding.ASCII.GetBytes(Convert.ToBase64String(data))));
            }
            Console.WriteLine(@"Client: Done, sending exit message");
            connection.Send(new SimpleMessage <int>(2));
            connection.Disconnect();
            Console.WriteLine(@"Waiting for listener thread to exit");
            listenerTask.Wait();
            Console.WriteLine(@"Listener thread has exited");
            Console.WriteLine();
            Console.WriteLine(@"Press any key to exit");
            Console.ReadKey();
        }
Esempio n. 7
0
 private void AcceptSocket(IConnectionStubListener<SimpleMessage<int>, int> listener)
 {
     Console.WriteLine(@"Server: Listening for connection");
     var remoteConnection = listener.AcceptNext();
     Console.WriteLine(@"Server: Got connection, closing listener");
     int read;
     Console.WriteLine(@"Server: Waiting for message");
     var msg = remoteConnection.ReadNext(out read);
     if (null == msg)
     {
         Console.WriteLine(@"Received null from connection");
         remoteConnection.Dispose();
         return;
     }
     Console.WriteLine(@"Server: Read {0} bytes", read);
     Console.WriteLine(@"Server: Message received; contents: {0}", Encoding.ASCII.GetString(msg.Data));
     Console.WriteLine(@"Server: Sending response");
     remoteConnection.Write(new SimpleMessage<int>(0, Encoding.ASCII.GetBytes("Hello to you too")));
     Console.WriteLine(@"Server: Done, exiting");
     remoteConnection.Dispose();
     listener.Close();
 }