/// <summary> /// Ends the asynchronous write of a native message length started by BeginWrite method. /// Then begins an asynchronous write of a native message content. /// </summary> /// <remarks> /// Any exception thrown in the method is saved and then is re-thrown in EndWrite method. /// </remarks> /// <param name="ar">The AsyncResult object that represents a native message asynchronous write.</param> /// <param name="messageAsyncResult">The IAsyncResult object that represents an asynchronous write of a native message length.</param> private void WriteLengthCallback(AsyncResult ar, IAsyncResult lengthAsyncResult) { try { //Debug.Assert(lengthAsyncResult.IsCompleted == true); ar.lengthIsCompleted = lengthAsyncResult.IsCompleted; ar.lengthCompletedSynchronously = lengthAsyncResult.CompletedSynchronously; ostream.EndWrite(lengthAsyncResult); ostream.BeginWrite( ar.messageBuffer, ar.messageOffset, ar.messageBuffer.Length - ar.messageOffset, delegate(IAsyncResult _ar) { ((AsyncResult)_ar.AsyncState).port.WriteMessageCallback((AsyncResult)_ar.AsyncState, _ar); }, ar); } catch (Exception ex) { ar.lengthException = ex; ar.wait.Set(); if (ar.callback != null) { ar.callback(ar); } } }
/// <summary> /// Ends the asynchronous read of a native message length started by BeginRead method. /// Then begins an asynchronous read of a native message content. /// </summary> /// <remarks> /// Any exception thrown in the method is saved and then is re-thrown in EndRead method. /// </remarks> /// <param name="ar">The AsyncResult object that represents a native message asynchronous read.</param> /// <param name="lengthAsyncResult">The IAsyncResult object that represents an asynchronous read of native message length.</param> 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); } } }
/// <summary> /// Ends the asynchronous read of a native message content started by ReadLengthCallback method. /// Then calls the callback specified for the operation. /// </summary> /// <remarks> /// Any exception thrown in the method is saved and then is re-thrown in EndRead method. /// </remarks> /// <param name="ar">The AsyncResult object that represents a native message asynchronous read.</param> /// <param name="messageAsyncResult">The IAsyncResult object that represents an asynchronous read of a native message content.</param> private void ReadMessageCallback(AsyncResult ar, IAsyncResult messageAsyncResult) { try { //Debug.Assert(messageAsyncResult.IsCompleted == true); ar.messageIsCompleted = messageAsyncResult.IsCompleted; ar.messageCompletedSynchronously = messageAsyncResult.CompletedSynchronously; int bytesRead = istream.EndRead(messageAsyncResult); Debug.Assert((0 <= bytesRead) && (bytesRead <= ar.messageBuffer.Length)); if (bytesRead == 0) { throw new ProtocolErrorException("Unexpected end of input stream."); } if (bytesRead < ar.messageBuffer.Length) { ar.messageOffset += bytesRead; istream.BeginRead( ar.messageBuffer, ar.messageOffset, ar.messageBuffer.Length - ar.messageOffset, delegate(IAsyncResult _ar) { ((AsyncResult)_ar.AsyncState).port.ReadMessageCallback((AsyncResult)_ar.AsyncState, _ar); }, ar); return; } ar.wait.Set(); if (ar.callback != null) { ar.callback(ar); } } catch (Exception ex) { ar.lengthException = ex; ar.wait.Set(); if (ar.callback != null) { ar.callback(ar); } } }
/// <summary> /// Ends the asynchronous write of a native message content started by WriteLengthCallback method. /// Then calls the callback specified for the operation. /// </summary> /// <remarks> /// Any exception thrown in the method is saved and then is re-thrown in EndRead method. /// </remarks> /// <param name="ar">The AsyncResult object that represents a native message asynchronous read.</param> /// <param name="messageAsyncResult">The IAsyncResult object that represents an asynchronous read of a native message content.</param> private void WriteMessageCallback(AsyncResult ar, IAsyncResult messageAsyncResult) { try { //Debug.Assert(messageAsyncResult.IsCompleted == true); ar.messageIsCompleted = messageAsyncResult.IsCompleted; ar.messageCompletedSynchronously = messageAsyncResult.CompletedSynchronously; ostream.EndWrite(messageAsyncResult); ar.wait.Set(); if (ar.callback != null) { ar.callback(ar); } } catch (Exception ex) { ar.messageException = ex; ar.wait.Set(); if (ar.callback != null) { ar.callback(ar); } } }
/// <summary> /// Ends the asynchronous write of a native message content started by WriteLengthCallback method. /// Then calls the callback specified for the operation. /// </summary> /// <remarks> /// Any exception thrown in the method is saved and then is re-thrown in EndRead method. /// </remarks> /// <param name="ar">The AsyncResult object that represents a native message asynchronous read.</param> /// <param name="messageAsyncResult">The IAsyncResult object that represents an asynchronous read of a native message content.</param> private void WriteMessageCallback(AsyncResult ar, IAsyncResult messageAsyncResult) { try { //Debug.Assert(messageAsyncResult.IsCompleted == true); ar.messageIsCompleted = messageAsyncResult.IsCompleted; ar.messageCompletedSynchronously = messageAsyncResult.CompletedSynchronously; ostream.EndWrite(messageAsyncResult); ar.wait.Set(); if (ar.callback != null) ar.callback(ar); } catch (Exception ex) { ar.messageException = ex; ar.wait.Set(); if (ar.callback != null) ar.callback(ar); } }
/// <summary> /// Ends the asynchronous write of a native message length started by BeginWrite method. /// Then begins an asynchronous write of a native message content. /// </summary> /// <remarks> /// Any exception thrown in the method is saved and then is re-thrown in EndWrite method. /// </remarks> /// <param name="ar">The AsyncResult object that represents a native message asynchronous write.</param> /// <param name="messageAsyncResult">The IAsyncResult object that represents an asynchronous write of a native message length.</param> private void WriteLengthCallback(AsyncResult ar, IAsyncResult lengthAsyncResult) { try { //Debug.Assert(lengthAsyncResult.IsCompleted == true); ar.lengthIsCompleted = lengthAsyncResult.IsCompleted; ar.lengthCompletedSynchronously = lengthAsyncResult.CompletedSynchronously; ostream.EndWrite(lengthAsyncResult); ostream.BeginWrite( ar.messageBuffer, ar.messageOffset, ar.messageBuffer.Length - ar.messageOffset, delegate(IAsyncResult _ar) { ((AsyncResult)_ar.AsyncState).port.WriteMessageCallback((AsyncResult)_ar.AsyncState, _ar); }, ar); } catch (Exception ex) { ar.lengthException = ex; ar.wait.Set(); if (ar.callback != null) ar.callback(ar); } }
/// <summary> /// Ends the asynchronous read of a native message content started by ReadLengthCallback method. /// Then calls the callback specified for the operation. /// </summary> /// <remarks> /// Any exception thrown in the method is saved and then is re-thrown in EndRead method. /// </remarks> /// <param name="ar">The AsyncResult object that represents a native message asynchronous read.</param> /// <param name="messageAsyncResult">The IAsyncResult object that represents an asynchronous read of a native message content.</param> private void ReadMessageCallback(AsyncResult ar, IAsyncResult messageAsyncResult) { try { //Debug.Assert(messageAsyncResult.IsCompleted == true); ar.messageIsCompleted = messageAsyncResult.IsCompleted; ar.messageCompletedSynchronously = messageAsyncResult.CompletedSynchronously; int bytesRead = istream.EndRead(messageAsyncResult); Debug.Assert((0 <= bytesRead) && (bytesRead <= ar.messageBuffer.Length)); if (bytesRead == 0) throw new ProtocolErrorException("Unexpected end of input stream."); if (bytesRead < ar.messageBuffer.Length) { ar.messageOffset += bytesRead; istream.BeginRead( ar.messageBuffer, ar.messageOffset, ar.messageBuffer.Length - ar.messageOffset, delegate(IAsyncResult _ar) { ((AsyncResult)_ar.AsyncState).port.ReadMessageCallback((AsyncResult)_ar.AsyncState, _ar); }, ar); return; } ar.wait.Set(); if (ar.callback != null) ar.callback(ar); } catch (Exception ex) { ar.lengthException = ex; ar.wait.Set(); if (ar.callback != null) ar.callback(ar); } }
/// <summary> /// Ends the asynchronous read of a native message length started by BeginRead method. /// Then begins an asynchronous read of a native message content. /// </summary> /// <remarks> /// Any exception thrown in the method is saved and then is re-thrown in EndRead method. /// </remarks> /// <param name="ar">The AsyncResult object that represents a native message asynchronous read.</param> /// <param name="lengthAsyncResult">The IAsyncResult object that represents an asynchronous read of native message length.</param> 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); } }