Пример #1
0
        public async Task ReactToLongMessages()
        {
            /// <see cref="TCPMessagePasser"/> has a buffer size of 256
            const int tooLongMessage = 300;

            IMessage message = new ChatMessageClient(0, new String('e', tooLongMessage));

            TcpListener listener = new TcpListener(endPoint);

            listener.Start();
            Task <TcpClient> receiverTask = listener.AcceptTcpClientAsync();

            using TcpClient sender = new TcpClient();
            sender.Connect(endPoint);
            sender.GetStream().Write(Serializer.Serialize(message, playerIntegerSize));
            sender.Close();

            using TcpClient receiver = await receiverTask;
            listener.Stop();
            Reactor          reactor       = new Reactor();
            TCPMessagePasser messagePasser = new TCPMessagePasser(receiver.GetStream(), reactor);

            messagePasser.PlayerIntegerSize = playerIntegerSize;
            messagePasser.ListenAndReactClient();

            Assert.IsTrue(new[] { message }.SequenceEqual(reactor.Log), "Should receive the same message as was sent even if it's longer than the buffer.");
        }
Пример #2
0
        public PlayReactor?Connect()
        {
            TcpClient client = localPort is int port ? new TcpClient(new IPEndPoint(IPAddress.Any, port)) : new TcpClient();

            console.Output.WriteLine($"Connecting to {ipAddress}:{this.port}...");
            client.Connect(new IPEndPoint(ipAddress, this.port));
            console.Output.WriteLine("Connected!");

            TCPMessageSender messageSender = new TCPMessageSender(client.GetStream());

            messageSender.QueueMessage(new ConnectClient());
            console.Output.WriteLine("Hailing server...");
            messageSender.SendOneMessage();

            TCPMessagePasser messagePasser = new TCPMessagePasser(client.GetStream(), this);

            while (!serverState.HasValue)
            {
                console.Output.WriteLine("Listening for response...");
                messagePasser.ListenAndReactClient();
            }

            ConnectServer ss = serverState.Value;

            if (ss.CanHost)
            {
                return(new PlayReactor(client, ss));
            }
            else
            {
                return(null);
            }
        }
Пример #3
0
 private void Listen()
 {
     try
     {
         while (true)
         {
             messagePasser.ListenAndReactClient();
         }
     }
     catch (Exception ex) when(!(ex is ObjectDisposedException))
     {
         Dispose();
     }
 }
Пример #4
0
        public async Task ReactToMessages()
        {
            IMessage message = new ChatMessageClient(0, "Hello World!");

            TcpListener listener = new TcpListener(endPoint);

            listener.Start();
            Task <TcpClient> receiverTask = listener.AcceptTcpClientAsync();

            using TcpClient sender = new TcpClient();
            sender.Connect(endPoint);
            sender.GetStream().Write(Serializer.Serialize(message, playerIntegerSize));
            sender.Close();

            using TcpClient receiver = await receiverTask;
            listener.Stop();
            Reactor          reactor       = new Reactor();
            TCPMessagePasser messagePasser = new TCPMessagePasser(receiver.GetStream(), reactor);

            messagePasser.PlayerIntegerSize = playerIntegerSize;
            messagePasser.ListenAndReactClient();

            Assert.IsTrue(new[] { message }.SequenceEqual(reactor.Log), "Should receive the same message as was sent.");
        }