CUGL 3.0
Cornell University Game Library
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
cugl::physics2::distrib::NetEventController Class Reference

#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< NetPhysicsControllergetPhysController ()
 
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< NetEventpopInEvent ()
 
void pushOutEvent (const std::shared_ptr< NetEvent > &e)
 
void updateNet ()
 

Static Public Member Functions

static std::shared_ptr< NetEventControlleralloc (const std::shared_ptr< AssetManager > &assets)
 

Protected Member Functions

std::shared_ptr< NetEventunwrap (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
 

Detailed Description

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.

Member Enumeration Documentation

◆ Status

This enum represents the current session status

Enumerator
IDLE 

No connection requested

CONNECTING 

Connecting to lobby (negotating)

CONNECTED 

Connected to lobby

HANDSHAKE 

Handshaking for game start

READY 

Ready for game start

INGAME 

Game is in progress

NETERROR 

Error in connection

Constructor & Destructor Documentation

◆ NetEventController()

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.

◆ ~NetEventController()

cugl::physics2::distrib::NetEventController::~NetEventController ( )
inline

Deletes this network controller and all of its resources.

Member Function Documentation

◆ alloc()

static std::shared_ptr< NetEventController > cugl::physics2::distrib::NetEventController::alloc ( const std::shared_ptr< AssetManager > &  assets)
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.

Parameters
assetsThe asset manager
Returns
a newly allocated controller for the given asset manager.

◆ attachEventType()

template<typename T >
void cugl::physics2::distrib::NetEventController::attachEventType ( )
inline

Attaches a new NetEvent type to the controller.

This method allows the controller the receive and send custom NetEvent classes. The template type T must be a subclass of NetEvent.

Template Parameters
TThe event type to be attached

◆ checkConnection()

bool cugl::physics2::distrib::NetEventController::checkConnection ( )
protected

Returns true if the connection is still active after a status check

This method updates the controller status according to the protocol.

Returns
true if the connection is still active after a status check

◆ connectAsClient()

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.

Parameters
roomIDThe room to connect to

◆ connectAsHost()

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.

◆ disablePhysics()

void cugl::physics2::distrib::NetEventController::disablePhysics ( )

Disables physics synchronization.

◆ disconnect()

void cugl::physics2::distrib::NetEventController::disconnect ( )

Disconnects from the current lobby.

◆ dispose()

void cugl::physics2::distrib::NetEventController::dispose ( )

Disposes the network controller, releasing all resources.

This controller can be safely reinitialized

◆ enablePhysics() [1/2]

void cugl::physics2::distrib::NetEventController::enablePhysics ( std::shared_ptr< NetWorld > &  world)
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.

Parameters
worldThe physics world to be synchronized.

◆ enablePhysics() [2/2]

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.

Parameters
worldThe physics world to be synchronized.
linkFuncFunction that links a scene node to an obstacle.

◆ getGameTick()

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.

Returns
the discrete timestamp since the game started.

◆ getNumPlayers()

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).

Returns
the number of players in the lobby.

◆ getPhysController()

std::shared_ptr< NetPhysicsController > cugl::physics2::distrib::NetEventController::getPhysController ( )
inline

Returns the physics synchronization controller.

If physics has not been enabled, this method returns nullptr.

Returns
the physics synchronization controller.

◆ getRoomID()

std::string cugl::physics2::distrib::NetEventController::getRoomID ( ) const
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.

Returns
the room ID currently assigned to this controller.

◆ getShortUID()

Uint32 cugl::physics2::distrib::NetEventController::getShortUID ( ) const
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.

Returns
the shortUID assigned by the host.

◆ getStatus()

Status cugl::physics2::distrib::NetEventController::getStatus ( ) const
inline

Returns the current status of the controller.

Returns
the current status of the controller.

◆ getType()

Uint8 cugl::physics2::distrib::NetEventController::getType ( const NetEvent e)
inlineprotected

Returns the type id of a NetEvent.

Returns
the type id of a NetEvent.

◆ init()

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.

Parameters
assetsThe asset manager
Returns
true if the network controller was successfully initialized

◆ isHost()

bool cugl::physics2::distrib::NetEventController::isHost ( ) const
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.

