Beispiel #1
0
        private void SendMessage <T>(ActorContext context, NameValueCollection header, T rep)
        {
            header["format"] = context.ResponseCovnert;

            var repMsg = new NetMQMessage();

            repMsg.Append(new byte[] { context.Version }); //版本号
            repMsg.Append("action");                       //动作
            repMsg.Append(context.SessionId);              //sessionid
            repMsg.Append(context.ClientKeys);             //客户端密钥
            repMsg.Append(context.ServerKeys);             //服务端密钥
            repMsg.Append(GetHeaderBytes(header));         //信息头
            if (!Equals(rep, default(T)))
            {
                repMsg.Append(ServerContainer.ServerIoc.ResolveKeyed <IResponseConvert>(context.ResponseCovnert).SerializeObject(rep));//信息体
            }
            else
            {
                repMsg.AppendEmptyFrame();
            }

            base.EnqueueMessage(repMsg);
        }
Beispiel #2
0
 protected abstract TOnlineRole CreateOnlineRole(ActorContext context, IToken token);
Beispiel #3
0
        //处理接收
        private void ProcessReceive(NetMQMessage msg)
        {
            var context = new ActorContext()
            {
                Version = msg[0].Buffer[0],            //版本号
                                                       //ActionName = msg[1].ConvertToString(),//动作
                SessionId  = msg[2].ConvertToString(), //sessionid
                ClientKeys = msg[3].Buffer,            //客户端密钥
                ServerKeys = msg[4].Buffer,            //服务端密钥
                Request    = new PirateXRequestInfo(
                    msg[5].Buffer,                     //信息头
                    msg[6].Buffer)                     //信息体
                , ResponseCovnert = "protobuf"
            };
            var format = context.Request.Headers["format"];

            if (!string.IsNullOrEmpty(format))
            {
                context.ResponseCovnert = format;
            }

            //执行动作
            var actionname = context.Request.C;

            using (var action = GetActionInstanceByName(actionname))
            {
                if (action != null)
                {
                    try
                    {
                        var token = GetToken(context.Request.Token);
                        context.Token = token;

                        //授权检查
                        if (!VerifyToken(ServerContainer.GetDistrictConfig(token.Did), token))
                        {
                            throw new PirateXException("AuthError", "授权失败")
                                  {
                                      Code = StatusCode.Unauthorized
                                  }
                        }
                        ;

                        //context.Request.Token
                        //获取session信息  从缓存中去获取session信息  session没有的时候需要提示客户端重新连接

                        if (Equals(actionname, "NewSeed"))
                        {
                            var onlinerole2 = CreateOnlineRole(context, token);
                            onlinerole2.ClientKeys = context.ClientKeys;
                            onlinerole2.ServerKeys = context.ServerKeys;

                            ServerContainer.ServerIoc.Resolve <IOnlineManager>().Login(onlinerole2);
                        }
                        else
                        {
                            var onlinerole = OnlineManager.GetOnlineRole(token.Rid);
                            action.Reslover = ServerContainer.GetDistrictContainer(token.Did).BeginLifetimeScope();

                            if (onlinerole == null)
                            {
                                var onlinerole2 = CreateOnlineRole(context, token);
                                onlinerole2.ClientKeys = context.ClientKeys;
                                onlinerole2.ServerKeys = context.ServerKeys;

                                ServerContainer.ServerIoc.Resolve <IOnlineManager>().Login(onlinerole2);
                            }
                            else if (!Equals(onlinerole.SessionId, context.SessionId))
                            {
                                //单设备登陆控制
                                throw new PirateXException("ReLogin", "ReLogin")
                                      {
                                          Code = StatusCode.ReLogin
                                      };
                            }

                            //if (!context.ServerKeys.Any())
                            //{   //第一次请求

                            //}
                            //else
                            //{
                            //    //单设备登陆控制
                            //    if (!Equals(onlinerole.SessionId, context.SessionId))
                            //        throw new PirateXException("ReLogin", "ReLogin") { Code = StatusCode.ReLogin };
                            //}

                            action.OnlieRole = onlinerole;
                        }

                        action.ServerReslover = ServerContainer.ServerIoc;
                        action.Context        = context;
                        action.Logger         = Logger;
                        action.MessageSender  = this;

                        action.Execute();
                    }
                    catch (Exception exception)
                    {
                        HandleException(context, exception);
                    }
                }
                else
                {
                    var headers = new NameValueCollection()
                    {
                        { "c", context.Request.C },
                        { "i", MessageType.Rep },
                        { "o", Convert.ToString(context.Request.O) },
                        { "code", Convert.ToString((int)StatusCode.NotFound) },
                        { "errorCode", "NotFound" },
                        { "errorMsg", $"Action {actionname} not found!" }
                    };
                    //返回类型

                    SendMessage <string>(context, headers, null);
                }
            }
        }