Ejemplo n.º 1
0
        /// <summary>
        /// Callback after 'BeginReceivePacket'.
        /// Called when there is data available for the packet size.
        /// </summary>
        /// <param name="ar">The result of the async operation.</param>
        private void ReceivedPacketSize(IAsyncResult ar)
        {
            try
            {
                // End the receive, and get the state
                int length = Socket.EndReceive(ar);
                ToffeeReceiveState state = (ToffeeReceiveState)ar.AsyncState;
                state.ReceivedLength += length;

                // Is there still more data we're expecting?
                if (!state.Received)
                {
                    // Keep receiving data for the size
                    Stream.BeginRead(
                        state.Buffer, state.ReceivedLength, state.AwaitingLength, ReceivedPacketSize, state);
                    return;
                }

                // We've got the entire size!
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(state.Buffer);
                }
                ushort packetSize = BitConverter.ToUInt16(state.Buffer, 0);
                state = new ToffeeReceiveState(packetSize);
                Stream.BeginRead(
                    state.Buffer, 0, packetSize, ReceivedPacket, state);
            }
            catch
            {
                // Something went wrong while trying to receive data
                Disconnect();
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Callback after the packet size has been received.
        /// Called when there is data available for the packet.
        /// </summary>
        /// <param name="ar">The result of the async operation.</param>
        private void ReceivedPacket(IAsyncResult ar)
        {
            byte[] packet = null;
            try
            {
                // End the receive, and get the state
                int length = Socket.EndReceive(ar);
                ToffeeReceiveState state = (ToffeeReceiveState)ar.AsyncState;
                state.ReceivedLength += length;

                // Is there still more data we're expecting?
                if (!state.Received)
                {
                    // Keep receiving data for the size
                    Stream.BeginRead(
                        state.Buffer, state.ReceivedLength, state.AwaitingLength, ReceivedPacket, state);
                    return;
                }

                // Begin receiving again
                packet = state.Buffer;
                BeginReceivePacket();
            }
            catch
            {
                // Something went wrong while trying to receive data
                Disconnect();
            }

            // Handle the packet
            if (packet != null)
            {
                HandlePacket(packet);
            }
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Begins receiving a packet.
 /// </summary>
 protected void BeginReceivePacket()
 {
     try
     {
         // Start receiving the packet size
         ToffeeReceiveState state = new ToffeeReceiveState(2);
         Stream.BeginRead(state.Buffer, 0, 2, ReceivedPacketSize, state);
     }
     catch
     {
         // Something went wrong while trying to receive data
         Disconnect();
     }
 }