netLink C++ 11
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Friends | List of all members
netLink::Socket Class Reference

Socket and stream buffer. More...

#include <Socket.h>

Inheritance diagram for netLink::Socket:
netLink::MsgPackSocket

Public Types

enum  IPVersion { IPv4 , IPv6 , ANY }
 Defines the version of IP. More...
 
enum  Type {
  NONE , TCP_CLIENT , TCP_SERVER , TCP_SERVERS_CLIENT ,
  UDP_PEER
}
 Defines the nature of a socket. More...
 
enum  Status {
  NOT_CONNECTED , CONNECTING , LISTENING , READY ,
  BUSY
}
 Defines the send status of a socket. More...
 

Public Member Functions

void initAsTcpClient (const std::string &hostRemote, unsigned portRemote, bool waitUntilConnected=false)
 
void initAsTcpServer (const std::string &hostLocal, unsigned portLocal, unsigned listenQueue=16)
 
void initAsUdpPeer (const std::string &hostLocal, unsigned portLocal)
 
IPVersion getIPVersion () const
 Returns the IPVersion of the socket.
 
Type getType () const
 Returns the SocketType of the socket.
 
Status getStatus () const
 Returns the SocketStatus of the socket.
 
std::streamsize showmanyc ()
 
std::streamsize receive (char_type *buffer, std::streamsize size)
 
std::streamsize send (const char_type *buffer, std::streamsize size)
 
std::streamsize redirect (const std::vector< std::shared_ptr< Socket > > &destinations)
 
std::streamsize getInputBufferSize ()
 Get the size of the input intermediate buffer in bytes.
 
std::streamsize getOutputBufferSize ()
 Get the size of the output intermediate buffer in bytes.
 
void setInputBufferSize (std::streamsize size)
 Set the size of the input intermediate buffer in bytes (unread contents are lost)
 
void setOutputBufferSize (std::streamsize size)
 Set the size of the output intermediate buffer in bytes (unwritten contents are lost)
 
void setBlockingMode (bool blocking)
 
void setBroadcast (bool active)
 
void setMulticastGroup (const std::string &address, bool join)
 
std::shared_ptr< Socketaccept ()
 
void disconnect ()
 Disconnects the socket, deletes the intermediate buffers and sets the handle to -1.
 
void disconnectOnError ()
 Check if there is a problem with this socket and disconnect in case there is one.
 

Public Attributes

std::set< std::shared_ptr< Socket > > clients
 Client sockets of a server.
 
std::string hostLocal
 Host string of local.
 
std::string hostRemote
 Host string of remote.
 
unsigned int portLocal
 Port of local.
 
unsigned int portRemote
 Port of remote.
 

Protected Member Functions

void initSocket (bool blocking)
 
virtual std::shared_ptr< SocketSocketFactory ()
 Generates new sockets for client connections of a server.
 

Protected Attributes

IPVersion ipVersion
 IP version which is in use.
 
Type type
 Type of the socket.
 
unsigned int status
 Or listen queue size if socket is TCP_SERVER.
 
int handle
 Handle used for the system interface.
 

Friends

class SocketManager
 

Detailed Description

Socket and stream buffer.

Member Enumeration Documentation

◆ IPVersion

Defines the version of IP.

Enumerator
IPv4 

IPv4 only.

IPv6 

IPv6 only.

ANY 

IPv4 or IPv6 (choosen by the system)

◆ Status

Defines the send status of a socket.

Enumerator
NOT_CONNECTED 

Socket is not even initialized or disconnected.

CONNECTING 

Socket is initialized but not connected yet and can not send or receive data.

LISTENING 

Socket is a server and can neither send nor receive data.

READY 

Socket is connected, can send and receive data.

BUSY 

Socket is connected and can not send but receive data (at the moment)

◆ Type

Defines the nature of a socket.

Enumerator
NONE 

No type defined.

TCP_CLIENT 

TCP socket connecting to a server.

TCP_SERVER 

TCP socket waiting for TCP_CLIENT to connect.

TCP_SERVERS_CLIENT 

TCP socket to represent a TCP_CLIENT connection at the TCP_SERVER.

UDP_PEER 

UDP socket.

Member Function Documentation

◆ accept()

