示例#1
0
文件: server.cs 项目: bitdotgames/bhl
        public void Start()
        {
            while (true)
            {
                try
                {
                    var success = ReadAndHandle();
                    if (!success)
                    {
                        break;
                    }
                }

#if BHLSP_DEBUG
                catch (Exception e)
                {
                    BHLSPLogger.WriteLine(e);
                }
#else
                catch
                {
                    // ignored
                }
#endif
            }
        }
示例#2
0
        private RpcResult CallRpcMethod(string name, JToken @params)
        {
            if (double.TryParse(name, out _))
            {
                return(RpcResult.Error(new ResponseError
                {
                    code = (int)ErrorCodes.InvalidRequest,
                    message = ""
                }));
            }

            foreach (var service in services)
            {
                foreach (var method in service.GetType().GetMethods())
                {
                    if (IsAllowedToInvoke(method, name))
                    {
                        object[] args = null;
                        var      pms  = method.GetParameters();
                        if (pms.Length > 0)
                        {
                            try
                            {
                                args = new[] { @params.ToObject(pms[0].ParameterType) };
                            }
#if BHLSP_DEBUG
                            catch (Exception e)
                            {
                                BHLSPLogger.WriteLine(e);
#else
                            catch
                            {
#endif
                                return(RpcResult.Error(new ResponseError
                                {
                                    code = (int)ErrorCodes.InvalidParams,
                                    message = ""
                                }));
                            }
                        }

                        return((RpcResult)method.Invoke(service, args));
                    }
                }
            }

            return(RpcResult.Error(new ResponseError
            {
                code = (int)ErrorCodes.MethodNotFound,
                message = "Method not found"
            }));
        }
示例#3
0
文件: server.cs 项目: bitdotgames/bhl
        bool ReadAndHandle()
        {
            string json = connection.Read();

            if (string.IsNullOrEmpty(json))
            {
                return(false);
            }

#if BHLSP_DEBUG
            var sw = new System.Diagnostics.Stopwatch();
            sw.Start();
#endif

            string response = rpc.HandleMessage(json);

#if BHLSP_DEBUG
            sw.Stop();
            BHLSPLogger.WriteLine($"HandleMessage done({Math.Round(sw.ElapsedMilliseconds/1000.0f,2)} sec)");
#endif

            try
            {
                if (!string.IsNullOrEmpty(response))
                {
                    connection.Write(response);
                }
            }

#if BHLSP_DEBUG
            catch (Exception e)
            {
                BHLSPLogger.WriteLine(e);
                return(false);
            }
#else
            catch
            {
                return(false);
            }
#endif

            return(true);
        }
示例#4
0
        public string HandleMessage(string json)
        {
            RequestMessage  req  = null;
            ResponseMessage resp = null;

            try
            {
                req = JsonConvert.DeserializeObject <RequestMessage>(json);
            }
#if BHLSP_DEBUG
            catch (Exception e)
            {
                BHLSPLogger.WriteLine(e);
                BHLSPLogger.WriteLine($"{json}");
#else
            catch
            {
#endif
                resp = new ResponseMessage
                {
                    error = new ResponseError
                    {
                        code    = (int)ErrorCodes.ParseError,
                        message = "Parse error"
                    }
                };
            }

#if BHLSP_DEBUG
            if (req != null)
            {
                BHLSPLogger.WriteLine($":: bhlsp <-- {req.method}({req.id.Value})");
                //BHLSPLogger.WriteLine($":: bhlsp <-- {json}");
            }
            else
            {
                //BHLSPLogger.WriteLine($":: bhlsp <-- {json}");
            }
#endif

            if (resp == null && req != null)
            {
                if (req.IsMessage())
                {
                    resp = HandleMessage(req);
                }
                else
                {
                    resp = new ResponseMessage
                    {
                        error = new ResponseError
                        {
                            code    = (int)ErrorCodes.InvalidRequest,
                            message = ""
                        }
                    }
                };
            }

            string response = string.Empty;

            if (resp != null)
            {
                /* *
                 * A processed notification message must not send a response back.
                 * They work like events.
                 */
                bool isNotification = req != null && req.id.Value == null;
                if (!isNotification)
                {
                    response = JsonConvert.SerializeObject(resp, Newtonsoft.Json.Formatting.None,
                                                           new JsonSerializerSettings
                    {
                        NullValueHandling = NullValueHandling.Ignore
                    });

#if BHLSP_DEBUG
                    if (req != null)
                    {
                        BHLSPLogger.WriteLine($":: bhlsp --> {req.method}({req.id.Value})");
                        //BHLSPLogger.WriteLine($":: bhlsp --> {response}");
                    }
                    else
                    {
                        //BHLSPLogger.WriteLine($":: bhlsp --> {response}");
                    }
#endif
                }
            }

            return(response);
        }

        ResponseMessage HandleMessage(RequestMessage request)
        {
            ResponseMessage response;

            try
            {
                if (!string.IsNullOrEmpty(request.method))
                {
                    RpcResult result = CallRpcMethod(request.method, request.@params);

                    response = new ResponseMessage
                    {
                        id     = request.id,
                        result = result.result,
                        error  = result.error
                    };
                }
                else
                {
                    response = new ResponseMessage
                    {
                        id = request.id, error = new ResponseError
                        {
                            code    = (int)ErrorCodes.InvalidRequest,
                            message = ""
                        }
                    };
                }
            }
#if BHLSP_DEBUG
            catch (Exception e)
            {
                BHLSPLogger.WriteLine(e);
#else
            catch
            {
#endif
                response = new ResponseMessage
                {
                    id = request.id, error = new ResponseError
                    {
                        code    = (int)ErrorCodes.InternalError,
                        message = ""
                    }
                };
            }

            return(response);
        }