Beispiel #1
0
        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;
            }
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        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();
            }
        }
Beispiel #4
0
        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;
            }
        }