Example #1
0
        /// <summary>
        /// Main entrypoint of the program.
        /// Starts the server, then listens for user commands.
        /// </summary>
        /// <param name="args">Command line arguments</param>
        static void Main(string[] args)
        {
            while (restart)
            {
                exit    = false;
                restart = true;
                Console.Out.WriteLine("Server autostarting at port " + PORT);
                //Start server

                ChatroomList chatroomList = new ChatroomList();

                Console.WriteLine("Loading...");
                if (LoadSaveData(chatroomList))
                {
                    Console.WriteLine("Loading successful");
                }
                else
                {
                    Console.WriteLine("No data found. Starting with defaults.");
                    ChatroomLogic mainRoom  = new ChatroomLogic();
                    ChatroomLogic mainRoom2 = new ChatroomLogic();
                    mainRoom.name  = "Global 1";
                    mainRoom2.name = "Global 2";
                    chatroomList.addChat(mainRoom);
                    chatroomList.addChat(mainRoom2);
                    ChatroomList.chatroomServices.CreateChatroom(
                        mainRoom.name, mainRoom.chatroomID, TEST_CLIENT, "pass", ChatType.MULTIPLE_USERS);
                    ChatroomList.chatroomServices.CreateChatroom(
                        mainRoom2.name, mainRoom2.chatroomID, TEST_CLIENT, "pass", ChatType.MULTIPLE_USERS);
                }

                TcpListener serverSocket
                    = new TcpListener(System.Net.IPAddress.Loopback, PORT);
                serverSocket.Start();
                Thread connectorThread = new Thread(
                    () => handleIncomingConnections(serverSocket, chatroomList));
                connectorThread.Name     = "Connector Thread";
                connectorThread.Priority = ThreadPriority.Lowest;
                connectorThread.Start();
                while (!exit)
                {
                    RespondToUserInput();
                }
                Console.WriteLine("Saving...");
                serverSocket.Stop();
                chatroomList.SendGlobalMessage(new Message {
                    chatID  = -1,
                    command = "CLOSING",
                    message = "0"
                });
                chatroomList.Stop();
                if (restart == true)
                {
                    Console.WriteLine("Restarting...");
                }
            }
        }
Example #2
0
        /// <summary>
        /// Updates all clients with the current client
        /// username list. Called when a client disconnects or connects;
        /// </summary>
        /// <param name="client"></param>
        /// <param name="chatroomList"></param>
        private void sendClientList()
        {
            String list = "";

            foreach (ClientConnection c in ClientConnection.clients)
            {
                list += c.username + ",";
            }
            chatroomList.SendGlobalMessage(new Message {
                chatID  = -1,
                command = "CLIENTLIST",
                message = list
            });
        }