Exemple #1
0
        public void TestDiscardCard()
        {
            RPCHandler handler = new RPCHandler();

            SecretHitlerGame game = new SecretHitlerGame(handler);

            var connections = new uint[] { 0, 1, 2 };
            var names       = new string[] { "0", "1", "2" };

            game.Init();
            game.OnStart(connections, names);
            game.OnTick();
            Assert.AreEqual(game.President, 0);
            Assert.AreEqual(game.Cancellor, -1);

            TestHelper.WrapAction(0, game.SelectCancellor, 1);

            Assert.AreEqual(game.President, 0);
            Assert.AreEqual(game.Cancellor, 1);

            TestHelper.WrapAction(0, game.Vote, true);
            TestHelper.WrapAction(1, game.Vote, true);

            Assert.AreEqual(game.President, 0);
            Assert.AreEqual(game.Cancellor, 1);
        }
Exemple #2
0
        public static RPCEntry Pop(RPCHandler handler, params object[] args)
        {
            RPCEntry rpcEntry = POOL.Pop();

            rpcEntry.handler = handler;
            rpcEntry.args    = args;
            return(rpcEntry);
        }
Exemple #3
0
        public void Send(ushort tokenId, Packet packet, RPCHandler callback)
        {
            KCPUserToken token = this._tokenManager.Get(tokenId);

            this._rpcManager.Accept(token, packet, callback);
            packet.OnSend();
            token?.Send(packet);
        }
    // Use this for initialization
    void Start()
    {
        RPCHandler handler = new RPCHandler();
        RPCParser  parser  = new RPCParser(handler);

        _client = new ClientNetworking(parser, "localhost", 7777);
        _client.connect();

        handler.addResponseListener("numberResult", handleResult, handleError);
    }
Exemple #5
0
        public void Accept(Packet packet, RPCHandler callback)
        {
            if (!packet.isRPCCall)
            {
                return;
            }
            ushort pid = this.GetNextPacketId();

            packet.pid             = pid;
            this._rpcHandlers[pid] = callback;
        }
Exemple #6
0
        public void Invoke(Packet packet)
        {
            if (!packet.isRPCReturn)
            {
                return;
            }
            RPCHandler handler = this._rpcHandlers[packet.srcPid];

            this._rpcHandlers.Remove(packet.srcPid);
            handler.Invoke(null, packet);
        }
Exemple #7
0
        public void Invoke(IUserToken token, Packet packet)
        {
            if (!packet.isRPCReturn)
            {
                return;
            }
            Dictionary <ushort, RPCHandler> map = this._rpcsHandlers[token];
            RPCHandler handler = map[packet.srcPid];

            map.Remove(packet.srcPid);
            handler.Invoke(token, packet);
        }
Exemple #8
0
        public SecretHitlerGame GetStartState()
        {
            RPCHandler handler = new RPCHandler();

            SecretHitlerGame game = new SecretHitlerGame(handler);

            game.Init();
            game.OnStart(connections, names);
            game.OnTick();

            return(game);
        }
Exemple #9
0
        public void Accept(IUserToken token, Packet packet, RPCHandler callback)
        {
            if (!packet.isRPCCall)
            {
                return;
            }
            ushort pid = this.GetNextPacketId(token);

            packet.pid = pid;
            Dictionary <ushort, RPCHandler> handlers = this._rpcsHandlers[token];

            handlers[pid] = callback;
        }
Exemple #10
0
    IEnumerator coLogin(LoginRequest request, RPCHandler <LoginReply> handler)
    {
        try
        {
            var reply = client.Login(request);

            handler(reply);
        }
        catch (Grpc.Core.RpcException e)
        {
            Debug.LogError("RPC failed " + e);
        }

        yield return(null);
    }
Exemple #11
0
    IEnumerator coRpcServiceExample(RpcServiceExampleRequest request, RPCHandler <RpcServiceExampleReply> handler)
    {
        try
        {
            var reply = client.RpcServiceExample(request);

            handler(reply);
        }
        catch (Grpc.Core.RpcException e)
        {
            Debug.LogError("RPC failed " + e);
        }

        yield return(null);
    }
