Esempio n. 1
0
        private bool SendHttpRequest(PeerBase peer, string uri, string data, WebRpcRequest rpcRequest)
        {
            var binData = Encoding.UTF8.GetBytes(data);

            HttpRequestQueueCallback callback =
                (result, request, state) => this.HandleHttpResponse(peer, rpcRequest.OperationRequest.OperationCode, result, rpcRequest, request, uri);


            var webRequest = (HttpWebRequest)WebRequest.Create(uri);

            webRequest.Proxy       = null;
            webRequest.Method      = "POST";
            webRequest.ContentType = "application/json";
            webRequest.Accept      = "application/json";
            webRequest.Timeout     = this.httpRequestTimeout;

            if (Log.IsDebugEnabled)
            {
                Log.DebugFormat("WebRpc request {0}:{1}", uri, data);
            }

            this.httpRequestQueue.Enqueue(webRequest, binData, callback, null);

            return(true);
        }
Esempio n. 2
0
        public bool HandleCall(PeerBase peer, string userId, OperationRequest request, object authResultsSecure, SendParameters sp)
        {
            var rpcRequest = new WebRpcRequest(peer.Protocol, request);

            if (!rpcRequest.IsValid)
            {
                var msg = string.Format("Invalid RPC request format: {0}", rpcRequest.GetErrorMessage());
                Log.Error(msg);
                this.SendErrorResponse(peer, request.OperationCode, sp, (short)ErrorCode.OperationInvalid, msg);
                return(false);
            }

            try
            {
                var data = this.SerializeRequest(rpcRequest, userId, authResultsSecure);

                var uri = this.MakeRequestUri(rpcRequest);

                return(this.SendHttpRequest(peer, uri, data, rpcRequest));
            }
            catch (Exception e)
            {
                var msg = string.Format("Exception during RPC request handling {0}", e);
                Log.Error(msg);
                this.SendErrorResponse(peer, request.OperationCode, sp, msg: msg);
            }

            return(true);
        }
Esempio n. 3
0
        private string SerializeRequest(WebRpcRequest request, string userId, object authResultsSecure)
        {
            if (request.RpcParams != null && typeof(Dictionary <string, object>) == request.RpcParams.GetType())
            {
                var rpcParams = (Dictionary <string, object>)request.RpcParams;

                rpcParams["AppId"]      = this.environment == null ? null : this.environment["AppId"];
                rpcParams["AppVersion"] = this.environment == null ? null : this.environment["AppVersion"];
                rpcParams["Region"]     = this.environment == null ? null : this.environment["Region"];
                rpcParams["UserId"]     = userId;
                if (WebFlags.ShouldSendAuthCookie(request.WebFlags))
                {
                    rpcParams["AuthCookie"] = authResultsSecure;
                }

                return(Newtonsoft.Json.JsonConvert.SerializeObject(rpcParams));
            }
            else
            {
                var rpcParams = new Dictionary <string, object>();

                rpcParams["AppId"]      = this.environment == null ? null : this.environment["AppId"];
                rpcParams["AppVersion"] = this.environment == null ? null : this.environment["AppVersion"];
                rpcParams["Region"]     = this.environment == null ? null : this.environment["Region"];
                rpcParams["UserId"]     = userId;
                if (WebFlags.ShouldSendAuthCookie(request.WebFlags))
                {
                    rpcParams["AuthCookie"] = authResultsSecure;
                }
                if (request.RpcParams != null)
                {
                    rpcParams["RpcParams"] = request.RpcParams;
                }

                return(Newtonsoft.Json.JsonConvert.SerializeObject(rpcParams));
            }
        }
Esempio n. 4
0
        private string MakeRequestUri(WebRpcRequest rpcRequest)
        {
            var urlbase = this.baseUrl;
            var urltail = string.Empty;

            if (this.baseUrl.Contains("?"))
            {
                var split = this.baseUrl.Split('?');
                if (split.Length > 1)
                {
                    urlbase = split[0];
                    urltail = split[1];
                }
            }

            var url = urlbase + "/" + rpcRequest.UriPath + (rpcRequest.UriPath.Contains("?") ? "&" : "?") + urltail;

            url = url.Replace("{AppId}", ((string)this.environment["AppId"]).Trim().Replace(" ", string.Empty));
            url = url.Replace("{AppVersion}", ((string)this.environment["AppVersion"]).Trim().Replace(" ", string.Empty));
            url = url.Replace("{Region}", ((string)this.environment["Region"]).Trim().Replace(" ", string.Empty));
            url = url.Replace("{Cloud}", ((string)this.environment["Cloud"]).Trim().Replace(" ", string.Empty));

            return(url);
        }
