/// <summary> /// Send a new remote function call to the specified player. /// </summary> void SendRFC(byte rfcID, string rfcName, int target, bool reliable, params object[] objs) { if (hasBeenDestroyed) { return; } if (TNManager.isConnected) { BinaryWriter writer = TNManager.BeginSend(Packet.ForwardToPlayer); writer.Write(target); writer.Write(GetUID(uid, rfcID)); if (rfcID == 0) { writer.Write(rfcName); } writer.WriteArray(objs); TNManager.EndSend(reliable); } else if (target == TNManager.playerID) { if (rfcID != 0) { Execute(rfcID, objs); } else { Execute(rfcName, objs); } } }
/// <summary> /// Send a new RFC call to the specified target. /// </summary> static void SendRFC(uint objID, byte rfcID, string rfcName, Target target, bool reliable, params object[] objs) { #if UNITY_EDITOR if (!Application.isPlaying) { return; } #endif bool executeLocally = false; if (target == Target.Host && TNManager.isHosting) { // We're the host, and the packet should be going to the host -- just echo it locally executeLocally = true; } else if (TNManager.isInChannel) { // We want to echo UDP-based packets locally instead of having them bounce through the server if (!reliable) { if (target == Target.All) { target = Target.Others; executeLocally = true; } else if (target == Target.AllSaved) { target = Target.OthersSaved; executeLocally = true; } } byte packetID = (byte)((int)Packet.ForwardToAll + (int)target); BinaryWriter writer = TNManager.BeginSend(packetID); writer.Write(GetUID(objID, rfcID)); if (rfcID == 0) { writer.Write(rfcName); } UnityTools.Write(writer, objs); TNManager.EndSend(reliable); } else if (!TNManager.isConnected && (target == Target.All || target == Target.AllSaved)) { // Offline packet executeLocally = true; } if (executeLocally) { if (rfcID != 0) { TNObject.FindAndExecute(objID, rfcID, objs); } else { TNObject.FindAndExecute(objID, rfcName, objs); } } }
public void DestroySelf() { if (!mDestroyed) { mDestroyed = true; if (TNManager.IsInChannel(channelID)) { if (TNManager.IsChannelLocked(channelID)) { Debug.LogWarning("Trying to destroy an object in a locked channel. Call will be ignored."); } else { Invoke("EnsureDestroy", 5f); BinaryWriter bw = TNManager.BeginSend(Packet.RequestDestroyObject); bw.Write(channelID); bw.Write(uid); TNManager.EndSend(channelID, true); } } else { if (onDestroy != null) { onDestroy(); } Object.Destroy(gameObject); } } }
/// <summary> /// Broadcast a remote function call to all players on the network. /// </summary> static void BroadcastToLAN(int port, uint objID, byte rfcID, string rfcName, params object[] objs) { BinaryWriter writer = TNManager.BeginSend(Packet.ForwardToAll); writer.Write(GetUID(objID, rfcID)); if (rfcID == 0) { writer.Write(rfcName); } UnityTools.Write(writer, objs); TNManager.EndSend(port); }
/// <summary> /// Transfer this object to another channel. Only the object's owner can perform this action. /// </summary> public void TransferToChannel(int newChannelID) { if (!mDestroyed && isMine && channelID != newChannelID && TNManager.IsInChannel(channelID)) { mDestroyed = true; BinaryWriter writer = TNManager.BeginSend(Packet.RequestTransferObject); writer.Write(channelID); writer.Write(newChannelID); writer.Write(uid); TNManager.EndSend(channelID, true); } }
public static void Send <T>(T message, int type) { string messageStr = JsonMapper.ToJson(message); if (TNManager.isConnected) { BinaryWriter write = TNManager.BeginSend(Packet.SelfServerPacket); write.Write(type); write.Write(messageStr); TNManager.EndSend(); } }
/// <summary> /// Remove a previously saved remote function call. /// </summary> static void RemoveSavedRFC(uint objID, byte rfcID, string funcName) { if (TNManager.isInChannel) { BinaryWriter writer = TNManager.BeginSend(Packet.RequestRemoveRFC); writer.Write(GetUID(objID, rfcID)); if (rfcID == 0) { writer.Write(funcName); } TNManager.EndSend(); } }
/// <summary> /// Send a new remote function call to the specified player. /// </summary> static void SendRFC(uint objID, byte rfcID, string rfcName, Player target, bool reliable, params object[] objs) { if (TNManager.isInChannel) { BinaryWriter writer = TNManager.BeginSend(Packet.ForwardToPlayer); writer.Write(target.id); writer.Write(GetUID(objID, rfcID)); if (rfcID == 0) { writer.Write(rfcName); } UnityTools.Write(writer, objs); TNManager.EndSend(reliable); } }
/// <summary> /// Broadcast a remote function call to all players on the network. /// </summary> void BroadcastToLAN(int port, byte rfcID, string rfcName, params object[] objs) { if (hasBeenDestroyed) { return; } BinaryWriter writer = TNManager.BeginSend(Packet.ForwardToAll); writer.Write(GetUID(uid, rfcID)); if (rfcID == 0) { writer.Write(rfcName); } writer.WriteArray(objs); TNManager.EndSend(port); }
public void DestroySelf() { if (!hasBeenDestroyed) { hasBeenDestroyed = true; if (TNManager.isConnected) { StartCoroutine(EnsureDestroy()); TNManager.BeginSend(Packet.RequestDestroy).Write(uid); TNManager.EndSend(); } else { GameObject.Destroy(gameObject); } } }
/// <summary> /// Send a new RFC call to the specified target. /// </summary> void SendRFC(byte rfcID, string rfcName, string targetName, bool reliable, params object[] objs) { #if UNITY_EDITOR if (!Application.isPlaying) { return; } #endif if (hasBeenDestroyed || string.IsNullOrEmpty(targetName)) { return; } if (targetName == TNManager.playerName) { if (rfcID != 0) { Execute(rfcID, objs); } else { Execute(rfcName, objs); } } else { BinaryWriter writer = TNManager.BeginSend(Packet.ForwardByName); writer.Write(targetName); writer.Write(GetUID(uid, rfcID)); if (rfcID == 0) { writer.Write(rfcName); } writer.WriteArray(objs); TNManager.EndSend(reliable); } }
/// <summary> /// Send a new RFC call to the specified target. /// </summary> void SendRFC(byte rfcID, string rfcName, Target target, bool reliable, params object[] objs) { #if UNITY_EDITOR if (!Application.isPlaying) { return; } #endif if (mDestroyed) { return; } if ((target == Target.AllSaved || target == Target.Others) && TNManager.IsChannelLocked(channelID)) { #if UNITY_EDITOR Debug.LogError("Can't send persistent RFCs while in a locked channel"); #endif return; } // Some very odd special case... sending a string[] as the only parameter // results in objs[] being a string[] instead, when it should be object[string[]]. if (objs != null && objs.GetType() != typeof(object[])) { objs = new object[] { objs } } ; bool executeLocally = false; bool connected = TNManager.isConnected; if (target == Target.Broadcast) { if (connected) { BinaryWriter writer = TNManager.BeginSend(Packet.Broadcast); writer.Write(TNManager.playerID); writer.Write(channelID); writer.Write(GetUID(uid, rfcID)); if (rfcID == 0) { writer.Write(rfcName); } writer.WriteArray(objs); TNManager.EndSend(channelID, reliable); } else { executeLocally = true; } } else if (target == Target.Admin) { if (connected) { BinaryWriter writer = TNManager.BeginSend(Packet.BroadcastAdmin); writer.Write(TNManager.playerID); writer.Write(channelID); writer.Write(GetUID(uid, rfcID)); if (rfcID == 0) { writer.Write(rfcName); } writer.WriteArray(objs); TNManager.EndSend(channelID, reliable); } else { executeLocally = true; } } else if (target == Target.Host && TNManager.isHosting) { // We're the host, and the packet should be going to the host -- just echo it locally executeLocally = true; } else { if (!connected || !reliable) { if (target == Target.All) { target = Target.Others; executeLocally = true; } else if (target == Target.AllSaved) { target = Target.OthersSaved; executeLocally = true; } } if (connected && TNManager.IsInChannel(channelID)) { byte packetID = (byte)((int)Packet.ForwardToAll + (int)target); BinaryWriter writer = TNManager.BeginSend(packetID); writer.Write(TNManager.playerID); writer.Write(channelID); writer.Write(GetUID(uid, rfcID)); if (rfcID == 0) { writer.Write(rfcName); } writer.WriteArray(objs); TNManager.EndSend(channelID, reliable); } } if (executeLocally) { if (rfcID != 0) { Execute(rfcID, objs); } else { Execute(rfcName, objs); } } } /// <summary> /// Send a new RFC call to the specified target. /// </summary> void SendRFC(byte rfcID, string rfcName, string targetName, bool reliable, params object[] objs) { #if UNITY_EDITOR if (!Application.isPlaying) { return; } #endif if (mDestroyed || string.IsNullOrEmpty(targetName)) { return; } if (targetName == TNManager.playerName) { if (rfcID != 0) { Execute(rfcID, objs); } else { Execute(rfcName, objs); } } else { BinaryWriter writer = TNManager.BeginSend(Packet.ForwardByName); writer.Write(TNManager.playerID); writer.Write(targetName); writer.Write(channelID); writer.Write(GetUID(uid, rfcID)); if (rfcID == 0) { writer.Write(rfcName); } writer.WriteArray(objs); TNManager.EndSend(channelID, reliable); } } /// <summary> /// Send a new remote function call to the specified player. /// </summary> void SendRFC(byte rfcID, string rfcName, int target, bool reliable, params object[] objs) { if (mDestroyed) { return; } if (TNManager.isConnected) { BinaryWriter writer = TNManager.BeginSend(Packet.ForwardToPlayer); writer.Write(TNManager.playerID); writer.Write(target); writer.Write(channelID); writer.Write(GetUID(uid, rfcID)); if (rfcID == 0) { writer.Write(rfcName); } writer.WriteArray(objs); TNManager.EndSend(channelID, reliable); } else if (target == TNManager.playerID) { if (rfcID != 0) { Execute(rfcID, objs); } else { Execute(rfcName, objs); } } } /// <summary> /// Broadcast a remote function call to all players on the network. /// </summary> void BroadcastToLAN(int port, byte rfcID, string rfcName, params object[] objs) { if (mDestroyed) { return; } BinaryWriter writer = TNManager.BeginSend(Packet.ForwardToAll); writer.Write(TNManager.playerID); writer.Write(channelID); writer.Write(GetUID(uid, rfcID)); if (rfcID == 0) { writer.Write(rfcName); } writer.WriteArray(objs); TNManager.EndSendToLAN(port); }