Esempio n. 1
0
        public override void OnTransactionStart(RpcServerContext context)
        {
            string m = context.MethodName;
            string unitName;
            TccAction action;
            if (m.StartsWith("try_")) {
                action = TccAction.Try;
                unitName = m.Substring("try_".Length);
            } else if (m.StartsWith("confirm_")) {
                action = TccAction.Confirm;
                unitName = m.Substring("confirm_".Length);
            } else if (m.StartsWith("cancel_")) {
                action = TccAction.Cancel;
                unitName = m.Substring("cancel_".Length);
            } else {
                context.ReturnError(RpcErrorCode.MethodNotFound, null);
                return;
            }

            ITccRpcHostUnit unit;
            if (_instance._units.TryGetValue(unitName, out unit)) {
                unit.Invoke(context, action);
            } else {
                context.ReturnError(RpcErrorCode.MethodNotFound, null);
            }
        }
Esempio n. 2
0
        public void ProcessTransaction(RpcServerTransaction tx)
        {
            RpcServerContext context = null;

            try {
                context = new RpcServerContext(tx);

                RpcServiceBase serviceBase;
                _perfCounter.InvokePerSec.Increment();
                _perfCounter.InvokeTotal.Increment();
                _perfCounter.ConcurrentThreads.Increment();
                _perfCounter.ConcurrentContext.Increment();

                if (_services.TryGetValue(context.ServiceName, out serviceBase))
                {
                    serviceBase.OnTransactionStart(context);
                }
                else
                {
                    context.ReturnError(RpcErrorCode.ServiceNotFound, new Exception(context.ServiceName + " NotFound"));
                }
            } catch (RpcException ex) {
                context.ReturnError(ex.RpcCode, ex);
            } catch (Exception ex) {
                context.ReturnError(RpcErrorCode.ServerError, ex);
            } finally {
                _perfCounter.ConcurrentThreads.Decrement();
            }
        }
Esempio n. 3
0
        public void Register(RpcServerContext ctx)
        {
            //
            // 已经连接过的就返回失败
            if (ctx.Connection.Contexts["session"] != null)
            {
                ctx.ReturnError(RpcErrorCode.ConnectionBroken, null);
                return;
            }

            var args = ctx.GetArgs <DuplexDemoRegisterArgs>();

            if (args.UserName == "foo" && args.Passwd == "bar")
            {
                //
                // 注册成功, 建立session对象
                var session = new DuplexDemoSession()
                {
                    Id         = Guid.NewGuid().ToString(),
                    LastTime   = DateTime.Now,
                    Connection = ctx.Connection,
                };
                ctx.Connection.Contexts["session"] = session;
                lock (_syncRoot) {
                    _agents.Add(ctx.From, session);
                }
                ctx.Return();
            }
            else
            {
                ctx.ReturnError(RpcErrorCode.ConnectionFailed, new Exception("UserNotFound"));
            }
        }
Esempio n. 4
0
 public override void Call(RpcServerContext ctx)
 {
     if (RatePerSecond != null)
     {
         try {
             if (RatePerSecond != null)
             {
                 RatePerSecond.Increment();
                 Concurrent.Increment();
             }
             Method.Invoke(Service, new object[] { new RpcBatchServerContext(ctx) });
         } catch (Exception ex) {
             ctx.ReturnError(RpcErrorCode.ServerError, ex);
         } finally {
             Concurrent.Decrement();
         }
     }
     else
     {
         try {
             Method.Invoke(Service, new object[] { new RpcBatchServerContext(ctx) });
         } catch (Exception ex) {
             ctx.ReturnError(RpcErrorCode.ServerError, ex);
         }
     }
 }
Esempio n. 5
0
        public override void OnTransactionStart(RpcServerContext context)
        {
            string    m = context.MethodName;
            string    unitName;
            TccAction action;

            if (m.StartsWith("try_"))
            {
                action   = TccAction.Try;
                unitName = m.Substring("try_".Length);
            }
            else if (m.StartsWith("confirm_"))
            {
                action   = TccAction.Confirm;
                unitName = m.Substring("confirm_".Length);
            }
            else if (m.StartsWith("cancel_"))
            {
                action   = TccAction.Cancel;
                unitName = m.Substring("cancel_".Length);
            }
            else
            {
                context.ReturnError(RpcErrorCode.MethodNotFound, null);
                return;
            }

            ITccRpcHostUnit unit;

            if (_instance._units.TryGetValue(unitName, out unit))
            {
                unit.Invoke(context, action);
            }
            else
            {
                context.ReturnError(RpcErrorCode.MethodNotFound, null);
            }
        }