private void SendUpdate() { int writepos = 0; SerializeValuesToBitstream(buffer, ref writepos); /// Serialize and send out this bitstream. if (UsingPUN) { NetMsgSends.Send(buffer, writepos, SERVER_SND_ID, ReceiveGroup.Others); } else { NetMsgSends.Send(buffer, writepos, AsServer ? SERVER_SND_ID : CLIENT_SND_ID, AsServer ? ReceiveGroup.Others : ReceiveGroup.Master); } }
private static void SerializeAllAndSend() { byte[] buffer = NetMsgSends.reusableBuffer; int bitposition = 0; SerializationFlags writeFlags; SerializationFlags flags; if (TickManager.needToSendInitialForNewConn) { writeFlags = SerializationFlags.NewConnection | SerializationFlags.ForceReliable | SerializationFlags.Force; flags = SerializationFlags.HasContent; TickManager.needToSendInitialForNewConn = false; } else { writeFlags = SerializationFlags.None; flags = SerializationFlags.None; } /// Write frameId buffer.Write((uint)_currFrameId, ref bitposition, TickEngineSettings.frameCountBits); NetMasterCallbacks.OnPreSerializeTickCallbacks(_currFrameId, buffer, ref bitposition); #region NetObject Serialization /// Loop through owned NetObjects NetObject.NetObjDictsLocked = true; NonAllocDictionary <int, NetObject> controlledObjs = NetObject.activeControlledNetObjs; SerializeNetObjDict(controlledObjs, buffer, ref bitposition, ref flags, writeFlags); //NonAllocDictionary<int, NetObject> ownedButNotControlledObjs = NetObject.activeOwnedNetObjs; //if (ownedButNotControlledObjs.Count > 0) // Debug.Log("LIMBO COUNT " + ownedButNotControlledObjs.Count); //SerializeNetObjDict(ownedButNotControlledObjs, buffer, ref bitposition, ref flags, writeFlags); //foreach (var no in ownedObjs) //{ // /// Not end of netobjs write bool // int holdStartPos = bitposition; // /// Write viewID // buffer.WritePackedBytes((uint)no.ViewID, ref bitposition, 32); // /// Write hadData bool // int holdHasDataPos = bitposition; // buffer.WriteBool(true, ref bitposition); // /// Log the data size write position and write a placeholder. // int holdDataSizePos = bitposition; // bitposition += BITS_FOR_NETOBJ_SIZE; // var objflags = no.OnNetSerialize(_currFrameId, buffer, ref bitposition, writeFlags); // /// Skip netobjs if they had nothing to say // if (objflags == SerializationFlags.None) // { // /// Rewind if this is a no-data write. // if (no.SkipWhenEmpty) // { // bitposition = holdStartPos; // } // else // { // bitposition = holdHasDataPos; // buffer.WriteBool(false, ref bitposition); // } // } // else // { // /// Revise the data size now that we know it. // flags |= objflags; // int bitcount = bitposition - holdDataSizePos; // buffer.Write((uint)bitcount, ref holdDataSizePos, BITS_FOR_NETOBJ_SIZE); // } // //Debug.Log(objflags + " / flg: " + (onNetSerialize[i] as Component).name + " " + flags); //} NetObject.NetObjDictsLocked = false; #endregion // Any deferrered Ownership changes from SyncState happen here while (NetMasterCallbacks.postSerializationActions.Count > 0) { NetMasterCallbacks.postSerializationActions.Dequeue().Invoke(); } if (flags == SerializationFlags.None) { return; } /// End of NetObject write bool buffer.WritePackedBytes(0, ref bitposition, 32); NetMsgSends.Send(buffer, bitposition, null, flags, true); }
public SerializationFlags OnNetSerialize(int frameId, byte[] buffer, ref int bitposition, SerializationFlags writeFlags) { #if PUN_2_OR_NEWER //Debug.Log(name + " OnNetSerialize NO " + pv.IsMine + " / " + pv.IsMine); if (!pv.IsMine) { return(SerializationFlags.None); } //if ((writeFlags & SerializationFlags.Force) != 0) // Debug.LogError("FORCE " + writeFlags); if (pv.Group != 0) { /// TODO: Ideally objects will not be individually serializing themselves into their own send. /// Serialize and Send this netobj state if this is a net tick. buffer = NetMsgSends.reusableNetObjBuffer; int localbitposition = 0; /// Write FrameId buffer.Write((uint)frameId, ref localbitposition, SimpleSyncSettings.FrameCountBits); /// Write not end of netObjs bool //buffer.WriteBool(true, ref localbitposition); /// Write netid buffer.WritePackedBytes((uint)netObjId, ref localbitposition, 32); /// Placeholder for data size. False means this is a contentless heartbea int holdHasDataPos = localbitposition; buffer.WriteBool(true, ref localbitposition); /// Placeholder for data size. False means this is a contentless heartbeat int holdDataSizePos = localbitposition; localbitposition += NetMaster.BITS_FOR_NETOBJ_SIZE; SerializationFlags lclflags = GenerateMessage(frameId, buffer, ref localbitposition, writeFlags); if (lclflags == SerializationFlags.None) { if (skipWhenEmpty) { return(SerializationFlags.None); } else { /// revise the hasData bool to be false and rewind the bitwriter. localbitposition = holdHasDataPos; buffer.WriteBool(false, ref bitposition); } } if (lclflags != SerializationFlags.None || !SkipWhenEmpty) { /// Revise the data size now that we know it buffer.Write((uint)(localbitposition - holdDataSizePos), ref holdDataSizePos, NetMaster.BITS_FOR_NETOBJ_SIZE); /// Write end of netObjs marker buffer.WritePackedBytes(0, ref localbitposition, 32); NetMsgSends.Send(buffer, localbitposition, gameObject, lclflags, false); } /// We sent this object at the netObj level, so we report back to the NetMaster that nothing has been added to the master byte[] send. return(SerializationFlags.None); } #endif var flags = GenerateMessage(frameId, buffer, ref bitposition, writeFlags); return(flags); }
private static void SerializeAndSendNetObjects() { byte[] buffer = NetMsgSends.reusableBuffer; int bitposition = 0; SerializationFlags writeFlags; SerializationFlags flags; if (TickManager.needToSendInitialForNewConn) { //Debug.LogError("needToSendInitialForNewConn"); //Debug.Log("<b>New connection</b> - send forced initial"); writeFlags = SerializationFlags.NewConnection | SerializationFlags.ForceReliable | SerializationFlags.Force; flags = SerializationFlags.HasChanged; // SerializationFlags.ForceReliable; TickManager.needToSendInitialForNewConn = false; } else { writeFlags = SerializationFlags.None; flags = SerializationFlags.None; } /// Write frameId buffer.Write((uint)_currFrameId, ref bitposition, frameCountBits); int cnt = onNetSerializes.Count; for (int i = 0; i < cnt; ++i) { IOnNetSerialize icb = onNetSerializes[i]; /// Not end of netobjs write bool int holdStartPos = bitposition; /// Write netid buffer.WritePackedBytes((uint)(icb as NetObject).netObjId, ref bitposition, 32); /// Write hadData bool int holdHasDataPos = bitposition; buffer.WriteBool(true, ref bitposition); /// Log the data size write position and write a placeholder. int holdDataSizePos = bitposition; bitposition += BITS_FOR_NETOBJ_SIZE; var objflags = icb.OnNetSerialize(_currFrameId, buffer, ref bitposition, writeFlags); /// Skip netobjs if they had nothing to say if (objflags == SerializationFlags.None) { /// Rewind if this is a no-data write. if (icb.SkipWhenEmpty) { bitposition = holdStartPos; //Debug.Log((icb as Component).name + " SLEEP OUT"); } else { bitposition = holdHasDataPos; buffer.WriteBool(false, ref bitposition); } } else { /// Revise the data size now that we know it. flags |= objflags; int bitcount = bitposition - holdDataSizePos; buffer.Write((uint)bitcount, ref holdDataSizePos, BITS_FOR_NETOBJ_SIZE); } //Debug.Log(objflags + " / flg: " + (onNetSerialize[i] as Component).name + " " + flags); } if (flags == SerializationFlags.None) { return; } /// End of NetObject write bool buffer.WritePackedBytes(0, ref bitposition, 32); NetMsgSends.Send(buffer, bitposition, null, flags, true); }