private void MessageCallback(ServerClient client, string data) { Packet packet = Packet.FromRaw(this.realmData, JObject.Parse(data)); Console.WriteLine("Received packet " + packet); User user; this.connectedUsers.TryGetValue(client, out user); if (packet is AuthentificationPacket) { // Special packets, (first sent from the client) AuthentificationPacket authPacket = (AuthentificationPacket)packet; // We check if an user already exists with this name user = this.realmData.users.FindLast(delegate(User u) { return(u.name == authPacket.name); }); if (user == null) { user = this.realmData.ServerAddUser(authPacket.name); this.connectedUsers.Add(client, user); Console.WriteLine("Creating user \"" + user.name + "\" with ID " + user.getID()); // We send a notify to all users connected about the new user this.realmData.BroadcastPacketExcept(new NewUserPacket { user = user }, user); } // We send a connect notification to all users user.connected = true; this.realmData.BroadcastPacketExcept(new UserConnectedPacket { user = user, connected = true }, user); // We respond with a StatePacket that contains all synchronisation data this.SendPacket(client, new SynchronisationPacket { user = user, realmData = this.realmData }); } else { // Normal packets, we defer the execution packet.Apply(user, this.realmData); } }
private void MessageCallback(ServerClient client, byte[] data) { lock (lockProcessPacket) { User user; this.connectedUsers.TryGetValue(client, out user); Packet packet = Packet.Deserialize(data, realmData, user); Log(LogLevel.DEBUG, string.Format("{0} -> Server: {1}", user != null ? user.name : client.ID, packet)); if (packet is AuthentificationPacket) { // Special packets, (first sent from the client) AuthentificationPacket authPacket = (AuthentificationPacket)packet; // We first check if the version corresponds if (authPacket.version != RealmData.VERSION) { this.SendPacket(client, user, new AuthentificationErrorPacket { error = "Server is version " + RealmData.VERSION + " but client is version " + authPacket.version } ); return; } // Check if the user wants to use a specific id int userId; if (authPacket.id != null) { // Link key to existing id, or a new one if it doesn't exist or the keys don't match userId = RegisterUserKey(authPacket.id.Value, authPacket.hashedKey); } else { // Generate a new id and link the key to it userId = RegisterUserKey(++realmData.lastUserGivenId, authPacket.hashedKey); } user = this.realmData.users.FindLast(delegate(User u) { return(userId == u.id); }); if (user == null) { user = this.realmData.ServerAddUser(authPacket.name, userId); user.connected = true; // We send a notify to all users connected about the new user this.realmData.BroadcastPacketExcept(new NewUserPacket { user = user }, user); } else { user.connected = true; // We send a connect notification to all users this.realmData.BroadcastPacketExcept(new UserConnectedPacket { user = user, connected = true }, user); } this.connectedUsers.Add(client, user); Log(LogLevel.INFO, string.Format("Client {0} connected as {1} ({2})", client.ID, user.name, user.id)); // We respond with a StatePacket that contains all synchronisation data this.SendPacket(client, user, new SynchronisationPacket { user = user, realmData = this.realmData }); } else if (packet is StartTransactionPacket) { if (user == null) { // We ignore this packet Log(LogLevel.ERROR, string.Format("{0} ignored because unknown user {1}", packet, client.ID)); return; } // Check whether the packet was sent too quickly TimeSpan timeSinceLastTransaction = DateTime.Now - user.lastTransactionTime; if (timeSinceLastTransaction > TimeSpan.FromSeconds(3)) { // Apply the packet as normal packet.Apply(user, this.realmData); } else { // Intercept the packet, returning it to sender StartTransactionPacket transactionPacket = packet as StartTransactionPacket; transactionPacket.transaction.state = TransactionResponse.TOOFAST; this.SendPacket(client, user, new ConfirmTransactionPacket { response = transactionPacket.transaction.state, toSender = true, transaction = transactionPacket.transaction }); // Report the packet to the log Log(LogLevel.ERROR, string.Format("{0} ignored because user {1} sent a packet less than 3 seconds ago", packet, client.ID)); } } else { if (user == null) { // We ignore this package Log(LogLevel.ERROR, string.Format("{0} ignored because unknown user {1}", packet, client.ID)); return; } // Normal packets, we defer the execution packet.Apply(user, this.realmData); } } }
private void MessageCallback(ServerClient client, byte[] data) { lock (lockProcessPacket) { User user; this.connectedUsers.TryGetValue(client, out user); Packet packet = Packet.Deserialize(data, realmData, user); Console.WriteLine("Received packet " + packet); if (packet is AuthentificationPacket) { // Special packets, (first sent from the client) AuthentificationPacket authPacket = (AuthentificationPacket)packet; // We first check if the version corresponds if (authPacket.version != RealmData.VERSION) { this.SendPacket(client, user, new AuthentificationErrorPacket { error = "Server is version " + RealmData.VERSION + " but client is version " + authPacket.version } ); return; } // We check if an user already uses this key user = this.realmData.users.FindLast(delegate(User u) { return(u.hashedKey == authPacket.hashedKey); }); if (user == null) { user = this.realmData.ServerAddUser(authPacket.name, authPacket.hashedKey); user.connected = true; Console.WriteLine("Creating user \"" + user.name + "\" with ID " + user.getID()); // We send a notify to all users connected about the new user this.realmData.BroadcastPacketExcept(new NewUserPacket { user = user }, user); } else { // We send a connect notification to all users user.connected = true; this.realmData.BroadcastPacketExcept(new UserConnectedPacket { user = user, connected = true }, user); } this.connectedUsers.Add(client, user); // We respond with a StatePacket that contains all synchronisation data this.SendPacket(client, user, new SynchronisationPacket { user = user, realmData = this.realmData }); } else { if (user == null) { // We ignore this package Console.WriteLine("Ignore packet because unknown user"); return; } // Normal packets, we defer the execution packet.Apply(user, this.realmData); } } }
private void MessageCallback(ServerClient client, byte[] data) { lock (lockProcessPacket) { User user; this.connectedUsers.TryGetValue(client, out user); Packet packet = Packet.Deserialize(data, realmData, user); Log(LogLevel.DEBUG, string.Format("{0} -> Server: {1}", user != null ? user.name : client.ID, packet)); if (packet is AuthentificationPacket) { // Special packets, (first sent from the client) AuthentificationPacket authPacket = (AuthentificationPacket)packet; // We first check if the version corresponds if (authPacket.version != RealmData.VERSION) { this.SendPacket(client, user, new AuthentificationErrorPacket { error = "Server is version " + RealmData.VERSION + " but client is version " + authPacket.version } ); return; } // We check if an user already uses this key user = this.realmData.users.FindLast(delegate(User u) { return(u.hashedKey == authPacket.hashedKey); }); if (user == null) { user = this.realmData.ServerAddUser(authPacket.name, authPacket.hashedKey); user.connected = true; // We send a notify to all users connected about the new user this.realmData.BroadcastPacketExcept(new NewUserPacket { user = user }, user); } else { // We send a connect notification to all users user.connected = true; this.realmData.BroadcastPacketExcept(new UserConnectedPacket { user = user, connected = true }, user); } this.connectedUsers.Add(client, user); Log(LogLevel.INFO, string.Format("Client {0} connected as {1} ({2})", client.ID, user.name, user.hashedKey)); // We respond with a StatePacket that contains all synchronisation data this.SendPacket(client, user, new SynchronisationPacket { user = user, realmData = this.realmData }); } else { if (user == null) { // We ignore this package Log(LogLevel.ERROR, string.Format("{0} ignored because unknown user {1}", packet, client.ID)); return; } // Normal packets, we defer the execution packet.Apply(user, this.realmData); } } }