示例#1
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"));
            }
        }
示例#2
0
 public void SendCommand(string from, string command)
 {
     lock (_syncRoot) {
         DuplexDemoSession session = _agents[from];
         RpcClientProxy    proxy   = session.Connection.GetCallbackProxy <IDuplexDemoCallbackService>();
         proxy.Invoke <string, string>("CallCommand", command);
     }
 }
示例#3
0
        public void KeepAlive(RpcServerContext ctx)
        {
            int seconds = ctx.GetArgs <int>();
            DuplexDemoSession session = (DuplexDemoSession)ctx.Connection.Contexts["session"];

            session.LastTime = DateTime.Now.AddSeconds(seconds);
            ctx.Return(0);              // 没意义
        }