예제 #1
0
        /// <summary>
        /// Asynchronously read a complete packet from a possibly-fragmented stream
        /// </summary>
        public IAsyncResult BeginReadFromStream(Stream stream, AsyncCallback asyncCallback, object asyncState)
        {
            ReadFromStreamAsyncResult internalState = new ReadFromStreamAsyncResult(
                stream,
                this,
                asyncCallback,
                asyncState);

            this.InternalBeginReadFromStream(
                stream,
                SsmPacketParser.ReadCompleted,
                internalState);

            return(internalState);
        }
예제 #2
0
        /// <summary>
        /// Returns the new packet to the caller
        /// </summary>
        public SsmPacket EndReadFromStream(IAsyncResult asyncResult)
        {
            ReadFromStreamAsyncResult internalState = (ReadFromStreamAsyncResult)asyncResult;

            try
            {
                if (internalState.Exception != null)
                {
                    throw internalState.Exception;
                }

                return(SsmPacket.ParseResponse(this.buffer, 0, this.bytesReceived));
            }
            finally
            {
                internalState.Dispose();
            }
        }
예제 #3
0
        /// <summary>
        /// Invoked each time stream.BeginRead completes
        /// </summary>
        /// <remarks>
        /// Parse the received bytes, notify the consumer if a full packet is
        /// received or if anything goes wrong.
        /// </remarks>
        private static void ReadCompleted(IAsyncResult asyncResult)
        {
            ReadFromStreamAsyncResult internalState = (ReadFromStreamAsyncResult)asyncResult.AsyncState;

            try
            {
                SsmPacketParser parser        = internalState.Parser;
                int             offset        = parser.bytesReceived;
                int             bytesReceived = internalState.Stream.EndRead(asyncResult);

                for (int i = 0; i < bytesReceived; i++)
                {
                    int index = offset + i;
                    internalState.Parser.CheckByte(index, internalState.Parser.buffer[index]);
                    parser.bytesReceived++;
                }

                if (internalState.Parser.IsComplete)
                {
                    internalState.Completed();
                }
                else
                {
                    internalState.Parser.InternalBeginReadFromStream(
                        internalState.Stream,
                        SsmPacketParser.ReadCompleted,
                        internalState);
                }
            }
            catch (System.Security.SecurityException ex)
            {
                internalState.Exception = ex;
                internalState.Completed();
            }
            catch (IOException ex)
            {
                internalState.Exception = ex;
                internalState.Completed();
            }
        }