Exemple #12
0
    IEnumerator coLogin(LoginRequest request, RPCHandler <LoginReply> handler)
    {
        //try
        //{
        //    var reply = client.Login(request);

        //    handler(reply);
        //}
        //catch (Grpc.Core.RpcException e)
        //{
        //    Debug.LogError("RPC failed " + e);
        //}

        yield return(null);
    }
    // Use this for initialization
    void Start()
    {
        Application.runInBackground = true;

        // RPC
        RPCHandler handler   = new RPCHandler();
        IRPCParser rpcParser = new RPCParser(handler);

        // Server
        ServerNetworking server = new ServerNetworking(rpcParser, 7777);

        server.CreateServer();

        Calculator calc = new Calculator(handler);
    }
Exemple #14
0
    IEnumerator coRankingList(RankingListRequest request, RPCHandler <RankingListReply> handler)
    {
        //try
        //{
        //    var metaData = new Metadata
        //    {
        //        { "access_key", GameDataManager.Instance.userData.accessKey }
        //    };

        //    var reply = client.RankingList(request, metaData);

        //    handler(reply);
        //}
        //catch (Grpc.Core.RpcException e)
        //{
        //    Debug.LogError("RPC failed " + e);
        //}

        yield return(null);
    }
Exemple #15
0
        public MatchLobbyContext MakeLobbyContext()
        {
            if (IsHost)
            {
                var lobby = new MatchLobby(GameData.Scenarios.First());
                lobby.ApplyAction(new AddPlayerAction(Player));
                var chat = new Chat();

                Server.MessageAdapter = new NonMatchMessageSerializer();
                Server.RPCHandler     =
                    new RPCHandler().Install(
                        new LobbyServerLayer(lobby, ConnectionCache), new ChatServerLayer(chat, ConnectionCache));

                lobby.OnActionApplied += (sender, e) => Server.Broadcast(new ApplyLobbyActionRequest(e.Value));
                chat.OnActionApplied  += (sender, e) => Server.Broadcast(new ApplyChatActionRequest(e.Value));

                return(new MatchLobbyContext(Server, ConnectionCache, lobby, chat));
            }
            else
            {
                var chat = new Chat();
                Client.MessageAdapter = new NonMatchMessageSerializer();
                var lobbyLayer = new LobbyLayer();
                var handler    = new RPCHandler().Install(lobbyLayer, new ChatLayer(chat));
                Client.RPCHandler = handler;

                if (Client.Call(
                        new ApplyLobbyActionRequest(new AddPlayerAction(Player))).Get <BooleanResponse>().Value)
                {
                    MatchLobby lobby = Client.Call(new GetLobbyRequest()).Get <GetLobbyResponse>().Lobby;
                    lobbyLayer.Lobby = lobby;
                    return(new MatchLobbyContext(Client, lobby, chat));
                }
                return(null);
            }
        }
