Simple++
Powerfull and lightning fast C++ Library
Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
Network::Connection Class Reference

Connection/Socket Class. More...

#include <Connection.h>

Inheritance diagram for Network::Connection:
Network::Address Network::AddrInfo

Public Member Functions

 Connection ()
 create a connection not initialized More...
 
 Connection (const Address &address)
 Create a connection already initialized. More...
 
 Connection (Connection &&connection)
 Move constructor. More...
 
 ~Connection ()
 destructor More...
 
Connectionoperator= (Connection &&socket)
 
const AddressgetAddress () const
 get the Address of this connection More...
 
void setAddess (const Address &address)
 set the Address of this connection (this will close the connection) More...
 
bool listen (int maxClients=100)
 Listen for clients /!\ The connection can only listen on one address/port at a time. More...
 
bool listen (const Address &address, int maxClients=100)
 Listen for clients on a selected Address /!\ The connection can only listen on one address/port at a time. More...
 
bool listen (const String &address, const String &service, SockType sockType=SockType::TCP, IpFamily ipFamily=IpFamily::Undefined, int maxClients=100)
 Listen for clients on a selected address/service. More...
 
bool listen (const String &address, unsigned short port, SockType sockType=SockType::TCP, IpFamily ipFamily=IpFamily::Undefined, int maxClients=100)
 Listen for clients on a selected address/port. More...
 
bool listen (unsigned short port, SockType sockType=SockType::TCP, IpFamily ipFamily=IpFamily::Undefined, int maxClients=100)
 Listen for clients on a selected port. More...
 
bool connect ()
 Connect to UDP or TCP server. More...
 
bool connect (const Address &address)
 Connect to a specified Address. More...
 
bool connect (const String &address, const String &service, SockType sockType=SockType::TCP, IpFamily ipFamily=IpFamily::Undefined)
 Connect to a specified address/service. More...
 
bool connect (const String &address, unsigned short port, SockType sockType=SockType::TCP, IpFamily ipFamily=IpFamily::Undefined)
 Connect to a specified address/port. More...
 
bool accept (Connection *clientSocket)
 Accept a new client (only when listening on TCP). This is a blocking method. More...
 
bool send (const char *buffer, int size)
 Send data to the connection (after a connect) More...
 
int receive (char *buffer, int maxSize)
 Wait for data from the connection (usually used for TCP transaction) More...
 
int receive (char *buffer, int maxSize, Address *addressFrom)
 Wait for data from the connection and retrieve the address of the sender. (usually used for UDP transaction) More...
 
bool send (char *buffer, int size, const Address &address)
 Send data to an address. More...
 
void close ()
 close the connection More...
 
SOCKET getSocket () const
 get the SOCKET (unrecommended) return SOCKET of this connection (0 if not connected) More...
 
bool isConnected () const
 Retrieve if the connection is connected or not. More...
 
- Public Member Functions inherited from Network::Address
 Address (SockType sockType=SockType::TCP, IpFamily ipFamily=IpFamily::Undefined)
 Constructor from a SockType and an IpFamily. More...
 
 Address (const String &address, const String &service, SockType sockType=SockType::TCP, IpFamily ipFamily=IpFamily::Undefined)
 Constructor from a IP, Service, SockType and IpFamily. More...
 
 Address (const String &address, const String &service, const AddrInfo &hints)
 Constructor from a IP, Service, SockType and IpFamily. More...
 
 Address (const AddrInfo &addrInfo)
 copy constructor to up cast More...
 
 Address (const Address &address)
 copy constructor More...
 
 Address (Address &&address)
 move constructor More...
 
 Address (const struct addrinfo &addrInfo)
 copy constructor from an old-school struct addrinfo More...
 
 ~Address ()
 destructor More...
 
Addressoperator= (const AddrInfo &addrInfo)
 copy operator from an AddrInfo More...
 
Addressoperator= (const Address &address)
 copy operator More...
 
Addressoperator= (Address &&address)
 move operator More...
 
unsigned short getPort ()
 get the port as unsigned short (this function use buffered values) More...
 
const StringgetIp ()
 get the IP as a String (this function use buffered values) More...
 

Static Public Member Functions

static SOCKET listenStatic (const AddrInfo &addrInfo, int maxClients=100)
 listen and return the Socket created More...
 
static SOCKET connectStatic (const AddrInfo &addrInfo)
 connect and return the Socket created More...
 

Protected Member Functions

 Connection (ctor)
 
- Protected Member Functions inherited from Network::Address
 Address (ctor)
 
void _update ()
 
