public bool Register(string host, ushort port, string username, string password) { Connect(host, port); if (socket.Connected) { socket.Send(Encoding.UTF8.GetBytes(LoginExchange.Encode(LoginExchangeType.Register, username, password))); byte[] responseBuffer = new byte[1024 * 2]; int responseSize = socket.Receive(responseBuffer); LoginExchangeResponse response = LoginExchangeResponse.Decode(responseBuffer, responseSize); LatestLoginAttemtResponse = response.ExchangeType; switch (LatestLoginAttemtResponse) { case LoginExchangeResponseCode.Successful: LoggedIn = true; Username = username; LoginToken = response.LoginToken; return(true); default: LoggedIn = false; return(false); } } else { LoggedIn = false; return(false); } }
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; } } }