/// <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(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) { NetworkServer.SendToAll(msgId, msg); } } else if (rcvGrp == ReceiveGroup.Master) { if (NetworkClient.active) { NetworkManager.singleton.client.Send(msgId, msg); } } else { if (NetworkServer.active) { //#if MIRROR // foreach (NetworkConnection nc in NetworkServer.connections.Values) //#else foreach (NetworkConnection nc in NetworkServer.connections) //#endif { if (nc == null) { continue; } /// Don't send to self if Host if (nc.connectionId == 0) { continue; } if (nc.isReady) { nc.Send(msgId, msg); } } } } //nc.FlushChannels(); }
/// <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, bool flush = false) { #if BANDWIDTH_MONITOR if (monitorTime != (int)Time.time) { /// Print last total Debug.Log("Sent Bytes/Sec: " + byteCountForTime); byteCountForTime = 0; monitorTime = (int)Time.time; } byteCountForTime += BytesMessageNonalloc.length; #endif /////TEST //rcvGrp = ReceiveGroup.All; /// Client's cant send to all, so we will just send to server to make 'others' always work. if (!NetworkServer.active) { var conn = ClientScene.readyConnection; if (conn != null) { #if MIRROR conn.Send <BytesMessageNonalloc>(msg, channel); #else conn.SendByChannel(msgId, msg, channel); if (flush) { conn.FlushChannels(); } #endif } } /// Server send to all. Owner client send to server. else if (rcvGrp == ReceiveGroup.All) { #if MIRROR var ni = (refObj) ? refObj.GetComponent <NetworkIdentity>() : null; NetworkServer.SendToReady(ni, msg, channel); #else NetworkServer.SendByChannelToReady(refObj, msgId, msg, channel); #endif } /// Send To Others else { var ni = (refObj) ? refObj.GetComponent <NetworkIdentity>() : null; var observers = ni ? ni.observers : null; #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 && (observers != null && observers.ContainsKey(conn.connectionId))) { conn.Send <BytesMessageNonalloc>(msg, channel); } #else if (conn.isReady && (observers == null || observers.Contains(conn))) { conn.SendByChannel(msgId, msg, channel); if (flush) { conn.FlushChannels(); } } #endif } } }
//[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 } } }