Esempio n. 1
0
        /// <summary>
        /// Makes the jsonrpc call to the node
        /// </summary>
        public async Task <RequestResponseData <T> > CallMethod <T>(string method, Dictionary <string, string> parameters, string apiUrl)
        {
            var    url     = $"{_config.NodeUrl}/{apiUrl}";
            string trace   = $"jsonrpc call - {method} - {url} ";
            var    callId  = Interlocked.Increment(ref rpcid);
            var    request = new Request(jrpcVersion, callId, method, parameters);
            HttpResponseMessage?httpResult = null;

            try
            {
                httpResult = await _http.PostAsJsonAsync(url, request);

                ResponseData <T>?apiResponse = null;
                if (httpResult.IsSuccessStatusCode)
                {
                    apiResponse = await httpResult.Content.ReadFromJsonAsync <ResponseData <T> >();
                }
                var result = new RequestResponseData <T>(apiResponse, httpResult.StatusCode,
                                                         httpResult.ReasonPhrase ?? httpResult.StatusCode.ToString(), request);
                _logger.LogTrace(trace + $"result : {Serialize(result)}");
                return(result);
            }
            catch (Exception e)
            {
                trace += $"Exception lors de l'appel : {method} - {url} - " + e.Message;
                if (httpResult != null)
                {
                    trace += @$ "HTTP{(int)httpResult.StatusCode} {httpResult.StatusCode}";
                    trace += await httpResult.Content.ReadAsStringAsync();
                }
                _logger.LogError(trace);
                throw;
            }
        }
        /*public static byte[] response_Bytes(this ProxyCache proxyCache, string key)
         * {
         *      if (proxyCache.hasMapping(key))
         *              return proxyCache.requestMappings().value(key).Response_Bytes;
         *      return null;
         * }
         *
         * public static string response_String(this ProxyCache proxyCache, string key)
         * {
         *      if (proxyCache.hasMapping(key))
         *              return proxyCache.requestMappings().value(key).Response_String;
         *      return null;
         * }*/

        public static ProxyCache add_ToCache(this ProxyCache proxyCache, RequestResponseData requestResponseData)
        {
            var requestPostBytes = requestResponseData.Request_PostBytes;
            var cacheKey         = proxyCache.cacheKey(requestResponseData);

            ProxyCache.Requests.Add(requestResponseData);
            ProxyCache.RequestCache.add(cacheKey, requestResponseData);
            return(proxyCache);
        }
Esempio n. 3
0
 private void HandleRequestMultiResponses(RequestResponseData rrData)
 {
     try
     {
         _serverConnectors.TriggerOnRequestMultiResponsesPacket(this, rrData.Module, rrData.Command, rrData.RequestId, rrData.Packet, RequestMultiResponsesCallback);
     }
     catch (Exception ex)
     {
         _serverConnectors.TriggerOnDebugLog(this, DebugLogType.OnRecvException, $"Module={rrData.Module} Command={rrData.Command} ex={ex.ToString()}");
         var resBuf = ConnectorsUtils.SerializeMultiResponsePacket(ConnectorsUtils.RequestTypeRequestMultiResponses, 0, 1, ex.Message, rrData.RequestId, true, 0, 0);
         TcpSocketsUtils.Send(Socket, resBuf, OnSend, OnExcp);
     }
 }
Esempio n. 4
0
 private void AddToCache(string key, RequestResponseData data)
 {
     if (!useCache)
     {
         return;
     }
     if (data.IsCached || !cacheManager.HasConfiguration(cacheName))
     {
         return;
     }
     if (cacheManager.Add(cacheName, data.CacheKey, data.Response))
     {
         data.IsCached = true;
         Log.LogEntry.Categories(CategoryFlags.Debug).Message("Adding {0} to cache. Thread: [{1}]", key, Thread.CurrentThread.ManagedThreadId).WriteVerbose();
     }
 }
        public static RequestResponseData add(this  ProxyCache proxyCache,
                                              HttpWebRequest webRequest,
                                              HttpWebResponse webResponse,
                                              byte[] requestPostBytes,
                                              byte[] responseBytes,
                                              string responseString)
        {
            var requestResponseData = new RequestResponseData()
            {
                WebRequest      = webRequest,
                WebResponse     = webResponse,
                Response_Bytes  = responseBytes,
                Response_String = responseString
            };

            if (requestPostBytes.notNull())
            {
                requestResponseData.Request_PostBytes  = requestPostBytes;
                requestResponseData.Request_PostString = requestPostBytes.ascii();
            }
            proxyCache.add_ToCache(requestResponseData);
            return(requestResponseData);
        }
 public virtual bool shouldCache(RequestResponseData requestRessponseData)
 {
     return(true);
 }
 public virtual string cacheKey(RequestResponseData requestResponseData)
 {
     return(cacheKey(requestResponseData.WebRequest, requestResponseData.Request_PostBytes));
 }
 public static string cacheKey(this ProxyCache proxyCache, RequestResponseData requestResponseData)
 {
     return(proxyCache.ProxyCache_Brain.cacheKey(requestResponseData));
 }
