public static void Write(PipeStream stream, byte[] buffer, int offset, int count, bool isAsync, TimeoutHelper timeoutHelper)
{
// 异步时,使用异步方式写入数据
if (isAsync)
{
IAsyncResult asyncResult = stream.BeginWrite(buffer, offset, count, null, null);
// 等待 timeoutHelper 计算后的剩余时间,如果在这段时间内没有读到数据,那么将直接返回,这时,由于没有读到数据,返回的数据长度为 0
asyncResult.AsyncWaitHandle.WaitOne(timeoutHelper == null ? 60000 : (int)timeoutHelper.RemainingTime().TotalMilliseconds);
if (asyncResult.GetType() == pipeStreamAsyncResultType)
{
pipeStreamAsyncResult_waitHandle.SetValue(asyncResult, null);
}
stream.EndWrite(asyncResult);
return;
}
// 使用系统内置的方式进行同步阻塞式读取,该方法直到读取到数据才返回
stream.Write(buffer, offset, count);
}