Networking
classes
network
methods
connect
Attempt to establish a connection with a server, only works when set up as a client.
uint64 network::connect(string hostname, uint16 port);
Arguments:
- string hostname: the hostname/IP address to connect to.
- uint16 port: the port to use.
Returns:
uint64: the peer ID of the connection, or 0 on error.
destroy
Destroys the network object, freeing all its resources, active connections, etc.
void network::destroy();
disconnect_peer
Tell a peer to disconnect and completely disregard its message queue. This means that the peer will be told to disconnect without sending any of its queued packets (if any).
bool network::disconnect_peer(uint peer_id);
Arguments:
- uint peer_id: the ID of the peer to disconnect.
Returns:
bool: true on success, false otherwise.
disconnect_peer_forcefully
Forcefully disconnect a peer. Unlike network::disconnect_peer()
, this function doesn't send any sort of notification of the disconnection to the remote peer, instead it closes the connection immediately.
bool network::disconnect_peer_forcefully(uint peer_id);
Arguments:
- uint peer_id: the ID of the peer to disconnect.
Returns:
bool: true on success, false otherwise.
disconnect_peer_softly
Send a disconnect packet for a peer after sending any remaining packets in the queue and notifying the peer.
bool network::disconnect_peer_softly(uint peer_id);
Arguments:
- uint peer_id: the ID of the peer to disconnect.
Returns:
bool: true on success, false otherwise.
get_peer_address
Returns the IP address of a particular peer.
string network::get_peer_address(uint peer_id);
Arguments:
- uint peer_id: the ID of the peer to check.
Returns:
string: the IP address of the specified peer.
get_peer_list
Return a list of all peer ID's currently connected to the server.
uint[]@ network::get_peer_list();
Returns:
uint[]@: a handle to an array containing the ID of every peer currently connected to the server.
request
This is the function you'll probably be calling the most when you're dealing with the network object in NVGT. It checks if an event has occurred since the last time it checked. If it has, it returns you a network_event handle with info about it.
network_event@ network::request(uint timeout = 0);
Arguments:
- uint timeout = 0: an optional timeout on your packet receiving (see remarks for more information).
Returns:
network_event@: a handle to a network_event
object containing info about the last received event.
Remarks:
The timeout parameter is in milliseconds, and it determines how long Enet will wait for new packets before returning control back to the calling application. However, if it receives a packet within the timeout period, it will return and you'll be handed the packet straight away.
send
Attempt to send a packet over the network.
bool network::send(uint peer_id, string message, uint8 channel, bool reliable = true);
Arguments:
- uint peer_id: the ID of the peer to send to (specify 1 to send to the server from a client).
- string message: the message to send.
- uint8 channel: the channel to send the message on (see the main networking documentation for more details).
- bool reliable = true: whether or not the packet should be sent reliably or not (see the main networking documentation for more details).
Returns:
bool: true if the packet was successfully sent, false otherwise.
send_reliable
Attempt to send a packet over the network reliably.
bool network::send_reliable(uint peer_id, string message, uint8 channel);
Arguments:
- uint peer_id: the ID of the peer to send to (specify 1 to send to the server from a client).
- string message: the message to send.
- uint8 channel: the channel to send the message on (see the main networking documentation for more details).
Returns:
bool: true if the packet was successfully sent, false otherwise.
send_unreliable
Attempt to send a packet over the network unreliably.
bool network::send_unreliable(uint peer_id, string message, uint8 channel);
Arguments:
- uint peer_id: the ID of the peer to send to (specify 1 to send to the server from a client).
- string message: the message to send.
- uint8 channel: the channel to send the message on (see the main networking documentation for more details).
Returns:
bool: true if the packet was successfully sent, false otherwise.
set_bandwidth_limits
Set the incoming and outgoing bandwidth limits of the server (in bytes-per-second).
void network::set_bandwidth_limits(uint incoming, uint outgoing);
Arguments:
- uint incoming: the maximum number of allowed incoming bytes per second.
- uint outgoing: the maximum number of allowed outgoing bytes per second.
setup_client
Sets up the network object as a client.
bool network::setup_client(uint8 max_channels, uint16 max_peers);
Arguments:
- uint8 max_channels: the maximum number of channels used on the connection (up to 255).
- uint16 max_peers: the maximum number of peers allowed by the connection (maximum is 65535).
Returns:
bool: true if the client was successfully set up, false otherwise.
setup_local_server
Sets up the network object as a local server on localhost.
bool network::setup_local_server(uint16 bind_port, uint8 max_channels, uint16 max_peers);
Arguments:
- uint16 bind_port: the port to bind the server to.
- uint8 max_channels: the maximum number of channels used on the connection (up to 255).
- uint16 max_peers: the maximum number of peers allowed by the connection (maximum is 65535).
Returns:
bool: true if the server was successfully set up, false otherwise.
setup_server
Sets up the network object as a server.
bool network::setup_server(uint16 bind_port, uint8 max_channels, uint16 max_peers);
Arguments:
- uint16 bind_port: the port to bind the server to.
- uint8 max_channels: the maximum number of channels used on the connection (up to 255).
- uint16 max_peers: the maximum number of peers allowed by the connection (maximum is 65535).
Returns:
bool: true if the server was successfully set up, false otherwise.
properties
active
Determine if the network object is active (e.g. a setup_* method has successfully been called on it).
bool network::active;
bytes_received
The number of bytes this network object has received since being set up.
uint network::bytes_received;
bytes_sent
The number of bytes this network object has sent since being set up.
uint network::bytes_sent;
connected_peers
The number of peers currently connected to the server.
uint network::connected_peers;
network_event
This class represents an event received with the network object's request()
method. It contains information such as the the ID of the peer that sent the event, the message the event was sent on, and the actual packet data itself.
methods
opAssign
This class implements the opAssign()
operator overload, meaning it can be assigned with the "=" operator to another network_event.
network_event@ opAssign(network_event@ e);
properties
channel
The channel this event was sent on. See the main networking documentation for more information.
uint network_event::channel;
message
The data associated with this event (AKA the packet).
string network_event::message;
peer_id
The peer ID of the connection this event came from. See the main networking documentation for more information.
uint network_event::peer_id;
type
The type of the network event (see event types for more information).
int network_event::type;
Constants
Event Types
This is a list of all the constants supported as event types by NVGT's networking layer.
- event_type_none: no event.
- event_type_connect: a new user wants to connect.
- event_type_disconnect: a user wants to disconnect.
- event_type_receive: a user sent a packet.