Ejemplo n.º 1
0
        public override Task <ISlarkProtocol> FindAsync(SlarkContext context)
        {
            if (context.Message is TMJsonRequest request)
            {
                if (request != null)
                {
                    var methods = context.Server.GetType().GetMethods();

                    foreach (var method in methods)
                    {
                        var requestAttributes = method.GetCustomAttributes(typeof(RequestAttribute), false);
                        if (requestAttributes.Length == 1)
                        {
                            var functionAttribute = (RequestAttribute)requestAttributes[0];
                            if (functionAttribute.Match(request))
                            {
                                RpcFunctionDelegate del = method.IsStatic ? (RpcFunctionDelegate)Delegate.CreateDelegate(typeof(RpcFunctionDelegate), method) : (RpcFunctionDelegate)Delegate.CreateDelegate(typeof(RpcFunctionDelegate), context.Server, method);
                                return(Task.FromResult(new StandardRpcFunctionHandler(del) as ISlarkProtocol));
                            }
                        }
                    }
                }
            }
            else if (context.Message is TMJsonResponse response)
            {
                if (context.Sender.Client is TMClient client)
                {
                    client.OnResponse?.Invoke(client, response);
                }

                return(Task.FromResult(new OkProtocol() as ISlarkProtocol));
            }

            return(base.FindAsync(context));
        }
Ejemplo n.º 2
0
 public Task OnRpcRedicrect(SlarkContext context)
 {
     try
     {
         if (context.Message is TMJsonRequest request)
         {
             var method = request.Url.Split("/").Last();
             if (!RpcFuncs.ContainsKey(method))
             {
                 throw new MissingMethodException($"MissingMethodException OnRpcRedicrect on {method}");
             }
             return(RpcFuncs[method].ExecuteAsync(context));
         }
         return(context.ReplyAsync());
     }
     catch (Exception ex)
     {
         var errorJson = new Dictionary <string, object>();
         if (ex is MissingMethodException mmex)
         {
             errorJson["code"]    = 404;
             errorJson["message"] = mmex.Message;
         }
         return(context.ReplyAsync());
     }
 }
Ejemplo n.º 3
0
        public async override Task ExecuteAsync(SlarkContext context)
        {
            var request = context.Message as PlayRequest;

            if (context.Server is PlayGameServer gameServer)
            {
                request.TryGet <string>("cid", "", out string cid);
                gameServer.Rooms.TryGet(r => r.Id == cid, out PlayRoom room);

                if (room != null)
                {
                    context.HasNotice = true;
                    var tuple = await room.NewPlayerJoinAsync(request, context.Sender);

                    context.Response = tuple.Item1.MetaText;
                    context.Notice   = tuple.Item2.MetaText;

                    context.Receivers = room.AvailableConnections;
                }
            }
            else if (context.Server is PlayLobbyServer lobby)
            {
                var responseBody = new Dictionary <string, object>()
                {
                    { "cmd", this.Command },
                    { "op", "added" },
                    { "i", request.CommandId },
                };

                var joinRequest = JsonConvert.DeserializeObject <RoomJoinRequest>(request.MetaText);

                var tuple = await lobby.MatchAsync(joinRequest);

                if (tuple.Item1 != null)
                {
                    var gs = tuple.Item1;
                    responseBody.Add("addr", gs.ClientConnectionUrl);
                    responseBody.Add("secureAddr", gs.ClientConnectionUrl);
                }

                if (tuple.Item2 != null)
                {
                    var room = tuple.Item2;
                    responseBody.Add("cid", room.Id);
                    responseBody.Add("open", room.IsOpen);
                    responseBody.Add("m", room.MemberIds);
                    responseBody.Add("memberIds", room.ActorIds);
                    responseBody.Add("visible", room.IsVisible);
                    responseBody.Add("maxMembers", room.MaxPlayerCount);
                }

                var response = new PlayResponse(responseBody);
                response.Timestamplize();
                response.SerializeBody();

                context.Response = response.MetaText;
            }
        }
Ejemplo n.º 4
0
 public virtual Task InvokeRpc(string functionName, SlarkContext context)
 {
     if (RpcFuncs.ContainsKey(functionName))
     {
         var function = RpcFuncs[functionName];
         return(function.ExecuteAsync(context));
     }
     return(Task.FromResult(""));
 }
