Example #1
0
 private static void SendMessage(Socket clientSocket, IPEndPoint endPt, PRSMessage msg)
 {
     msg.SendMessage(clientSocket, endPt);
 }
Example #2
0
        static void Main(string[] args)
        {
            // defaults
            ushort SERVER_PORT          = 30000;
            ushort STARTING_CLIENT_PORT = 40000;
            ushort ENDING_CLIENT_PORT   = 40099;
            int    KEEP_ALIVE_TIMEOUT   = 10; //300

            try
            {
                // -p < service port >
                // -s < starting client port number >
                // -e < ending client port number >
                // -t < keep alive time in seconds >

                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i] == "-p")
                    {
                        SERVER_PORT = ushort.Parse(args[i + 1]);
                    }
                    if (args[i] == "-s")
                    {
                        SERVER_PORT = ushort.Parse(args[i + 1]);
                    }
                    if (args[i] == "-e")
                    {
                        ENDING_CLIENT_PORT = ushort.Parse(args[i + 1]);
                    }
                    if (args[i] == "-t")
                    {
                        KEEP_ALIVE_TIMEOUT = int.Parse(args[i + 1]);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            // initialize the PRS server
            PRS prs = new PRS(STARTING_CLIENT_PORT, ENDING_CLIENT_PORT, KEEP_ALIVE_TIMEOUT);

            // create the socket for receiving messages at the server
            Socket listeningSocket = new Socket(SocketType.Dgram, ProtocolType.Udp);

            // bind the listening socket to the PRS server port
            listeningSocket.Bind(new IPEndPoint(IPAddress.Any, SERVER_PORT));

            //
            // Process client messages
            //

            while (!prs.Stopped)
            {
                EndPoint clientEndPoint = null;
                try
                {
                    // receive a message from a client
                    clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
                    PRSMessage msg = PRSMessage.ReceiveMessage(listeningSocket, ref clientEndPoint);

                    // let the PRS handle the message
                    PRSMessage response = prs.HandleMessage(msg);

                    // send response message back to client
                    response.SendMessage(listeningSocket, clientEndPoint);
                }
                catch (Exception ex)
                {
                    // attempt to send a UNDEFINED_ERROR response to the client, if we know who that was
                    if (clientEndPoint != null)
                    {
                        PRSMessage errorMsg = new PRSMessage(PRSMessage.MESSAGE_TYPE.RESPONSE, "", 0, PRSMessage.STATUS.UNDEFINED_ERROR);
                        errorMsg.SendMessage(listeningSocket, clientEndPoint);
                    }
                }
            }

            // close the listening socket
            listeningSocket.Close();
            // wait for a keypress from the user before closing the console window
            Console.WriteLine("Press Enter to exit");
            Console.ReadKey();
        }
