AES-DH Implementation
Loading...
Searching...
No Matches
network Namespace Reference

The namespace for communication along a socket. More...

Classes

struct  packet
 

Enumerations

enum  meta {
  ERROR , EMPTY , DATA , HMAC ,
  NONCE , IV , FINAL , MESSAGE ,
  ACK , REFUSED , REEXCHANGE
}
 

Functions

int send_packet (const packet &p, const size_t &timeout=5)
 Send a packet.
 
packet recv_packet (const size_t &timeout=5)
 Receive a packet.
 
template<typename T >
int send_value (const T &value, const network::meta &type=DATA, const size_t &timeout=5)
 Send a value.
 
template<typename T >
const T recv_value (const size_t &timeout=5)
 Receive a value.
 
int send_string (const std::string &message, const network::meta &type=DATA, const size_t &timeout=5)
 Send a string of any size.
 
std::string recv_string (const size_t &timeout=5)
 Receive a string.
 
void get_client (const int &port)
 Listen on the socket for a client to initiate a connection.
 
void get_server (const size_t &port, const char address[]="127.0.0.1")
 Try and connect to the server.
 

Variables

int sock = -1
 The FD sock for when the program is in Listen mode.
 
int connection = -1
 the FD sock that peers communicate over.
 

Detailed Description

The namespace for communication along a socket.

Function Documentation

◆ get_client()

void network::get_client ( const int & port)

Listen on the socket for a client to initiate a connection.

Parameters
portThe port to listen on.
Warning
This function will initialize network::out; be sure to close it before closing the application.
This function will initialize network::connection; be sure to close it before closing the application.
You are responsible for closing sockets.

◆ get_server()

void network::get_server ( const size_t & port,
const char address[] = "127.0.0.1" )

Try and connect to the server.

Parameters
portThe port to connect to
addressThe address of the server
Warning
This function will initialize network::out; be sure to close it before closing the application.
This function will initialize network::connection; be sure to close it before closing the application.
You are responsible for closing sockets.

◆ recv_packet()

packet network::recv_packet ( const size_t & timeout = 5)

Receive a packet.

Parameters
timeoutHow long to wait (seconds) before throwing an error.
Returns
The packet.
Remarks
If an error occured, the packet will have type network::meta::ERROR. Causes of this include the peer hanging up the connection or a timeout.

◆ recv_string()

std::string network::recv_string ( const size_t & timeout = 5)
inline

Receive a string.

Parameters
timeoutA listening timeout before aborting.
Returns
: The string.

◆ recv_value()

template<typename T >
const T network::recv_value ( const size_t & timeout = 5)
inline

Receive a value.

Template Parameters
TThe datatype of the value.
Parameters
timeoutA listening timeout before aborting.
Returns
The send value.
Exceptions
std::runtime_errorif an ERROR packet is sent.

◆ send_packet()

int network::send_packet ( const packet & p,
const size_t & timeout = 5 )

Send a packet.

Parameters
pThe packet to send.
timeoutHow long to wait (seconds) before throwing an error.
Returns
The return code of send.

◆ send_string()

int network::send_string ( const std::string & message,
const network::meta & type = DATA,
const size_t & timeout = 5 )
inline

Send a string of any size.

Parameters
messageThe string to send.
typeWhether you want to tag this data with something other than DATA.
timeoutA listening timeout before aborting.
Returns
0 if the string was sent succesfully. -1 Otherwise.
Remarks
This function simply breaks the string into packet sized blocks, and sends them across one at a time. The last package will be sent with a FINAL type, which will terminate the exchange.
A length package will be sent to trim excessive 0's.

◆ send_value()

template<typename T >
int network::send_value ( const T & value,
const network::meta & type = DATA,
const size_t & timeout = 5 )
inline

Send a value.

Template Parameters
TThe datatype of the value.
Parameters
valueThe value to send.
typeWhether to tag the data with something.
timeoutA listening timeout before aborting.
Returns
0 if success, -1 if error.

Variable Documentation

◆ sock

int network::sock = -1

The FD sock for when the program is in Listen mode.

Remarks
This is not the FD that peers communicate over.
This is only used for the initial connection.