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, log);

            if (packetMsg == null)
            {
                log.LogDebug(nameof(EnvelopeEncryptedConnection), "Failed to parse message during channel setup, shutting down connection");
                Disconnect(userInitiated: false);
                return;
            }
            else if (!IsExpectedEMsg(packetMsg.MsgType))
            {
                log.LogDebug(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;
            }
        }
Ejemplo n.º 2
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();

            log.LogDebug("UdpConnection", "Dispatching message; {0} bytes", data.Length);

            NetMsgReceived?.Invoke(this, new NetMsgEventArgs(data, CurrentEndPoint !));

            return(true);
        }
Ejemplo n.º 3
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;
            }
        }
Ejemplo n.º 4
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;

            DebugLog.Assert(cancellationToken != null, nameof(TcpConnection), "null cancellationToken in NetLoop");

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

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

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

                byte[] packData;

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

                try
                {
                    NetMsgReceived?.Invoke(this, new NetMsgEventArgs(packData, CurrentEndPoint !));
                }
                catch (Exception ex)
                {
                    log.LogDebug(nameof(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);
        }