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); }
public async Task SendRequestAsync(RpcRequestMessage request, IRpcStreamingResponseHandler requestResponseHandler, 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 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 { _semaphoreSlim.Release(); } }
private async Task HandleIncomingMessagesAsync() { var logger = new RpcLogger(_log); while (!_cancellationTokenSource.IsCancellationRequested) { try { if (_clientWebSocket != null && _clientWebSocket.State == WebSocketState.Open && AnyQueueRequests()) { using (var memoryData = await ReceiveFullResponseAsync(_clientWebSocket).ConfigureAwait(false)) { if (memoryData.Length == 0) { return; } memoryData.Position = 0; using (var streamReader = new StreamReader(memoryData)) using (var reader = new JsonTextReader(streamReader)) { var serializer = JsonSerializer.Create(JsonSerializerSettings); var message = serializer.Deserialize <RpcStreamingResponseMessage>(reader); HandleResponse(message); logger.LogResponse(message); } } } } catch (Exception ex) { HandleError(ex); logger.LogException(ex); } } }
protected override async Task <RpcResponseMessage> SendAsync(RpcRequestMessage request, string route = null, CancellationToken cancellationToken = default(CancellationToken)) { 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; var val = client.SendAsync(new ArraySegment <byte>(requestBytes, 0, requestBytes.Length), SocketFlags.None).Result; 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) { logger.LogException(ex); throw new RpcClientUnknownException("Error occurred when trying to send ipc requests(s)", ex); } }
protected override 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(Task.FromResult(message)); } } } } catch (Exception ex) { var exception = new RpcClientUnknownException("Error occurred when trying to send ipc requests(s)", ex); logger.LogException(exception); throw exception; } }
protected async Task <TResponse> SendAsync <TRequest, TResponse>(TRequest request) where TResponse : RpcResponseMessage { var logger = new RpcLogger(_log); try { //lock (_lockingObject) // { var rpcRequestJson = JsonConvert.SerializeObject(request, JsonSerializerSettings); var requestBytes = new ArraySegment <byte>(Encoding.UTF8.GetBytes(rpcRequestJson)); logger.LogRequest(rpcRequestJson); //add timeout; var webSocket = GetClientWebSocket(); await webSocket.SendAsync(requestBytes, WebSocketMessageType.Text, true, CancellationToken.None).ConfigureAwait(false); using (var memoryData = await ReceiveFullResponseAsync(webSocket)) { memoryData.Position = 0; using (StreamReader streamReader = new StreamReader(memoryData)) using (JsonTextReader reader = new JsonTextReader(streamReader)) { var serializer = JsonSerializer.Create(JsonSerializerSettings); var message = serializer.Deserialize <TResponse>(reader); logger.LogResponse(message); return(message); } } //} } catch (Exception ex) { logger.LogException(ex); throw new RpcClientUnknownException("Error occurred when trying to send ipc requests(s)", ex); } }
private async Task HandleIncomingMessagesAsync() { var logger = new RpcLogger(_log); while (_cancellationTokenSource != null && !_cancellationTokenSource.IsCancellationRequested) { try { await _semaphoreSlimListener.WaitAsync().ConfigureAwait(false); if (_clientWebSocket != null && _clientWebSocket.State == WebSocketState.Open && AnyQueueRequests()) { using (var memoryData = await ReceiveFullResponseAsync(_clientWebSocket).ConfigureAwait(false)) { if (memoryData.Length == 0) { return; } memoryData.Position = 0; using (var streamReader = new StreamReader(memoryData)) using (var reader = new JsonTextReader(streamReader)) { var serializer = JsonSerializer.Create(JsonSerializerSettings); var message = serializer.Deserialize <RpcStreamingResponseMessage>(reader); //Random random = new Random(); //var x = random.Next(0, 50); //if (x == 5) throw new Exception("Error"); HandleResponse(message); logger.LogResponse(message); } } } } catch (Exception ex) { //HandleError should not be called from any of the functions that are called from this function //which also rethrow the exception to avoid calling the error handlers twice for the same error. //if cancellation requested ignore it as we will end up in a loop if (!_cancellationTokenSource.IsCancellationRequested) { HandleError(ex); logger.LogException(ex); } } finally { //release the semaphore for the listener so it can be disposed _semaphoreSlimListener.Release(); } } }
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 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(); } }