/// <summary> /// Sends the packet to client. /// </summary> /// <typeparam name="CustomPacketEnum">The type of the custom packet enum.</typeparam> /// <param name="packet">The packet.</param> /// <param name="targetID">The target identifier.</param> public void SendPacketToClient <CustomPacketEnum>(BasePacket <CustomPacketEnum> packet, int targetID) { ClientConnection clientCon = null; if (ParentServerConnection != null) { while (UniqueID == -1) { } } if (targetID > -1) { if (Clients.TryGetValue(targetID, out clientCon)) { if (!clientCon.ThisClient.Connected) { return; } packet.PacketOriginTotalLatency = clientCon.Ping + (packet.PacketOriginClientID != -1 ? Clients[packet.PacketOriginClientID].Ping : 0); byte[] data = packet.FinalizePacket(); try { clientCon.ThisClient.GetStream().Write(data, 0, data.Length); } catch (IOException ex) { // failed to write } } } }
/// <summary> /// Sends the packet to all clients. /// </summary> /// <typeparam name="CustomPacketEnum">The type of the custom packet enum.</typeparam> /// <param name="packet">The packet.</param> /// <param name="idToIgnore">The identifier to ignore.</param> public void SendPacketToAllClients <CustomPacketEnum>(BasePacket <CustomPacketEnum> packet, int idToIgnore = -1) { ClientListMutex.WaitOne(); foreach (KeyValuePair <int, ClientConnection> cc in Clients) { if (cc.Key == idToIgnore) { continue; } if (cc.Value.ThisClient.Connected) { packet.PacketOriginTotalLatency = cc.Value.Ping + packet.PacketOriginClientID != -1 ? Clients[packet.PacketOriginClientID].Ping : 0; byte[] data = packet.FinalizePacket(); try { cc.Value.ThisClient.GetStream().Write(data, 0, data.Length); } catch (IOException ex) { // failed to write ClientListMutex.ReleaseMutex(); } } } ClientListMutex.ReleaseMutex(); }
/// <summary> /// Sends the packet to parent. /// </summary> /// <typeparam name="T">Type of Packet.</typeparam> /// <param name="packet">The packet.</param> public void SendPacketToParent <T>(BasePacket <T> packet) { if (ParentServerConnection != null) { while (UniqueID == -1) { } packet.PacketOriginClientID = _uniqueID; byte[] data = packet.FinalizePacket(); ParentServerConnection.ThisClient.GetStream().Write(data, 0, data.Length); } }
/// <summary> /// Sends the packet to all clients. /// </summary> /// <typeparam name="CustomPacketEnum">The type of the custom packet enum.</typeparam> /// <param name="packet">The packet.</param> /// <param name="idToIgnore">The identifier to ignore.</param> public void SendPacketToAllClients <CustomPacketEnum>(BasePacket <CustomPacketEnum> packet, int idToIgnore = -1) { foreach (KeyValuePair <int, ClientConnection> cc in Clients) { if (cc.Key == idToIgnore) { continue; } if (cc.Value.ThisClient.Connected) { packet.PacketOriginTotalLatency = cc.Value.Ping + packet.PacketOriginClientID != -1 ? Clients[packet.PacketOriginClientID].Ping : 0; byte[] data = packet.FinalizePacket(); cc.Value.ThisClient.GetStream().Write(data, 0, data.Length); } } }