![]() |
CUGL 3.0
Cornell University Game Library
|
#include <CUNetEventController.h>
Public Types | |
enum class | Status : int { IDLE = 0 , CONNECTING = 1 , CONNECTED = 2 , HANDSHAKE = 3 , READY = 4 , INGAME = 5 , NETERROR = 6 } |
Public Member Functions | |
NetEventController (void) | |
~NetEventController () | |
void | dispose () |
bool | init (const std::shared_ptr< AssetManager > &assets) |
bool | isHost () const |
std::string | getRoomID () const |
Uint32 | getShortUID () const |
int | getNumPlayers () const |
Status | getStatus () const |
bool | connectAsHost () |
bool | connectAsClient (std::string roomID) |
void | disconnect () |
void | startGame () |
bool | markReady () |
std::shared_ptr< NetPhysicsController > | getPhysController () |
Uint64 | getGameTick () const |
void | enablePhysics (std::shared_ptr< NetWorld > &world) |
void | enablePhysics (std::shared_ptr< NetWorld > &world, ObstacleLink linkFunc) |
void | disablePhysics () |
template<typename T > | |
void | attachEventType () |
bool | isInAvailable () |
std::shared_ptr< NetEvent > | popInEvent () |
void | pushOutEvent (const std::shared_ptr< NetEvent > &e) |
void | updateNet () |
Static Public Member Functions | |
static std::shared_ptr< NetEventController > | alloc (const std::shared_ptr< AssetManager > &assets) |
Protected Member Functions | |
std::shared_ptr< NetEvent > | unwrap (const std::vector< std::byte > &data, std::string source) |
const std::vector< std::byte > | wrap (const std::shared_ptr< NetEvent > &e) |
void | processReceivedData () |
void | processReceivedEvent (const std::shared_ptr< NetEvent > &e) |
void | processGameStateEvent (const std::shared_ptr< GameStateEvent > &e) |
bool | checkConnection () |
void | sendQueuedOutData () |
Uint8 | getType (const NetEvent &e) |
Protected Attributes | |
Uint64 | _startGameTimeStamp |
cugl::netcode::NetcodeConfig | _config |
std::shared_ptr< cugl::netcode::NetcodeConnection > | _network |
Status | _status |
std::string | _roomid |
bool | _isHost |
Uint8 | _numReady |
std::unordered_map< std::type_index, Uint8 > | _eventTypeMap |
std::vector< std::shared_ptr< NetEvent > > | _newEventVector |
std::queue< std::shared_ptr< NetEvent > > | _inEventQueue |
std::queue< std::shared_ptr< NetEvent > > | _reservedInEventQueue |
std::vector< std::shared_ptr< NetEvent > > | _outEventQueue |
Uint32 | _shortUID |
bool | _physEnabled |
std::shared_ptr< NetPhysicsController > | _physController |
This class is a network controller for multiplayer physics based game.
This class holds a cugl::netcode::NetcodeConnection
and is an extension of the original network controller. It is built around an event-based system that fully encapsulates the network connection. Events across the network are automatically serialized and deserialized.
Connection to to the lobby is provided by the methods connectAsHost
and connectAsClient
. When starting a game, the host locks the lobby and calls startGame()
to initiate a handshake. The host then distributes a shortUID to all players (including the host), and players respond by calling markReady
after they receive the shortUID and finish their initialization. When host receives responses from all players, the game will officially start and getStatus
will return INGAME.
Physics synchronization is an optional feature, and is enabled by calling enablePhysics
. Upon enabling physics, a dedicated controller is created to handle physics synchronization. For fine-tuning and more info, see NetPhysicsController
.
There are three built-in event types: GameStateEvent
, PhysSyncEvent
, and PhysObstEvent
. See the NetEvent
class and attachEventType
for how to add and setup custom events.
|
strong |
cugl::physics2::distrib::NetEventController::NetEventController | ( | void | ) |
Creates a degenerate network controller
This object will have only default values and has not yet been initialized.
NEVER USE A CONSTRUCTOR WITH NEW. If you want to allocate an object on the heap, use one of the static constructors instead.
|
inline |
Deletes this network controller and all of its resources.
|
inlinestatic |
Returns a newly allocated controller for the given asset manager.
This method requires the asset manager to have a JSON value with key "server". The JSON value should match the structure required by cugl::netcode::NetcodeConfig
.
assets | The asset manager |
|
inline |
|
protected |
Returns true if the connection is still active after a status check
This method updates the controller status according to the protocol.
bool cugl::physics2::distrib::NetEventController::connectAsClient | ( | std::string | roomID | ) |
Connects to an existing lobby as client.
If successful, the controller status changes to Status#CONNECTED
.
roomID | The room to connect to |
bool cugl::physics2::distrib::NetEventController::connectAsHost | ( | ) |
Connects to a new lobby as host.
If successful, the controller status changes to Status#CONNECTED
, and getRoomID
is set to the lobby id.
void cugl::physics2::distrib::NetEventController::disablePhysics | ( | ) |
Disables physics synchronization.
void cugl::physics2::distrib::NetEventController::disconnect | ( | ) |
Disconnects from the current lobby.
void cugl::physics2::distrib::NetEventController::dispose | ( | ) |
Disposes the network controller, releasing all resources.
This controller can be safely reinitialized
|
inline |
Enables physics synchronization.
This method requires the shortUID to be assigned to this controller. This version of the method does not link the physics world to a secent graph and requires the user to handle view changes (due to obstacle creation and deletion) manually.
world | The physics world to be synchronized. |
void cugl::physics2::distrib::NetEventController::enablePhysics | ( | std::shared_ptr< NetWorld > & | world, |
ObstacleLink | linkFunc | ||
) |
Enables physics synchronization.
This method requires the shortUID to be assigned to this controller. The linkFunc should be a function that links a scene node to an obstacle with a listener, and then adds that scene node to a scene graph. See NetPhysicsController
for more information.
world | The physics world to be synchronized. |
linkFunc | Function that links a scene node to an obstacle. |
Uint64 cugl::physics2::distrib::NetEventController::getGameTick | ( | ) | const |
Returns the discrete timestamp since the game started.
Peers should have similar timestamps regardless of when their app was launched, although peer gameticks might fluctuate due to network latency.
int cugl::physics2::distrib::NetEventController::getNumPlayers | ( | ) | const |
Returns the number of players in the lobby.
This value is only valid after a connection. If there is no connection, it returns 1 (for this player).
|
inline |
Returns the physics synchronization controller.
If physics has not been enabled, this method returns nullptr.
|
inline |
Returns the room ID currently assigned to this controller.
This value is only valid after a connection. It will always return the empty string if there is no connection.
|
inline |
Returns the shortUID assigned by the host.
This value is only valid after connection. If the shortUID is 0, the controller did not receive a ID from the host yet. An assigned shortUID is required for physics synchronization, and is always non-zero.
|
inline |
Returns the current status of the controller.
|
inlineprotected |
bool cugl::physics2::distrib::NetEventController::init | ( | const std::shared_ptr< AssetManager > & | assets | ) |
Initializes the controller for the given asset manager.
This method requires the asset manager to have a JSON value with key "server". The JSON value should match the structure required by cugl::netcode::NetcodeConfig
.
assets | The asset manager |
|
inline |
Returns whether this device is host.
This value is only valid after a connection. It will always return false if there is no connection.
bool cugl::physics2::distrib::NetEventController::isInAvailable | ( | ) |
Returns true if there are remaining custom inbound events.
Thhe events in this queue is to be polled and processed by outside classes. Inbound events are preserved acrossupdates, and only cleared by popInEvent
.
bool cugl::physics2::distrib::NetEventController::markReady | ( | ) |
Marks the client as ready for game start.
This method is only valid after receiving a shortUID from the host.
std::shared_ptr< NetEvent > cugl::physics2::distrib::NetEventController::popInEvent | ( | ) |
Returns the next custom inbound event and removes it from the queue.
This method requires there to be remaining inbound events. If there are none, it returns nullptr.
|
protected |
Processes a GameStateEvent.
This method updates the controller status based on the event received.
e | The received event |
|
protected |
Processes all received packets received during the last update.
This method unwraps byte vectors into NetEvents and calls processReceivedEvent()
.
|
protected |
Processes all events received during the last update.
This method either processes events internally if it is a built-in event and adds them to the inbound event queue otherwise.
e | The received event |
void cugl::physics2::distrib::NetEventController::pushOutEvent | ( | const std::shared_ptr< NetEvent > & | e | ) |
Queues an outbound event to be sent to peers.
Queued events are sent when updateNet
is called. and cleared after sending.
e | The event to send |
|
protected |
Broadcasts all queued outbound events.
void cugl::physics2::distrib::NetEventController::startGame | ( | ) |
Starts the handshake process for starting a game.
Once the handshake is finished, the controller changes status to Status#INGAME
. It also starts sending synchronization events if physics is enabled.
|
protected |
Unwraps the a byte vector data into a NetEvent.
The controller automatically detects the type of event, spawns a new empty instance of that event, and calls the event's NetEvent#deserialize
method. This method is only called on outbound events.
data | The message received |
source | The UUID of the sender |
void cugl::physics2::distrib::NetEventController::updateNet | ( | ) |
Updates the network controller.
This method pushes out all queued events and processes all incoming events.
|
protected |
Wraps a NetEvent into a byte vector.
The controller calls the event's NetEvent#serialize()
method and packs the event into byte data. This method is only on inbound events.
e | The event to wrap |
|
protected |
The network configuration
|
protected |
Map from attached NetEvent types to uniform event type id
|
protected |
Queue for all received custom events. Preserved across updates.
|
protected |
Whether this device is host
|
protected |
The network connection
|
protected |
Vector of NetEvents instances for constructing new events
|
protected |
The number of ready players during game start handshake (HOST ONLY).
|
protected |
Queue for all outbound events. Cleared every update
|
protected |
The physics synchronization controller
|
protected |
Whether physics is enabled.
|
protected |
Queue reserved for built-in events
|
protected |
The room id of the connected lobby.
|
protected |
Short user id assigned by the host during session
|
protected |
The App fixed-time stamp when the game starts
|
protected |
The network controller status