Esempio n. 5
0
        private string SerializeRequest(WebRpcRequest request, string userId, object authResultsSecure)
        {
            if (request.RpcParams != null && typeof(Dictionary<string, object>) == request.RpcParams.GetType())
            {
                var rpcParams = (Dictionary<string, object>)request.RpcParams;

                rpcParams["AppId"] = this.environment == null ? null : this.environment["AppId"];
                rpcParams["AppVersion"] = this.environment == null ? null : this.environment["AppVersion"];
                rpcParams["Region"] = this.environment == null ? null : this.environment["Region"];
                rpcParams["UserId"] = userId;
                if (WebFlags.ShouldSendAuthCookie(request.WebFlags))
                {
                    rpcParams["AuthCookie"] = authResultsSecure;
                }

                return Newtonsoft.Json.JsonConvert.SerializeObject(rpcParams);
            }
            else
            {
                var rpcParams = new Dictionary<string, object>();

                rpcParams["AppId"] = this.environment == null ? null : this.environment["AppId"];
                rpcParams["AppVersion"] = this.environment == null ? null : this.environment["AppVersion"];
                rpcParams["Region"] = this.environment == null ? null : this.environment["Region"];
                rpcParams["UserId"] = userId;
                if (WebFlags.ShouldSendAuthCookie(request.WebFlags))
                {
                    rpcParams["AuthCookie"] = authResultsSecure;
                }
                if (request.RpcParams != null)
                {
                    rpcParams["RpcParams"] = request.RpcParams;
                }

                return Newtonsoft.Json.JsonConvert.SerializeObject(rpcParams);
            }
        }
Esempio n. 6
0
        private bool SendHttpRequest(PeerBase peer, string uri, string data, WebRpcRequest rpcRequest)
        {
            var binData = Encoding.UTF8.GetBytes(data);

            HttpRequestQueueCallback callback =
                (result, request, state) => this.HandleHttpResponse(peer, rpcRequest.OperationRequest.OperationCode, result, rpcRequest, request, uri);

            var webRequest = (HttpWebRequest)WebRequest.Create(uri);
            webRequest.Proxy = null;
            webRequest.Method = "POST";
            webRequest.ContentType = "application/json";
            webRequest.Accept = "application/json";
            webRequest.Timeout = this.httpRequestTimeout;

            if (Log.IsDebugEnabled)
            {
                Log.DebugFormat("WebRpc request {0}:{1}", uri, data);
            }

            this.httpRequestQueue.Enqueue(webRequest, binData, callback, null);

            return true;
        }
Esempio n. 7
0
        private string MakeRequestUri(WebRpcRequest rpcRequest)
        {
            var urlbase = this.baseUrl;
            var urltail = string.Empty;

            if (this.baseUrl.Contains("?"))
            {
                var split = this.baseUrl.Split('?');
                if (split.Length > 1)
                {
                    urlbase = split[0];
                    urltail = split[1];
                }
            }

            var url = urlbase + "/" + rpcRequest.UriPath + (rpcRequest.UriPath.Contains("?") ? "&" : "?") + urltail;
            url = url.Replace("{AppId}", ((string)this.environment["AppId"]).Trim().Replace(" ", string.Empty));
            url = url.Replace("{AppVersion}", ((string)this.environment["AppVersion"]).Trim().Replace(" ", string.Empty));
            url = url.Replace("{Region}", ((string)this.environment["Region"]).Trim().Replace(" ", string.Empty));
            url = url.Replace("{Cloud}", ((string)this.environment["Cloud"]).Trim().Replace(" ", string.Empty));

            return url;
        }
