static ClientLogInPacket GetLogInPacketFromBytes(byte[] buffer) { ClientLogInPacket packet = new ClientLogInPacket(); int size = Marshal.SizeOf(packet); IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.Copy(buffer, 0, ptr, size); packet = (ClientLogInPacket)Marshal.PtrToStructure(ptr, packet.GetType()); Marshal.FreeHGlobal(ptr); return(packet); }
public static void LogInHandler(StateObject state, Socket handler) { Console.WriteLine("Received 'Log In' Request from {0}:{1}", ((IPEndPoint)handler.RemoteEndPoint).Address, ((IPEndPoint)handler.RemoteEndPoint).Port); ClientLogInPacket dataReceived = GetLogInPacketFromBytes(state.buffer); //Confirm log in credentials through database string databasePath = ""; Platform platform = RunningPlatform(); if (platform == Platform.Windows) { databasePath = "Data Source=" + System.Environment.CurrentDirectory + "\\database.db; FailIfMissing=True"; } else if (platform == Platform.Mac) { databasePath = "Data Source=" + System.Environment.CurrentDirectory + "/data/database.db; FailIfMissing=True"; } SQLiteConnection db_connection = new SQLiteConnection(databasePath); db_connection.Open(); string query = "select * from Users where USERNAME = @username and PASSWORD = @password"; SQLiteCommand command = new SQLiteCommand(query, db_connection); command.Parameters.AddWithValue("@username", dataReceived.username); command.Parameters.AddWithValue("@password", dataReceived.password); SQLiteDataReader reader = command.ExecuteReader(); ServerConfirmationPacket packet; packet.ID = 0; packet.msg = 0; if (reader.HasRows) { while (reader.Read()) { //Check if the username & password are correct if ((string)reader["USERNAME"] == dataReceived.username && (string)reader["PASSWORD"] == dataReceived.password) { uint id = Convert.ToUInt32(reader["ID"]); //Check if that user has already logged in if (CheckPlayerLogged(id)) { packet.msg = (uint)ServerMessage.Server_Denied_AlreadyLogged; break; } if (packet.msg != (uint)ServerMessage.Server_Denied_AlreadyLogged) { PlayerInfo newPlayer; newPlayer.ID = id; newPlayer.handler = handler; newPlayer.authority = false; newPlayer.username = dataReceived.username; newPlayer.ready = false; newPlayer.bomb = 0; newPlayer.dead = false; newPlayer.winCount = Convert.ToUInt32(reader["WINCOUNT"]); newPlayer.ep = null; packet.ID = id; packet.msg = (uint)ServerMessage.Server_Accepted; if (players.Count < 1) { packet.msg = (uint)ServerMessage.Server_AcceptedAuthority; newPlayer.authority = true; } //Broadcast the message to the rest of the players ServerMessagePacket broadcastPacket; broadcastPacket.msg = (uint)ServerMessage.Server_PlayerConnected; Broadcast(ConnectionType.TCP, GetBytesFromPacket(broadcastPacket)); players.Add(newPlayer); } break; } } } db_connection.Close(); SendTCP(handler, GetBytesFromPacket(packet)); }