- Protected Member Functions inherited from Network::AddrInfo
 AddrInfo (ctor)
 
 AddrInfo (SockType sockType=SockType::TCP, IpFamily ipFamily=IpFamily::Undefined)
 Create an AddrInfo from a SockType and an IpFamily. More...
 
 AddrInfo (const String &address, const String &service, SockType sockType=SockType::TCP, IpFamily ipFamily=IpFamily::Undefined)
 Constructor from a IP, Service, SockType and IpFamily. More...
 
 AddrInfo (const String &address, const String &service, const AddrInfo &hints)
 Constructor from a IP, Service, SockType and IpFamily. More...
 
 AddrInfo (const AddrInfo &addrInfo)
 Copy constructor. More...
 
 AddrInfo (AddrInfo &&addrInfo)
 move constructor More...
 
 AddrInfo (const AddrInfo &addrInfo, SockType sockType, IpFamily ipFamily, unsigned short port)
 Copy constructor with an another SockType, IpFamily and port. More...
 
 AddrInfo (const struct addrinfo &addrInfo)
 Copy constructor from an old-school struct addrinfo. More...
 
 ~AddrInfo ()
 destructor More...
 
const struct addrinfo * getAddrInfoStruct () const
 get the old-school struct addrinfo inside this type. More...
 
void setSockAddr (const struct sockaddr *sockAddr, size_t sockAddrLen)
 set the struct sockaddr contained inside this object More...
 
AddrInfooperator= (const AddrInfo &addrInfo)
 set the inside address More...
 
AddrInfooperator= (const struct addrinfo &addrInfo)
 copy operator from a old-school struct addrinfo More...
 
AddrInfooperator= (AddrInfo &&addrInfo)
 move operator More...
 
const struct sockaddr * getSockAddr () const
 get the struct sockaddr of this object More...
 
void newSockAddr (size_t newSize)
 set the struct sockaddr to a new one filled with 0 More...
 
void setPort (const struct sockaddr *sockAddr)
 set the sockaddr port from an another sockaddr More...
 
size_t getSockAddrLen () const
 get the length in bytes of the struct sockaddr More...
 
IpFamily getIpFamily (const std::string &ip)
 get the IP family More...
 
unsigned short getPort () const
 get the port More...
 
IpFamily getIpFamily () const
 get the IP family More...
 
const StringgetIpFamilyS () const
 get the IP�family as a String More...
 
int getProtocol () const
 get the Protocol More...
 
SockType getSockType () const
 get the SockType (UDP or TCP) More...
 
const StringgetSockTypeS () const
 get the SockType as a String More...
 
void setFlags (int flags)
 set flags More...
 
int getFlags () const
 get Flags More...
 
void addFlag (Flags flags)
 add a Flag to this object Flag to add More...
 
void setCanonName (const String &name)
 set the CanonName of this object More...
 
String getNameInfo () const
 getnameinfo() on the struct sockaddr inside this object More...
 
void setPort (unsigned short port)
 set the sockaddr port More...
 
