Exemplo n.º 1
0
 public void ReadTick()
 {
     lock (this._pendingReadBuffers)
     {
         while (this._deletionQueue.Count > 0)
         {
             this._pendingReadBuffers.Remove(this._deletionQueue.Dequeue());
         }
         uint local_0;
         while (this.IsPacketAvailable(out local_0))
         {
             byte[]   local_1 = this._bufferPool.Count != 0 ? this._bufferPool.Dequeue() : new byte[Math.Max(local_0, 4096U)];
             uint     local_3;
             CSteamID local_2;
             bool     local_4;
             lock (this.SteamLock)
                 local_4 = SteamNetworking.ReadP2PPacket(local_1, (uint)local_1.Length, out local_3, out local_2, this._channel);
             if (local_4)
             {
                 if (this._readEvent != null && !this._readEvent(local_1, (int)local_3, local_2))
                 {
                     this._bufferPool.Enqueue(local_1);
                 }
                 else
                 {
                     if (!this._pendingReadBuffers.ContainsKey(local_2))
                     {
                         this._pendingReadBuffers[local_2] = new Queue <SteamP2PReader.ReadResult>();
                     }
                     this._pendingReadBuffers[local_2].Enqueue(new SteamP2PReader.ReadResult(local_1, local_3));
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
// ReSharper disable once UnusedMember.Local
        private void Update()
        {
            if (!IsInitialized)
            {
                return;
            }

            Profiler.BeginSample("SteamClient.RunCallbacks");
            SteamAPI.RunCallbacks();
            Profiler.EndSample();

            Profiler.BeginSample("SteamAvatarCache.Cycle");
            SteamAvatarCache.Cycle();
            Profiler.EndSample();

            Profiler.BeginSample("SteamNetworking.P2P");
            uint DataAvailable = 0;

            while (SteamNetworking.IsP2PPacketAvailable(out DataAvailable))
            {
                var      dest = new byte[DataAvailable];
                CSteamID steamid;
                if (SteamNetworking.ReadP2PPacket(dest, DataAvailable, out DataAvailable, out steamid))
                {
                    ProcessSteamNetworkingPacket(dest, steamid);
                }
            }
            Profiler.EndSample();
        }
Exemplo n.º 3
0
        private void Update()
        {
            uint size;

            while (SteamNetworking.IsP2PPacketAvailable(out size))
            {
                byte[]   buffer = new byte[size];
                uint     bytesRead;
                CSteamID remoteId;

                if (SteamNetworking.ReadP2PPacket(buffer, size, out bytesRead, out remoteId))
                {
                    switch ((eventCode)buffer[0])
                    {
                    case eventCode.Internal:
                        UnpackInternal(buffer, remoteId);
                        break;

                    case eventCode.Rpc:
                        UnpackRpc(buffer);
                        break;

                    case eventCode.Sync:
                        UnpackSync(buffer);
                        break;

                    default:
                        UnpackEvent(buffer, remoteId);
                        break;
                    }
                }
            }
        }
Exemplo n.º 4
0
        public static Tuple <UInt64, byte[]> ReadBytes()
        {
            UInt32 msgSize = 0;
            bool   result  = SteamNetworking.IsP2PPacketAvailable(out msgSize, 0);

            if (!result)
            {
                return(null);
            }

            var      packet = new byte[msgSize];
            CSteamID steamIDRemote;
            UInt32   bytesRead = 0;

            byte[] Bytes = new byte[msgSize];
            if (SteamNetworking.ReadP2PPacket(packet, msgSize, out bytesRead, out steamIDRemote, 0))
            {
                for (int i = 0; i < msgSize; i++)
                {
                    Bytes[i] = packet[i];
                }
            }

            return(new Tuple <UInt64, byte[]>((UInt64)steamIDRemote, Bytes));
        }
Exemplo n.º 5
0
    void getNetworkData()
    {
        //Recieve packets from other members in the lobby with us
        uint msgSize;

        while (SteamNetworking.IsP2PPacketAvailable(out msgSize))
        {
            byte[]   packet = new byte[msgSize];
            CSteamID steamIDRemote;
            uint     bytesRead = 0;
            if (SteamNetworking.ReadP2PPacket(packet, msgSize, out bytesRead, out steamIDRemote))
            {
                int    TYPE = packet[0];
                string msg  = System.Text.Encoding.UTF8.GetString(SubArray(packet, 1, packet.Length - 1));

                switch (TYPE)
                {
                case 1:     //Packet type 1 == GOT IN-GAME CHAT MESSAGE
                    Debug.log(lobby_names_[getPlayerIndex(steamIDRemote)] + " says: " + msg);
                    break;

                default: Debug.Log("BAD PACKET"); break;
                }
            }
        }
    }
Exemplo n.º 6
0
        public static void PacketCollectionThread()
        {
            uint     packetSize;
            uint     readBytes;
            CSteamID remoteID;

            byte[] data;

            while (SteamNetworking.IsP2PPacketAvailable(out packetSize))
            {
                data = new byte[packetSize];
                if (SteamNetworking.ReadP2PPacket(data, packetSize, out readBytes, out remoteID))
                {
                    CustomPacket newPacket = ParseCustomPacket(data);

                    if (newPacket != null)
                    {
                        if (newPacket.PACKETID >= GlobalPacketID && !newPacket.ignorePacketID)
                        {
                            PacketsReady.Add(newPacket);
                            GlobalPacketID = newPacket.PACKETID + 1;
                        }
                        else
                        {
                            //Junk packet
                        }
                    }
                }
            }
        }
        public static IEnumerable <SteamPacket> ReadPackets(int recvChannel)
        {
            while (SteamNetworking.IsP2PPacketAvailable(out uint size, recvChannel))
            {
                byte[] data = new byte[size];

                if (!SteamNetworking.ReadP2PPacket(data, size, out uint sizeRead, out CSteamID remote, recvChannel))
                {
                    continue;
                }
                if (data.Length <= 0)
                {
                    continue;
                }

                var    reader     = new ByteReader(data);
                byte   flags      = reader.ReadByte();
                bool   joinPacket = (flags & 1) > 0;
                bool   reliable   = (flags & 2) > 0;
                bool   hasChannel = (flags & 4) > 0;
                ushort channel    = hasChannel ? reader.ReadUShort() : (ushort)0;

                yield return(new SteamPacket()
                {
                    remote = remote, data = reader, joinPacket = joinPacket, reliable = reliable, channel = channel
                });
            }
        }
Exemplo n.º 8
0
    public unsafe static SteamPacket ReadPacket()
    {
        if (!_initialized)
        {
            return(null);
        }
        uint size;

        if (SteamNetworking.IsP2PPacketAvailable(out size))
        {
            byte[] data;
            if (size > kPacketBufferSize)
            {
                data = new byte[size];
            }
            else
            {
                data = _packetData;
            }
            CSteamID user;
            if (SteamNetworking.ReadP2PPacket(data, size, out size, out user))
            {
                if (data == _packetData)
                {
                    data = new byte[size];
                    Array.Copy(_packetData, data, size);
                }
                return(new SteamPacket {
                    data = data,
                    connection = User.GetUser(user)
                });
            }
        }
        return(null);
    }
Exemplo n.º 9
0
        private static void ReceivePackets(int channel = -1)
        {
            int i   = 0;
            int max = 10;

            if (channel != -1)
            {
                i   = channel;
                max = channel + 1;
            }
            for (i = 0; i < max; i++)
            {
                while (SteamNetworking.IsP2PPacketAvailable(out uint messageSize, i))
                {
                    Console.WriteLine("Packet available of size: " + messageSize);
                    byte[] pubDest = new byte[messageSize];
                    if (SteamNetworking.ReadP2PPacket(pubDest, messageSize, out uint bytesRead, out CSteamID steamIdRemote, i))
                    {
                        switch (i)
                        {
                        case BB_LevelData:
                            IsActive = true;
                            WorldConfigFile config = (WorldConfigFile)CalcHelper.ConvertToObject(pubDest);
                            config.LoadIntoEditor();
                            break;

                        case BB_Ready:
                            _playersReadyCount++;
                            break;

                        case BB_StartTime:
                            _startTime = new DateTime((long)CalcHelper.ConvertToObject(pubDest));
                            break;

                        case BB_TileIdChange:
                            Packet.TileIdChange packet = (Packet.TileIdChange)CalcHelper.ConvertToObject(pubDest);
                            LevelEditor.UpdateTileFromP2P(packet);
                            break;

                        case BB_AllEntities:
                            UpdateEntities(pubDest);
                            break;

                        case BB_EntityUpdate:
                            UpdateEntities(pubDest);
                            break;
                        }

                        // Relay message to others.
                        if (IsHost)
                        {
                            var allButSender = (from client in OtherPlayers
                                                where client != steamIdRemote
                                                select client).ToList();
                            Send(pubDest, EP2PSend.k_EP2PSendReliable, i, allButSender);
                        }
                    }
                }
            }
        }
Exemplo n.º 10
0
 public void ReadTick()
 {
     lock (this._pendingReadBuffers)
     {
         while (this._deletionQueue.Count > 0)
         {
             this._pendingReadBuffers.Remove(this._deletionQueue.Dequeue());
         }
         uint local_2;
         while (this.IsPacketAvailable(out local_2))
         {
             byte[]   local_3 = this._bufferPool.Count != 0 ? this._bufferPool.Dequeue() : new byte[(int)Math.Max(local_2, 4096U)];
             uint     local_5;
             CSteamID local_4;
             bool     local_6;
             lock (this.SteamLock)
                 local_6 = SteamNetworking.ReadP2PPacket(local_3, (uint)local_3.Length, ref local_5, ref local_4, this._channel);
             if (local_6)
             {
                 if (this._readEvent == null || this._readEvent(local_3, (int)local_5, local_4))
                 {
                     if (!this._pendingReadBuffers.ContainsKey(local_4))
                     {
                         this._pendingReadBuffers[local_4] = new Queue <SteamP2PReader.ReadResult>();
                     }
                     this._pendingReadBuffers[local_4].Enqueue(new SteamP2PReader.ReadResult(local_3, local_5));
                 }
                 else
                 {
                     this._bufferPool.Enqueue(local_3);
                 }
             }
         }
     }
 }
Exemplo n.º 11
0
        protected bool Receive(out uint readPacketSize, out SteamId clientSteamID, out byte[] receiveBuffer, int channel)
        {
            readPacketSize = 0;
            clientSteamID  = new SteamId();
            receiveBuffer  = null;

            if (!Steamworks.SteamClient.IsValid)
            {
                throw new ObjectDisposedException("Steamworks");
            }

            if (SteamNetworking.IsP2PPacketAvailable(channel))
            {
                packet = SteamNetworking.ReadP2PPacket(channel);

                if (packet != null)
                {
                    readPacketSize = (uint)packet.Value.Data.Length;
                    clientSteamID  = packet.Value.SteamId;
                    receiveBuffer  = packet.Value.Data;
                }

                return(packet != null);
            }

            return(false);
        }
Exemplo n.º 12
0
    void BattlesFinish()
    {
        MSM.CloseMainSkillMenu();
        Learning           = false;
        WaitingSkillLevels = false;
        TimeCount          = 0;
        ResetSelf();
        Debug.Log("All Battle Finished" + RoundNow);
        GameObject safeground = GameObject.FindGameObjectWithTag("Ground");

        Destroy(safeground);
        GameObject[] pcs = GameObject.FindGameObjectsWithTag("Player");
        foreach (GameObject pc in pcs)
        {
            Destroy(pc);
        }
        RoundNow = -10;
        TOmb     = null;
        ShowMC();
        testMenu02.LeaveLobby();
        uint msgSize;

        while (SteamNetworking.IsP2PPacketAvailable(out msgSize))
        {
            byte[]   packet = new byte[msgSize];
            CSteamID steamIDRemote;
            uint     bytesRead = 0;
            if (SteamNetworking.ReadP2PPacket(packet, msgSize, out bytesRead, out steamIDRemote))
            {
                Debug.Log("PACKET after Exit");
            }
        }
    }
Exemplo n.º 13
0
 public void ReadTick()
 {
     lock (this._pendingReadBuffers)
     {
         while (this._deletionQueue.Count > 0)
         {
             this._pendingReadBuffers.Remove(this._deletionQueue.Dequeue());
         }
         uint size1;
         while (this.IsPacketAvailable(out size1))
         {
             byte[]   data = this._bufferPool.Count != 0 ? this._bufferPool.Dequeue() : new byte[(int)Math.Max(size1, 4096U)];
             uint     size2;
             CSteamID index;
             bool     flag;
             lock (this.SteamLock)
                 flag = SteamNetworking.ReadP2PPacket(data, (uint)data.Length, ref size2, ref index, this._channel);
             if (flag)
             {
                 if (this._readEvent == null || this._readEvent(data, (int)size2, index))
                 {
                     if (!this._pendingReadBuffers.ContainsKey(index))
                     {
                         this._pendingReadBuffers[index] = new Queue <SteamP2PReader.ReadResult>();
                     }
                     this._pendingReadBuffers[index].Enqueue(new SteamP2PReader.ReadResult(data, size2));
                 }
                 else
                 {
                     this._bufferPool.Enqueue(data);
                 }
             }
         }
     }
 }
Exemplo n.º 14
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            uint size;

            long oldPos = incoming.Position;

            incoming.Position = incoming.Seek(0, SeekOrigin.End);
            while (incoming.Length - oldPos < count)//(SteamNetworking.IsP2PPacketAvailable(out size, conn.channel))
            {
                //Log.trace("" + incoming.Length + " " + incoming.Position + " " + oldPos + " " + count);
                if (!SteamNetworking.IsP2PPacketAvailable(out size, conn.channel))
                {
                    Thread.Sleep(1);
                    continue;
                }
                byte[]   tmp = new byte[size];
                CSteamID id  = new CSteamID();
                SteamNetworking.ReadP2PPacket(tmp, size, out size, out id, conn.channel);
                if (id.m_SteamID != conn.friend.id)
                {
                    continue;
                }
                incoming.Write(tmp, 0, (int)size);
            }
            incoming.Position = oldPos;

            return(incoming.Read(buffer, offset, count));
        }
Exemplo n.º 15
0
        public static IEnumerable <SteamPacket> ReadPackets()
        {
            while (SteamNetworking.IsP2PPacketAvailable(out uint size, 0))
            {
                byte[] data = new byte[size];

                if (!SteamNetworking.ReadP2PPacket(data, size, out uint sizeRead, out CSteamID remote, 0))
                {
                    continue;
                }
                if (data.Length <= 0)
                {
                    continue;
                }

                var  reader     = new ByteReader(data);
                byte info       = reader.ReadByte();
                bool joinPacket = (info & 1) > 0;
                bool reliable   = (info & 2) > 0;

                yield return(new SteamPacket()
                {
                    remote = remote, data = reader, joinPacket = joinPacket, reliable = reliable
                });
            }
        }
Exemplo n.º 16
0
        void Update()
        {
            uint size;

            try
            {
                while (SteamNetworking.IsP2PPacketAvailable(out size))
                {
                    var      buffer = new byte[size];
                    uint     bytesRead;
                    CSteamID remoteId;
                    if (SteamNetworking.ReadP2PPacket(buffer, size, out bytesRead, out remoteId))
                    {
                        var          message = Encoding.UTF8.GetString(buffer).Replace(" ", "");
                        PlayerPacket info    = new PlayerPacket(message);
                        if (info.playerId != SteamAPI.GetUserID() && SteamAPI.getLobbyID().m_SteamID != 0)
                        {
                            UpsertPlayer(info);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
            }
        }
Exemplo n.º 17
0
    public VoicePacketInfo Receive(GamePacket packet)
    {
        CSteamID id;
        uint     packSize;

        if (SteamNetworking.IsP2PPacketAvailable(out packSize))
        {
            if (packSize > packet.MaxCapacity)
            {
                return(VoicePacketInfo.InvalidPacket);
            }

            uint b;

            SteamNetworking.ReadP2PPacket(temp1024.Data, packSize, out b, out id);

            VoicePacketInfo info = new VoicePacketInfo();
            info.NetId           = temp1024.ReadUInt(0);
            info.Frequency       = temp1024.ReadUShort();
            info.Channels        = temp1024.ReadByte();
            info.Format          = (AudioDataTypeFlag)temp1024.ReadByte();
            info.ValidPacketInfo = true;

            packet.WriteByteData(temp1024.Data, FirstPacketByteAvailable, (int)b);

            return(info);
        }

        return(VoicePacketInfo.InvalidPacket);
    }
Exemplo n.º 18
0
 public static void ClearChannel(int channel)
 {
     while (SteamNetworking.IsP2PPacketAvailable(out uint size, channel))
     {
         SteamNetworking.ReadP2PPacket(new byte[size], size, out uint sizeRead, out CSteamID remote, channel);
     }
 }
Exemplo n.º 19
0
        public unsafe void Update()
        {
            fixed(byte *ptr = _readBuffer)
            {
                uint    dataSize = 0u;
                SteamId userId   = 0UL;

                while (SteamNetworking.ReadP2PPacket(_readBuffer, ref dataSize, ref userId))
                {
                    var handle  = new AccountHandle(userId);
                    var handled = false;
                    foreach (var lobby in _connectedLobbies.Values)
                    {
                        if (lobby.Members.TryGetValue(handle, out LobbyMember member))
                        {
                            member.DispatchNetworkMessage(new ReadOnlySpan <byte>(_readBuffer, 0, (int)dataSize));
                            handled = true;
                        }
                    }
                    if (!handled)
                    {
                        Debug.LogWarning($"[Steam] Unexpected network message from user: {userId}");
                    }
                }
            }
        }
Exemplo n.º 20
0
        /// <summary>
        /// A simple little function to join force-join a server unauthenticated and send chat messages.
        /// </summary>
        void DoNonAuthenticatedChat()
        {
            // Make steam happy.
            new Thread(() => {
                SteamAPI.RunCallbacks();
                if (SteamNetworking.IsP2PPacketAvailable(out uint packetSize, (int)CWTNet.Channel.SC))
                {
                    byte[] data = new byte[packetSize];
                    bool ok     = SteamNetworking.ReadP2PPacket(data, packetSize, out uint newPacketSize, out CSteamID remoteSteamID, (int)CWTNet.Channel.SC);
                }
            }).Start();

            // Join the game.
            var joinPacket = new CWTNet.Packets.CS.ClientJoinPacket();

            PacketUtil.SendP2PPacket(m_hostSteamID, joinPacket);

            Thread.Sleep(1000);

            Console.WriteLine("Type your messages:");
            while (true)
            {
                var message = Console.ReadLine();
                PacketUtil.SendP2PPacket(m_hostSteamID, new CWTNet.Packets.CS.ClientChatMessage(message));
            }
        }
Exemplo n.º 21
0
        void PlayingFunc()
        {
            uint networkSize;
            var  networkBuffer    = new byte[8000];
            var  inputBuffer      = new byte[8000];
            var  decompressBuffer = new byte[20000];

            while (true)
            {
                if (Program.CloseOnNext)
                {
                    return;
                }
                if (SteamAPI.IsSteamRunning() && SteamNetworking.IsP2PPacketAvailable(out networkSize))
                {
                    CSteamID remotUSer;
                    if (SteamNetworking.ReadP2PPacket(networkBuffer, (uint)networkBuffer.Length, out networkSize, out remotUSer))
                    {
                        PlaySoundFromNetworkData(remotUSer.m_SteamID, networkBuffer, networkSize, inputBuffer, decompressBuffer);
                    }
                }
                else
                {
                    Thread.Sleep(networkCheckIntervalMs);
                }
                NotifyNotTalking();
            }
        }
Exemplo n.º 22
0
    public void Receive()
    {
        System.Diagnostics.Stopwatch timeSpentProcessingPackets = new System.Diagnostics.Stopwatch();
        timeSpentProcessingPackets.Start();

        /*
         * if (failedPackets.Count > 0) {
         *  MeshPacket p = failedPackets[0];
         *  failedPackets.RemoveAt(0);
         *  ParseData(p);
         * }
         */

        uint bufferLength = 0;

        while (timeSpentProcessingPackets.Elapsed.TotalMilliseconds < msAllowedForPackets)
        {
            bufferLength = 0;
            if (SteamNetworking.IsP2PPacketAvailable(out bufferLength))
            {
                //Debug.Log("Receiving Packet, " + bufferLength + " bytes long");
                byte[]   destBuffer = new byte[bufferLength];
                UInt32   bytesRead  = 0;
                CSteamID remoteID;
                SteamNetworking.ReadP2PPacket(destBuffer, bufferLength, out bytesRead, out remoteID);
                //Debug.Log("CSteamID remoteID = " + remoteID.m_SteamID);
                ParseData(new MeshPacket(destBuffer));
            }
            else
            {
                break;
            }
        }
    }
 public unsafe void Update()
 {
     fixed(byte *ptr = _readBuffer)
     {
         while (SteamNetworking.ReadP2PPacket(_readBuffer, kMaxMessageSize,
                                              out uint dataSize, out CSteamID userId))
         {
             var handle  = new AccountHandle(userId.m_SteamID);
             var handled = false;
             foreach (var lobby in _connectedLobbies.Values)
             {
                 if (lobby.Members.TryGetValue(handle, out LobbyMember member))
                 {
                     var buffer = new FixedBuffer(ptr, dataSize);
                     member.DispatchNetworkMessage(buffer);
                     handled = true;
                 }
             }
             if (!handled)
             {
                 Debug.LogWarning($"[Steam] Unexpected network message from user: {userId}");
             }
         }
     }
 }
Exemplo n.º 24
0
        /// <summary>
        /// Check for and return any packets sent to us
        /// </summary>
        /// <param name="from">SteamId of the player who sent us this packet</param>
        /// <returns>Packet in BMSByte format</returns>
        public BMSByte Receive(out SteamId from)
        {
            CheckDisposed();

            if (!SteamNetworking.IsP2PPacketAvailable())
            {
                from = default;
                return(null);
            }

            recBuffer.Clear();

            var packet = SteamNetworking.ReadP2PPacket();

            if (packet.HasValue)
            {
                from = packet.Value.SteamId;
                recBuffer.SetSize(packet.Value.Data.Length);
                recBuffer.byteArr = packet.Value.Data;
                return(recBuffer);
            }

            from = default;
            return(null);
        }
Exemplo n.º 25
0
 private bool ReadP2PPacket(bool isServer, byte[] data, out uint msgSize, out CSteamID id)
 {
     if (isServer)
     {
         return(SteamGameServerNetworking.ReadP2PPacket(data, (uint)data.Length, out msgSize, out id, 0));
     }
     return(SteamNetworking.ReadP2PPacket(data, (uint)data.Length, out msgSize, out id, 0));
 }
Exemplo n.º 26
0
 protected bool ReceiveInternal(out uint readPacketSize, out CSteamID clientSteamID)
 {
     if (!SteamManager.Initialized)
     {
         throw new ObjectDisposedException("Steamworks");
     }
     return(SteamNetworking.ReadP2PPacket(receiveBufferInternal, 1, out readPacketSize, out clientSteamID, (int)SteamChannels.SEND_INTERNAL));
 }
        private void Update()
        {
            if (PlayerCount != PlayerList.Count)
            {
                PlayerCount = PlayerList.Count;
                UpdatePlayerList();
            }

            #region Recieve Data Packet

            uint size;
            while (SteamNetworking.IsP2PPacketAvailable(out size))
            {
                var      data = new byte[size];
                uint     bytesRead;
                CSteamID remoteId;
                if (!SteamNetworking.ReadP2PPacket(data, size, out bytesRead, out remoteId))
                {
                    continue;
                }
                var package = (P2PPackage) new BinaryFormatter().Deserialize(new MemoryStream(data));
                //Debug.Log("从 " + SteamFriends.GetFriendPersonaName(remoteId) + " 收到包" + package.type + " ID " +
                //          package.Object_identity);
                if (NetworkLobbyManager.instance.lobby.m_SteamID != 0)
                {
                    Analyze(package, remoteId);
                }
            }

            #endregion

            JunkPackagetime -= Time.deltaTime;
            if (JunkPackagetime <= 0)
            {
                JunkPackagetime = 1 / 8f;
                if (NetworkLobbyManager.instance.lobby.m_SteamID != 0)
                {
                    SendPackets(new P2PPackage(null, P2PPackageType.Broadcast), EP2PSend.k_EP2PSendUnreliable, false);
                }
            }

            if (checking_joined)
            {
                var ready = true;
                foreach (var t in player_need_to_connect)
                {
                    if (!t)
                    {
                        ready = false;
                    }
                }
                if (ready)
                {
                    events.JoinedLobby.Invoke();
                    checking_joined = false;
                }
            }
        }
Exemplo n.º 28
0
        public void ReadTick()
        {
            bool flag1 = false;
            Dictionary <CSteamID, Queue <SteamP2PReader.ReadResult> > pendingReadBuffers;

            try
            {
                Monitor.Enter((object)(pendingReadBuffers = this._pendingReadBuffers), ref flag1);
                while (this._deletionQueue.Count > 0)
                {
                    this._pendingReadBuffers.Remove(this._deletionQueue.Dequeue());
                }
                uint size1;
                while (this.IsPacketAvailable(out size1))
                {
                    byte[]   data  = this._bufferPool.Count != 0 ? this._bufferPool.Dequeue() : new byte[(IntPtr)Math.Max(size1, 4096U)];
                    bool     flag2 = false;
                    object   steamLock;
                    uint     size2;
                    CSteamID index;
                    bool     flag3;
                    try
                    {
                        Monitor.Enter(steamLock = this.SteamLock, ref flag2);
                        flag3 = SteamNetworking.ReadP2PPacket(data, (uint)data.Length, ref size2, ref index, this._channel);
                    }
                    finally
                    {
                        if (flag2)
                        {
                            Monitor.Exit(steamLock);
                        }
                    }
                    if (flag3)
                    {
                        if (this._readEvent == null || this._readEvent(data, (int)size2, index))
                        {
                            if (!this._pendingReadBuffers.ContainsKey(index))
                            {
                                this._pendingReadBuffers[index] = new Queue <SteamP2PReader.ReadResult>();
                            }
                            this._pendingReadBuffers[index].Enqueue(new SteamP2PReader.ReadResult(data, size2));
                        }
                        else
                        {
                            this._bufferPool.Enqueue(data);
                        }
                    }
                }
            }
            finally
            {
                if (flag1)
                {
                    Monitor.Exit((object)pendingReadBuffers);
                }
            }
        }
 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);
                 }
             }
Exemplo n.º 30
0
 public void ReadMessages()
 {
     while (SteamNetworking.IsP2PPacketAvailable(out var size))
     {
         var data   = new byte[size];
         var packet =
             SteamNetworking.ReadP2PPacket(data, (uint)data.Length, out var msgSize, out var steamUser);
         ReadData(data);
     }
 }