private unsafe int ProcessRead(SerialStreamIORequest r) { SerialStreamReadRequest readRequest = (SerialStreamReadRequest)r; Span <byte> buff = readRequest.Buffer.Span; fixed(byte *bufPtr = buff) { // assumes dequeue-ing happens on a single thread int numBytes = Interop.Serial.Read(_handle, bufPtr, buff.Length); if (numBytes < 0) { Interop.ErrorInfo lastError = Interop.Sys.GetLastErrorInfo(); // ignore EWOULDBLOCK since we handle timeout elsewhere if (lastError.Error != Interop.Error.EWOULDBLOCK) { readRequest.Complete(Interop.GetIOException(lastError)); } } else if (numBytes > 0) { readRequest.Complete(numBytes); return(numBytes); } else // numBytes == 0 { RaiseDataReceivedEof(); } } return(0); }
public override ValueTask <int> ReadAsync(Memory <byte> buffer, CancellationToken cancellationToken = default) { CheckHandle(); if (buffer.IsEmpty) { return(new ValueTask <int>(0)); } SerialStreamReadRequest result = new SerialStreamReadRequest(cancellationToken, buffer); _readQueue.Enqueue(result); EnsureIOLoopRunning(); return(new ValueTask <int>(result.Task)); }
public override Task <int> ReadAsync(byte[] array, int offset, int count, CancellationToken cancellationToken) { CheckReadWriteArguments(array, offset, count); if (count == 0) { return(Task <int> .FromResult(0)); // return immediately if no bytes requested; no need for overhead. } Memory <byte> buffer = new Memory <byte>(array, offset, count); SerialStreamReadRequest result = new SerialStreamReadRequest(cancellationToken, buffer); _readQueue.Enqueue(result); EnsureIOLoopRunning(); return(result.Task); }