示例#1
0
        /// <summary>
        /// Incoming message receiver.
        /// </summary>
        public static void ReceiveMessage(object conn, int connId, byte[] buffer)
        {
            int bitposition = 0;
            /// Read frameId
            int frameId = (int)buffer.Read(ref bitposition, frameCountBits);

            int localFrameId = TickManager.LogIncomingFrame(connId, frameId);

            /// Read all netobjs
            while (true)
            {
                /// Read next netid
                int netid = (int)buffer.ReadPackedBytes(ref bitposition, 32);

                if (netid == 0)
                {
                    break;
                }

                /// Read hasData bool
                bool hasData = buffer.ReadBool(ref bitposition);

                /// No data... this is just a heartbeat
                if (!hasData)
                {
                    continue;
                }

                /// Read data size
                int holdDataSizePos = bitposition;
                int bitcount        = (int)buffer.Read(ref bitposition, BITS_FOR_NETOBJ_SIZE);

                var netobj = netid.FindComponentByNetId <NetObject>();
                //Debug.Log("Incoming " + netid + " " + netobj.name);

                /// If netobj can't be found, jump to the next object in the stream
                if (ReferenceEquals(netobj, null))
                {
                    bitposition = holdDataSizePos + bitcount;
                    continue;
                }

                netobj.OnDeserialize(frameId, frameId, localFrameId, buffer, ref bitposition, hasData);
            }
        }
示例#2
0
        /// <summary>
        /// Incoming message receiver.
        /// </summary>
        public static void ReceiveMessage(object conn, int connId, byte[] buffer)
        {
            int frameCount  = TickEngineSettings.frameCount;
            int bitposition = 0;


            /// Read frameId
            int originFrameId = (int)buffer.Read(ref bitposition, TickEngineSettings.frameCountBits);

            //Debug.Log("in " + originFrameId);

#if DEVELOPMENT_BUILD || UNITY_EDITOR
            if (originFrameId < 0 || originFrameId >= frameCount)
            {
                Debug.LogError("INVALID FRAME ID " + originFrameId + " CORRUPT STREAM LIKELY DUE TO READ/WRITE DISAGREEMENT.");
            }
#endif
            FrameArrival arrival;
            /*var offsets = */
            TickManager.LogIncomingFrame(connId, originFrameId, out arrival);

            ///// Read and Tick Pre Serialization
            //NetMasterCallbacks.OnPreDeserializeTickCallbacks(localFrameId, buffer, ref bitposition);

            /// Read all netobjs. Exits when we get a false leading bit.
            while (true)
            {
                /// Read next viewID
                int viewid = (int)buffer.ReadPackedBytes(ref bitposition, 32);

                if (viewid == 0)
                {
                    break;
                }

                /// Read hasData bool
                bool hasData = buffer.ReadBool(ref bitposition);

                /// No data... this is just a heartbeat
                if (!hasData)
                {
                    continue;
                }

                /// Read data size
                int holdDataSizePos     = bitposition;
                int bitcount            = (int)buffer.Read(ref bitposition, BITS_FOR_NETOBJ_SIZE);
                int expectedBitPosition = holdDataSizePos + bitcount;

                var pv     = PhotonNetwork.GetPhotonView(viewid);
                var netobj = pv ? pv.GetComponent <NetObject>() : null;

                /// If netobj can't be found, jump to the next object in the stream
                if (ReferenceEquals(netobj, null))
                {
                    bitposition = expectedBitPosition;
                    continue;
                }

                /// Determine if this object should be deserialized or ignored based on controller authority.
                if (netobj.IgnoreNonControllerUpdates)
                {
                    int controllerActorNr = pv.ControllerActorNr;
                    int ownerActorNr      = pv.OwnerActorNr;

                    if (controllerActorNr == -1)
                    {
#if UNITY_EDITOR || DEVELOPMENT_BUILD
                        Debug.Log(pv.name + ":" + pv.ViewID + " OwnershipUpdate has not yet arrived from master, so owner is still unknown. Accepting update from Actor " + connId + " as temporary controller until OwnershipUpdate arrives and sets the Owner/Controller.");
#endif
                        pv.SetControllerInternal(connId);
                    }
                    else if (controllerActorNr != connId && ownerActorNr != connId)
                    {
                        {
#if UNITY_EDITOR || DEVELOPMENT_BUILD
                            Debug.LogError("fid: " + originFrameId + "Update from ActorId: " + connId + " for object " + netobj.name + ":" + pv.ViewID + " , but that conn does not have controller authority. Current cowner:controller is "
                                           + pv.OwnerActorNr + ":" + controllerActorNr);
#endif
                            bitposition = expectedBitPosition;
                            continue;
                        }
                    }
                }



                netobj.OnDeserialize(connId, originFrameId, buffer, ref bitposition, hasData, arrival);


#if UNITY_EDITOR
                if (bitposition != expectedBitPosition)
                {
                    Debug.LogWarning("Deserialization mismatch for object " + netobj.name + " viewId: " + netobj.ViewID + " owned by: " + netobj.photonView.OwnerActorNr +
                                     ". Bitposition off by: " + (expectedBitPosition - bitposition) + " bits.");
                }
#endif
                /// Realign position for next NetObject to ensure any read/write mismatches from the previous object don't cascade.
                bitposition = expectedBitPosition;
            }
        }