Exemplo n.º 1
0
        /// <summary>
        /// This is meant to Manage the IP_Tato either as an object that can be returned
        ///   Or just a function to take care of the logic of the potato.
        /// </summary>
        /// <param name="tater"></param>
        private void IP_TatoHandler(IP_Tato tater)
        {
            //TODO: Finish translating what is in PassDataToClient into this function.
            tater.LastClient = HostInformation;
            while (tater.Passes > 0)
            {
                // Select the target client
                tater.TargetClient = RouteIP_Tato(tater);

                Console.WriteLine("The new targetClient is: {0}", tater.TargetClient);
                // Set up the target address
                IPEndPoint targetClient = tater.TargetClient.EndPoint();
                // Pass data to be sent to client
                // This function returns the response data from the client.
                object responseObject = PassDataToClient(tater, targetClient);

                if (responseObject is IP_Tato)
                {
                    // Do stuff with the tater (probably reroute)
                    tater = responseObject as IP_Tato;
                    if (tater.Exploded)
                    {
                        Console.WriteLine("Tater has exploded \n removing client from Hostlist.");
                        this.KickPlayer(tater.TargetClient);
                        //OnRaiseClientDisconnectedEvent(tater.TargetClient);
                    }
                }
                else if (responseObject is string)
                {
                    switch ((string)responseObject)
                    {
                    case "CMD:Disconnect":
                        this.KickPlayer(tater.TargetClient);
                        break;

                    case "RESPONSE:Disconnect":
                        OnRaiseClientDisconnectedEvent(tater.TargetClient);
                        break;
                    }
                    // Right now the only string that will be returned is that the client refused the tater.
                }

                // Verify the current state is correct
                Console.WriteLine("Server received: {0}", tater.ToString());
            }
        }
Exemplo n.º 2
0
        public static void TestTater()
        {
            // Test IP_Tato Object
            Console.WriteLine("Test the IP_Tato");
            IP_Tato tater = new IP_Tato("Tater", 5);

            Console.WriteLine("Hot IP_Tato Object:");
            Console.WriteLine(tater.ToString());

            // Test the methods within the tater
            // tater.TestMethods();

            // Serialization Test
            Console.WriteLine("Testing Serialization and Deserialization.");
            Message serializedTater = Utilities.Serialize(tater);

            Console.WriteLine("Serialized Tater: {0}", serializedTater);
            Console.WriteLine("Size of the serialized object: {0} bytes", serializedTater.data.Length);

            IP_Tato newTater = new IP_Tato();

            newTater = (IP_Tato)Utilities.Deserialize(serializedTater);
            Console.WriteLine("Deserialized Tater: {0}", newTater.ToString());
        }