Ejemplo n.º 5
0
        public override Task <ISlarkProtocol> FindAsync(SlarkContext context)
        {
            var request = new PlayRequest(context.Message.MetaText);

            if (request.IsValid)
            {
                if (CommandHandlers.ContainsKey(request.CommandHandlerKey))
                {
                    return(Task.FromResult(CommandHandlers[request.CommandHandlerKey] as ISlarkProtocol));
                }
            }

            return(base.FindAsync(context));
        }
Ejemplo n.º 6
0
        public static object[] BindParamters(MethodInfo memberInfo, SlarkContext context)
        {
            List <object> result = new List <object>();

            ParameterInfo[] rpcParameters = memberInfo.GetParameters();
            if (context.Message is TMJsonRequest request)
            {
                var pObjs = request.Body["args"] as List <object>;
                for (int i = 0; i < rpcParameters.Length - 1; i++)
                {
                    var pInfo  = rpcParameters[i];
                    var pObj   = AVDecoder.Instance.Decode(pObjs[i]);
                    var pValue = pObj;
                    result.Add(pValue);
                }
                result.Add(context);
            }
            return(result.ToArray());
        }
Ejemplo n.º 7
0
        public override Task ExecuteAsync(SlarkContext context)
        {
            var request = context.Message as PlayRequest;

            var responseBody = new Dictionary <string, object>()
            {
                { "cmd", this.Command },
                { "op", "started" },
                { "i", request.CommandId },
            };

            if (context.Server is PlayLobbyServer lobby)
            {
                var randomOne = lobby.GameServerUrls.RandomOne();
                responseBody.Add("addr", randomOne);
                responseBody.Add("secureAddr", randomOne);
            }

            return(Task.FromResult(responseBody.ToJsonString()));
        }
Ejemplo n.º 8
0
        public override Task ExecuteAsync(SlarkContext context)
        {
            var request      = context.Message as PlayRequest;
            var responseBody = new Dictionary <string, object>()
            {
                { "cmd", this.Command },
                { "op", "updated" },
                { "i", request.CommandId },
            };

            if (request.Body.ContainsKey("casAttr") || request.Body.ContainsKey("attr"))
            {
                context.HasNotice = true;
            }

            var response = new PlayResponse(responseBody);

            response.Timestamplize();
            response.SerializeBody();

            return(Task.FromResult(response.MetaText));
        }
Ejemplo n.º 9
0
        public override async Task ExecuteAsync(SlarkContext context)
        {
            var request = context.Message as PlayRequest;

            var responseBody = new Dictionary <string, object>()
            {
                { "cmd", this.Command },
                { "op", "started" },
                { "i", request.CommandId },
            };

            var roomConfig = JsonConvert.DeserializeObject <RoomConfig>(context.Message.MetaText);

            if (context.Server is PlayGameServer gameServer)
            {
                if (context.Sender.Client is PlayClient client)
                {
                    var room = await gameServer.CreateWithConfigAsync(roomConfig, context.Sender);

                    responseBody.Add("cid", room.Id);
                    responseBody.Add("visible", room.IsVisible);
                    responseBody.Add("masterClientId", room.MasterClientId);
                    responseBody.Add("memberIds", room.ActorIds);
                    responseBody.Add("actorIds", room.ActorIds);
                    responseBody.Add("m", room.MemberIds);
                    responseBody.Add("masterActorId", room.MasterClient.ActorId);
                    responseBody.Add("ttlSecs", room.TimeToKeep);
                    responseBody.Add("open", room.IsOpen);
                    responseBody.Add("emptyRoomTtl", room.EmptyTimeToLive);
                    responseBody.Add("expectMembers", room.ExpectedMemberPeerIds);
                    responseBody.Add("maxMembers", room.MaxPlayerCount);
                    responseBody.Add("members", room.MembersJsonFormatting);
                }
            }
            context.Response = responseBody.ToJsonString();
        }
Ejemplo n.º 10
0
        public override async Task ExecuteAsync(SlarkContext context)
        {
            context.Sender.Client.Token = SlarkToken.NewToken();
            var request = context.Message as PlayRequest;

            if (context.Server is PlayLobbyServer lobby)
            {
                if (context.Sender.Client is PlayClient client)
                {
                    client.PeerId = request.Body["peerId"].ToString();
                    lobby.TokenClientMap.TryAdd(context.Sender.Client.Token.Token, client);
                }
            }

            else if (context.Server is PlayGameServer gameServer)
            {
                var token = request.Body["st"].ToString();
                if (context.Sender.Client is PlayClient client)
                {
                    context.Sender.Client = await gameServer.LobbyServer.FindClientAsync(token);
                }
            }

            var responseBody = new Dictionary <string, object>()
            {
                { "cmd", this.Command },
                { "op", "opened" },
                { "i", request.CommandId },
                { "st", context.Sender.Client.Token.Token },
                { "stTtl", 2880 },
                { "appId", request.Body["appId"] },
                { "peerId", request.Body["peerId"] }
            };

            context.Response = responseBody.ToJsonString();
        }
