public static void SendUpdate(ref UdpBitStream bitstream, ref UdpBitStream outstream) { // Send the bitstream to the UNET writer writer.StartMessage(masterMsgTypeId); writer.WriteUncountedByteArray(NSTMaster.bitstreamByteArray, bitstream.BytesUsed); writer.FinishMessage(); // if this is the server - send to all. if (NetworkServer.active) { writer.SendPayloadArrayToAllClients(masterMsgTypeId, Channels.DefaultUnreliable); //NetworkServer.connections[0].FlushChannels(); // If this is the server as client, run the ReceiveUpdate since local won't get this run. //if (NetworkClient.active) NSTMaster.ReceiveUpdate(ref bitstream, ref outstream, false, 0); } // if this is a client send to server. else { // TODO: find reliable way to cache this condition - Is here to eliminate some shut down warnings, and not critical if (cachedNetworkClient != null && cachedNetworkClient.isConnected) { NetworkManager.singleton.client.SendWriter(writer, Channels.DefaultUnreliable); } //NetworkManager.singleton.client.connection.FlushChannels(); } }
/// <summary> /// Updates over the network arrive here - AFTER the Update() runs (not tested for all platforms... thanks unet for the great docs.) /// The incoming bitstream is read /// </summary> /// <param name="msg"></param> private static void ReceiveUpdate(NetworkMessage msg) { UdpBitStream bitstream = new UdpBitStream(msg.reader.ReadBytesNonAlloc(NSTMaster.bitstreamByteArray, msg.reader.Length), msg.reader.Length); UdpBitStream outstream = new UdpBitStream(NSTMaster.outstreamByteArray); NSTMaster.ReceiveUpdate(ref bitstream, ref outstream, NetworkServer.active, msg.conn.connectionId); // Write a clone message and pass it to all the clients if this is the server receiving if (NetworkServer.active) // && msg.conn == nst.NI.clientAuthorityOwner) { writer.StartMessage(msg.msgType); writer.WriteUncountedByteArray(outstream.Data, outstream.BytesUsed); writer.SendPayloadArrayToAllClients(msg.msgType); if (NetworkServer.connections[0] != null) { NetworkServer.connections[0].FlushChannels(); } } }