std::shared_ptr< Socket > netLink::Socket::accept ( )

Accepts a TCP connection and returns it

Returns
The new accepted socket (type will be TCP_SERVERS_CLIENT)
Precondition
Type needs to be TCP_SERVER

◆ initAsTcpClient()

void netLink::Socket::initAsTcpClient ( const std::string &  hostRemote,
unsigned  portRemote,
bool  waitUntilConnected = false 
)

Setup socket as TCP client

Parameters
hostRemoteThe remote host to connect to
portRemoteThe remote port to connect to
waitUntilConnectedSet blocking mode until connected

◆ initAsTcpServer()

void netLink::Socket::initAsTcpServer ( const std::string &  hostLocal,
unsigned  portLocal,
unsigned  listenQueue = 16 
)

Setup socket as TCP server

Parameters
hostLocalThe host to be listening to:

"" or "*" to listen to any incoming data (IPVersion will be choosen by system) "0.0.0.0" to listen to any incoming data (IPVersion will be IPv4) "::0" to listen to any incoming data (IPVersion will be IPv6)

Parameters
portLocalThe local port
listenQueueQueue size for outstanding sockets to accept

◆ initAsUdpPeer()

void netLink::Socket::initAsUdpPeer ( const std::string &  hostLocal,
unsigned  portLocal 
)

Setup socket as UDP server

Parameters
hostLocalThe host to be listening to:

"" or "*" to listen to any incoming data (IPVersion will be choosen by system) "0.0.0.0" to listen to any incoming data (IPVersion will be IPv4) "::0" to listen to any incoming data (IPVersion will be IPv6) A multicast address of the multicast group to listen to

Parameters
portLocalThe local port

◆ initSocket()

void netLink::Socket::initSocket ( bool  blocking)
protected

Initzialize system handle

Parameters
blockingWaits for connection if true

◆ receive()

std::streamsize netLink::Socket::receive ( char_type *  buffer,
std::streamsize  size 
)

Receives size bytes into buffer

Returns
The actual number of bytes which were received
Precondition
Type must not be TCP_SERVER and status must be READY or BUSY
Warning
In most cases you don't want to call this directly but use the iostream API instead (see wiki)

◆ redirect()

std::streamsize netLink::Socket::redirect ( const std::vector< std::shared_ptr< Socket > > &  destinations)

Redirects received data to all Sockets in destinations

Parameters
destinationsA list of destinations to sent the data to
Returns
The actual number of bytes which were redirected
Warning
If one destination can't handle the outgoing load, a Expection is thrown and all other destinations get out of sync (data loss)

◆ send()

std::streamsize netLink::Socket::send ( const char_type *  buffer,
std::streamsize  size 
)

Sends size bytes from buffer

Returns
The actual number of bytes which were sent
Precondition
Type must not be TCP_SERVER and status must be READY
Warning
In most cases you don't want to call this directly but use the iostream API instead (see wiki)

◆ setBlockingMode()

void netLink::Socket::setBlockingMode ( bool  blocking)

Updates the blocking mode of the socket. A socket will be non blocking by default. Try to avoid using blocking mode. Use a SocketManager with listen(sec > 0.0) instead.

◆ setBroadcast()

void netLink::Socket::setBroadcast ( bool  active)

Enables or disables the broadcasting (only relevant for sending)

Parameters
activeIf true enables broadcasting else disables broadcasting
Precondition
Type needs to be UDP_PEER and IPv4

◆ setMulticastGroup()

void netLink::Socket::setMulticastGroup ( const std::string &  address,
bool  join 
)

Joines or leaves a muticast group (only relevant for receiving)

Parameters
addressAddress of the group
joinIf true it joines the group else it leaves it
Precondition
Type needs to be UDP_PEER

◆ showmanyc()

std::streamsize netLink::Socket::showmanyc ( )

Returns only the number of outstanding bytes to be received from the system cache

Returns
Number of bytes in the system cache, not including iostream buffers
Warning
Use in_avail() instead if you are interested in the total number of bytes which can be read

◆ SocketFactory()

virtual std::shared_ptr< Socket > netLink::Socket::SocketFactory ( )
inlineprotectedvirtual

Generates new sockets for client connections of a server.

Reimplemented in netLink::MsgPackSocket.


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