Exemplo n.º 1
0
        /// <summary>
        /// Dispatches up to one message to the rest of SteamKit
        /// </summary>
        /// <returns>True if a message was dispatched, false otherwise</returns>
        private bool DispatchMessage()
        {
            uint numPackets = ReadyMessageParts();

            if (numPackets == 0)
            {
                return(false);
            }

            MemoryStream payload = new MemoryStream();

            for (uint i = 0; i < numPackets; i++)
            {
                UdpPacket packet;

                inPackets.TryGetValue(++inSeqHandled, out packet);
                inPackets.Remove(inSeqHandled);

                packet.Payload.WriteTo(payload);
            }

            byte[] data = payload.ToArray();

            if (NetFilter != null)
            {
                data = NetFilter.ProcessIncoming(data);
            }

            DebugLog.WriteLine("UdpConnection", "Dispatching message; {0} bytes", data.Length);

            OnNetMsgReceived(new NetMsgEventArgs(data, remoteEndPoint));

            return(true);
        }
Exemplo n.º 2
0
        void ReadPacket()
        {
            // the tcp packet header is considerably less complex than the udp one
            // it only consists of the packet length, followed by the "VT01" magic
            uint packetLen   = 0;
            uint packetMagic = 0;

            byte[] packData = null;

            try
            {
                try
                {
                    packetLen   = netReader.ReadUInt32();
                    packetMagic = netReader.ReadUInt32();
                }
                catch (IOException ex)
                {
                    throw new IOException("Connection lost while reading packet header.", ex);
                }

                if (packetMagic != TcpConnection.MAGIC)
                {
                    throw new IOException("Got a packet with invalid magic!");
                }

                // rest of the packet is the physical data
                packData = netReader.ReadBytes(( int )packetLen);

                if (packData.Length != packetLen)
                {
                    throw new IOException("Connection lost while reading packet payload");
                }

                // decrypt the data off the wire if needed
                if (NetFilter != null)
                {
                    packData = NetFilter.ProcessIncoming(packData);
                }
            }
            catch (IOException ex)
            {
                DebugLog.WriteLine("TcpConnection", "Socket exception occurred while reading packet: {0}", ex);

                // signal that our connection is dead
                isConnected = false;

                Cleanup();

                OnDisconnected(EventArgs.Empty);
                return;
            }

            OnNetMsgReceived(new NetMsgEventArgs(packData, sock.RemoteEndPoint as IPEndPoint));
        }
Exemplo n.º 3
0
        // this is now a steamkit meme
        /// <summary>
        /// Nets the loop.
        /// </summary>
        void NetLoop(object param)
        {
            // poll for readable data every 100ms
            const int POLL_MS = 100;
            Socket    socket  = param as Socket;

            while (!wantsNetShutdown)
            {
                bool canRead = socket.Poll(POLL_MS * 1000, SelectMode.SelectRead);

                if (!canRead)
                {
                    // nothing to read yet
                    continue;
                }

                netLock.EnterUpgradeableReadLock();
                byte[] packData = null;

                if (netStream == null)
                {
                    break;
                }

                try
                {
                    // read the packet off the network
                    packData = ReadPacket();
                }
                catch (IOException ex)
                {
                    DebugLog.WriteLine("TcpConnection", "Socket exception occurred while reading packet: {0}", ex);

                    // signal that our connection is dead
                    Cleanup();

                    OnDisconnected(EventArgs.Empty);
                    return;
                }
                finally
                {
                    netLock.ExitUpgradeableReadLock();
                }

                // decrypt the data off the wire if needed
                if (NetFilter != null)
                {
                    packData = NetFilter.ProcessIncoming(packData);
                }

                OnNetMsgReceived(new NetMsgEventArgs(packData, socket.RemoteEndPoint as IPEndPoint));
            }
        }