Example #3
0
        static void Main(string[] args)
        {
            // defaults
            ushort SERVER_PORT          = 30000;
            ushort STARTING_CLIENT_PORT = 40000;
            ushort ENDING_CLIENT_PORT   = 40099;
            int    KEEP_ALIVE_TIMEOUT   = 10;

            try
            {
                // process command options
                // -p < service port >
                // -s < starting client port number >
                // -e < ending client port number >
                // -t < keep alive time in seconds >
                for (int i = 0; i < args.Length; i++)
                {
                    switch (args[i])
                    {
                    case "-p":
                        SERVER_PORT = Convert.ToUInt16(args[++i]);
                        break;

                    case "-s":
                        STARTING_CLIENT_PORT = Convert.ToUInt16(args[++i]);
                        break;

                    case "-e":
                        ENDING_CLIENT_PORT = Convert.ToUInt16(args[++i]);
                        break;

                    case "-t":
                        KEEP_ALIVE_TIMEOUT = Convert.ToInt16(args[++i]);
                        break;
                    }
                }

                if (STARTING_CLIENT_PORT <= SERVER_PORT || STARTING_CLIENT_PORT >= ENDING_CLIENT_PORT)
                {
                    throw new Exception("Invalid range: -p must be outside of -s to -e range and -e must be larger then -s");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Invalid Command line Arguments");
                Console.WriteLine(ex);
                Usage();
            }


            // initialize the PRS server
            PRS prs = new PRS(STARTING_CLIENT_PORT, ENDING_CLIENT_PORT, KEEP_ALIVE_TIMEOUT);

            // create the socket for receiving messages at the server
            Socket listeningSocket = new Socket(SocketType.Dgram, ProtocolType.Udp);

            // bind the listening socket to the PRS server port
            listeningSocket.Bind(new IPEndPoint(IPAddress.Any, SERVER_PORT));

            // Process client messages

            while (!prs.Stopped)
            {
                EndPoint clientEndPoint = null;
                try
                {
                    // receive a message from a client
                    clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
                    PRSMessage msg = PRSMessage.ReceiveMessage(listeningSocket, ref clientEndPoint);

                    // let the PRS handle the message
                    PRSMessage responMessage = prs.HandleMessage(msg);

                    // send response message back to client
                    responMessage.SendMessage(listeningSocket, clientEndPoint);
                }
                catch (Exception ex)
                {
                    // attempt to send a UNDEFINED_ERROR response to the client, if we know who that was
                    if (clientEndPoint != null)
                    {
                        PRSMessage errorMessage = new PRSMessage(PRSMessage.MESSAGE_TYPE.RESPONSE, "", 0, PRSMessage.STATUS.UNDEFINED_ERROR);
                    }
                }
            }

            // close the listening socket
            listeningSocket.Close();

            // wait for a keypress from the user before closing the console window
            Console.WriteLine("Press Enter to exit");
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            // defaults
            ushort SERVER_PORT          = 30000;
            ushort STARTING_CLIENT_PORT = 40000;
            ushort ENDING_CLIENT_PORT   = 40099;
            int    KEEP_ALIVE_TIMEOUT   = 300;

            // process command options
            // -p < service port >
            // -s < starting client port number >
            // -e < ending client port number >
            // -t < keep alive time in seconds >

            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i])
                {
                case "-p":
                {
                    SERVER_PORT = ushort.Parse(args[++i]);
                }
                break;

                case "-s":
                {
                    STARTING_CLIENT_PORT = ushort.Parse(args[++i]);
                }
                break;

                case "-e":
                {
                    ENDING_CLIENT_PORT = ushort.Parse(args[++i]);
                }
                break;

                case "-t":
                {
                    KEEP_ALIVE_TIMEOUT = int.Parse(args[++i]);
                }
                break;

                default:
                {
                    Console.WriteLine($"Error: Invalid argument - {args[i]}");
                    return;
                }
                }
            }

            // check for valid STARTING_CLIENT_PORT and ENDING_CLIENT_PORT
            if (STARTING_CLIENT_PORT >= ENDING_CLIENT_PORT ||
                STARTING_CLIENT_PORT == 0 ||
                ENDING_CLIENT_PORT == 0 ||
                STARTING_CLIENT_PORT == SERVER_PORT ||
                ENDING_CLIENT_PORT == SERVER_PORT)
            {
                Console.WriteLine("Error: Invalid starting and/or ending port(s)");
                return;
            }

            Console.WriteLine("Server starting...");
            Console.WriteLine($"\tServer port: {SERVER_PORT}");
            Console.WriteLine($"\tStarting port: {STARTING_CLIENT_PORT}");
            Console.WriteLine($"\tEnding port: {ENDING_CLIENT_PORT}");
            Console.WriteLine($"\tTimeout: {KEEP_ALIVE_TIMEOUT}");

            // initialize the PRS server
            PRS prs = new PRS(STARTING_CLIENT_PORT, ENDING_CLIENT_PORT, KEEP_ALIVE_TIMEOUT);

            // create the socket for receiving messages at the server
            Socket socket = new Socket(SocketType.Dgram, ProtocolType.Udp);

            // bind the listening socket to the PRS server port
            socket.Bind(new IPEndPoint(IPAddress.Any, SERVER_PORT));

            //
            // Process client messages
            //

            while (!prs.Stopped)
            {
                try
                {
                    // receive a message from a client
                    EndPoint   remoteEndPoint  = new IPEndPoint(IPAddress.Any, 0);
                    PRSMessage messageReceived = PRSMessage.ReceiveMessage(socket, ref remoteEndPoint);

                    // let the PRS handle the message
                    PRSMessage messageResponse = prs.HandleMessage(messageReceived);

                    // send response message back to client
                    messageResponse.SendMessage(socket, remoteEndPoint);
                }
                catch (Exception ex)
                {
                    // attempt to send a UNDEFINED_ERROR response to the client, if we know who that was
                    Console.WriteLine(ex.Message);
                }
            }

            // close the listening socket
            socket.Close();

            // wait for a keypress from the user before closing the console window
            Console.WriteLine("Press Enter to exit");
            Console.ReadKey();
        }