public void Send(MeshPacket packet) { //Debug.Log("Send(): Dest. object: " + packet.GetTargetObjectId()); if (meshnet.database == null) { Debug.LogError("Trying to send packet when database does not exist."); } if (packet.GetTargetPlayerId() == meshnet.GetLocalPlayerID()) { ParseData(packet); return; } byte[] data = packet.GetSerializedBytes(); Player[] allPlayers = meshnet.database.GetAllPlayers(); if (packet.GetTargetPlayerId() == (byte)ReservedPlayerIDs.Broadcast) { foreach (Player p in allPlayers) { if (p.GetUniqueID() == meshnet.GetLocalPlayerID() && packet.GetPacketType() == PacketType.TransformUpdate) { continue; } SteamNetworking.SendP2PPacket(new CSteamID(p.GetUniqueID()), data, (uint)data.Length, packet.qos); } } else { SteamNetworking.SendP2PPacket(new CSteamID(packet.GetTargetPlayerId()), data, (uint)data.Length, packet.qos); } }
void ParseData(MeshPacket incomingPacket) { //Debug.Log("Packet parsing: type = " + incomingPacket.GetPacketType() + ", source playerID = " + incomingPacket.GetSourcePlayerId() + ", target objectID = " + incomingPacket.GetTargetObjectId()); if (incomingPacket.GetSourcePlayerId() == meshnet.GetLocalPlayerID()) { //Debug.Log("Discarding packet from self"); //return; } if (incomingPacket.GetPacketType() == PacketType.PlayerJoin) { Debug.Log("PlayerJoin packet identified"); if (meshnet.database == null) { Debug.LogError("Database not intialized yet!"); return; } if (meshnet.database.GetAuthorized() == false) { Debug.Log("I'm not the provider. Discarding PlayerJoin packet"); return; } CSteamID sID = new CSteamID(incomingPacket.GetSourcePlayerId()); Player p = meshnet.ConstructPlayer(sID); meshnet.database.AddPlayer(p, true); return; } else if (incomingPacket.GetPacketType() == PacketType.DatabaseUpdate) { if (meshnet.database == null) { Debug.Log("Received first database update, no database to send it to."); Debug.Log("Rerouting to MeshNetwork."); meshnet.InitializeDatabaseClientside(incomingPacket); return; } } else if (incomingPacket.GetPacketType() == PacketType.KickPacket) { meshnet.initiateDisconnect(); } //If the packet is neither a PlayerJoin or a DatabaseUpdate Player source = meshnet.database.LookupPlayer(incomingPacket.GetSourcePlayerId()); //retrieve which player sent this packet if (source == null) //hmmm, the NBD can't find the player { Debug.LogError("Player from which packet originated does not exist on local NDB."); return; } MeshNetworkIdentity targetObject = meshnet.database.LookupObject(incomingPacket.GetTargetObjectId()); if (targetObject == null) { Debug.LogError("Packet's target object doesn't exist on the database!"); //Debug.LogError("type = " + incomingPacket.GetPacketType() + ", sourceObject = " + incomingPacket.GetSourceObjectId() + ", source player = " + incomingPacket.GetSourcePlayerId() + ", target object = " + incomingPacket.GetTargetObjectId()); return; } targetObject.ReceivePacket(incomingPacket); }