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
        /// <summary>
        /// Sends the specified client net message.
        /// </summary>
        /// <param name="clientMsg">The client net message.</param>
        public override void Send(IClientMsg clientMsg)
        {
            if (!isConnected)
            {
                DebugLog.WriteLine("TcpConnection", "Attempting to send client message when not connected: {0}", clientMsg.MsgType);
                return;
            }

            byte[] data = clientMsg.Serialize();

            // encrypt outgoing traffic if we need to
            if (NetFilter != null)
            {
                data = NetFilter.ProcessOutgoing(data);
            }

            lock ( sock )
            {
                // write header
                netWriter.Write(( uint )data.Length);
                netWriter.Write(TcpConnection.MAGIC);

                netWriter.Write(data);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Sends the specified client net message.
        /// </summary>
        /// <param name="clientMsg">The client net message.</param>
        public override void Send(IClientMsg clientMsg)
        {
            byte[] data = clientMsg.Serialize();

            // encrypt outgoing traffic if we need to
            if (NetFilter != null)
            {
                data = NetFilter.ProcessOutgoing(data);
            }

            netLock.EnterReadLock();

            try
            {
                if (netStream == null)
                {
                    DebugLog.WriteLine("TcpConnection", "Attempting to send client message when not connected: {0}", clientMsg.MsgType);
                    return;
                }

                // need to ensure ordering between concurrent Sends
                lock ( netWriter )
                {
                    // write header
                    netWriter.Write((uint)data.Length);
                    netWriter.Write(TcpConnection.MAGIC);

                    netWriter.Write(data);
                }
            }
            finally
            {
                netLock.ExitReadLock();
            }
        }
Exemplo n.º 4
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.º 5
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));
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Serializes and sends the provided message to the server in as many packets as is necessary.
        /// </summary>
        /// <param name="clientMsg">The ClientMsg</param>
        public override void Send(IClientMsg clientMsg)
        {
            if (state != State.Connected)
                return;

            byte[] data = clientMsg.Serialize();

            if (NetFilter != null)
                data = NetFilter.ProcessOutgoing(data);

            SendData(new MemoryStream(data));
        }