//Reads a packet of data sent from one of the clients and passes it onto its registered handler function public static void ReadClientPacket(int ClientID, string PacketData) { //Store the total set of packet data into a new PacketData object for easier reading NetworkPacket TotalPacket = new NetworkPacket(PacketData); //Fetch the client connection who sent this to us, making sure they're still active ClientConnection Client = ConnectionManager.GetClient(ClientID); if (Client == null) { MessageLog.Print("ERROR: Client #" + ClientID + " not found, unable to read network packet from them."); return; } //Iterate over all the packet data until we finished reading and handling all of it while (!TotalPacket.FinishedReading()) { //Read the packets order number and packet type enum values int OrderNumber = TotalPacket.ReadInt(); ClientPacketType PacketType = TotalPacket.ReadType(); //Get the rest of the values for this set based on the packet type, then put the orer number back in the front of it NetworkPacket SectionPacket = ReadPacketValues(PacketType, TotalPacket); //Compared this packets order number to see if its arrived in the order we were expecting int ExpectedOrderNumber = Client.LastPacketRecieved + 1; bool InOrder = OrderNumber == ExpectedOrderNumber; //If the packet arrived in order then it gets processed normally if (InOrder) { //Reset the packets data before we pass it to the handler SectionPacket.ResetRemainingData(); //Read away the packet type value as its not needed when processing packets immediately SectionPacket.ReadType(); //Pass the section packet onto its registered handler function if (PacketHandlers.TryGetValue(PacketType, out Packet Packet)) { Packet.Invoke(ClientID, ref SectionPacket); } //Store this as the last packet that we have processed for this client Client.LastPacketRecieved = OrderNumber; } //If packets arrive out of order we tell the client what number we were expecting to receive next so everything since then gets resent else { SystemPacketSender.SendMissingPacketsRequest(ClientID, ExpectedOrderNumber); return; } } }
//Sends a message to any player who just entered into or left from the battle arena that their PVP status has changed public static void AlertTravellers() { //First message all arrivals letting them know pvp is active in this area foreach (CharacterData Character in CharactersEntering) { //Get the client who controls this characters and send the message to them ClientConnection CharacterClient = ConnectionManager.GetClient(Character); SystemPacketSender.SendUIMessage(CharacterClient.ClientID, "Now entering PVP area, beware..."); } //Also message any departures letting them know pvp is no longer active foreach (CharacterData Character in CharactersExiting) { ClientConnection CharacterClient = ConnectionManager.GetClient(Character); SystemPacketSender.SendUIMessage(CharacterClient.ClientID, "Leaving PVP area, you are safe again."); } }
public static void CheckClients(float DeltaTime) { NextClientCheck -= DeltaTime; if (NextClientCheck <= 0.0f) { NextClientCheck = ClientCheckInterval; foreach (KeyValuePair <int, ClientConnection> Client in ActiveConnections) { SystemPacketSender.SendStillConnectedCheck(Client.Key); int LastHeard = Client.Value.LastCommunication.AgeInSeconds(); if (LastHeard >= ClientConnectionTimeout) { Client.Value.ConnectionDead = true; } } } }
//Tries using the command arguments for performing a player kick private void TryKickPlayer(string[] Input) { //Get the characters name string CharacterName = Input[1]; //Make sure the character exists if (!CharactersDatabase.DoesCharacterExist(CharacterName)) { MessageLog.Print("That character doesnt exist, cant kick them."); return; } //Get the client who this character belongs to ClientConnection Client = ClientSubsetFinder.GetClientUsingCharacter(CharacterName); //If the client couldnt be found then the character isnt logged in currently if (Client == null) { MessageLog.Print("That character is not in the game right now, cant kick them."); return; } //Show that the player is being kicked MessageLog.Print("Kicking " + CharacterName + " from the game..."); //Tell the client that they have been kicked from the game and mark them to be cleaned up from the game SystemPacketSender.SendKickedFromServer(Client.ClientID); Client.ConnectionDead = true; //Tell everyone else to remove the client from their games List <ClientConnection> OtherClients = ClientSubsetFinder.GetInGameClientsExceptFor(Client.ClientID); foreach (ClientConnection OtherClient in OtherClients) { PlayerManagementPacketSender.SendRemoveRemotePlayer(OtherClient.ClientID, Client.Character.Name, Client.Character.IsAlive); } }
void Awake() { Instance = this; }