// Copyright (c) 2012-2022 Wojciech Figat. All rights reserved. #pragma once #include "NetworkConnection.h" #include "Types.h" #include "NetworkConnectionState.h" #include "Engine/Core/Delegate.h" #include "Engine/Core/Collections/Array.h" #include "Engine/Scripting/ScriptingType.h" /// /// The high-level network manage modes. /// API_ENUM(Namespace="FlaxEngine.Networking") enum class NetworkManagerMode { // Disabled. Offline = 0, // Server-only without a client (host a game but not participate). Server, // Client-only (connected to Server or Host). Client, // Both server and client (other clients can connect). Host, }; /// /// The high-level network client connection data. Can be used to accept/deny new connection. /// API_STRUCT(Namespace="FlaxEngine.Networking") struct NetworkClientConnectionData { DECLARE_SCRIPTING_TYPE_MINIMAL(NetworkClientConnectionData); // The incoming client. API_FIELD() NetworkClient* Client; // 0 if accept new connection, error code otherwise. Send back to the client. API_FIELD() int32 Result; // Client platform type (can be different that current one when using cross-play). API_FIELD() PlatformType Platform; // Client platform architecture (can be different that current one when using cross-play). API_FIELD() ArchitectureType Architecture; // Custom data to send from client to server as part of connection initialization and verification. Can contain game build info, platform data or certificate needed upon joining the game. API_FIELD() Array PayloadData; }; /// /// High-level networking manager for multiplayer games. /// API_CLASS(static, Namespace = "FlaxEngine.Networking") class FLAXENGINE_API NetworkManager { DECLARE_SCRIPTING_TYPE_MINIMAL(NetworkManager); public: /// /// The target amount of the network logic updates per second (frequency of replication, events sending and network ticking). Use 0 to run every game update. /// API_FIELD() static float NetworkFPS; /// /// Current network peer (low-level). /// API_FIELD(ReadOnly) static NetworkPeer* Peer; /// /// Current manager mode. /// API_FIELD(ReadOnly) static NetworkManagerMode Mode; /// /// Current network connection state. /// API_FIELD(ReadOnly) static NetworkConnectionState State; /// /// Current network system frame number (incremented every tick). Can be used for frames counting in networking and replication. /// API_FIELD(ReadOnly) static uint32 Frame; /// /// Server client identifier. Constant value of 0. /// API_FIELD(ReadOnly) static constexpr uint32 ServerClientId = 0; /// /// Local client identifier. Valid even on server that doesn't have LocalClient. /// API_FIELD(ReadOnly) static uint32 LocalClientId; /// /// Local client, valid only when Network Manager is running in client or host mode (server doesn't have a client). /// API_FIELD(ReadOnly) static NetworkClient* LocalClient; /// /// List of all clients: connecting, connected and disconnected. Empty on clients. /// API_FIELD(ReadOnly) static Array Clients; public: /// /// Event called when network manager state gets changed (eg. client connected to the server, or server shutdown started). /// API_EVENT() static Action StateChanged; /// /// Event called when new client is connecting. Can be used to accept/deny connection. Called on client to fill PayloadData send and called on server/host to validate incoming client connection (eg. with PayloadData validation). /// API_EVENT() static Delegate ClientConnecting; /// /// Event called after new client successfully connected. /// API_EVENT() static Delegate ClientConnected; /// /// Event called after new client successfully disconnected. /// API_EVENT() static Delegate ClientDisconnected; public: // Returns true if network is a client. API_PROPERTY() FORCE_INLINE static bool IsClient() { return Mode == NetworkManagerMode::Client; } // Returns true if network is a server. API_PROPERTY() FORCE_INLINE static bool IsServer() { return Mode == NetworkManagerMode::Server; } // Returns true if network is a host (both client and server). API_PROPERTY() FORCE_INLINE static bool IsHost() { return Mode == NetworkManagerMode::Host; } // Returns true if network is connected and online. API_PROPERTY() FORCE_INLINE static bool IsConnected() { return State == NetworkConnectionState::Connected; } /// /// Gets the network client for a given connection. Returns null if failed to find it. /// /// Network connection identifier. /// Found client or null. API_FUNCTION() static NetworkClient* GetClient(API_PARAM(Ref) const NetworkConnection& connection); public: /// /// Starts the network in server mode. Returns true if failed (eg. invalid config). /// API_FUNCTION() static bool StartServer(); /// /// Starts the network in client mode. Returns true if failed (eg. invalid config). /// API_FUNCTION() static bool StartClient(); /// /// Starts the network in host mode. Returns true if failed (eg. invalid config). /// API_FUNCTION() static bool StartHost(); /// /// Stops the network. /// API_FUNCTION() static void Stop(); };