scclib
Stable Cloud Computing C++ Library
Public Member Functions | Protected Member Functions | List of all members
scc::net::TcpSocket Class Referenceabstract

Tcp socket base class. More...

#include <socket.h>

Inheritance diagram for scc::net::TcpSocket:
Inheritance graph
[legend]
Collaboration diagram for scc::net::TcpSocket:
Collaboration graph
[legend]

Public Member Functions

virtual void reset ()=0
 
void listen (int maxConnections=10)
 Set to accept connections. More...
 
void connect (SockaddrBase &)
 Connect to a socket address. More...
 
void connect (SockaddrBase &, std::error_code &) noexcept
 Connect to a socket address. More...
 
virtual size_t read (void *loc, size_t len)
 Read bytes. More...
 
virtual size_t write (const void *loc, size_t len)
 Write bytes. More...
 
void shutdown ()
 Shutdown the connection; no further reads or writes will be allowed. More...
 
- Public Member Functions inherited from scc::net::SocketBase
 SocketBase (SocketBase &&other)
 Move construct socket.
 
const SocketBaseoperator= (SocketBase &&other)
 Move assign socket.
 
int fd () const
 Return the underlying socket handle.
 
 operator int () const
 Allow object to be cast to a socket handle.
 
void reuse_addr (bool r=true)
 Set address reusable. More...
 
void reuse_port (bool r=true)
 Set port reusable. More...
 
void non_blocking (bool b=true)
 Set the socket non-blocking. More...
 
std::error_code error_code ()
 Get the current error code (status). More...
 
void recv_bufsize (unsigned)
 Set receive buffer size. More...
 
unsigned recv_bufsize ()
 Get total receive buffer size including overhead.
 
void send_bufsize (unsigned)
 Set send buffer size. More...
 
unsigned send_bufsize ()
 Get total send buffer size including overhead.
 
void send_timeout (std::chrono::milliseconds)
 Set the send timeout. More...
 
void recv_timeout (std::chrono::milliseconds)
 Set the receive timeout. More...
 
void bind (const SockaddrBase &)
 Bind an address to the socket.
 
size_t recv (void *loc, size_t len)
 Receive bytes, throwing an exception on error. More...
 
size_t recv (void *loc, size_t len, std::error_code &e) noexcept
 Receive bytes. More...
 
size_t send (const void *loc, size_t len)
 Send bytes, throwing an exception on error. More...
 
size_t send (const void *loc, size_t len, std::error_code &e) noexcept
 Send bytes, setting the error code for the call. More...
 
void close ()
 Close the socket.
 

Protected Member Functions

 TcpSocket (int fd)
 
 TcpSocket (int domain, int stype, int proto)
 
int accept (sockaddr *, int len, std::error_code &) noexcept
 Accept a connection. More...
 
- Protected Member Functions inherited from scc::net::SocketBase
 SocketBase (int fd)
 
 SocketBase (int, int, int)
 
void reset (int, int, int)
 
void move (SocketBase &other)
 
void get_sockaddr (sockaddr &)
 

Detailed Description

Tcp socket base class.

Definition at line 290 of file socket.h.

Member Function Documentation

◆ accept()

int TcpSocket::accept ( sockaddr *  peer,
int  len,
std::error_code &  ec 
)
protectednoexcept

Accept a connection.

Returns a file descriptor for the new socket, or return an error code on failure. Sets the exception error code for the call.

If the sockaddr pointer is set, sets it to the address of the connected peer.

Definition at line 344 of file socket.cc.

◆ connect() [1/2]

void TcpSocket::connect ( SockaddrBase p)

Connect to a socket address.

Make a connection to this address.

Throw an exception on error.

Definition at line 397 of file socket.cc.

◆ connect() [2/2]

void TcpSocket::connect ( SockaddrBase p,
std::error_code &  ec 
)
noexcept

Connect to a socket address.

Set the error code for the call. Caller must check code(), which is 0 on success.

Definition at line 376 of file socket.cc.

◆ listen()

void TcpSocket::listen ( int  maxConnections = 10)

Set to accept connections.

After maximum connection backlog is reached (of unaccepted connections), system may refuse new connections on the socket.

Definition at line 334 of file socket.cc.

◆ read()

virtual size_t scc::net::TcpSocket::read ( void *  loc,
size_t  len 
)
inlinevirtual

Read bytes.

Returns number of bytes read, or 0 on peer shutdown (if non-zero bytes requested).

Throws an exception on error.

See also
scc::util::Reader

Implements scc::util::Reader.

Definition at line 329 of file socket.h.

◆ shutdown()

void TcpSocket::shutdown ( )

Shutdown the connection; no further reads or writes will be allowed.

If the socket is shared (for example, via a fork) then communication will be stopped, even if all references to the socket have not been closed.

Definition at line 415 of file socket.cc.

◆ write()

virtual size_t scc::net::TcpSocket::write ( const void *  loc,
size_t  len 
)
inlinevirtual

Write bytes.

Returns number of bytes written.

Throws an exception on error.

See also
scc::util::Writer

Implements scc::util::Writer.

Definition at line 338 of file socket.h.


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