public static void Send(this byte[] buffer, ushort bytecount, short msgId, ReceiveGroup rcvGrp) { bytesmsg.buffer = buffer; bytesmsg.length = bytecount; Send(bytesmsg, msgId, rcvGrp); }
//private static List<int> reliableTargets = new List<int>(); //private static List<int> unreliableTargets = new List<int>(); public static void Send(this byte[] buffer, int bitposition, UnityEngine.Object refObj, SerializationFlags flags, bool flush = false) { var currentRoom = PhotonNetwork.CurrentRoom; if (PhotonNetwork.OfflineMode || currentRoom == null || currentRoom.Players == null) { return; } bool sendToSelf = (flags & SerializationFlags.SendToSelf) != 0; // no need to send OnSerialize messages while being alone (these are not buffered anyway) if (!sendToSelf && !TickEngineSettings.single.sendWhenSolo && currentRoom.Players.Count <= 1) { return; } ReceiveGroup sendTo = sendToSelf ? ReceiveGroup.All : ReceiveGroup.Others; int bytecount = (bitposition + 7) >> 3; var nc = PhotonNetwork.NetworkingClient; DeliveryMode deliveryMode; if (newPlayers.Count > 0) { deliveryMode = DeliveryMode.Reliable; newPlayers.Clear(); } else { bool forceReliable = (flags & SerializationFlags.ForceReliable) != 0; deliveryMode = unreliableCapable ? (forceReliable ? DeliveryMode.ReliableUnsequenced : DeliveryMode.Unreliable) : DeliveryMode.Reliable; } //if (deliveryMode != DeliveryMode.Unreliable) // Debug.LogError("Forced Reliable Send"); SendOptions sendOptions = new SendOptions() { DeliveryMode = deliveryMode }; #if PUN_2_19_OR_NEWER var slice = PhotonNetwork.NetworkingClient.LoadBalancingPeer.ByteArraySlicePool.Acquire(buffer, 0, bytecount); #else System.ArraySegment <byte> slice = new System.ArraySegment <byte>(buffer, 0, bytecount); #endif ///// Send reliable copies of the update to new connections //if (newPlayers.Count != 0) //{ // // Convert newPlayers hash into two target lists for sending. // reliableTargets.Clear(); // unreliableTargets.Clear(); // foreach(var player in PhotonNetwork.PlayerList) // { // int playerId = player.ActorNumber; // if (newPlayers.Contains(player.ActorNumber)) // { // reliableTargets.Add(playerId); // } // else // { // unreliableTargets.Add(playerId); // } // } // newPlayers.Clear(); // // TODO: this check for UDP eventually can be removed. Workaround for known PUN2 bug in sending unreliable to websockets // SendOptions forceSendOpts = new SendOptions { DeliveryMode = unreliableCapable ? DeliveryMode.ReliableUnsequenced : DeliveryMode.Reliable }; // targetRaiseOpts.TargetActors = reliableTargets.ToArray(); // nc.OpRaiseEvent(NetMsgCallbacks.DEF_MSG_ID, slice, targetRaiseOpts, forceSendOpts); // targetRaiseOpts.TargetActors = unreliableTargets.ToArray(); // nc.OpRaiseEvent(NetMsgCallbacks.DEF_MSG_ID, slice, targetRaiseOpts, sendOptions); //} //else { nc.OpRaiseEvent(NetMsgCallbacks.DEF_MSG_ID, slice, opts[(int)sendTo], sendOptions); } if (flush) { nc.Service(); } }
//private static List<int> reliableTargets = new List<int>(); //private static List<int> unreliableTargets = new List<int>(); public static void Send(this byte[] buffer, int bitposition, UnityEngine.Object refObj, SerializationFlags flags, bool flush = false) { var currentRoom = PhotonNetwork.CurrentRoom; if (PhotonNetwork.OfflineMode || currentRoom == null || currentRoom.Players == null) { return; } bool sendToSelf = (flags & SerializationFlags.SendToSelf) != 0; // no need to send OnSerialize messages while being alone (these are not buffered anyway) if (!sendToSelf && !TickEngineSettings.single.sendWhenSolo && currentRoom.Players.Count <= 1) { return; } ReceiveGroup sendTo = sendToSelf ? ReceiveGroup.All : ReceiveGroup.Others; int bytecount = (bitposition + 7) >> 3; var nc = PhotonNetwork.NetworkingClient; DeliveryMode deliveryMode; if (newPlayers.Count > 0) { deliveryMode = DeliveryMode.Reliable; newPlayers.Clear(); } else { bool forceReliable = (flags & SerializationFlags.ForceReliable) != 0; deliveryMode = unreliableCapable ? (forceReliable ? DeliveryMode.ReliableUnsequenced : DeliveryMode.Unreliable) : DeliveryMode.Reliable; } //if (deliveryMode != DeliveryMode.Unreliable) // Debug.LogError("Forced Reliable Send"); SendOptions sendOptions = new SendOptions() { DeliveryMode = deliveryMode }; var peer = PhotonNetwork.NetworkingClient.LoadBalancingPeer; #if PUN_2_19_OR_NEWER if (peer.SerializationProtocolType == SerializationProtocol.GpBinaryV16) { System.ArraySegment <byte> slice = new System.ArraySegment <byte>(buffer, 0, bytecount); nc.OpRaiseEvent(NetMsgCallbacks.DEF_MSG_ID, slice, opts[(int)sendTo], sendOptions); } else { ByteArraySlice slice = peer.ByteArraySlicePool.Acquire(buffer, 0, bytecount); nc.OpRaiseEvent(NetMsgCallbacks.DEF_MSG_ID, slice, opts[(int)sendTo], sendOptions); } #else System.ArraySegment <byte> slice = new System.ArraySegment <byte>(buffer, 0, bytecount); nc.OpRaiseEvent(NetMsgCallbacks.DEF_MSG_ID, slice, opts[(int)sendTo], sendOptions); #endif if (flush) { while (peer.SendOutgoingCommands()) { ; } } }
public static void Send(this byte[] buffer, int bitposition, GameObject refObj, ReceiveGroup rcvGrp) { int bytecount = (bitposition + 7) >> 3; //TODO replace this GC generating mess with something prealloc System.ArraySegment <byte> byteseg = new System.ArraySegment <byte>(buffer, 0, bytecount); PhotonNetwork.NetworkingClient.OpRaiseEvent(NetMsgCallbacks.DEF_MSG_ID, byteseg, opts[(int)rcvGrp], sendOpts); PhotonNetwork.NetworkingClient.Service(); }
//[System.Obsolete()] //public static void Send(this byte[] buffer, ReceiveGroup rcvGrp) //{ // BytesMessageNonalloc.buffer = buffer; // BytesMessageNonalloc.length = (ushort)buffer.Length; // Send(null, bytesmsg, NetMsgCallbacks.DEF_MSG_ID, rcvGrp); //} //[System.Obsolete()] //public static void Send(this byte[] buffer, int bitcount, short msgId, ReceiveGroup rcvGrp) //{ // BytesMessageNonalloc.buffer = buffer; // BytesMessageNonalloc.length = (ushort)((bitcount + 7) >> 3); // Send(null, bytesmsg, msgId, rcvGrp); //} //[System.Obsolete()] //public static void Send(this byte[] buffer, ushort bytecount, short msgId, ReceiveGroup rcvGrp) //{ // BytesMessageNonalloc.buffer = buffer; // BytesMessageNonalloc.length = bytecount; // Send(null, bytesmsg, msgId, rcvGrp); //} //[System.Obsolete()] //public static void Send(this byte[] buffer, short msgId, ReceiveGroup rcvGrp) //{ // BytesMessageNonalloc.buffer = buffer; // BytesMessageNonalloc.length = (ushort)buffer.Length; // Send(null, bytesmsg, msgId, rcvGrp); //} /// <summary> /// Sends byte[] to each client, making any needed per client alterations, such as changing the frame offset value in the first byte. /// </summary> public static void Send(GameObject refObj, BytesMessageNonalloc msg, short msgId, ReceiveGroup rcvGrp, int channel = Channels.DefaultUnreliable) { /// Server send to all. Owner client send to server. if (rcvGrp == ReceiveGroup.All) { if (NetworkServer.active) #if MIRROR { NetworkServer.SendToAll <BytesMessageNonalloc>(msg, channel); } #else { NetworkServer.SendByChannelToReady(refObj, msgId, msg, channel); } #endif } else if (rcvGrp == ReceiveGroup.Master) { var conn = ClientScene.readyConnection; if (conn != null) { #if MIRROR conn.Send <BytesMessageNonalloc>(msg, channel); #else conn.SendByChannel(msgId, msg, channel); conn.FlushChannels(); #endif } } /// Send To Others else { if (NetworkServer.active) { #if MIRROR foreach (NetworkConnection conn in NetworkServer.connections.Values) #else foreach (NetworkConnection conn in NetworkServer.connections) #endif { if (conn == null) { continue; } /// Don't send to self if Host if (conn.connectionId == 0) { continue; } #if MIRROR if (conn.isReady && (!refObj || refObj.GetComponent <NetworkIdentity>().observers.ContainsKey(conn.connectionId))) { conn.Send <BytesMessageNonalloc>(msg, channel); } #else if (conn.isReady && (!refObj || refObj.GetComponent <NetworkIdentity>().observers.Contains(conn))) { conn.SendByChannel(msgId, msg, channel); conn.FlushChannels(); } #endif } } /// Client's cant send to all, so we will just send to server to make 'others' always work. else if (ClientScene.readyConnection != null) { #if MIRROR ClientScene.readyConnection.Send <BytesMessageNonalloc>(msg, channel); #else ClientScene.readyConnection.SendByChannel(msgId, msg, channel); ClientScene.readyConnection.FlushChannels(); #endif } } }
//[System.Obsolete] //public static void Send(ref Bitstream buffer, short msgId, ReceiveGroup rcvGrp) //{ // BitstreamExtensions.ReadOut(ref buffer, reusableOutgoingBuffer); // BytesMessageNonalloc.buffer = reusableOutgoingBuffer; // BytesMessageNonalloc.length = (ushort)buffer.BytesUsed; // Send(null, bytesmsg, msgId, rcvGrp); //} public static void Send(this byte[] buffer, int bitcount, GameObject refObj, ReceiveGroup rcvGrp) { BytesMessageNonalloc.outgoingbuffer = buffer; BytesMessageNonalloc.length = (ushort)((bitcount + 7) >> 3); Send(refObj, bytesmsg, NetMsgCallbacks.DEF_MSG_ID, rcvGrp); }