Exemple #16
0
        public void handleMessage(byte[] msg)
        {
            BinaryReader reader = new BinaryReader(new MemoryStream(msg));

            int cmd = reader.ReadInt16();

            if (cmd == 0)
            {
                bool ok = reader.ReadBoolean();
                if (ok)
                {
                    ConnectionId  = reader.ReadUInt32();
                    this.Username = reader.ReadString();
                    SceneManager.LoadScene("LobbyScene", LoadSceneMode.Single);
                }
                else
                {
                    this.State        = 0;
                    this.Error        = true;
                    this.ErrorMessage = reader.ReadString();
                }
            }
            else if (cmd == 2)
            {
                var connId = reader.ReadUInt32();
                if (connId == ConnectionId)
                {
                    this.State = 2;
                    GameLobby  = new GameLobby();
                    GameLobby.Players.Add(connId, new GameLobbyPlayer(connId, reader.ReadString()));
                    SceneManager.LoadScene("GameLobbyScene", LoadSceneMode.Single);
                }
                else
                {
                    GameLobby.Players.Add(connId, new GameLobbyPlayer(connId, reader.ReadString()));
                }
            }
            else if (cmd == 3)
            {
                var connId = reader.ReadUInt32();
                GameLobby.HostId = connId;
            }
            else if (cmd == 4)
            {
                var connId = reader.ReadUInt32();
                if (connId == ConnectionId)
                {
                    State     = 1;
                    GameLobby = null;
                }
                else
                {
                    GameLobby.Players.Remove(connId);
                }
            }

            else if (cmd == 5)
            {
                var gameId = reader.ReadUInt32();

                RPCHandler RPCHandler = new RPCHandler();
                game = new SecretHitlerGame(RPCHandler);
                FindObjectOfType <GameData>().setGameData(game);
                game.Init();

                uint[]   ids   = new uint[this.GameLobby.Players.Count];
                string[] names = new string[this.GameLobby.Players.Count];

                int index = 0;
                foreach (var p in this.GameLobby.Players.Values)
                {
                    ids[index]   = p.ConnectionId;
                    names[index] = p.Username;
                    index++;
                }

                game.OnStart(ids, names);
                SceneManager.LoadScene("SecretHitler", LoadSceneMode.Single);
            }
            else if (cmd == 6)
            {
                //RPC
            }
            else if (cmd == 7)
            {
                //tick
                game.OnTick();
                Debug.Log("Tick");
            }
        }
Exemple #17
0
 public void Send(ushort tokenId, Packet packet, RPCHandler callback)
 {
 }
Exemple #18
0
 // RankingList
 public void RankingList(RankingListRequest request, RPCHandler <RankingListReply> handler)
 {
     StartCoroutine(coRankingList(request, handler));
 }
Exemple #19
0
 public void Clear()
 {
     this.handler = null;
     this.args    = null;
 }
Exemple #20
0
 // LoadGameData
 public void LoadGameData(LoadGameDataRequest request, RPCHandler <LoadGameDataReply> handler)
 {
     StartCoroutine(coLoadGameData(request, handler));
 }
Exemple #21
0
 // SaveGameData
 public void SaveGameData(SaveGameDataRequest request, RPCHandler <SaveGameDataReply> handler)
 {
     StartCoroutine(coSaveGameData(request, handler));
 }
Exemple #22
0
 // Login
 public void Login(LoginRequest request, RPCHandler <LoginReply> handler)
 {
     StartCoroutine(coLogin(request, handler));
 }
Exemple #23
0
 public void Install(RPCHandler Handler)
 {
     Handler.RegisterRPC <ExecuteOrderRequest>(ExecuteOrder);
     Handler.RegisterRPC <ValidateOrderRequest>(ValidateOrder);
 }
Exemple #24
0
 public void Install(RPCHandler Handler)
 {
     Handler.RegisterRPC <ApplyLobbyActionRequest>(ApplyLobbyAction);
     Handler.RegisterRPC <GetLobbyRequest>(GetLobby);
 }
