public void CompleteTask(JsonRpcParamsResponse response)
            {
                this.response = response;
                this.ResponseTask.Start();

                _ = Task.Delay(1000)
                    .ContinueWith(t =>
                {
                    this.Dispose();
                });
            }
            public void Dispose()
            {
                try
                {
                    this.ResponseTask.Dispose();
                }
                catch (Exception)
                {
                }

                //this.Id = default;
                this.ResponseTask = null;
                this.response     = null;
            }
Exemple #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        /// <remarks>
        /// ここでの戻り値[JsonRpcParamsResponse]は直接通信で返すものではない。
        /// 通信応答を返す際に、ここの応答をJsonRpcFactory.CreateResultで整形して返す。
        /// </remarks>
        public async Task <JsonRpcParamsResponse> Exec(JsonRpcQuery request)
        {
            var url      = Settings.HttpPostUrl;
            var sendJson = JsonConvert.SerializeObject(request);
            var content  = new StringContent(sendJson, Encoding.UTF8, "application/json");

            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json")
                    );
                client.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter");
                client.Timeout = TimeSpan.FromMilliseconds(500000); // 500秒

                try
                {
                    using (var message = await client.PostAsync(url, content))
                    {
                        var isQuery = (request is JsonRpcQueryRequest);
                        if (!isQuery)
                        {
                            return(new JsonRpcParamsResponse());
                        }

                        var json = await message.Content.ReadAsStringAsync();

                        try
                        {
                            var response = JsonConvert.DeserializeObject <JsonRpcParamsResponse>(json);

                            return(response);
                        }
                        catch (Exception)
                        {
                            // Response is NOT JSON.
                            var result = new JsonRpcParamsResponse()
                            {
                                Error = json
                            };

                            return(result);
                        }
                    }
                }
                catch (Exception ex)
                {
                    var response = new JsonRpcParamsResponse()
                    {
                        Error = ex
                    };
                    if (request is JsonRpcQueryRequest)
                    {
                        response.Id = ((JsonRpcQueryRequest)request).Id;
                    }

                    return(response);
                }
            }
        }
        public async Task <JsonRpcParamsResponse> Exec(JsonRpcQuery request)
        {
            if (this._client == null)
            {
                if (!await this.Connect())
                {
                    throw new WebSocketException("Connection Refused.");
                }
            }

            var isQuery = (request is JsonRpcQueryRequest);
            var resTask = default(Task <JsonRpcParamsResponse>);

            if (isQuery)
            {
                var id     = ((JsonRpcQueryRequest)request).Id;
                var resSet = new ResponseSet();
                resTask = resSet.ResponseTask;

                if (!this._responseDictionary.TryAdd(id, resSet))
                {
                    throw new ThreadStateException("ResponseSet Cannot Registered.");
                }
            }
            else
            {
                resTask = Task <JsonRpcParamsResponse> .Run(() =>
                {
                    return(new JsonRpcParamsResponse());
                });
            }

            try
            {
                var sendJson = JsonConvert.SerializeObject(request);
                var bytes    = Encoding.UTF8.GetBytes(sendJson);
                var segment  = new ArraySegment <byte>(bytes);

                await this._client.SendAsync(
                    segment,
                    WebSocketMessageType.Text,
                    true,
                    CancellationToken.None
                    );
            }
            catch (Exception ex)
            {
                var response = new JsonRpcParamsResponse()
                {
                    Error = ex
                };
                if (request is JsonRpcQueryRequest)
                {
                    response.Id = ((JsonRpcQueryRequest)request).Id;
                }

                return(response);
            }

            return(await resTask);
        }