ngscopeclient 0.1-dev+51fbda87c
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Socket Class Reference

Class representing a network socket. More...

#include <Socket.h>

Public Member Functions

 Socket (int af, int type, int protocol)
 Creates a socket. More...
 
 Socket (ZSOCKET sock, int af=PF_INET)
 Wraps an existing socket. More...
 
virtual ~Socket (void)
 Closes a socket.
 
bool Connect (const std::string &host, uint16_t port)
 Establishes a TCP connection to a remote host. More...
 
bool Bind (unsigned short port)
 Binds the socket to an address. More...
 
bool Listen ()
 Puts the socket in listening mode.
 
Socket Accept (sockaddr_in *addr, ZSOCKLEN len)
 Accepts an IPv4 connection on the socket. More...
 
Socket Accept (sockaddr_in6 *addr, ZSOCKLEN len)
 Accepts a IPv6 connection on the socket. More...
 
Socket Accept ()
 Accepts a connection on the socket. More...
 
ZSOCKET Detach ()
 Detaches the socket from this object. More...
 
void FlushRxBuffer (void)
 Flush RX buffer. More...
 
bool SendLooped (const unsigned char *buf, int count)
 Sends data over the socket. More...
 
bool RecvLooped (unsigned char *buf, int len)
 Recives data from a UDP socket. More...
 
bool RecvPascalString (std::string &str)
 Reads a Pascal-style string from a socket. More...
 
bool SendPascalString (const std::string &str)
 Sends a string to a socket. More...
 
bool DisableNagle ()
 Disable the Nagle algorithm on the socket so that messages get sent right away. More...
 
bool DisableDelayedACK ()
 Disable delayed-ACK so that we send ACKs immediately upon packet receipt. More...
 
bool SetReuseaddr (bool on=true)
 Set SO_REUSEADDR on our socket, allowing binding to it again without waiting for timeout if our task crashed hard. More...
 
bool SetRxTimeout (unsigned int microSeconds)
 
bool SetTxTimeout (unsigned int microSeconds)
 
bool SetTxBuffer (int bufsize)
 
bool SetRxBuffer (int bufsize)
 
 operator const ZSOCKET & () const
 Convert us to the native OS socket type. More...
 
bool IsValid () const
 
Socketoperator= (ZSOCKET rhs)
 
void Close ()
 

Protected Member Functions

void Open ()
 

Protected Attributes

int m_af
 Address family of this socket (typically AF_INET or AF_INET6)
 
int m_type
 Type of the socket.
 
int m_protocol
 Protocol of the socket.
 
double m_rxtimeout
 
double m_txtimeout
 
ZSOCKET m_socket
 The socket handle.
 

Detailed Description

Class representing a network socket.

Constructor & Destructor Documentation

◆ Socket() [1/2]

Socket::Socket ( int  af,
int  type,
int  protocol 
)

Creates a socket.

Parameters
afAddress family of the socket (layer 3 protocol selection)
typeType of the socket (stream or datagram)
protocolProtocol of the socket (layer 4 protocol selection)

◆ Socket() [2/2]

Socket::Socket ( ZSOCKET  sock,
int  af = PF_INET 
)

Wraps an existing socket.

Parameters
sockSocket to encapsulate
afAddress family of the provided socket

Member Function Documentation

◆ Accept() [1/3]

Socket Socket::Accept ( )

Accepts a connection on the socket.

addr Output address of accepted connection

len Size of the output buffer

Returns
Socket for the client connection

◆ Accept() [2/3]

Socket Socket::Accept ( sockaddr_in *  addr,
ZSOCKLEN  len 
)

Accepts an IPv4 connection on the socket.

addr Output address of accepted connection

len Size of the output buffer

Returns
Socket for the client connection

◆ Accept() [3/3]

Socket Socket::Accept ( sockaddr_in6 *  addr,
ZSOCKLEN  len 
)

Accepts a IPv6 connection on the socket.

addr Output address of accepted connection

len Size of the output buffer

Returns
Socket for the client connection

◆ Bind()

bool Socket::Bind ( unsigned short  port)

Binds the socket to an address.

TODO: allow binding to specific addresses etc

Parameters
portPort to listen on
Returns
true on success, false on fail

◆ Connect()

bool Socket::Connect ( const std::string &  host,
uint16_t  port 
)

Establishes a TCP connection to a remote host.

Parameters
hostDNS name or string IP address of remote host
portPort to connect to (host byte order)
Returns
true on success, false on fail

◆ Detach()

ZSOCKET Socket::Detach ( )

Detaches the socket from this object.

Returns
Socket handle. The caller is responsible for closing the handle.

◆ DisableDelayedACK()

bool Socket::DisableDelayedACK ( )

Disable delayed-ACK so that we send ACKs immediately upon packet receipt.

Returns
true on success, false on fail

◆ DisableNagle()

bool Socket::DisableNagle ( )

Disable the Nagle algorithm on the socket so that messages get sent right away.

Returns
true on success, false on fail

◆ FlushRxBuffer()

void Socket::FlushRxBuffer ( void  )

Flush RX buffer.

Returns
true on success, false on fail

◆ operator const ZSOCKET &()

Socket::operator const ZSOCKET & ( ) const
inline

Convert us to the native OS socket type.

Returns
A reference to our socket handle

◆ RecvLooped()

bool Socket::RecvLooped ( unsigned char *  buf,
int  len 
)

Recives data from a UDP socket.

Parameters
bufOutput buffer
lenLength of the buffer
addrIP address of the sender
flagsSocket flags
Returns
Number of bytes read

Sends data to a UDP socket

Parameters
bufInput buffer
lenLength of the buffer
addrIP address of the recipient
flagsSocket flags
Returns
Number of bytes sent

Recieves data from the socket

Parameters
bufThe buffer to read into
lenLength of read buffer
Returns
true on success, false on fail

◆ RecvPascalString()

bool Socket::RecvPascalString ( std::string &  str)

Reads a Pascal-style string from a socket.

Returns
true on success, false on fail

◆ SendLooped()

bool Socket::SendLooped ( const unsigned char *  buf,
int  count 
)

Sends data over the socket.

Parameters
bufBuffer to send
countLength of data buffer
Returns
true on success, false on fail

◆ SendPascalString()

bool Socket::SendPascalString ( const std::string &  str)

Sends a string to a socket.

Parameters
fdSocket handle
strString to send
Returns
true on success, false on fail

◆ SetReuseaddr()

bool Socket::SetReuseaddr ( bool  on = true)

Set SO_REUSEADDR on our socket, allowing binding to it again without waiting for timeout if our task crashed hard.

Returns
true on success, false on fail

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