Esempio n. 9
0
        internal void OnRecv(byte[] buf, int grossRecvBytes)
        {
            try
            {
                TotalDataBytesReceived  += buf.Length;
                TotalGrossBytesReceived += grossRecvBytes;

                _serverConnectors.TriggerOnDebugLog(this, DebugLogType.Info, $"OnRecv - start, dataRecvBytes:{buf.Length}, grossRecvBytes:{grossRecvBytes}, TotalDataBytesReceived:{TotalDataBytesReceived}, TotalGrossBytesReceived:{TotalGrossBytesReceived}");

                if (buf[0] == 0) //request response packet
                {
                    object reqPacket = null;
                    int    requestId = 0;
                    string exceptionMsg = null;
                    byte   module = 0, command = 0, requestType = 0;
                    try
                    {
                        requestType = buf[1];

                        _serverConnectors.TriggerOnDebugLog(this, DebugLogType.Info, $"Recv requestType = {requestType}");


                        //if (requestType != ConnectorsUtils.RequestTypeRequestMultiResponses)
                        {
                            reqPacket = ConnectorsUtils.DeserializeRequestPacket(buf, _serverConnectors._settings.PacketsMap, out requestId, out module, out command);
                        }
                    }
                    catch (Exception ex) { exceptionMsg = ex.Message; }


                    if (requestType == ConnectorsUtils.RequestTypeKeepAlive) //keep alive
                    {
                        _lastReceivedKeepAliveTimestamp = (long)reqPacket;
                        _serverConnectors.TriggerOnDebugLog(this, DebugLogType.OnKeepAlive, reqPacket.ToString());
                        return;
                    }

                    if (requestType == ConnectorsUtils.RequestTypeRecvInProgress) //client RecvInProgress (keep alive should be less sensitive)
                    {
                        _lastReceivedInProgressTime = DateTime.UtcNow;
                        _serverConnectors.TriggerOnDebugLog(this, DebugLogType.OnKeepAlive, "Recv in progress");
                        return;
                    }

                    if (requestType == ConnectorsUtils.RequestTypeRequestResponse)
                    {
                        if (exceptionMsg != null)
                        {
                            var resBuf = ConnectorsUtils.SerializeRequestPacket(ConnectorsUtils.RequestTypeRequestResponse, 0, 1, exceptionMsg, requestId);
                            TcpSocketsUtils.Send(Socket, resBuf, OnSend, OnExcp);
                        }
                        else
                        {
                            var rrData = new RequestResponseData()
                            {
                                RequestId = requestId,
                                Module    = module,
                                Command   = command,
                                Packet    = reqPacket,
                            };

                            new Task(() => HandleRequestResponse(rrData)).Start();
                        }
                    }

                    if (requestType == ConnectorsUtils.RequestTypeRequestMultiResponses)
                    {
                        if (exceptionMsg != null)
                        {
                            var resBuf = ConnectorsUtils.SerializeMultiResponsePacket(
                                ConnectorsUtils.RequestTypeRequestMultiResponses, 0, 1, exceptionMsg, requestId,
                                true, 0, 0);
                            TcpSocketsUtils.Send(Socket, resBuf, OnSend, OnExcp);
                        }
                        else
                        {
                            //reqPacket = ConnectorsUtils.DeserializeMultiResponsePacket(
                            //    buf, _serverConnectors._settings.PacketsMap, out requestId,
                            //    out bool isLast, out int nReceived, out int nTotal,
                            //    out module, out command);

                            _serverConnectors.TriggerOnDebugLog(this, DebugLogType.Info, $"Recv RequestTypeRequestResponse requestId = {requestId}");

                            var rrData = new RequestResponseData()
                            {
                                RequestId = requestId,
                                Module    = module,
                                Command   = command,
                                Packet    = reqPacket,
                            };
                            new Task(() => HandleRequestMultiResponses(rrData)).Start();
                        }
                    }
                }
                else //packet
                {
                    var packet = ConnectorsUtils.DeserializePacket(buf, _serverConnectors._settings.PacketsMap, out byte module, out byte command);
                    _serverConnectors.TriggerOnPacket(this, module, command, packet);
                }
            }
            catch (Exception ex)
            {
                _serverConnectors.TriggerOnDebugLog(this, DebugLogType.OnRecvException, ex.ToString());
            }
        }
Esempio n. 10
0
 public static void ShouldBeOk <T>(this RequestResponseData <T> response)
 {
     response.status.Should().Be(HttpStatusCode.OK);
     response.response.jsonRpc.Should().Be(JRPCApi.jrpcVersion);
     response.response.Should().NotBeNull();
 }