ReadAsync() public method

public ReadAsync ( byte buffer, int offset, int count, System cancellationToken ) : System.Threading.Tasks.Task
buffer byte
offset int
count int
cancellationToken System
return System.Threading.Tasks.Task
Ejemplo n.º 1
0
    static async Task<byte[]> ReadBytesAsync(PipeStream pipeStream, int length)
    {
        Assert.True(pipeStream.IsConnected);

        byte[] buffer = new byte[length];
        Assert.True(length > 0);

        int readSoFar = 0;

        while (readSoFar < length)
        {
            int len = await pipeStream.ReadAsync(buffer, readSoFar, length - readSoFar);
            if (len == 0) break;
            readSoFar += len;
        }

        return buffer;
    }
Ejemplo n.º 2
0
    static byte[] ReadBytesAsync(PipeStream pipeStream, int length)
    {
        Assert.True(pipeStream.IsConnected);

        byte[] buffer = new byte[length];
        Assert.True(length > 0);

        int readSoFar = 0;

        while (readSoFar < length)
        {
            Task<int> readTask = pipeStream.ReadAsync(buffer, readSoFar, length - readSoFar);
            readTask.Wait();
            int len = readTask.Result;
            readSoFar += len;
        }

        return buffer;
    }
Ejemplo n.º 3
0
        // The IsConnected property on named pipes does not detect when the client has disconnected
        // if we don't attempt any new I/O after the client disconnects. We start an async I/O here
        // which serves to check the pipe for disconnection. 
        private async Task MonitorPipeForDisconnectionAsync(PipeStream pipeStream, CancellationToken cancellationToken)
        {
            byte[] buffer = new byte[0];

            while (!cancellationToken.IsCancellationRequested && pipeStream.IsConnected)
            {
                CompilerServerLogger.Log("Before poking pipe.");
                try
                {
                    await pipeStream.ReadAsync(buffer, 0, 0).ConfigureAwait(continueOnCapturedContext: false);
                }
                catch (ObjectDisposedException)
                {
                    // Another thread may have closed the stream already.  Not a problem.
                    CompilerServerLogger.Log("Pipe has already been closed.");
                    return;
                }
                CompilerServerLogger.Log("After poking pipe.");
                // Wait a hundredth of a second before trying again
                await Task.Delay(10).ConfigureAwait(false);
            }
                
            if (!cancellationToken.IsCancellationRequested)
            {
                throw new PipeBrokenException();
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// This task does not complete until we are completely done reading.
 /// </summary>
 internal static async Task ReadAllAsync(
     PipeStream stream,
     byte[] buffer,
     int count,
     CancellationToken cancellationToken)
 {
     int totalBytesRead = 0;
     do
     {
         CompilerServerLogger.Log("Attempting to read {0} bytes from the stream",
             count - totalBytesRead);
         int bytesRead = await stream.ReadAsync(buffer,
                                                totalBytesRead,
                                                count - totalBytesRead,
                                                cancellationToken).ConfigureAwait(false);
         if (bytesRead == 0)
         {
             CompilerServerLogger.Log("Unexpected -- read 0 bytes from the stream.");
             throw new EndOfStreamException("Reached end of stream before end of read.");
         }
         CompilerServerLogger.Log("Read {0} bytes", bytesRead);
         totalBytesRead += bytesRead;
     } while (totalBytesRead < count);
     CompilerServerLogger.Log("Finished read");
 }
Ejemplo n.º 5
0
 public Task <int> ReadAsync(byte[] messageBuffer, int i, int messageBufferSize)
 {
     return(PipeStream.ReadAsync(messageBuffer, i, messageBufferSize));
 }