コード例 #1
0
        void ReceiveCallback(IAsyncResult ar)
        {
            bool     recsuccess = false;
            RecState state      = null;

            try
            {
                int bytesgotten = S.EndReceive(ar);
                state             = (RecState)ar.AsyncState;
                state.BytesSoFar += bytesgotten;
                recsuccess        = true;

#if DEBUG
                Console.WriteLine("Receive Callback. Packet: {0} First packet: {1}, Bytes so far: {2}", state.PacketCount, state.IsPacketLength, state.BytesSoFar);
#endif
            }
            catch
            {
                connected = false;
                OnServerOutput("Error: " + ConnectionClosed);
            }

            if (recsuccess)
            {
                ProcessIncomingData(state);
            }
        }
コード例 #2
0
        void ProcessIncomingData(RecState state)
        {
            if (state.IsPacketLength)
            {
                // First 4 bytes of a new packet.
                state.PacketLength = BitConverter.ToInt32(state.Data, 0);

                state.IsPacketLength = false;
                state.BytesSoFar     = 0;
                state.Data           = new byte[state.PacketLength];
                try
                {
                    S.BeginReceive(state.Data, 0, state.PacketLength, SocketFlags.None, new AsyncCallback(ReceiveCallback), state);
                }
                catch
                {
                    OnServerOutput("Error: Failed BeginReceive");
                }
            }
            else
            {
                // Do something with data...

                if (state.BytesSoFar < state.PacketLength)
                {
                    try
                    {
                        // Missing data.
                        S.BeginReceive(state.Data, state.BytesSoFar, state.PacketLength - state.BytesSoFar, SocketFlags.None, new AsyncCallback(ReceiveCallback), state);
                    }
                    catch
                    {
                        OnServerOutput("Error: Failed BeginReceive");
                    }
                }
                else
                {
                    // Process data.
#if DEBUG
                    Console.WriteLine("Complete packet.");
#endif

                    RCONPacket RetPack = new RCONPacket();
                    RetPack.ParseFromBytes(state.Data, this);

                    ProcessResponse(RetPack);

                    // Wait for new packet.
                    StartGetNewPacket();
                }
            }
        }
コード例 #3
0
        void StartGetNewPacket()
        {
            RecState state = new RecState();

            state.IsPacketLength = true;
            state.Data           = new byte[4];
            state.PacketCount    = PacketCount;
            PacketCount++;
#if DEBUG
            TempPackets.Add(state);
#endif
            try
            {
                S.BeginReceive(state.Data, 0, 4, SocketFlags.None, new AsyncCallback(ReceiveCallback), state);
            }
            catch
            {
                OnServerOutput("Error: Failed To BeginRecieve");
            }
        }