async Task <ViscaPacket> IViscaClient.SendAsync(ViscaPacket request, CancellationToken cancellationToken) { await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); try { if (request.GetByte(0) != 0x81 || request.Length < 5) { return(SyntaxError); } return(request.GetByte(1) switch { 0x01 => await ExecuteCommand(request).ConfigureAwait(false), 0x09 => ExecuteQuery(request), _ => SyntaxError }); }
async Task <ViscaPacket> IViscaClient.SendAsync(ViscaPacket request, CancellationToken cancellationToken) { bool reconnect = true; await semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); try { await SendPacketAsync(request, cancellationToken).ConfigureAwait(false); while (true) { ViscaPacket response = await ReceivePacketAsync(cancellationToken).ConfigureAwait(false); if (response.Length < 2) { throw new ViscaProtocolException($"Received packet of length {response.Length} from VISCA endpoint"); } switch (response.GetByte(1) >> 4) { // Command received case 4: continue; // Success case 5: reconnect = false; return(response); // Error reported by device case 6: throw new ViscaResponseException($"Error reported by VISCA interface. Error data: {response}"); // VISCA protocol violation default: throw new ViscaProtocolException($"Invalid packet returned from VISCA interface. Error data: {response}"); } } } finally { try { if (reconnect) { await ReconnectAsync(cancellationToken).ConfigureAwait(false); } } finally { semaphore.Release(); } } }