xyginext  0.2.0
Second generation of xygine game creation framework
Public Member Functions | List of all members
xy::NetHost Class Referencefinal

Creates a network host. Network hosts, or servers, can have multiple clients connected to them, via a reliable UDP stream. More...

#include <NetHost.hpp>

Public Member Functions

 NetHost (const NetHost &)=delete
 
NetHostoperator= (const NetHost &)=delete
 
 NetHost (NetHost &&)=delete
 
NetHostoperator= (NetHost &&)=delete
 
template<typename T = EnetHostImpl>
bool start (const std::string &address, sf::Uint16 port, std::size_t maxClient, std::size_t maxChannels, sf::Uint32 incoming=0, sf::Uint32 outgoing=0)
 Creates a host listening on the given address and port. More...
 
void stop ()
 Stops the host, if it is running.
 
bool pollEvent (NetEvent &)
 Polls the connection for events. This must be called at least once per frame to make sure all received packets are parsed and pending data is sent. Any data received is placed in the given event object. Make sure this happens on both ends of the connection (NetHost and NetClient) More...
 
template<typename T >
void broadcastPacket (std::uint8_t id, const T &data, NetFlag flags, sf::Uint8 channel=0)
 Broadcasts a packet to all connected clients. Note that all packets are queued until the next time pollEvent() is called. More...
 
void broadcastPacket (std::uint8_t id, const void *data, std::size_t size, NetFlag flags, sf::Uint8 channel=0)
 Broadcasts the given stream of bytes to all connected clients Use this for pre-serialised data. More...
 
template<typename T >
void sendPacket (const NetPeer &peer, std::uint8_t id, const T &data, NetFlag flags, sf::Uint8 channel=0)
 Sends a packet to the given peer if a connection is established, else does nothing. NOTE: Packets are actually queued and not sent over the connection until the next time pollEvent() is called. More...
 
void sendPacket (const NetPeer &peer, std::uint8_t id, const void *data, std::size_t size, NetFlag flags, sf::Uint8 channel=0)
 Sends the given array of bytes out over the given peer if it is active, else does nothing. Use this for pre-serialised data. More...
 
std::size_t getConnectedPeerCount () const
 Returns the number of currently connected peers.
 
std::uint32_t getAddress () const
 
std::uint16_t getPort () const
 
template<typename T >
bool start (const std::string &address, sf::Uint16 port, std::size_t maxClients, std::size_t maxChannels, sf::Uint32 incoming, sf::Uint32 outgoing)
 
template<typename T >
void broadcastPacket (std::uint8_t id, const T &data, NetFlag flags, sf::Uint8 channel)
 
template<typename T >
void sendPacket (const NetPeer &peer, std::uint8_t id, const T &data, NetFlag flags, sf::Uint8 channel)
 

Detailed Description

Creates a network host. Network hosts, or servers, can have multiple clients connected to them, via a reliable UDP stream.

Member Function Documentation

◆ broadcastPacket() [1/2]

template<typename T >
void xy::NetHost::broadcastPacket ( std::uint8_t  id,
const T &  data,
NetFlag  flags,
sf::Uint8  channel = 0 
)

Broadcasts a packet to all connected clients. Note that all packets are queued until the next time pollEvent() is called.

Parameters
idunique ID for this packet
dataStruct of simple data to send. Structs are serialised and sent out as an array of bytes - thus members such as pointers are effectively useless, as the pointers themselves will be sent, and not the data pointed to.
flagsUsed to denote reliability of packet sending
See also
NetFlag
Parameters
channelStream channel on which to send the data. Lower number channels have higher priority, with 0 being highest.

◆ broadcastPacket() [2/2]

void xy::NetHost::broadcastPacket ( std::uint8_t  id,
const void *  data,
std::size_t  size,
NetFlag  flags,
sf::Uint8  channel = 0 
)

Broadcasts the given stream of bytes to all connected clients Use this for pre-serialised data.

Parameters
idUnique ID for this packet
dataPointer to the data to send
sizeSize of the data, in bytes
flagsUsed to indicated the requested reliability of packet sent
See also
NetFlag
Parameters
channelStream channel over which to send the data. Lower number channels have higher priority, with 0 being highest.

◆ pollEvent()

bool xy::NetHost::pollEvent ( NetEvent )

Polls the connection for events. This must be called at least once per frame to make sure all received packets are parsed and pending data is sent. Any data received is placed in the given event object. Make sure this happens on both ends of the connection (NetHost and NetClient)

  • this is the most common reason communication fails.
    Returns
    true if there is incoming data in the buffer, else false

◆ sendPacket() [1/2]

template<typename T >
void xy::NetHost::sendPacket ( const NetPeer peer,
std::uint8_t  id,
const T &  data,
NetFlag  flags,
sf::Uint8  channel = 0 
)

Sends a packet to the given peer if a connection is established, else does nothing. NOTE: Packets are actually queued and not sent over the connection until the next time pollEvent() is called.

Parameters
peerThe peer over which to send the packet.
idunique ID for this packet
dataStruct of simple data to send. Structs are serialised and sent out as an array of bytes - thus members such as pointers are effectively useless, as the pointers themselves will be sent, and not the data pointed to.
flagsUsed to denote reliability of packet sending
See also
NetFlag
Parameters
channelStream channel on which to send the data. Lower number channels have higher priority, with 0 being highest.

◆ sendPacket() [2/2]

void xy::NetHost::sendPacket ( const NetPeer peer,
std::uint8_t  id,
const void *  data,
std::size_t  size,
NetFlag  flags,
sf::Uint8  channel = 0 
)

Sends the given array of bytes out over the given peer if it is active, else does nothing. Use this for pre-serialised data.

Parameters
peerThe peer over which to send the packet.
idUnique ID for this packet
dataPointer to the data to send
sizeSize of the data, in bytes
flagsUsed to indicated the requested reliability of packet sent
See also
NetFlag
Parameters
channelStream channel over which to send the data. Lower number channels have higher priority, with 0 being highest.

◆ start()

template<typename T = EnetHostImpl>
bool xy::NetHost::start ( const std::string &  address,
sf::Uint16  port,
std::size_t  maxClient,
std::size_t  maxChannels,
sf::Uint32  incoming = 0,
sf::Uint32  outgoing = 0 
)

Creates a host listening on the given address and port.

Parameters
addressString representing an IPv4 address in the form "x.x.x.x". This may be left empty to listen on any available address.
portAn unsigned short representing the port on which to listen
maxClientMaximum number of connections to allow to the host
maxChannelsMaximum number of channels allowed (indexed from 0)
incomingLimit the incoming bandwidth in bytes per second. 0 is no limit (default)
outgoingLimit the outgoing bandwidth in bytes per second. 0 is no limit (default)
Returns
true if created successfully, else false. NOTE Although this function is a template it is generally not required to pass a type here, unless specifying a custom network implementation. This needs to be called at least once before attempting to use any of the other functions in this class