public override void SendPacketToServer <T>(T packet) { NetOutgoingMessage msg = client.CreateMessage(); msg = PacketHelper.AddDefaultInformationToPacketWithId(msg, typeof(T).Name, packet.Get_PacketIdentifier(), player_id); msg = packet.PackPacketIntoMessage(msg, packet); client.SendMessage(msg, NetDeliveryMethod.ReliableOrdered); }
public void SendPacketToExistingConnection <T>(T packet, NetConnection conn, int playerID) where T : Packet <T> { NetOutgoingMessage msg = server.CreateMessage(); msg = PacketHelper.AddDefaultInformationToPacketWithId(msg, typeof(T).Name, packet.Get_PacketIdentifier(), playerID); msg = packet.PackPacketIntoMessage(msg, packet); server.SendMessage(msg, conn, NetDeliveryMethod.ReliableOrdered); }
public void SendPacketToAll <T>(T packet, int playerID) where T : Packet <T> { NetOutgoingMessage msg = server.CreateMessage(); //Console.WriteLine("Sent packet of type " + typeof(T).Name + " with identifier of " + packet.Get_PacketIdentifier()); msg = PacketHelper.AddDefaultInformationToPacketWithId(msg, typeof(T).Name, packet.Get_PacketIdentifier(), playerID); msg = packet.PackPacketIntoMessage(msg, packet); server.SendToAll(msg, NetDeliveryMethod.ReliableOrdered); }
protected override void OnDataReceived(NetIncomingMessage msg) { string packet_name = msg.ReadString(); string packet_identifier = msg.ReadString(); int player_id = msg.ReadInt32(); NetOutgoingMessage outMsg = server.CreateMessage(); if (Packet_Register.Instance.packetTypes.ContainsKey(packet_name)) { Object instance = Activator.CreateInstance(Packet_Register.Instance.packetTypes[packet_name], packet_identifier); object packet = null; bool shouldResend = false; MethodInfo openMethod = Packet_Register.Instance.packetTypes[packet_name].GetMethod("OpenPacketFromMessage"); packet = openMethod.Invoke(instance, new object[] { msg }); //If it needs to be adjusted then adjust the packet if (Packet_Register.Instance.serverPacketReceivedRegister.ContainsKey(packet_identifier)) { if (Packet_Register.Instance.serverPacketReceivedRegister[packet_identifier] == null) { shouldResend = true; } else { shouldResend = Packet_Register.Instance.serverPacketReceivedRegister[packet_identifier].Invoke(new object[] { packet, player_id, msg.SenderConnection }); } } MethodInfo packMethod = Packet_Register.Instance.packetTypes[packet_name].GetMethod("PackPacketIntoMessage"); outMsg = PacketHelper.AddDefaultInformationToPacketWithId(outMsg, packet_name, packet_identifier, player_id); outMsg = packMethod.Invoke(instance, new object[] { outMsg, packet }) as NetOutgoingMessage; if (shouldResend) { Console.WriteLine("Sent Packet To Everyone!"); server.SendToAll(outMsg, NetDeliveryMethod.ReliableOrdered); } } }