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

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
 
NetClientoperator= (const NetClient &)=delete
 
NetClientoperator= (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 NetPeergetPeer () 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)
 

Detailed Description

Creates a clientside host which can be used to create a peer connected to a NetHost server.

Member Function Documentation

◆ connect()

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.

Parameters
addressAddress or hostname to connect to.
portThe port number on which this client will attempt to connect to the server.
timeoutNumber 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.
Returns
true on success or false if the attempt timed out.

◆ create()

template<typename T = EnetClientImpl>
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()

Parameters
maxChannelsMaximum 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.
maxClientsNumber of client connections this host will allow to a server. This is usually 1 (default).
incomingMaximum incoming bandwidth allowed in bytes per second.
outgoingMaximum outgoing bandwidth allow in bytes per second. A value of 0 sets throttling to automatic (default).
Returns
true if successful or false if something went wrong Calling this 2 or more times with different parameters will attempt to recreate the host. NOTE: this is a templated function which defaults to the ENet library implementation. Generally this type does not need to be specified, and is useful only when providing a custom netowrking implmentation.

◆ getPeer()

const NetPeer& xy::NetClient::getPeer ( ) const

Returns a reference to the client's peer. Peers are only valid when connected to a server.

See also
NetPeer

◆ pollEvent()

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)

  • 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::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.

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.

◆ sendPacket() [2/2]

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.

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.