Ejemplo n.º 1
0
 private void HandleClientDisconnectedEvent(object sender, HelloPacket client)
 {
     Console.WriteLine($"Client {client.ToString()} has disconnected.");
     Console.WriteLine($"Removing {client.ToString()} from HostList.");
     if (HostList.ToArray().Length == 1)
     {
         SendCommandToClient(HostList[0], "CMD:Win");
         GamePaused = true;
     }
 }
Ejemplo n.º 2
0
        // This is the UDP listener which listens for the hello broadcast.
        // It will then start a tcp client to reply to the server.
        // TCP is required for the response to ensure connection.


        // This is the UDP listener which listens for the hello broadcast.
        private static void StartListener(HelloPacket serverInfo)
        {
            Console.WriteLine($"Creating UDP server listener {serverInfo.ToString()}");
            //IPEndPoint listenEP = new IPEndPoint(IPAddress.Parse(clientInfo.address), listenPort);
            //UdpClient listener = new UdpClient(listenEP);
            //IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
            UdpClient server       = new UdpClient(listenPort);
            Message   responseData = Utilities.Serialize(serverInfo);

            try
            {
                while (true)
                {
                    Console.WriteLine("UDP Waiting for broadcast");
                    // The groupEP gets the IP address needed for the RSVP
                    // So I could just send the port as a string, but what if some other program is using this port?
                    // Then with that an object may be best.

                    IPEndPoint clientEP = new IPEndPoint(IPAddress.Any, 0);
                    Message    message  = new Message();
                    message.data = server.Receive(ref clientEP);

                    HelloPacket receivedData = Utilities.Deserialize(message) as HelloPacket;

                    Console.WriteLine($"Received broadcast from {clientEP} :");
                    Console.WriteLine($" RSVP address {receivedData.ToString()}");

                    // Add client to the hostList
                    Console.WriteLine("Adding {0} to hostList", receivedData.ToString());
                    hostList.Add(receivedData);

                    // Send the server data to the client
                    Console.WriteLine("UDP Server sending response");
                    server.Send(responseData.data, responseData.data.Length, clientEP);

                    // Start TCP client (separate function I guess).
                    // StartTCPClient(clientInfo, serverData);
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                server.Close();
            }
        }
Ejemplo n.º 3
0
        // This is the UDP broadcast which will discover IP_Tato Servers on the network.
        // Stuff will have to be done for subnetting
        public static void StartBroadcast(HelloPacket thisHost)
        {
            // Thread BroadcastResponseListener = new Thread(() => StartTCPListener(thisHost));
            // BroadcastResponseListener.Start();
            // Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            // Console.WriteLine();

            UdpClient Client  = new UdpClient();
            Message   request = new Message();

            request = Utilities.Serialize(thisHost);

            IPEndPoint serverEP = new IPEndPoint(IPAddress.Any, 0);

            Client.EnableBroadcast = true;
            Client.Send(request.data, request.data.Length, new IPEndPoint(IPAddress.Broadcast, listenPort));

            Console.WriteLine("Message sent to the broadcast address");
            Message responseMessage = new Message(256);

            // s.Receive(responseMessage.data);

            responseMessage.data = Client.Receive(ref serverEP);
            HelloPacket responseData = (HelloPacket)Utilities.Deserialize(responseMessage);

            Console.WriteLine($"Received respone from {responseData.ToString()}");

            Client.Close();
        }
Ejemplo n.º 4
0
        public void Start()
        {
            Console.WriteLine("Starting the UDP Broadcast at {0}", ClientInfo.ToString());
            Thread UDPBroadcast = new Thread(() => JoinServer());

            UDPBroadcast.Start();

            Client client         = new Client();
            Thread ListenerThread = new Thread(() => StartListener());

            ListenerThread.IsBackground = true;
            ListenerThread.Start();
            //Console.WriteLine("IP is {0}", ClientInfo.address);
            //IPAddress localip = IPAddress.Parse((ClientInfo.address));
            //Console.WriteLine("Starting a tcplistener at {0} using port {1}", localip, ClientInfo.port);
            //TcpListener listener = new TcpListener(localip, ClientInfo.port);
            //listener.Start();
            //Console.WriteLine("Listener has started.");
            //while (true)
            //{
            //    if (listener.Pending())
            //    {
            //        Thread listenthread = new Thread(() => {
            //        DoBeginAcceptTcpClient(listener);
            //        });
            //        listenthread.Start();
            //    }
            //}
        }
Ejemplo n.º 5
0
        private void HandleClientDisconnectedEvent(object sender, HelloPacket client)
        {
            Console.WriteLine($"This Client: {client.ToString()} has disconnected.");

            // Send a message to the server informing them of the disconnection
            // **** This will use the UDP broadcast
            // **** the UDP server is still running on the server so why not send the info to it.
        }
Ejemplo n.º 6
0
        private void btn_Join_Click(object sender, RoutedEventArgs e)
        {
            // Start the network discovery
            Console.WriteLine("Starting the UDP Broadcast at {0}", clientInfo.ToString());
            Thread UDPBroadcast = new Thread(() => StartBroadcast(clientInfo));

            UDPBroadcast.Start();

            Client client         = new Client();
            Thread ListenerThread = new Thread(() => client.Start());

            ListenerThread.Start();
        }
Ejemplo n.º 7
0
        private static void Test()
        {
            Console.WriteLine("\n\nTesting the Program:");
            int udpClientNum = 2;
            // Start up the server thread.
            HelloPacket hello1     = new HelloPacket("server", "127.0.0.1", port);
            Thread      udpserver1 = new Thread(() => StartListener(hello1));

            udpserver1.Start();

            // Start up the UDP client threads
            Thread[] clientThreadList = new Thread[listenerMaxNumber];
            for (int x = 0; x < clientThreadList.Length; x++)
            {
                Console.WriteLine("Creating UDP Client {0}", x + 1);
                HelloPacket clientInfo = new HelloPacket($"client {x + 1}", $"127.0.0.{udpClientNum}", port);
                Console.WriteLine($"UDP Client Created: {clientInfo.ToString()}");
                clientThreadList[x] = new Thread(() => StartBroadcast(clientInfo));
                clientThreadList[x].IsBackground = true;
                clientThreadList[x].Start();
                Console.WriteLine("Creating TCP Client {0}", x + 1);
                clientThreadList[x] = new Thread(() => CallListenerTest(clientInfo));
                clientThreadList[x].IsBackground = true;
                clientThreadList[x].Start();
                udpClientNum++;
            }


            // Test the Client and Listener Threads

            Console.WriteLine("Press any key to start the taters...");
            Console.ReadKey();

            Thread[] taterThreadList = new Thread[taterMaxNumber];
            for (int x = 0; x < taterThreadList.Length; x++)
            {
                Console.WriteLine("Creating IP_Tato server {0}", x + 1);
                IP_Tato tater = new IP_Tato("Tater #" + x, 5);
                taterThreadList[x] = new Thread(() => ServerTest(tater));
                taterThreadList[x].Start();
            }
        }
Ejemplo n.º 8
0
        /*
         * public static void TestClientandListenerThreads()
         * {
         *  Console.WriteLine("\n\nTesting the Program:");
         *  // Test the Client and Listener Threads
         *
         *  // Create Threadstarts to the methods.
         *  // ThreadStart listenerref = new ThreadStart(CallListenerTest);
         *  Console.WriteLine("In Main: Creating the Listener threads");
         *
         *  Thread[] listenerThreadList = new Thread[listenerMaxNumber];
         *  for (int x = 0; x < listenerThreadList.Length; x++)
         *  {
         *      Console.WriteLine("Creating Listener {0}", x + 1);
         *      listenerThreadList[x] = new Thread(() => CallListenerTest()));
         *      listenerThreadList[x].IsBackground = true;
         *      listenerThreadList[x].Start();
         *  }
         *  Console.WriteLine("Press any key to continue...");
         *  Console.ReadKey();
         *
         *  Thread[] serverThreadList = new Thread[taterMaxNumber];
         *  for (int x = 0; x < serverThreadList.Length; x++)
         *  {
         *      Console.WriteLine("Creating IP_Tato server {0}", x + 1);
         *      IP_Tato tater = new IP_Tato("Tater #" + x, 5);
         *      serverThreadList[x] = new Thread(() => ServerTest(tater));
         *      serverThreadList[x].Start();
         *  }
         *
         *
         *
         *  // Create the listener thread.
         *  // Thread listenerThread = new Thread(listenerref);
         *  // This makes it so that the program never is stuck open waiting on the listener to shut down.
         *
         *
         *  // Keep going on the main thread.
         *
         *  Console.WriteLine("\n\nMain Thread has concluded.\n\n");
         *
         *  Console.ReadKey();
         * }
         */
        public static void TestUDPHello()
        {
            int udpClientNum = 2;
            // Start up the server thread.
            HelloPacket hello1     = new HelloPacket("server", "127.0.0.1", listenPort);
            Thread      udpserver1 = new Thread(() => StartListener(hello1));

            udpserver1.Start();

            // Start up the UDP client threads
            Thread[] listenerThreadList = new Thread[listenerMaxNumber];
            for (int x = 0; x < listenerThreadList.Length; x++)
            {
                Console.WriteLine("Creating UDP Client {0}", x + 1);
                HelloPacket clientInfo = new HelloPacket($"client {x+1}", $"127.0.0.{udpClientNum}", listenPort);
                Console.WriteLine($"UDP Client Created: {clientInfo.ToString()}");
                listenerThreadList[x] = new Thread(() => StartBroadcast(clientInfo));
                listenerThreadList[x].IsBackground = true;
                listenerThreadList[x].Start();
                udpClientNum++;
            }
        }
Ejemplo n.º 9
0
        private void Application_Startup()
        {
            Console.WriteLine("Application Startup has been called.");
            // TODO: add in a config screen which will update the client information.
            // IP_Tato tater = new IP_Tato();
            // ProcessPotato(tater);

            HelloPacket clientInfo = new HelloPacket("client", Networking.getExternalIPE(), 13000);

            // Start the network discovery
            Console.WriteLine("Starting the UDP Broadcast at {0}", clientInfo.ToString());
            Thread UDPBroadcast = new Thread(() => StartBroadcast(clientInfo));

            UDPBroadcast.Start();

            // Start the network listener.
            Thread ListenerThread = new Thread(() => StartListener(clientInfo));

            ListenerThread.Start();
            // When the listener processes the correct request
            // Create and show the popup.
        }
Ejemplo n.º 10
0
        private void SendCommandToClient(HelloPacket targetClient, string command)
        {
            UdpClient client  = new UdpClient();
            Message   request = new Message();

            request = Utilities.Serialize(command);

            IPEndPoint serverEP = new IPEndPoint(IPAddress.Any, 0);

            client.Send(request.data, request.data.Length, targetClient.EndPoint());

            Console.WriteLine($"Command {command} sent to the client: {targetClient.ToString()}");
            Message responseMessage = new Message(256);

            // s.Receive(responseMessage.data);

            responseMessage.data = client.Receive(ref serverEP);
            object responseData = Utilities.Deserialize(responseMessage);

            Console.WriteLine($"Received respone from {responseData.ToString()}");

            client.Close();
        }
Ejemplo n.º 11
0
        private void SendUDPToServer(string command)
        {
            UdpClient client  = new UdpClient(ClientInfo.EndPoint());
            Message   request = new Message();

            request = Utilities.Serialize(command);

            IPEndPoint clientEP = new IPEndPoint(IPAddress.Any, 0);

            client.Send(request.data, request.data.Length, ServerInfo.EndPoint());

            Console.WriteLine($"Command {command} sent to the server: {ServerInfo.ToString()}");
            Message responseMessage = new Message(256);

            // s.Receive(responseMessage.data);

            responseMessage.data = client.Receive(ref clientEP);
            object responseData = Utilities.Deserialize(responseMessage);

            Console.WriteLine($"Received respone from {responseData.ToString()}");

            client.Close();
        }
Ejemplo n.º 12
0
        /// <summary>
        /// This will be the way that clients are added to the current group.
        /// Games can be hosted as open or closed. (closed will keep new players from joining)
        /// Closed will be the default.
        ///
        /// Returns a reference to the Listener
        /// </summary>
        private void ClientDiscover()
        {
            // UDP listener logic
            Console.WriteLine($"Creating UDP server listener {HostInformation.ToString()}");

            Message responseData = Utilities.Serialize(HostInformation);

            try
            {
                // Stop listening when the game starts (in closed mode).
                while (!ClosedGame)
                {
                    Console.WriteLine("UDP Waiting for broadcast");

                    IPEndPoint clientEP = new IPEndPoint(IPAddress.Any, 0);
                    Message    message  = new Message();
                    message.data = server.Receive(ref clientEP);
                    if (!ClosedGame)
                    {
                        object receivedData = Utilities.Deserialize(message);
                        if (receivedData is HelloPacket)
                        {
                            // Add new client to Lists
                            HelloPacket newClient = receivedData as HelloPacket;
                            this.OnRaiseClientJoinedEvent(newClient);
                        }
                        if (receivedData is string)
                        {
                            switch (receivedData as string)
                            {
                            case "RESPONSE:Disconnect":
                                OnRaiseClientDisconnectedEvent(HostList.Find(HelloPacket => HelloPacket.EndPoint() == clientEP));
                                break;
                            }
                        }

                        Console.WriteLine($"Received broadcast from {clientEP} :");
                        Console.WriteLine($" RSVP address {receivedData.ToString()}");



                        // Add the client EndPoint to the ClientList
                        Console.WriteLine($"{clientEP} added to the clientlist");
                        ClientList.Add(clientEP);


                        // Send the server data to the client
                        Console.WriteLine("UDP Server sending response");
                        server.Send(responseData.data, responseData.data.Length, clientEP);
                    }
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine(e);
            }
            catch (ThreadAbortException e)
            {
                Console.WriteLine("Thread Aborted: {0}", e);
                Console.WriteLine("Disposing of UDP server");
            }
            finally
            {
                server.Close();
            }
        }
Ejemplo n.º 13
0
 private void HandleClientJoinedEvent(object sender, HelloPacket client)
 {
     Console.WriteLine($"Adding {client.ToString()} to HostList.");
     HostList.Add(client);
 }