void setIpFamily (IpFamily ipFamily)
 set the IP family of this addrinfo (this won't change the struct sockaddr and may corrupt this object) More...
 
void setProtocol (int protocol)
 set the Protocol of this (this won't change the struct sockaddr and may corrupt this object) More...
 
void setSockType (SockType sockType)
 set the SockType (TCP or UDP) (this won't change the struct sockaddr and may corrupt this object) More...
 

Additional Inherited Members

- Protected Types inherited from Network::AddrInfo
enum  ctor { null }
 
- Static Protected Member Functions inherited from Network::AddrInfo
static String getNameInfo (const struct sockaddr &sockAddr, size_t sockAddrLen)
 alias of getnameinfo() and cast the result to a String More...
 
static String getNameInfo (const struct addrinfo &addrInfo)
 alias of getnameinfo() of the struct sockaddr inside the struct addrinfo More...
 
static unsigned short getPort (const struct sockaddr &sockAddr)
 get the port a struct sockaddr as an unsigned short More...
 

Detailed Description

Connection/Socket Class.

Constructor & Destructor Documentation

Network::Connection::Connection ( )

create a connection not initialized

Network::Connection::Connection ( const Address address)

Create a connection already initialized.

Parameters
addressaddress as a String (ip or domain name)
serviceService or port as a string
sockTypeTCP or UDP
ipFamilyIPv4 or IPv6 or Undefined for testing the two Constructor from an address
addressAddress to asign to this connection
Network::Connection::Connection ( Connection &&  connection)

Move constructor.

Parameters
connectionObject to be moved from.
Network::Connection::~Connection ( )

destructor

Network::Connection::Connection ( ctor  )
protected

Member Function Documentation

bool Network::Connection::accept ( Connection clientSocket)

Accept a new client (only when listening on TCP). This is a blocking method.

Parameters
outclientSocket Pointer to a Connection object that will be filled when a client connect (the object has to be already allocated)
Returns
true when a new client has connected.
void Network::Connection::close ( )

close the connection

bool Network::Connection::connect ( )

Connect to UDP or TCP server.

Returns
true if success else false
bool Network::Connection::connect ( const Address address)

Connect to a specified Address.

Parameters
addrInfoAddress to connect to.
Returns
true if success else false
bool Network::Connection::connect ( const String address,
const String service,
SockType  sockType = SockType::TCP,
IpFamily  ipFamily = IpFamily::Undefined 
)

Connect to a specified address/service.

Parameters
addressaddress as a String (ip or domain name)
serviceService or port as a string
sockTypeTCP or UDP
ipFamilyIPv4 or IPv6 or Undefined for testing the two
Returns
true if success else false
bool Network::Connection::connect ( const String address,
unsigned short  port,
SockType  sockType = SockType::TCP,
IpFamily  ipFamily = IpFamily::Undefined 
)

Connect to a specified address/port.

Parameters
addressaddress as a String (ip or domain name)
portport
sockTypeTCP or UDP
ipFamilyIPv4 or IPv6 or Undefined for testing the two
Returns
true if success else false
SOCKET Network::Connection::connectStatic ( const AddrInfo addrInfo)
static

connect and return the Socket created

Parameters
addrInfoAddress to connect on.
Returns
SOCKET created
const Address & Network::Connection::getAddress ( ) const

get the Address of this connection

Returns
Address of this connection
SOCKET Network::Connection::getSocket ( ) const

get the SOCKET (unrecommended) return SOCKET of this connection (0 if not connected)

bool Network::Connection::isConnected ( ) const

Retrieve if the connection is connected or not.

Returns
if connected or not.
bool Network::Connection::listen ( int  maxClients = 100)

Listen for clients /!\ The connection can only listen on one address/port at a time.

Parameters
maxClientsMaximum number of clients accepted
Returns
true if success else false (Only for TCP)
bool Network::Connection::listen ( const Address address,
int  maxClients = 100 
)

Listen for clients on a selected Address /!\ The connection can only listen on one address/port at a time.

Parameters
addrInfoAddress to be binded
maxClientsMaximum number of clients accepted (Only for TCP)
Returns
true if success else false
bool Network::Connection::listen ( const String address,
const String service,
SockType  sockType = SockType::TCP,
IpFamily  ipFamily = IpFamily::Undefined,
int  maxClients = 100 
)

Listen for clients on a selected address/service.

Parameters
addressaddress as a String (ip or domain name)
serviceService or port as a string
sockTypeTCP or UDP
ipFamilyIPv4 or IPv6 or Undefined for testing the two
maxClientsMaximum number of clients accepted (Only for TCP)
Returns
true if success else false
bool Network::Connection::listen ( const String address,
unsigned short  port,
SockType  sockType = SockType::TCP,
IpFamily  ipFamily = IpFamily::Undefined,
int  maxClients = 100 
)

Listen for clients on a selected address/port.

Parameters
addressaddress as a String (ip or domain name)
portport
sockTypeTCP or UDP
ipFamilyIPv4 or IPv6 or Undefined for testing the two
maxClientsMaximum number of clients accepted (Only for TCP)
Returns
true if success else false
bool Network::Connection::listen ( unsigned short  port,
SockType  sockType = SockType::TCP,
IpFamily  ipFamily = IpFamily::Undefined,
int  maxClients = 100 
)

Listen for clients on a selected port.

Parameters
portport
sockTypeTCP or UDP
ipFamilyIPv4 or IPv6 or Undefined for testing the two
maxClientsMaximum number of clients accepted (Only for TCP)
Returns
true if success else false
SOCKET Network::Connection::listenStatic ( const AddrInfo addrInfo,
int  maxClients = 100 
)
static

listen and return the Socket created

Parameters
addrInfoAddress to listen on.
maxClientsMaximum number of clients
Returns
SOCKET created
Connection & Network::Connection::operator= ( Connection &&  socket)

brief move operator

Parameters
connectionObject to be moved from.
Returns
reference to this
int Network::Connection::receive ( char *  buffer,
int  maxSize 
)

Wait for data from the connection (usually used for TCP transaction)

Parameters
bufferData buffer where to store the read bytes
maxSizeMaximum size of the buffer
Returns
number of bytes read
int Network::Connection::receive ( char *  buffer,
int  maxSize,
Address addressFrom 
)

Wait for data from the connection and retrieve the address of the sender. (usually used for UDP transaction)

Parameters
bufferData buffer where to store the read bytes
maxSizeMaximum size of the buffer
outaddressFrom Address Object to be filled with the address of sender. (the object has to be already allocated)
Returns
number of bytes read
bool Network::Connection::send ( const char *  buffer,
int  size 
)

Send data to the connection (after a connect)

Parameters
bufferData buffer to be sent
sizesize of the data buffer
Returns
true if success else false
bool Network::Connection::send ( char *  buffer,
int  size,
const Address address 
)

Send data to an address.

Parameters
bufferData buffer to be sent
sizesize of the data buffer
addressAddress to send to
Returns
true if success else false
void Network::Connection::setAddess ( const Address address)

set the Address of this connection (this will close the connection)

Parameters
addressNew Address of this connection.

The documentation for this class was generated from the following files: