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 (filter != null)
            {
                data = filter.ProcessIncoming(data);
            }

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

            OnNetMsgReceived(new NetMsgEventArgs(data, remoteEndPoint));

            return(true);
        }
Exemplo n.º 2
0
        void OnNetMsgReceived(object sender, NetMsgEventArgs e)
        {
            if (state == EncryptionState.Encrypted)
            {
                var plaintextData = encryption.ProcessIncoming(e.Data);
                NetMsgReceived?.Invoke(this, e.WithData(plaintextData));
                return;
            }

            var packetMsg = CMClient.GetPacketMsg(e.Data);

            if (!IsExpectedEMsg(packetMsg.MsgType))
            {
                DebugLog.WriteLine(nameof(EnvelopeEncryptedConnection), "Rejected EMsg: {0} during channel setup", packetMsg.MsgType);
                return;
            }

            switch (packetMsg.MsgType)
            {
            case EMsg.ChannelEncryptRequest:
                HandleEncryptRequest(packetMsg);
                break;

            case EMsg.ChannelEncryptResult:
                HandleEncryptResult(packetMsg);
                break;
            }
        }
Exemplo n.º 3
0
        // this is now a steamkit meme
        /// <summary>
        /// Nets the loop.
        /// </summary>
        void NetLoop()
        {
            // poll for readable data every 100ms
            const int POLL_MS = 100;

            while (!cancellationToken.IsCancellationRequested)
            {
                bool canRead = false;

                try
                {
                    canRead = socket.Poll(POLL_MS * 1000, SelectMode.SelectRead);
                }
                catch (SocketException ex)
                {
                    DebugLog.WriteLine("TcpConnection", "Socket exception while polling: {0}", ex);
                    break;
                }

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

                byte[] packData = null;

                try
                {
                    // read the packet off the network
                    packData = ReadPacket();

                    // 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);
                    break;
                }

                try
                {
                    OnNetMsgReceived(new NetMsgEventArgs(packData, destination));
                }
                catch (Exception ex)
                {
                    DebugLog.WriteLine("TcpConnection", "Unexpected exception propogated back to NetLoop: {0}", ex);
                }
            }

            // Thread is shutting down, ensure socket is shut down and disposed
            bool userShutdown = cancellationToken.IsCancellationRequested;

            if (userShutdown)
            {
                Shutdown();
            }
            Release(userShutdown);
        }