Exemplo n.º 3
0
        // This listener will basically function as the client for most intents and purposes.
        public static void CallListenerTest(HelloPacket clientInfo)
        {
            TcpListener listener = null;

            try
            {
                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);
                listener = new TcpListener(localip, clientInfo.port);
                listener.Start();
                Console.WriteLine("Listener has started.");

                // Create Buffer
                byte[] buffer = new byte[buffersize];

                while (true)
                {
                    // Add an extra space to help distinguish between each server transaction.
                    Console.WriteLine();
                    Console.WriteLine("Client wating for a connection... ");

                    // Accept a pending connection
                    TcpClient client = listener.AcceptTcpClient();
                    Console.WriteLine("Connected!");

                    // Instantiate the stream
                    NetworkStream stream = client.GetStream();

                    // While there is data to be read
                    // TODO: Implement the ability to read more data with a smaller buffer.
                    while ((stream.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        try
                        {
                            // Instantiate a Message object to hold the incoming object
                            Message incomingMessage = new Message();
                            // Assign the data which has been read to incomingMessage
                            incomingMessage.data = buffer;
                            // Deserialize the inbound data into an object which can be processed
                            //   By the function or workerthread.
                            IP_Tato receivedTato = Utilities.Deserialize(incomingMessage) as IP_Tato;
                            // Verify that the server received the correct data
                            Console.WriteLine("Client Received: " + receivedTato.ToString());

                            Console.WriteLine("Processing Request...");

                            // TODO: Create a worker thread to work with the potato.
                            //      This will be especially necessary when UI gets involved.
                            // For now it is just going to call a function
                            IP_Tato objectResponse = (IP_Tato)ProcessPotato(receivedTato);

                            if (objectResponse.Exploded)
                            {
                                // Exact the consequences of an exploded tater
                            }
                            else
                            {
                                Console.WriteLine($"You have received an IP_Tato from {receivedTato.LastClient}");
                                Console.WriteLine("Press any key to send the tater back!");
                                Console.ReadKey();
                            }

                            // Instantiate a Message to hold the response message
                            Message responseMessage = new Message();
                            responseMessage = Utilities.Serialize(objectResponse);

                            // Send back a response.
                            stream.Write(responseMessage.data, 0, responseMessage.data.Length);
                            // Verify that the data sent against the client receipt.
                            Console.WriteLine("Client Sent {0}", objectResponse.ToString());
                        }
                        catch (Exception ErrorProcessRequest)
                        {
                            Console.WriteLine("The request failed to be processed. Error details: " + ErrorProcessRequest);
                        }



                        // byte[] msg = System.Text.Encoding.ASCII.GetBytes("Server Received: " + data.ToString());
                    }
                    Console.WriteLine("---Listener Transaction Closed---");
                    stream.Close();
                    client.Close();
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("Server SocketException: {0}", e);
            }
            finally
            {
                listener.Stop();
            }
        }
Exemplo n.º 4
0
        private static void ServerTest(IP_Tato tater)
        {
            // TODO Add in a test over a network connection (to verify how much it can handle)
            // TODO Figure out how to negate Denial of Service Attacks.

            // Add in tater stuff

            // Each instance of this method handles just one tater until it's lifetime expires
            // So then really this function is more like a StartTato() method.
            // Especially once potato routing is a thing. - It is a thing now.

            // When this all is converted to be OOP, then hostlist may become a part of the server class

            // for (int x = 1; x < listenerMaxNumber + 1; x++)
            // {
            //    string tempip = "127.0.0." + x;
            //    Console.WriteLine("Adding {0} to hostList", tempip);
            //    hostList.Add(tempip);
            //}



            while (tater.Passes > 0)
            {
                // Choose a targetClient
                tater.TargetClient = RouteTater(hostList, tater.LastClient);

                Console.WriteLine("The new targetClient is: {0}", tater.TargetClient);



                // Set the values which will be used for each connection
                IPAddress  serveraddress = IPAddress.Parse(tater.TargetClient.address);
                int        port          = 13000;
                IPEndPoint server        = new IPEndPoint(serveraddress, port);

                TcpClient client = new TcpClient();

                // Initialize the retry counter (which may be overkill)
                int retries;
                for (retries = 0; retries < 5; retries++)
                {
                    // Handle unseen errors
                    try
                    {
                        // Connect to the server
                        client.Connect(server);

                        // Serialize the object which will be sent
                        Message outboundMessage = Utilities.Serialize(tater);

                        // Initialize the stream
                        NetworkStream stream = client.GetStream();

                        // Send the object through the stream
                        Console.WriteLine("Server Sent {0} bytes.", outboundMessage.data.Length);
                        stream.Write(outboundMessage.data);
                        Console.WriteLine("Server Sent: {0} \n Retry #: {1}", tater.ToString(), retries);

                        // -- Part 2: Receive the response. --

                        // Create a Message object to receive the response
                        // Create Buffer
                        // The size of the buffer will need to be adapted to the
                        // Size of the IP_Tato object. --- Until I make a dynamic buffer
                        Message inboundMessage = new Message(buffersize);

                        // This reads the server's response from the network
                        // It assigns the response byte [] to the buffer.
                        int bytes = stream.Read(inboundMessage.data, 0, inboundMessage.data.Length);

                        if (inboundMessage.Equals(0))
                        {
                            Console.WriteLine("Received an empty message");
                        }

                        // Deserialize the data which was received and create a tater
                        object responseData = Utilities.Deserialize(inboundMessage) as object;
                        tater = responseData as IP_Tato;
                        // Verify the current state is correct
                        Console.WriteLine("Server received: {0}", tater.ToString());



                        // Close out the connection
                        stream.Close();
                        Console.WriteLine("---Server Transaction Closed---");
                        if (tater.Passing == false)
                        {
                            Console.WriteLine($"{tater.TargetClient} has disconnected");
                            hostList.Remove(tater.TargetClient);
                            continue;
                        }
                        // Break out of the retry loop.
                        break;
                    }
                    catch (ArgumentNullException e)
                    {
                        Console.WriteLine("Server ArgumentNullException: {0}", e);
                        continue;
                    }
                    catch (SocketException e)
                    {
                        Console.WriteLine("Server SocketException: {0}", e);
                        continue;
                    }
                }
                client.Close();
            }
            Console.WriteLine("The IP_Tato {0} has exploded.", tater.Name);
        }