Exemple #25
0
    // HealthCheck - Check
    //public void Check(HealthCheckRequest request, RPCHandler<HealthCheckResponse> handler)
    //{
    //    healthCheckThread = new Thread(() =>
    //    {
    //        int intervalTime = 60 * 1000;//Time.time + (10.0f * 60.0f);

    //        //try
    //        //{
    //        //    while (running)
    //        //    {
    //        //        //if (Time.time < intervalTime)
    //        //        //{
    //        //        //    continue;
    //        //        //}

    //        //        var metaData = new Metadata
    //        //        {
    //        //            { "access_key", GameDataManager.Instance.userData.accessKey }
    //        //        };

    //        //        var reply = health.Check(request, metaData);

    //        //        handler(reply);

    //        //        Thread.Sleep(intervalTime);
    //        //    }
    //        //}
    //        //catch (Grpc.Core.RpcException e)
    //        //{
    //        //    Debug.LogError("RPC failed " + e);
    //        //}
    //    });


    //    healthCheckThread.Start();
    //    //StartCoroutine(coCheck(request, handler));
    //}

    //IEnumerator coCheck(HealthCheckRequest request, RPCHandler<HealthCheckResponse> handler)
    //{
    //    try
    //    {
    //        while (running)
    //        {
    //            //if (Time.time < intervalTime)
    //            //{
    //            //    continue;
    //            //}

    //            var metadata = new Metadata
    //            {
    //                { "access_key", GameDataManager.Instance.userData.accessKey }
    //            };

    //            var reply = health.Check(request, metadata);

    //            handler(reply);
    //        }
    //    }
    //    catch (Grpc.Core.RpcException e)
    //    {
    //        Debug.LogError("RPC failed " + e);
    //    }

    //    yield return null;
    //}

    // HealthCheck - Watch
    //public void Watch(HealthCheckRequest request,
    //    RPCHandler<HealthCheckResponse> handler)
    //{
    //    //coWatch(request, handler);

    //    //await task;
    //    //StartCoroutine(coWatch(request, handler));
    //    //await coWatch(request, handler);

    //    healthWatchThread = new Thread(async () =>
    //    {
    //        //try
    //        //{
    //        //    while (running)
    //        //    {
    //        //        var metaData = new Metadata
    //        //        {
    //        //            { "access_key", GameDataManager.Instance.userData.accessKey }
    //        //        };

    //        //        using (var call = health.Watch(request, metaData))
    //        //        {
    //        //            while (await call.ResponseStream.MoveNext())
    //        //            {
    //        //                HealthCheckResponse reply = call.ResponseStream.Current;
    //        //                handler(reply);
    //        //            }
    //        //        }
    //        //    }
    //        //}
    //        //catch (Grpc.Core.RpcException e)
    //        //{
    //        //    Debug.LogError("RPC failed " + e);
    //        //}
    //    });

    //    healthWatchThread.Start();
    //}

    //void coWatch(HealthCheckRequest request,
    //    RPCHandler<HealthCheckResponse> handler)
    //{
    //    Thread thread = new Thread(async () =>
    //    {
    //        try
    //        {
    //            using (var call = health.Watch(request))
    //            {
    //                while (await call.ResponseStream.MoveNext())
    //                {
    //                    HealthCheckResponse reply = call.ResponseStream.Current;
    //                    handler(reply);
    //                }
    //            }

    //            //var reply = health.Watch(request);

    //            //handler(reply);
    //        }
    //        catch (Grpc.Core.RpcException e)
    //        {
    //            Debug.LogError("RPC failed " + e);
    //        }
    //    });
    //}

    // RpcServiceExample
    public void RpcServiceExample(RpcServiceExampleRequest request, RPCHandler <RpcServiceExampleReply> handler)
    {
        StartCoroutine(coRpcServiceExample(request, handler));
    }
Exemple #26
0
 public static void Send(string serverName, ushort tokenId, Packet packet, RPCHandler callback = null)
 {
     GetServer(serverName)?.Send(tokenId, packet, callback);
 }
Exemple #27
0
 public static void Send(string clientName, Packet packet, RPCHandler callback = null)
 {
     GetClient(clientName)?.Send(packet, callback);
 }
Exemple #28
0
 public void Install(RPCHandler Handler)
 {
     Handler.RegisterRPC <LogInPlayerRequest>(LogInPlayer);
     Handler.RegisterRPC <RegisterPlayerRequest>(RegisterPlayer);
     Handler.RegisterRPC <GetUnitConfigurationPacksRequest>(GetUnitConfigurationPacks);
 }
Exemple #29
0
 public void Send(Packet packet, RPCHandler callback = null)
 {
     this._rpcManager.Accept(packet, callback);
     packet.OnSend();
     this.Send(NetworkHelper.EncodePacket(packet));
 }
 public void Install(RPCHandler Handler)
 {
     Handler.RegisterRPC <ApplyChatActionRequest>(ApplyChatAction);
 }