Пример #1
0
        private static void OnHeaderRead(IAsyncResult ar)
        {
            MessageReadState state = (MessageReadState)ar.AsyncState;
            int n = state.Stream.EndRead(ar);

            state.HeaderLength += n;

            int index  = state.HeaderLength;
            int length = state.Header.Length;

            if (index == length)
            {
                // Allocate the body. We are done reading the header
                byte[] data = new byte[state.GetDataLength()];
                state.Data = data;
                state.Stream.BeginRead(data, 0, data.Length, OnDataRead, state);
            }
            else if (n > 0)
            {
                // Read the rest of the header
                state.Stream.BeginRead(state.Header, index, length - index, OnHeaderRead, state);
            }
            else
            {
                // The stream has ended, but we have not finished reading the header
                state.Finish();
            }
        }
Пример #2
0
        public static AsyncResult BeginRead(Stream stream)
        {
            AsyncResult      ar    = new AsyncResult();
            MessageReadState state = new MessageReadState(stream, ar);

            stream.BeginRead(state.Header, 0, state.Header.Length, OnHeaderRead, state);
            return(ar);
        }