Exemplo n.º 1
0
        private void OnPing(Error error, RTTInfo payload)
        {
            m_rttInfo = payload;

            if (m_matchServer.HasError(error))
            {
                Error(error);
                m_matchServer.Disconnect();
                return;
            }

            if (Ping != null)
            {
                Ping(new Error(StatusCode.OK), payload);
            }

            m_matchServer.Pong(m_gSettings.ClientId, pongError =>
            {
                if (m_matchServer.HasError(pongError))
                {
                    Stopped(pongError);
                }
            });
        }
        protected override void OnRequest(ILowProtocol sender, LowRequestArgs request)
        {
            RemoteCall rpc;

            try
            {
                rpc = m_serializer.Deserialize <RemoteCall>(request.Data);
            }
            catch (Exception e)
            {
                Log.Error("Invalid RemoteCall format ", e);

                #warning This code is not tested
                sender.Disable();

                throw;
            }

            switch (rpc.Procedure)
            {
            case RemoteCall.Proc.RegisterClient:
            {
                RegisterClient(sender, rpc.ClientId);
                Return(sender, request, new Error(StatusCode.OK));
            }
            break;

            case RemoteCall.Proc.CreateMatch:
            {
                if (rpc.ClientId != ServerIdentity)
                {
                    Return(sender, request, new Error(StatusCode.NotAuthorized));
                }
                else
                {
                    Guid       creatorClientId = rpc.Get <Guid>(0);
                    Room       room            = rpc.Get <Room>(1);
                    Guid[]     clientIds       = rpc.Get <Guid[]>(2);
                    Player[]   players         = rpc.Get <Player[]>(3);
                    ReplayData replay          = rpc.Get <ReplayData>(4);

                    if (m_matchServer == null)
                    {
                        MatchServerImpl matchServer = new MatchServerImpl(this, m_path, creatorClientId, room, clientIds, players, replay);
                        m_matchServer = matchServer;

                        m_matchServer.Tick           += OnTick;
                        m_matchServer.ReadyToPlayAll += OnReadyToPlayAll;
                        m_matchServer.Paused         += OnPaused;
                        m_matchServer.Ping           += OnPing;
                        m_matchServer.ChatMessage    += OnChatMessage;
                    }

                    Return(sender, request, new Error(StatusCode.OK));
                }
            }
            break;

            case RemoteCall.Proc.GetState:
            {
                m_matchServer.GetState(rpc.ClientId, (error, arg, arg2, arg3, arg4, arg5, arg6, arg7, arg8) =>
                    {
                        Room room = arg7;
                        if (room.Mode == GameMode.Replay)
                        {
                            Return(sender, request,
                                   error,
                                   RemoteArg.Create(arg),
                                   RemoteArg.Create(new Guid[0]),
                                   RemoteArg.Create(arg3),
                                   RemoteArg.Create(arg4),
                                   RemoteArg.Create(arg5),
                                   RemoteArg.Create(arg6),
                                   RemoteArg.Create(arg7),
                                   RemoteArg.Create(arg8));
                        }
                        else
                        {
                            Dictionary <Guid, Dictionary <Guid, Player> > clientIdToPlayers = arg2;
                            Dictionary <Guid, Player> players;
                            if (!clientIdToPlayers.TryGetValue(rpc.ClientId, out players))
                            {
                                Return(sender, request, new Error(StatusCode.NotFound));
                            }

                            else
                            {
                                Return(sender, request,
                                       error,
                                       RemoteArg.Create(arg),
                                       RemoteArg.Create(players.Keys.ToArray()),
                                       RemoteArg.Create(arg3),
                                       RemoteArg.Create(arg4),
                                       RemoteArg.Create(arg5),
                                       RemoteArg.Create(arg6),
                                       RemoteArg.Create(arg7),
                                       RemoteArg.Create(arg8));
                            }
                        }
                    });
            }
            break;

            case RemoteCall.Proc.GetReplay:

                m_matchServer.GetReplay(rpc.ClientId, (error, replayData, room) =>
                {
                    Return(sender, request, error, RemoteArg.Create(replayData), RemoteArg.Create(room));
                });
                break;

            case RemoteCall.Proc.DownloadMapData:
                m_matchServer.DownloadMapData(rpc.ClientId, (Error error, byte[] data) =>
                {
                    Return(sender, request, error, RemoteArg.Create(data));
                });
                break;

            case RemoteCall.Proc.ReadyToPlay:
                m_matchServer.ReadyToPlay(rpc.ClientId, error =>
                {
                    Return(sender, request, error);
                });
                break;

            case RemoteCall.Proc.Submit:
                m_matchServer.Submit(rpc.ClientId, rpc.Get <int>(0), rpc.Get <Cmd>(1), (error, returnedCommand) =>
                {
                    Return(sender, request, error, RemoteArg.Create(returnedCommand));
                });
                break;

            case RemoteCall.Proc.SubmitResponse:
                m_matchServer.SubmitResponse(rpc.ClientId, rpc.Get <ClientRequest>(0), (error, response) =>
                {
                    Return(sender, request, error, RemoteArg.Create(response));
                });
                break;

            case RemoteCall.Proc.Pong:
                m_matchServer.Pong(rpc.ClientId, error =>
                {
                    Return(sender, request, error);
                });
                break;

            case RemoteCall.Proc.Pause:
                m_matchServer.Pause(rpc.ClientId, rpc.Get <bool>(0), error =>
                {
                    Return(sender, request, error);
                });
                break;

            case RemoteCall.Proc.IsAliveCheck:
                Return(sender, request, new Error(StatusCode.OK));
                break;

            case RemoteCall.Proc.SendChatMessage:
                m_matchServer.SendMessage(rpc.ClientId, rpc.Get <ChatMessage>(0), (error, messageId) =>
                {
                    Return(sender, request, error, RemoteArg.Create(messageId));
                });
                break;
            }
        }