Esempio n. 8
0
        private void HandleSuccessHttpResponse(PeerBase peer, byte operationCode, WebRpcRequest rpcRequest, AsyncHttpRequest request)
        {
            try
            {
                var serializer = new JavaScriptSerializer();
                var responseAsText = Encoding.UTF8.GetString(request.Response);
                if (string.IsNullOrEmpty(responseAsText))
                {
                    var msg = string.Format("WebRpc response for request to address {0} is empty", request.WebRequest.RequestUri);
                    if (Log.IsDebugEnabled)
                    {
                        Log.DebugFormat(msg);
                    }

                    this.SendErrorResponse(peer, operationCode, new SendParameters(), msg: msg);
                    return;
                }
                WebRpcResponse rpcResponse;
                try
                {
                    rpcResponse = serializer.Deserialize<WebRpcResponse>(responseAsText);
                }
                catch (Exception ex)
                {
                    if (Log.IsDebugEnabled)
                    {
                        Log.DebugFormat("WebRpc failed to deserialize resonse. Uri={0} response={1} error={2}",
                            request.WebRequest.RequestUri, responseAsText, ex.Message);
                    }

                    var msg = string.Format("WebRpc failed to deserialize resonse. Uri={0} response={1} error={2}",
                        request.WebRequest.RequestUri, responseAsText.Substring(0, Math.Min(200, responseAsText.Length)), ex.Message);

                    this.SendErrorResponse(peer, operationCode, new SendParameters(), msg: msg);
                    return;
                }

                if (rpcResponse == null)
                {
                    if (Log.IsDebugEnabled)
                    {
                        Log.DebugFormat("WebRpc response for request to address {0} has wrong format: {1}",
                            request.WebRequest.RequestUri, responseAsText);
                    }

                    var msg = string.Format("WebRpc response for request to address {0} has wrong format: {1}",
                        request.WebRequest.RequestUri, responseAsText.Substring(0, Math.Min(200, responseAsText.Length)));

                    this.SendErrorResponse(peer, operationCode, new SendParameters(), msg: msg);
                    return;
                }

                var data = rpcResponse.Data as IEnumerable;
                var hasData = data != null && data.GetEnumerator().MoveNext();
                if (Log.IsDebugEnabled)
                {
                    Log.DebugFormat("WebRpc response {0} for request to address {1}: {2}", rpcResponse, request.WebRequest.RequestUri, responseAsText);
                }

                var path = rpcRequest.UriPath.Contains("?") ? rpcRequest.UriPath.Substring(0, rpcRequest.UriPath.IndexOf("?")) : rpcRequest.UriPath;
                var response = new OperationResponse
                {
                    OperationCode = operationCode,
                    ReturnCode = 0,
                    DebugMessage = hasData ? Newtonsoft.Json.JsonConvert.SerializeObject(rpcResponse.Data) : "",
                    Parameters =
                        hasData
                        ?
                        new Dictionary<byte, object>
                            {
                                { (byte)ParameterKey.RpcCallRetCode, rpcResponse.ResultCode },
                                { (byte)ParameterKey.RpcCallRetMessage, rpcResponse.Message },
                                { (byte)ParameterKey.UriPath, path },
                                { (byte)ParameterKey.RpcCallParams, rpcResponse.Data },
                            }
                        :
                        new Dictionary<byte, object>
                            {
                                { (byte)ParameterKey.RpcCallRetCode, rpcResponse.ResultCode },
                                { (byte)ParameterKey.RpcCallRetMessage, rpcResponse.Message },
                                { (byte)ParameterKey.UriPath, path },
                            }
                };

                peer.SendOperationResponse(response, new SendParameters());
            }
            catch (Exception e)
            {
                var msg = string.Format("Handling of successfull response failed with exception {0}", e);
                this.SendErrorResponse(peer, operationCode, new SendParameters(), msg: msg);

                Log.Error(msg);
            }
        }
Esempio n. 9
0
 private void HandleHttpResponse(PeerBase peer, byte operationCode, HttpRequestQueueResultCode result, WebRpcRequest rpcRequest, AsyncHttpRequest request, string requestUri)
 {
     if (result == HttpRequestQueueResultCode.Success)
     {
         this.HandleSuccessHttpResponse(peer, operationCode, rpcRequest, request);
     }
     else
     {
         this.HandleErrorHttpRespone(peer, operationCode, result, request, requestUri);
     }
 }
Esempio n. 10
0
        public bool HandleCall(PeerBase peer, string userId, OperationRequest request, object authResultsSecure, SendParameters sp)
        {
            var rpcRequest = new WebRpcRequest(peer.Protocol, request);
            if (!rpcRequest.IsValid)
            {
                var msg = string.Format("Invalid RPC request format: {0}", rpcRequest.GetErrorMessage());
                Log.Error(msg);
                this.SendErrorResponse(peer, request.OperationCode, sp, (short)ErrorCode.OperationInvalid, msg);
                return false;
            }

            try
            {
                var data = this.SerializeRequest(rpcRequest, userId, authResultsSecure);

                var uri = this.MakeRequestUri(rpcRequest);

                return this.SendHttpRequest(peer, uri, data, rpcRequest);
            }
            catch (Exception e)
            {
                var msg = string.Format("Exception during RPC request handling {0}", e);
                Log.Error(msg);
                this.SendErrorResponse(peer, request.OperationCode, sp, msg: msg);
            }

            return true;
        }