Ejemplo n.º 11
0
 public Task ExecuteAsync(SlarkContext context)
 {
     context.HasNotice = true;
     context.Notice    = context.Message.MetaText;
     return(Task.FromResult(context.Notice));
 }
Ejemplo n.º 12
0
        public override Task <IEnumerable <SlarkClientConnection> > GetTargetsAsync(SlarkContext context)
        {
            var request = context.Message as PlayRequest;

            if (context.Server is PlayGameServer gameServer)
            {
                request.TryGet <string>("cid", "", out string cid);
                gameServer.Rooms.TryGet(r => r.Id == cid, out PlayRoom room);
                var notice = new PlayNotice()
                {
                    Body = new Dictionary <string, object>()
                    {
                        { "cmd", this.Command },
                        { "op", "updated" },
                    }
                };

                if (context.Sender.Client is PlayClient client)
                {
                    var player = room.Players.FirstOrDefault(p => p.Client == client);
                    notice.Body.Add("initby", player.PeerId);
                    notice.Body.Add("initByActor", player.ActorId);
                    notice.Body.Add("fromSA", false);
                }
                var noticeAttr = new Hashtable();
                if (request.Body.ContainsKey("casAttr"))
                {
                    var casAttr = (JObject)request.JsonObject["casAttr"];
                    Dictionary <string, PlayCAS> cas = new Dictionary <string, PlayCAS>();
                    foreach (var p in casAttr.Properties())
                    {
                        cas.Add(p.Name, new PlayCAS()
                        {
                            ExpectedValue = p.Value["expect"],
                            ValueToSet    = p.Value["value"],
                        });
                    }

                    var casNoticeAttr = room.CustomRoomProperties.AutomicUpdateOrAdd(cas);
                    noticeAttr.AutomicSet(casNoticeAttr);
                }

                if (request.Body.ContainsKey("attr"))
                {
                    var attr      = (JObject)request.JsonObject["attr"];
                    var attrTable = new Hashtable();
                    foreach (var p in attr.Properties())
                    {
                        attrTable.Add(p.Name, p.Value);
                    }
                    var setNoticeAttr = room.CustomRoomProperties.AutomicSet(attrTable);
                    noticeAttr.AutomicSet(setNoticeAttr);
                }

                notice.Body["attr"] = noticeAttr;
                context.Notice      = notice.Body.ToJsonString();

                return(Task.FromResult(room.Players.Select(p => p.ClientConnection)));
            }

            return(base.GetTargetsAsync(context));
        }
Ejemplo n.º 13
0
 public PlayContext(SlarkContext context)
 {
     coreContext = context;
 }
Ejemplo n.º 14
0
 public Task ExecuteAsync(SlarkContext context)
 {
     context.HasNotice = false;
     return(Task.FromResult(context.Message.MetaText));
 }
Ejemplo n.º 15
0
 public virtual Task <ISlarkProtocol> FindAsync(SlarkContext context)
 {
     return(Task.FromResult(new EchoProtocol() as ISlarkProtocol));
 }
Ejemplo n.º 16
0
 public Task ExecuteAsync(SlarkContext context)
 {
     return(FuncDel.Invoke(context));
 }
Ejemplo n.º 17
0
        public virtual Task <IEnumerable <SlarkClientConnection> > GetTargetsAsync(SlarkContext context)
        {
            var receivers = context.Receivers ?? context.Sender.ToEnumerable();

            return(Task.FromResult(receivers));
        }
Ejemplo n.º 18
0
 public Task ExecuteAsync(SlarkContext context)
 {
     context.HasNotice = false;
     return(Task.FromResult("ok"));
 }
Ejemplo n.º 19
0
        public virtual Task ExecuteAsync(SlarkContext context)
        {
            var response = context.Response ?? context.Message.MetaText;

            return(Task.FromResult(response));
        }
Ejemplo n.º 20
0
        public virtual Task <string> NotifyAsync(SlarkContext context)
        {
            var notice = context.Notice ?? context.Message.MetaText;

            return(Task.FromResult(notice));
        }