Returns
whether this device is host.

◆ isInAvailable()

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.

Returns
true if there are remaining custom inbound events.

◆ markReady()

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.

Returns
true if the communiction was successful.

◆ popInEvent()

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.

Returns
the next custom inbound event

◆ processGameStateEvent()

void cugl::physics2::distrib::NetEventController::processGameStateEvent ( const std::shared_ptr< GameStateEvent > &  e)
protected

Processes a GameStateEvent.

This method updates the controller status based on the event received.

Parameters
eThe received event

◆ processReceivedData()

void cugl::physics2::distrib::NetEventController::processReceivedData ( )
protected

Processes all received packets received during the last update.

This method unwraps byte vectors into NetEvents and calls processReceivedEvent().

◆ processReceivedEvent()

void cugl::physics2::distrib::NetEventController::processReceivedEvent ( const std::shared_ptr< NetEvent > &  e)
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.

Parameters
eThe received event

◆ pushOutEvent()

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.

Parameters
eThe event to send

◆ sendQueuedOutData()

void cugl::physics2::distrib::NetEventController::sendQueuedOutData ( )
protected

Broadcasts all queued outbound events.

◆ startGame()

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.

◆ unwrap()

std::shared_ptr< NetEvent > cugl::physics2::distrib::NetEventController::unwrap ( const std::vector< std::byte > &  data,
std::string  source 
)
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.

Parameters
dataThe message received
sourceThe UUID of the sender

◆ updateNet()

void cugl::physics2::distrib::NetEventController::updateNet ( )

Updates the network controller.

This method pushes out all queued events and processes all incoming events.

◆ wrap()

const std::vector< std::byte > cugl::physics2::distrib::NetEventController::wrap ( const std::shared_ptr< NetEvent > &  e)
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.

Parameters
eThe event to wrap

Member Data Documentation

◆ _config

cugl::netcode::NetcodeConfig cugl::physics2::distrib::NetEventController::_config
protected

The network configuration

◆ _eventTypeMap

std::unordered_map<std::type_index, Uint8> cugl::physics2::distrib::NetEventController::_eventTypeMap
protected

Map from attached NetEvent types to uniform event type id

◆ _inEventQueue

std::queue<std::shared_ptr<NetEvent> > cugl::physics2::distrib::NetEventController::_inEventQueue
protected

Queue for all received custom events. Preserved across updates.

◆ _isHost

bool cugl::physics2::distrib::NetEventController::_isHost
protected

Whether this device is host

◆ _network

std::shared_ptr<cugl::netcode::NetcodeConnection> cugl::physics2::distrib::NetEventController::_network
protected

The network connection

◆ _newEventVector

std::vector<std::shared_ptr<NetEvent> > cugl::physics2::distrib::NetEventController::_newEventVector
protected

Vector of NetEvents instances for constructing new events

◆ _numReady

Uint8 cugl::physics2::distrib::NetEventController::_numReady
protected

The number of ready players during game start handshake (HOST ONLY).

◆ _outEventQueue

std::vector<std::shared_ptr<NetEvent> > cugl::physics2::distrib::NetEventController::_outEventQueue
protected

Queue for all outbound events. Cleared every update

◆ _physController

std::shared_ptr<NetPhysicsController> cugl::physics2::distrib::NetEventController::_physController
protected

The physics synchronization controller

◆ _physEnabled

bool cugl::physics2::distrib::NetEventController::_physEnabled
protected

Whether physics is enabled.

◆ _reservedInEventQueue

std::queue<std::shared_ptr<NetEvent> > cugl::physics2::distrib::NetEventController::_reservedInEventQueue
protected

Queue reserved for built-in events

◆ _roomid

std::string cugl::physics2::distrib::NetEventController::_roomid
protected

The room id of the connected lobby.

◆ _shortUID

Uint32 cugl::physics2::distrib::NetEventController::_shortUID
protected

Short user id assigned by the host during session

◆ _startGameTimeStamp

Uint64 cugl::physics2::distrib::NetEventController::_startGameTimeStamp
protected

The App fixed-time stamp when the game starts

◆ _status

Status cugl::physics2::distrib::NetEventController::_status
protected

The network controller status


The documentation for this class was generated from the following file: