예제 #1
0
        public static void RegisterMethod(MethodInfo method, Func <object[], object> callback)
        {
            var rpcResponse = method.GetCustomAttribute <RpcResponseAttribute>(true);
            var id          = rpcResponse.GetId();

            if (RpcResponseManager.GetRpcMethod(id) != null)
            {
                Logger.Warn($"方法:{id}已经注册!");
                return;
            }

            RpcResponseManager.AddRpcMethod(id, callback);
        }
예제 #2
0
        public static void RegisterMethod(string id, Func <object[], object> callback)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentNullException(nameof(id));
            }

            if (RpcResponseManager.GetRpcMethod(id) != null)
            {
                Logger.Warn($"方法:{id}已经注册!");
                return;
            }

            RpcResponseManager.AddRpcMethod(id, callback);
        }
예제 #3
0
        public void Handle(ulong remoteMessageId, IPeer peer, IReadStream readStream)
        {
            if (peer == null)
            {
                Logger.Error($"{nameof(peer)} 为 null");
                return;
            }

            var method = RpcResponseManager.GetRpcMethod(MethodId);

            if (method == null)
            {
                Logger.Warn($"{MethodId}未注册!");
                return;
            }

            var length = readStream.ShiftRight <byte>();

            Logger.Debug($"MethodId:{MethodId} 参数个数:{length}");

            var deserializable = new Deserializable(readStream);
            var values         = deserializable.Deserialize(new object[length + 2], length);

            values[values.Length - 2] = peer;
            Action          temp   = () => { };
            Action <Action> action = callback => temp += callback;

            values[values.Length - 1] = action;

            ResponseCallback responseCallback = (rm, sd) => {
                temp?.Invoke();
            };

            object result          = null;
            var    lateBoundMethod = RpcResponseManager.GetRpcMethod(MethodId);
            var    exception       = "";

            if (lateBoundMethod == null)
            {
                exception = $"没有方法:{MethodId}";
                Logger.Warn(exception);
            }
            else
            {
                var stopwatch = Stopwatch.StartNew();
                try {
                    result = lateBoundMethod.Invoke(values);
                } catch (Exception e) {
                    Logger.Error($"方法:[{MethodId}] " + e);
                    exception = $"方法:[{MethodId}] 发生异常!";
                } finally {
                    stopwatch.Stop();
                }

                Logger.Debug($"当前[{MethodId}]方法执行时间:{stopwatch.ElapsedMilliseconds}毫秒");
            }

            var rpcResponse = new RpcResponse(exception, MethodId, remoteMessageId);

            using (var serializable = new Serializable(null)) {
                serializable.SerializableObject(result);
                RpcProxy.Invoke(MessageType.RpcResponse, rpcResponse,
                                serializable.WriteStream, peer, responseCallback);
            }
        }