CSReportWebServer.NativeMessaging.Port.ReadLengthCallback C# (CSharp) Метод

ReadLengthCallback() приватный Метод

Ends the asynchronous read of a native message length started by BeginRead method. Then begins an asynchronous read of a native message content.
Any exception thrown in the method is saved and then is re-thrown in EndRead method.
private ReadLengthCallback ( AsyncResult ar, IAsyncResult lengthAsyncResult ) : void
ar AsyncResult The AsyncResult object that represents a native message asynchronous read.
lengthAsyncResult IAsyncResult The IAsyncResult object that represents an asynchronous read of native message length.
Результат void
        private void ReadLengthCallback(AsyncResult ar, IAsyncResult lengthAsyncResult)
        {
            try
            {
                //Debug.Assert(lengthAsyncResult.IsCompleted == true);
                ar.lengthIsCompleted = lengthAsyncResult.IsCompleted;
                ar.lengthCompletedSynchronously = lengthAsyncResult.CompletedSynchronously;
                int bytesRead = istream.EndRead(lengthAsyncResult);
                Debug.Assert((0 <= bytesRead) && (bytesRead <= ar.lengthBuffer.Length));
                if (bytesRead == 0)
                {
                    if (ar.lengthOffset == 0) throw new EndOfInputStreamException("End of input stream.");
                    else throw new ProtocolErrorException("Unexpected end of input stream.");
                }
                if (bytesRead < ar.lengthBuffer.Length)
                {
                    ar.lengthOffset += bytesRead;
                    istream.BeginRead(
                        ar.lengthBuffer,
                        ar.lengthOffset,
                        ar.lengthBuffer.Length - ar.lengthOffset,
                        delegate (IAsyncResult _ar) { ((AsyncResult)_ar.AsyncState).port.ReadLengthCallback((AsyncResult)_ar.AsyncState, _ar); },
                        ar);
                    return;
                }
                int messageLength = System.BitConverter.ToInt32(ar.lengthBuffer, 0);
                if (messageLength <= 0) throw new ProtocolErrorException(string.Format("Read zero or negative input message length : {0}", messageLength));
                ar.messageBuffer = new byte[messageLength];
                ar.messageOffset = 0;
                istream.BeginRead(
                    ar.messageBuffer,
                    ar.messageOffset,
                    ar.messageBuffer.Length - ar.messageOffset,
                    delegate (IAsyncResult _ar) { ((AsyncResult)_ar.AsyncState).port.ReadMessageCallback((AsyncResult)_ar.AsyncState, _ar); },
                    ar);
            }
            catch (Exception ex)
            {
                ar.lengthException = ex;
                ar.wait.Set();
                if (ar.callback != null) ar.callback(ar);
            }
        }