protected override async Task <RpcResponseMessage> SendAsync(RpcRequestMessage request, string route = null) { var logger = new RpcLogger(_log); try { lock (_lockingObject) { var rpcRequestJson = JsonConvert.SerializeObject(request, JsonSerializerSettings); var requestBytes = Encoding.UTF8.GetBytes(rpcRequestJson); logger.LogRequest(rpcRequestJson); GetPipeClient().Write(requestBytes, 0, requestBytes.Length); using (var memoryData = ReceiveFullResponse(GetPipeClient())) { memoryData.Position = 0; using (StreamReader streamReader = new StreamReader(memoryData)) using (JsonTextReader reader = new JsonTextReader(streamReader)) { var serializer = JsonSerializer.Create(JsonSerializerSettings); var message = serializer.Deserialize <RpcResponseMessage>(reader); logger.LogResponse(message); return(message); } } } } catch (Exception ex) { var exception = new RpcClientUnknownException("Error occurred when trying to send ipc requests(s)", ex); logger.LogException(exception); throw exception; } }
protected override async Task <RpcResponseMessage> SendAsync(RpcRequestMessage request, string route = null) { RpcLogger rpcLogger = new RpcLogger(_log); RpcResponseMessage rpcResponseMessage; try { var cancellationTokenSource = new CancellationTokenSource(); cancellationTokenSource.CancelAfter(ConnectionTimeout); using (var pipeStream = new NamedPipeClientStream(IpcPath)) { await pipeStream.ConnectAsync(cancellationTokenSource.Token); string str = JsonConvert.SerializeObject(request, JsonSerializerSettings); byte[] bytes = Encoding.UTF8.GetBytes(str); rpcLogger.LogRequest(str); await pipeStream.WriteAsync(bytes, 0, bytes.Length, cancellationTokenSource.Token); using (MemoryStream fullResponse = await ReceiveFullResponseAsync(pipeStream, cancellationTokenSource.Token)) { fullResponse.Position = 0L; using (StreamReader streamReader = new StreamReader(fullResponse)) { using (JsonTextReader jsonTextReader = new JsonTextReader(streamReader)) { RpcResponseMessage responseMessage = JsonSerializer.Create(JsonSerializerSettings).Deserialize <RpcResponseMessage>(jsonTextReader); rpcLogger.LogResponse(responseMessage); rpcResponseMessage = responseMessage; } } } } } catch (TaskCanceledException ex) { var exception = new RpcClientTimeoutException($"Rpc timeout after {ConnectionTimeout.TotalMilliseconds} milliseconds", ex); rpcLogger.LogException(exception); throw exception; } catch (Exception ex) { var unknownException = new RpcClientUnknownException("Error occurred when trying to send ipc requests(s)", ex); rpcLogger.LogException(unknownException); throw unknownException; } return(rpcResponseMessage); }
protected override async Task <RpcResponseMessage> SendAsync(RpcRequestMessage request, string route = null) { var logger = new RpcLogger(_log); try { await semaphoreSlim.WaitAsync().ConfigureAwait(false); var rpcRequestJson = JsonConvert.SerializeObject(request, JsonSerializerSettings); var requestBytes = new ArraySegment <byte>(Encoding.UTF8.GetBytes(rpcRequestJson)); logger.LogRequest(rpcRequestJson); var cancellationTokenSource = new CancellationTokenSource(); cancellationTokenSource.CancelAfter(ConnectionTimeout); using (var webSocket = await GetClientWebSocketAsync().ConfigureAwait(false)) { await webSocket.SendAsync(requestBytes, WebSocketMessageType.Text, true, cancellationTokenSource.Token) .ConfigureAwait(false); using (var memoryData = await ReceiveFullResponseAsync(webSocket).ConfigureAwait(false)) { memoryData.Position = 0; using (var streamReader = new StreamReader(memoryData)) using (var reader = new JsonTextReader(streamReader)) { var serializer = JsonSerializer.Create(JsonSerializerSettings); var message = serializer.Deserialize <RpcResponseMessage>(reader); logger.LogResponse(message); return(message); } } } } catch (Exception ex) { var exception = new RpcClientUnknownException("Error occurred when trying to web socket requests(s)", ex); logger.LogException(exception); throw exception; } finally { semaphoreSlim.Release(); } }
protected override async Task<RpcResponseMessage> SendAsync(RpcRequestMessage request, string route = null) { var logger = new RpcLogger(_log); try { lock (_lockingObject) { var rpcRequestJson = JsonConvert.SerializeObject(request, JsonSerializerSettings); var requestBytes = Encoding.UTF8.GetBytes(rpcRequestJson); logger.LogRequest(rpcRequestJson); var client = GetSocket(); client.SendBufferSize = requestBytes.Length; #if NET461 var val = client.Send(requestBytes, SocketFlags.None); #else var val = client.SendAsync(new ArraySegment<byte>(requestBytes, 0, requestBytes.Length), SocketFlags.None).Result; #endif using (var memoryStream = ReceiveFullResponse(client)) { memoryStream.Position = 0; using (var streamReader = new StreamReader(memoryStream)) using (var reader = new JsonTextReader(streamReader)) { var serializer = JsonSerializer.Create(JsonSerializerSettings); var message = serializer.Deserialize<RpcResponseMessage>(reader); logger.LogResponse(message); return message; } } } } catch (Exception ex) { var exception = new RpcClientUnknownException("Error occurred when trying to send ipc requests(s)", ex); logger.LogException(exception); throw exception; } }
protected override async Task SendAsync(RpcRequestMessage request, string route = null) { var logger = new RpcLogger(_log); try { await semaphoreSlim.WaitAsync().ConfigureAwait(false); var rpcRequestJson = JsonConvert.SerializeObject(request, JsonSerializerSettings); var requestBytes = new ArraySegment <byte>(Encoding.UTF8.GetBytes(rpcRequestJson)); logger.LogRequest(rpcRequestJson); var cancellationTokenSource = new CancellationTokenSource(); cancellationTokenSource.CancelAfter(ConnectionTimeout); var webSocket = await GetClientWebSocketAsync().ConfigureAwait(false); await webSocket.SendAsync(requestBytes, WebSocketMessageType.Text, true, cancellationTokenSource.Token) .ConfigureAwait(false); if (listener != null) { cancellationTokenSource.Cancel(); cancellationTokenSource.Dispose(); cancellationTokenSource = new CancellationTokenSource(); } listener = Task.Factory.StartNew(async() => { await HandleIncomingMessagesAsync(_clientWebSocket, CancellationToken.None); }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Current); } catch (Exception ex) { logger.LogException(ex); throw new RpcClientUnknownException("Error occurred trying to send web socket requests(s)", ex); } finally { semaphoreSlim.Release(); } }
public async Task SendRequestAsync(RpcRequestMessage request, IRpcStreamingResponseHandler requestResponseHandler, string route = null) { if (_clientWebSocket == null) { throw new InvalidOperationException("Websocket is null. Ensure that StartAsync has been called to create the websocket."); } var logger = new RpcLogger(_log); CancellationTokenSource timeoutCancellationTokenSource = null; try { await _semaphoreSlim.WaitAsync().ConfigureAwait(false); var rpcRequestJson = JsonConvert.SerializeObject(request, JsonSerializerSettings); var requestBytes = new ArraySegment <byte>(Encoding.UTF8.GetBytes(rpcRequestJson)); logger.LogRequest(rpcRequestJson); timeoutCancellationTokenSource = new CancellationTokenSource(); timeoutCancellationTokenSource.CancelAfter(ConnectionTimeout); var webSocket = _clientWebSocket; await webSocket.SendAsync(requestBytes, WebSocketMessageType.Text, true, timeoutCancellationTokenSource.Token) .ConfigureAwait(false); HandleRequest(request, requestResponseHandler); } catch (Exception ex) { logger.LogException(ex); throw new RpcClientUnknownException("Error occurred trying to send web socket requests(s)", ex); } finally { timeoutCancellationTokenSource?.Dispose(); _semaphoreSlim.Release(); } }