![]() |
xyginext
0.2.0
Second generation of xygine game creation framework
|
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 | |
NetHost & | operator= (const NetHost &)=delete |
NetHost (NetHost &&)=delete | |
NetHost & | operator= (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) |
Creates a network host. Network hosts, or servers, can have multiple clients connected to them, via a reliable UDP stream.
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.
id | unique ID for this packet |
data | Struct 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. |
flags | Used to denote reliability of packet sending |
channel | Stream channel on which to send the data. Lower number channels have higher priority, with 0 being highest. |
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.
id | Unique ID for this packet |
data | Pointer to the data to send |
size | Size of the data, in bytes |
flags | Used to indicated the requested reliability of packet sent |
channel | Stream channel over which to send the data. Lower number channels have higher priority, with 0 being highest. |
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)
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.
peer | The peer over which to send the packet. |
id | unique ID for this packet |
data | Struct 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. |
flags | Used to denote reliability of packet sending |
channel | Stream channel on which to send the data. Lower number channels have higher priority, with 0 being highest. |
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.
peer | The peer over which to send the packet. |
id | Unique ID for this packet |
data | Pointer to the data to send |
size | Size of the data, in bytes |
flags | Used to indicated the requested reliability of packet sent |
channel | Stream channel over which to send the data. Lower number channels have higher priority, with 0 being highest. |
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.
address | String representing an IPv4 address in the form "x.x.x.x". This may be left empty to listen on any available address. |
port | An unsigned short representing the port on which to listen |
maxClient | Maximum number of connections to allow to the host |
maxChannels | Maximum number of channels allowed (indexed from 0) |
incoming | Limit the incoming bandwidth in bytes per second. 0 is no limit (default) |
outgoing | Limit the outgoing bandwidth in bytes per second. 0 is no limit (default) |