示例#1
0
        public void Start(int port, int maxConnections, int maxMemory = -1)
        {
            Console.WriteLine("{0} - Starting ChatRoomServer on port {1} for {2} clients...", ChatTime.GetTimeFormat(), port, maxConnections);
            userHandler = new UserHandler("users.db", maxConnections);
            socket.Bind(new IPEndPoint(IPAddress.Any, port));
            socket.Listen(5);
            clientConnections = new ClientConnection[maxConnections];
            Process process;

            for (int i = 0; i < maxConnections; i++)
            {
                int tempI = i;
                new Thread(new ParameterizedThreadStart(delegate { ClientHandler(tempI); })).Start();
                if (i % 2000 == 0)
                {
                    process = Process.GetCurrentProcess();
                    if (maxMemory != -1 && (process.PrivateMemorySize64 / 2000000) > maxMemory)
                    {
                        Console.WriteLine("Exceeded memory limit at {0} megabytes at client number {1}.", maxMemory, i + 1);
                        break;
                    }
                }
            }
            Console.WriteLine("{0} - Done!", ChatTime.GetTimeFormat());
        }
示例#2
0
        private void ClientHandler(int id)
        {
            while (true)
            {
                ClientConnection currentClient = clientConnections[id] = new ClientConnection(id, socket.Accept());
                try
                {
                    Console.WriteLine("{0} <{1}> - Received connection from {2}", ChatTime.GetTimeFormat(), currentClient.ID, currentClient.GetConnectionString());
                    byte[]        receiveBuffer = new byte[1024 * 2];
                    int           receiveSize   = currentClient.Connection.Receive(receiveBuffer);
                    LoginExchange loginExchange = LoginExchange.Decode(receiveBuffer, receiveSize);
                    Console.WriteLine("{0} <{1}> - Login request from '{2}'.", ChatTime.GetTimeFormat(), currentClient.ID, loginExchange.Username);
                    LoginExchangeResponse loginExchangeResponse = userHandler.HandleLoginExchange(loginExchange, currentClient);

                    string loginResponseData = loginExchangeResponse.Encode();
                    byte[] loginResponse     = Encoding.UTF8.GetBytes(loginResponseData);
                    currentClient.Connection.Send(loginResponse);

                    if (loginExchangeResponse.ExchangeType == LoginExchangeResponseCode.Successful)
                    {
                        User user = userHandler.GetUser(id);
                        Console.WriteLine("{0} <{1}> - User '{2}' logged in with token '{3}'.", ChatTime.GetTimeFormat(), currentClient.ID, user.Name, user.Token);
                        while (user != null)
                        {
                            receiveBuffer = new byte[1024 * 2];
                            receiveSize   = currentClient.Connection.Receive(receiveBuffer);
                            if (receiveSize > 0)
                            {
                                ChatExchange chatExchange = ChatExchange.Decode(Encoding.UTF8.GetString(receiveBuffer, 0, receiveSize));
                                if (user.Verify(chatExchange))
                                {
                                    switch (chatExchange.ExchangeType)
                                    {
                                    case ChatExchangeType.Message:
                                        Console.WriteLine("{0} <{1}> - [{2}] {3}: {4}", ChatTime.GetTimeFormat(), currentClient.ID, user.CurrentChatRoom.Name, user.Name, chatExchange.Exchange);
                                        MessageHandler(chatExchange, user);
                                        break;

                                    case ChatExchangeType.Command:
                                        Console.WriteLine("{0} <{1}> - Command: {2}", ChatTime.GetTimeFormat(), currentClient.ID, chatExchange.Exchange);
                                        CommandHandler(chatExchange, user);
                                        break;

                                    case ChatExchangeType.ChatRoomList:
                                        Console.WriteLine("{0} <{1}> - Requested Chat Room List", ChatTime.GetTimeFormat(), currentClient.ID);
                                        ChatRoomListHandler(chatExchange, user);
                                        break;

                                    case ChatExchangeType.JoinRoom:
                                        Console.WriteLine("{0} <{1}> - Join Room: ID '{2}'", ChatTime.GetTimeFormat(), currentClient.ID, chatExchange.ExchangeTargetName);
                                        JoinRoomHandler(chatExchange, user);
                                        break;

                                    case ChatExchangeType.LeaveRoom:
                                        Console.WriteLine("{0} <{1}> - Leave Room", ChatTime.GetTimeFormat(), currentClient.ID);
                                        LeaveRoomHandler(chatExchange, user);
                                        break;

                                    case ChatExchangeType.CreateRoom:
                                        Console.WriteLine("{0} <{1}> - Create Room: '{2}'", ChatTime.GetTimeFormat(), currentClient.ID, chatExchange.ExchangeTargetName);
                                        CreateChatRoomHandler(chatExchange, user);
                                        break;

                                    case ChatExchangeType.Error:
                                        Console.WriteLine("{0} <{1}> - ChatExchange Error Occurred", ChatTime.GetTimeFormat(), currentClient.ID, chatExchange.ExchangeTargetName);
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    Console.WriteLine("{0} <{1}> - Ending connection.", ChatTime.GetTimeFormat(), currentClient.ID);
                }
                catch (SocketException)
                {
                    Console.WriteLine("{0} <{1}> - Lost connection.", ChatTime.GetTimeFormat(), currentClient.ID);
                }
                finally
                {
                    userHandler.Logout(id);
                    currentClient = null;
                }
            }
        }