Пример #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...");
                }
            }
        }
Пример #2
0
 /// <summary>
 /// One thread will be assigned to
 /// handling new connections and will create a new
 /// ClientConnection object for every new connection.
 /// </summary>
 /// <param name="serverSocket">The socket to listen for
 /// incoming connections</param>
 /// <param name="chatroomList">The current chatroomList</param>
 private static void handleIncomingConnections(
     TcpListener serverSocket, ChatroomList chatroomList)
 {
     try
     {
         while (true)
         {
             TcpClient socket = serverSocket.AcceptTcpClient();
             Console.WriteLine("A new client has connected");
             NetworkStream    stream = socket.GetStream();
             ClientConnection client
                 = new ClientConnection(stream, chatroomList);
             client.StartAsync();
         }
     }
     catch (ThreadAbortException tae) {}
     catch (SocketException se) {}
 }
Пример #3
0
 /// <summary>
 /// Helper function for loading in SQL database data.
 /// </summary>
 /// <param name="chatroomList"></param>
 /// <returns>Returns true if load was successful,
 /// false otherwise.</returns>
 private static bool LoadSaveData(ChatroomList chatroomList)
 {
     try
     {
         int       hightestChatroomID = 0;
         DataTable dataTable
             = ChatroomList.chatroomServices.GetAllChatrooms();
         if (dataTable.Rows.Count != 0)
         {
             foreach (DataRow row in dataTable.Rows)
             {
                 string        name = row["chatroomname"].ToString();
                 int           id   = int.Parse(row["chatroomid"].ToString());
                 ChatroomLogic temp = new ChatroomLogic();
                 temp.name          = name;
                 temp.chatroomID    = id;
                 hightestChatroomID =
                     hightestChatroomID > id ? hightestChatroomID : id;
                 chatroomList.addChat(temp);
                 DataTable userTable
                     = ChatroomList.chatroomServices.GetChatUsers(
                           temp.chatroomID);
                 foreach (DataRow userRow in userTable.Rows)
                 {
                     int userId = -1;
                     if (int.TryParse(userRow["userid"].ToString(),
                                      out userId))
                     {
                         temp.RegisteredUsers.Add(userId);
                     }
                 }
             }
             ChatroomLogic.numChatRoomsCreated = hightestChatroomID + 1;
             return(true);
         }
     }
     catch (Exception e) { }
     return(false);
 }
Пример #4
0
 /// <summary>
 /// Constructor that requires a network stream to listen to and
 /// a chatroom list.
 /// </summary>
 /// <param name="ns"></param>
 /// <param name="chatroomList"></param>
 public ClientConnection(NetworkStream ns, ChatroomList chatroomList)
 {
     this.networkStream = ns;
     this.chatroomList  = chatroomList;
     messageService     = new MessageService(ns);
 }