![]() |
xyginext
0.2.0
Second generation of xygine game creation framework
|
Creates a clientside host which can be used to create a peer connected to a NetHost server. More...
#include <NetClient.hpp>
Public Member Functions | |
NetClient (const NetClient &)=delete | |
NetClient (NetClient &&)=delete | |
NetClient & | operator= (const NetClient &)=delete |
NetClient & | operator= (NetClient &&)=delete |
template<typename T = EnetClientImpl> | |
bool | create (std::size_t maxChannels, std::size_t maxClients=1, sf::Uint32 incoming=0, sf::Uint32 outgoing=0) |
bool | connect (const std::string &address, sf::Uint16 port, sf::Uint32 timeout=5000) |
Attempts to connect to a server on the given IPv4 address or host name. More... | |
bool | connected () const |
Returns true if connected to a NetHost and is ready to poll for events else returns false. | |
void | disconnect () |
Closes any active connections. This is blocking for up to 3 seconds while waiting for graceful disconnection before forcefully closing the connection and causing a timeout on the remote server. | |
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 | sendPacket (std::uint8_t id, const T &data, NetFlag flags, sf::Uint8 channel=0) |
Sends a packet to the server 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 (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 connection if it is active, else does nothing. Use this for pre-serialised data. More... | |
const NetPeer & | getPeer () const |
Returns a reference to the client's peer. Peers are only valid when connected to a server. More... | |
std::uint32_t | getAddress () const |
std::uint16_t | getPort () const |
template<typename T > | |
bool | create (std::size_t maxChannels, std::size_t maxClients, sf::Uint32 incoming, sf::Uint32 outgoing) |
template<typename T > | |
void | sendPacket (std::uint8_t id, const T &data, NetFlag flags, sf::Uint8 channel) |
Creates a clientside host which can be used to create a peer connected to a NetHost server.
bool xy::NetClient::connect | ( | const std::string & | address, |
sf::Uint16 | port, | ||
sf::Uint32 | timeout = 5000 |
||
) |
Attempts to connect to a server on the given IPv4 address or host name.
address | Address or hostname to connect to. |
port | The port number on which this client will attempt to connect to the server. |
timeout | Number of milliseconds to wait before connection attempt times out. This function is blocking until the server either responds with a connection successful event, or the timeout is reached. The default timeout is 5 seconds, and should be greater than 0, which may falsely return true as the connection attempt will not wait for a response from the server. |
bool xy::NetClient::create | ( | std::size_t | maxChannels, |
std::size_t | maxClients = 1 , |
||
sf::Uint32 | incoming = 0 , |
||
sf::Uint32 | outgoing = 0 |
||
) |
brief Creates a client host ready for connection to a server. This must be called at least once before trying to use connect()
maxChannels | Maximum number of channels to create on the connection. This should match the number used when creating the NetHost instance to which this client will connect. |
maxClients | Number of client connections this host will allow to a server. This is usually 1 (default). |
incoming | Maximum incoming bandwidth allowed in bytes per second. |
outgoing | Maximum outgoing bandwidth allow in bytes per second. A value of 0 sets throttling to automatic (default). |
const NetPeer& xy::NetClient::getPeer | ( | ) | const |
Returns a reference to the client's peer. Peers are only valid when connected to a server.
bool xy::NetClient::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::NetClient::sendPacket | ( | std::uint8_t | id, |
const T & | data, | ||
NetFlag | flags, | ||
sf::Uint8 | channel = 0 |
||
) |
Sends a packet to the server 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.
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::NetClient::sendPacket | ( | 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 connection if it is active, else does nothing. 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. |