Esempio n. 11
0
        private void HandleSuccessHttpResponse(PeerBase peer, byte operationCode, WebRpcRequest rpcRequest, AsyncHttpRequest request)
        {
            try
            {
                var serializer     = new JavaScriptSerializer();
                var responseAsText = Encoding.UTF8.GetString(request.Response);
                if (string.IsNullOrEmpty(responseAsText))
                {
                    var msg = string.Format("WebRpc response for request to address {0} is empty", request.WebRequest.RequestUri);
                    if (Log.IsDebugEnabled)
                    {
                        Log.DebugFormat(msg);
                    }

                    this.SendErrorResponse(peer, operationCode, new SendParameters(), msg: msg);
                    return;
                }
                WebRpcResponse rpcResponse;
                try
                {
                    rpcResponse = serializer.Deserialize <WebRpcResponse>(responseAsText);
                }
                catch (Exception ex)
                {
                    if (Log.IsDebugEnabled)
                    {
                        Log.DebugFormat("WebRpc failed to deserialize resonse. Uri={0} response={1} error={2}",
                                        request.WebRequest.RequestUri, responseAsText, ex.Message);
                    }

                    var msg = string.Format("WebRpc failed to deserialize resonse. Uri={0} response={1} error={2}",
                                            request.WebRequest.RequestUri, responseAsText.Substring(0, Math.Min(200, responseAsText.Length)), ex.Message);

                    this.SendErrorResponse(peer, operationCode, new SendParameters(), msg: msg);
                    return;
                }

                if (rpcResponse == null)
                {
                    if (Log.IsDebugEnabled)
                    {
                        Log.DebugFormat("WebRpc response for request to address {0} has wrong format: {1}",
                                        request.WebRequest.RequestUri, responseAsText);
                    }

                    var msg = string.Format("WebRpc response for request to address {0} has wrong format: {1}",
                                            request.WebRequest.RequestUri, responseAsText.Substring(0, Math.Min(200, responseAsText.Length)));

                    this.SendErrorResponse(peer, operationCode, new SendParameters(), msg: msg);
                    return;
                }

                var data    = rpcResponse.Data as IEnumerable;
                var hasData = data != null && data.GetEnumerator().MoveNext();
                if (Log.IsDebugEnabled)
                {
                    Log.DebugFormat("WebRpc response {0} for request to address {1}: {2}", rpcResponse, request.WebRequest.RequestUri, responseAsText);
                }

                var path     = rpcRequest.UriPath.Contains("?") ? rpcRequest.UriPath.Substring(0, rpcRequest.UriPath.IndexOf("?")) : rpcRequest.UriPath;
                var response = new OperationResponse
                {
                    OperationCode = operationCode,
                    ReturnCode    = 0,
                    DebugMessage  = hasData ? Newtonsoft.Json.JsonConvert.SerializeObject(rpcResponse.Data) : "",
                    Parameters    =
                        hasData
                        ?
                        new Dictionary <byte, object>
                    {
                        { (byte)ParameterKey.RpcCallRetCode, rpcResponse.ResultCode },
                        { (byte)ParameterKey.RpcCallRetMessage, rpcResponse.Message },
                        { (byte)ParameterKey.UriPath, path },
                        { (byte)ParameterKey.RpcCallParams, rpcResponse.Data },
                    }
                        :
                    new Dictionary <byte, object>
                    {
                        { (byte)ParameterKey.RpcCallRetCode, rpcResponse.ResultCode },
                        { (byte)ParameterKey.RpcCallRetMessage, rpcResponse.Message },
                        { (byte)ParameterKey.UriPath, path },
                    }
                };

                peer.SendOperationResponse(response, new SendParameters());
            }
            catch (Exception e)
            {
                var msg = string.Format("Handling of successfull response failed with exception {0}", e);
                this.SendErrorResponse(peer, operationCode, new SendParameters(), msg: msg);

                Log.Error(msg);
            }
        }
Esempio n. 12
0
 private void HandleHttpResponse(PeerBase peer, byte operationCode, HttpRequestQueueResultCode result, WebRpcRequest rpcRequest, AsyncHttpRequest request, string requestUri)
 {
     if (result == HttpRequestQueueResultCode.Success)
     {
         this.HandleSuccessHttpResponse(peer, operationCode, rpcRequest, request);
     }
     else
     {
         this.HandleErrorHttpRespone(peer, operationCode, result, request, requestUri);
     }
 }