dodo
0.0.1
A C++ library to create containerized Linux services
|
Go to the documentation of this file.
25 #include <netinet/tcp.h>
117 setsockopt(
socket_, SOL_TCP, TCP_NODELAY, &value,
sizeof(value) );
121 int value = enable?1:0;
122 setsockopt(
socket_, SOL_SOCKET, SO_KEEPALIVE, &value,
sizeof(value) );
126 int flags = fcntl(
socket_, F_GETFL, 0 );
128 return ! (flags & O_NONBLOCK);
133 int flags = fcntl(
socket_, F_GETFL, 0 );
136 if ( flags & O_NONBLOCK ) {
137 int rc = fcntl(
socket_, F_SETFL, flags ^ O_NONBLOCK );
141 if ( !(flags & O_NONBLOCK) ) {
142 int rc = fcntl(
socket_, F_SETFL, flags | O_NONBLOCK );
155 sizeof(opt_enable) );
165 sizeof(opt_enable) );
170 socklen_t optvar = 0;
171 socklen_t optlen =
sizeof(optvar);
172 auto rc = getsockopt(
socket_, SOL_SOCKET, SO_SNDBUF, &optvar, &optlen );
178 socklen_t optvar = 0;
179 socklen_t optlen =
sizeof(optvar);
180 auto rc = getsockopt(
socket_, SOL_SOCKET, SO_RCVBUF, &optvar, &optlen );
187 socklen_t optlen =
sizeof(optvar);
190 rc = getsockopt(
socket_, SOL_IP, IP_TTL, &optvar, &optlen );
193 rc = getsockopt(
socket_, SOL_IP, IP_TTL, &optvar, &optlen );
200 auto rc = setsockopt(
socket_, SOL_SOCKET, SO_SNDBUF, &size,
sizeof(size) );
205 int rc = setsockopt(
socket_, SOL_SOCKET, SO_RCVBUF, &size,
sizeof(size) );
211 frac = modf ( sec , &whole );
212 struct timeval tv_out;
213 tv_out.tv_sec = (time_t)whole;
214 tv_out.tv_usec = (time_t)(frac * 1.0E6);
215 int rc = setsockopt(
socket_, SOL_SOCKET, SO_RCVTIMEO, &tv_out,
sizeof(tv_out) );
221 frac = modf ( sec , &whole );
222 struct timeval tv_out;
223 tv_out.tv_sec = (time_t)whole;
224 tv_out.tv_usec = (time_t)(frac * 1.0E6);
225 int rc = setsockopt(
socket_, SOL_SOCKET, SO_SNDTIMEO, &tv_out,
sizeof(tv_out) );
231 rc = setsockopt(
socket_, SOL_IP, IP_TTL, &ttl,
sizeof(ttl) );
232 if ( errno == 92 ) rc = setsockopt(
socket_, SOL_IPV6, IPV6_UNICAST_HOPS, &ttl,
sizeof(ttl) );
237 sockaddr_storage addr;
238 socklen_t sz =
sizeof(addr);
239 int rc = getsockname(
socket_, (sockaddr*)&addr, &sz );
240 if ( rc < 0 ) addr.ss_family = AF_UNSPEC;
245 socklen_t optvar = 0;
246 socklen_t optlen =
sizeof(optvar);
247 int rc = getsockopt(
socket_, SOL_SOCKET, SO_DOMAIN, &optvar, &optlen );
253 socklen_t optvar = 0;
254 socklen_t optlen =
sizeof(optvar);
255 int rc = getsockopt(
socket_, SOL_SOCKET, SO_TYPE, &optvar, &optlen );
261 socklen_t optvar = 0;
262 socklen_t optlen =
sizeof(optvar);
263 int rc = getsockopt(
socket_, SOL_SOCKET, SO_PROTOCOL, &optvar, &optlen );
274 sockaddr_storage addr;
275 socklen_t sz =
sizeof(addr);
276 int rc = getpeername(
socket_, (sockaddr*)&addr, &sz );
277 if ( rc < 0 ) addr.ss_family = AF_UNSPEC;
282 uint8_t nwbo = value;
283 return send( &nwbo,
sizeof(nwbo) );
288 ssize_t request =
sizeof(t);
289 ssize_t received = 0;
293 error =
receive( &t, request, received );
303 uint16_t nwbo = htons( value );
304 return send( &nwbo,
sizeof(nwbo) );
309 ssize_t request =
sizeof(t);
310 ssize_t received = 0;
314 error =
receive( &t, request, received );
325 return send( &nwbo,
sizeof(nwbo), more );
330 ssize_t request =
sizeof(t);
331 ssize_t received = 0;
335 error =
receive( &t, request, received );
345 int16_t nwbo = htons( value );
346 return send( &nwbo,
sizeof(nwbo), more );
351 ssize_t request =
sizeof(t);
352 ssize_t received = 0;
356 error =
receive( &t, request, received );
366 uint32_t nwbo = htonl( value );
367 return send( &nwbo,
sizeof(nwbo), more );
372 ssize_t request =
sizeof(t);
373 ssize_t received = 0;
377 error =
receive( &t, request, received );
387 int32_t nwbo = htonl( value );
388 return send( &nwbo,
sizeof(nwbo), more );
393 ssize_t request =
sizeof(t);
394 ssize_t received = 0;
398 error =
receive( &t, request, received );
408 uint64_t nwbo = htobe64( value );
409 return send( &nwbo,
sizeof(nwbo), more );
414 ssize_t request =
sizeof(t);
415 ssize_t received = 0;
419 error =
receive( &t, request, received );
423 value = be64toh( t );
429 int64_t nwbo = htobe64( value );
430 return send( &nwbo,
sizeof(nwbo), more );
435 ssize_t request =
sizeof(t);
436 ssize_t received = 0;
440 error =
receive( &t, request, received );
444 value = be64toh( t );
454 error =
send( s.c_str(), s.size(), more );
463 uint64_t stringsize = 0;
467 ssize_t received = 0;
470 error =
receive( buf, stringsize, received );
482 std::stringstream ss;
484 error =
send( ss.str().c_str(), ss.str().length(), more );
490 ssize_t received = 0;
491 std::stringstream ss;
494 if ( c !=
'\r' ) ss << c;
495 error =
receive( &c, 1, received );
void setReceiveTimeout(double sec)
Set the receive timeout - a receive will fail if there is no data received before the timeout expires...
@ ecEADDRINUSE
98 Address already in use
common::SystemError sendInt8(int8_t value, bool more=false)
Send an int8_t.
AddressFamily getAddressFamily() const
Get the AddressFamily.
void setTCPNoDelay(bool set)
Set TCP_NODELAY.
Address getPeerAddress() const
Get the peer (remote) address for this socket.
common::SystemError listen(const Address &address, int backlog)
Sets up a listening socket on Address.
virtual common::SystemError connect(const Address &address)
Connect to the address.
#define throw_ExceptionObject(what, thing)
Throws an Exception with DebugContext, passes FILE and LINE to constructor.
@ ecEALREADY
114 Operation already in progress
common::SystemError sendLine(const std::string &s, bool more)
Sends the std::string terminated by a ' '.
common::SystemError receiveInt64(int64_t &value)
receive an int64_t
void setBlocking(bool blocking)
Set the Socket blocking mode.
std::string asString(bool withport=false) const
Return a string representation of this Address.
virtual socklen_t getReceiveBufSize() const
Get the maximum buffer length for receive.
@ ecETIMEDOUT
110 Connection timed out
virtual void close()
Closes the socket, causing the connection, if it exists, to be terminated.
void setReUseAddress()
Enable the socket to re-use an address when listen/bind is called.
common::SystemError sendInt16(int16_t value, bool more=false)
Send an int16_t.
@ ecECONNREFUSED
111 Connection refused
AddressFamily
Addres family type.
Generic network Address, supporting ipv4 and ipv6 transparently.
void setTTL(int ttl)
Set the Socket TTL (Time to Live).
SocketParams::SocketType getSocketType() const
Get the SocketParams::SocketType of the socket.
common::SystemError sendUInt8(uint8_t value, bool more=false)
Send an uint8_t.
common::SystemError receiveInt8(int8_t &value)
receive an int8_t
common::SystemError receiveLine(std::string &s)
receive a stream of characters until a ' ' is read (consumed from the socket read buffer but not adde...
common::SystemError sendUInt32(uint32_t value, bool more=false)
Send an uint32_t.
common::SystemError sendInt64(int64_t value, bool more=false)
Send an int64_t.
common::SystemError sendInt32(int32_t value, bool more=false)
Send an int32_t.
@ ecEINPROGRESS
115 Operation now in progress
@ ecEADDRNOTAVAIL
99 Cannot assign requested address
socklen_t getSendBufSize() const
Get the maximum buffer length for send.
common::SystemError sendUInt16(uint16_t value, bool more=false)
Send an uint16_t.
void setTCPKeepAlive(bool enable)
Enable or disable TCP keep-alive on the socket.
@ ecEISCONN
106 Transport endpoint is already connected
BaseSocket()
Default constructor creates an invalid socket.
common::SystemError receiveUInt16(uint16_t &value)
receive an uint16_t
SocketParams::AddressFamily getAddressFamily() const
Get the SocketParams::AddressFamily of the socket.
Socket parameters - the family (domain), socket type and protocol triplet.
common::SystemError sendUInt64(uint64_t value, bool more=false)
Send an sendUInt64.
int socket_
The socket file decsriptor.
int getTTL() const
Get the Socket TTL (time to live) or the max number of packet hops.
virtual bool getBlocking() const
Rerurn true if the socket is operating in blocking mode.
@ ecENETUNREACH
101 Network is unreachable
virtual common::SystemError receive(void *buf, ssize_t request, ssize_t &received)=0
Receive bytes from the socket.
Address getAddress() const
Get the local address for this socket.
#define throw_Exception(what)
Throws an Exception, passes FILE and LINE to constructor.
#define throw_SystemExceptionObject(what, errno, thing)
Throws an Exception with errno, passes FILE and LINE to constructor.
common::SystemError receiveString(std::string &s)
Receive a std::string as sent by sendString( const std::string& ).
@ ecOK
0 Not an error, success
Interface for network communication.
SocketParams getSocketParams() const
Return the SocketParams.
void setSendBufSize(socklen_t size)
Set the size of the send buffer.
bool isValid() const
Return true when the socket descriptor is a valid, hence 'possible' descriptor.
common::SystemError receiveUInt8(uint8_t &value)
receive an uint8_t
virtual std::string debugDetail() const
Return debug object state as a string.
common::SystemError receiveUInt64(uint64_t &value)
receive an uint64_t
SocketType getSocketType() const
Get the SocketType.
const sockaddr_storage * getAddress() const
Get to the underlying sockaddr_storage.
Linux system error primitive to provide a consistent interface to Linux error codes.
SocketParams::ProtocolNumber getProtocolNumber() const
Get the SocketParams::ProtocolNumber of the socket.
struct sockaddr_storage addr_
The address storage (for either ipv4 or ipv6).
void setReceiveBufSize(socklen_t size)
Set the size of the receive buffer.
common::SystemError sendString(const std::string &s, bool more=false)
Sends the std::string as understood by receiveString().
BaseSocket & operator=(int socket)
Assign from existing socket descriptor (int).
ProtocolNumber
IANA Protocol number.
SocketType
Socket Type type.
@ ecEACCES
13 Permission denied
void setReUsePort()
Make the socket re-use a port when listen is called.
common::SystemError receiveInt32(int32_t &value)
receive an int32_t
common::SystemError receiveUInt32(uint32_t &value)
receive an uint32_t
void setSendTimeout(double sec)
Set the send timeout - a send will fail if there is no data send before the timeout expires.
common::SystemError receiveInt16(int16_t &value)
receive an int16_t
ProtocolNumber getProtocol() const
Get the ProtocolNumber.
common::SystemError bind(const Address &address)
Bind the socket to the Address.
Interface to and common implementation of concrete sockets (Socket, TLSSocket).
virtual common::SystemError send(const void *buf, ssize_t len, bool more=false)=0
Send bytes on the socket.