Example #1
0
        private void HandleRequest(RequestData request)
        {
            switch (request.Request)
            {
            case Request.Disconnect:
                // server closed or kicked player
                HandleDisconnect();
                break;

            case Request.Heartbeat:
                // Response is send below.
                break;

            case Request.StartGame:
                GameStarted?.Invoke(this, EventArgs.Empty);
                SendStartGameRequest();
                // Start game messages are sent asynchronously.
                return;

            case Request.AllowUserInput:
                InputAllowed?.Invoke(this, EventArgs.Empty);
                break;

            case Request.DisallowUserInput:
                InputDisallowed?.Invoke(this, EventArgs.Empty);
                break;

            case Request.Pause:
                GamePaused?.Invoke(this, EventArgs.Empty);
                break;

            case Request.Resume:
                GameResumed?.Invoke(this, EventArgs.Empty);
                break;

            default:
                // all other requests can not be send to a client
                throw new ExceptionFreeserf(ErrorSystemType.Network, $"Request {request} can not be send to a client.");
            }

            RespondToRequest(request);
        }
Example #2
0
        private void HandleRequest(RequestData request, ResponseHandler responseHandler)
        {
            switch (request.Request)
            {
            case Request.Disconnect:
                // server closed or kicked player
                HandleDisconnect();
                break;

            case Request.Heartbeat:
                // Response is send below.
                break;

            case Request.StartGame:
                if (Game != null)
                {
                    responseHandler?.Invoke(ResponseType.BadState);
                    return;
                }
                serverState = ServerState.Loading;
                if (PlayerIndex == 0u)
                {
                    // We have to wait for first heartbeat to set the player index.
                    SendHeartbeatRequest(response =>
                    {
                        GameStarted?.Invoke(this, EventArgs.Empty);
                        SendStartGameRequest();
                    });
                }
                else
                {
                    GameStarted?.Invoke(this, EventArgs.Empty);
                    SendStartGameRequest();
                }
                return;

            case Request.AllowUserInput:
                if (serverState == ServerState.Loading)
                {
                    serverState = ServerState.Game;
                }
                InputAllowed?.Invoke(this, EventArgs.Empty);
                break;

            case Request.DisallowUserInput:
                InputDisallowed?.Invoke(this, EventArgs.Empty);
                break;

            case Request.Pause:
                GamePaused?.Invoke(this, EventArgs.Empty);
                break;

            case Request.Resume:
                GameResumed?.Invoke(this, EventArgs.Empty);
                break;

            default:
                // all other requests can not be send to a client
                throw new ExceptionFreeserf(ErrorSystemType.Network, $"Request {request} can not be send to a client.");
            }

            RespondToRequest(request);
        }