private void ReadPackets(bool multithreaded)
 {
     for (int i = 0; i < 2; i++)
     {
         uint pcubMsgSize = 0u;
         while (SteamNetworking.IsP2PPacketAvailable(out pcubMsgSize, i) && (NetGame.isNetStarting || NetGame.isNetStarted))
         {
             int    num          = NetStream.CalculateTierForSize((int)pcubMsgSize);
             byte[] array        = NetStream.AllocateBuffer(num);
             uint   pcubMsgSize2 = 0u;
             if (SteamNetworking.ReadP2PPacket(array, pcubMsgSize, out pcubMsgSize2, out CSteamID psteamIDRemote, i))
             {
                 if (array[0] == 0)
                 {
                     int num2 = array[1] * 256 * 256 + array[2] * 256 + array[3];
                     if (multipartBuffer == null)
                     {
                         int num3 = NetStream.CalculateSizeForTier(0) - 4;
                         multipartBufferTier   = NetStream.CalculateTierForSize((num2 + 1) * num3);
                         multipartBuffer       = NetStream.AllocateBuffer(multipartBufferTier);
                         multipartBufferOffset = 0u;
                     }
                     Array.Copy(array, 4L, multipartBuffer, multipartBufferOffset, pcubMsgSize2 - 4);
                     multipartBufferOffset += pcubMsgSize2 - 4;
                     NetStream.ReleaseBuffer(num, array);
                     if (num2 == 0)
                     {
                         num             = multipartBufferTier;
                         array           = multipartBuffer;
                         pcubMsgSize2    = multipartBufferOffset;
                         multipartBuffer = null;
                     }
                     else
                     {
                         array = null;
                     }
                 }
                 if (array != null)
                 {
                     if (multithreaded && i == 0)
                     {
                         lock (reliableQueueLock)
                         {
                             reliableQueue.Enqueue(new QueuedData
                             {
                                 steamIDRemote = psteamIDRemote,
                                 buffer        = array,
                                 bufferTier    = num,
                                 bytesRead     = (int)pcubMsgSize2
                             });
                         }
                         continue;
                     }
                     NetGame.instance.OnData(psteamIDRemote, array, num, (int)pcubMsgSize2);
                     NetStream.ReleaseBuffer(num, array);
                 }
             }
Exemple #2
0
 public bool CanBuffer(NetStream stream, int expectedContainerId)
 {
     if (unreliableStream.position == 0)
     {
         return(true);
     }
     if (currentBufferTag != NetGame.currentLevelInstanceID)
     {
         return(false);
     }
     if (expectedContainerId != -1 && expectedContainerId != writeFrameId)
     {
         return(false);
     }
     return(unreliableStream.UseBuffedSize() + stream.UseBuffedSize() + 2 < NetStream.CalculateSizeForTier(0));
 }
Exemple #3
0
        public static int CalcMaxPossibleSizeForContainerContents(int tier)
        {
            int num  = NetStream.CalculateSizeForTier(tier);
            int num2 = num;
            int num3 = CalcMaxEstimate(num2);

            num2 -= num3 - num;
            for (num3 = CalcMaxEstimate(num2); num3 < num; num3 = CalcMaxEstimate(++num2))
            {
            }
            while (num3 > num)
            {
                num3 = CalcMaxEstimate(--num2);
            }
            if (num2 > 4194303)
            {
                num2 = 4194303;
            }
            return(num2);
        }
 public void Apply()
 {
     if (exitingLevel || !isStarted)
     {
         return;
     }
     if (ReplayRecorder.isPlaying)
     {
         ReplayRecorder.instance.Play(this);
         return;
     }
     if (!validated)
     {
         validated = true;
         int num  = CalculateMaxDeltaSizeInBits(netId);
         int num2 = NetHost.CalcMaxPossibleSizeForContainerContentsTier0() * 8;
         if (num > num2)
         {
             Debug.LogErrorFormat(this, "Net Scope \"{0}\" (netid={1}) might produce a data packet larger than {2} bytes, which could fail to send on some platforms! Estimated worst-case size of this scope is {3} bytes; maximum safe limit is {4} bytes", base.name, netId, NetStream.CalculateSizeForTier(0), (num + 7) / 8, num2 >> 3);
         }
     }
     if (NetGame.isClient)
     {
         lock (frames.framesLock)
         {
             if (frames.frameQueue.Count == 0)
             {
                 return;
             }
             FrameState frameState = frames.frameQueue[frames.frameQueue.Count - 1];
             int        frameId    = frameState.frameId;
             if (frameId < renderedFrame)
             {
                 maxLagFrames = Mathf.Min(30, maxLagFrames + 1);
                 framesWithImprovedLagCount = 0;
                 couldImproveBy             = 30;
                 renderedFrame = frameId;
             }
             if (frameId - maxLagFrames > renderedFrame)
             {
                 renderedFrame = frameId - maxLagFrames;
             }
             if (renderedFrame + 1 <= frameId)
             {
                 framesWithImprovedLagCount++;
                 couldImproveBy = Mathf.Min(couldImproveBy, frameId - (renderedFrame + 1));
                 if (framesWithImprovedLagCount > 600 && maxLagFrames > 2)
                 {
                     maxLagFrames--;
                     couldImproveBy--;
                     if (couldImproveBy == 0)
                     {
                         framesWithImprovedLagCount = 0;
                         couldImproveBy             = 30;
                     }
                 }
             }
             else
             {
                 framesWithImprovedLagCount = 0;
                 couldImproveBy             = 30;
             }
             lagReceiveToRender = frameId - renderedFrame;
         }
         int num3 = RenderState(frames, renderedFrame, fraction / Time.fixedDeltaTime);
         int num4 = PlaybackEvents(frames, 0, renderedFrame);
         lock (frames.framesLock)
         {
             frames.DropOldEvents(num4 + 1);
             lastFrame0idx = num3;
             int frameId2 = Mathf.Min(lastBaseFrame, lastFrame0idx - 30);
             frames.DropOldStates(frameId2);
         }
     }
 }