dodo  0.0.1
A C++ library to create containerized Linux services
socket.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the dodo library (https://github.com/jmspit/dodo).
3  * Copyright (c) 2019 Jan-Marten Spit.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, version 3.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /**
19  * @file socket.hpp
20  * Defines the dodo::network::Socket class.
21  */
22 
23 #ifndef network_socket_hpp
24 #define network_socket_hpp
25 
26 #include <arpa/inet.h>
27 
28 #include <map>
29 #include <netdb.h>
30 #include <stdint.h>
31 #include <sys/socket.h>
32 
33 #include "common/systemerror.hpp"
34 #include "network/address.hpp"
35 #include "network/basesocket.hpp"
36 
37 namespace dodo::network {
38 
39  /**
40  * A Linux socket.
41  * Socket objects refer to, but do not own a particular socket descriptor. The programmer controls closing the socket
42  * by calling close(), the Socket destructor does not do this implicitly, as two Socket objects may refer to the
43  * same socket descriptor.
44  *
45  * The Socket operates either in blocking or non-blocking mode. In blocking mode, calls to listen() or receive()
46  * will block until data is received. Moreover, in blocking mode, there is no limit to the amount of data that can be
47  * sent or received in one go. In non-blocking mode, the send and receive sizes per call must fit in
48  * Socket::getSendBufSize() or Socket::getReceiveBufSize().
49  */
50  class Socket : public BaseSocket {
51  public:
52 
53 
54  /**
55  * Default constructor creates an invalid socket.
56  */
57  Socket() : BaseSocket() {};
58 
59  /**
60  * Construct from a socket descriptor.
61  * @param socket The socket file descriptor.
62  */
63  Socket( int socket ) : BaseSocket(socket) {};
64 
65  /**
66  * Construct from SocketParams.
67  * @param blocking If true, create the socket in blocking mode.
68  * @param params The SocketParams to use.
69  */
70  Socket( bool blocking, SocketParams params ) : BaseSocket( blocking, params ) {};
71 
72  /**
73  * Construct with default SocketParams.
74  * @param blocking If true, create the socket in blocking mode.
75  */
76  Socket( bool blocking ) : BaseSocket( blocking ) {};
77 
78  /**
79  * Destructs this Socket, but does not call close().
80  */
81  virtual ~Socket() {};
82 
83  /**
84  * Send len bytes as a bytestream. connect must have been called first.
85  * @param buf The bytes to send.
86  * @param len The number of bytes to send from buf.
87  * @param more If true, do no forec a network send right away, but wait until send buffer full or
88  * a subsequent call with more set to false.
89  * @return SystemError::ecOK or
90  * - SystemError::ecEAGAIN: For non-blocking sockets, neither failure nor succces, try again.
91  * - SystemError::ecECONNRESET : The peer has disconnected during the send.
92  * @see sendTo()
93  */
94  virtual common::SystemError send( const void* buf, ssize_t len, bool more = false );
95 
96  /**
97  * Send raw packets to the given Address.
98  * @param address The destination Address
99  * @param buf The bytes to send
100  * @param len The number of bytes to send.
101  * @return The SystemError.
102  */
103  virtual common::SystemError sendTo( const Address& address, const void* buf, ssize_t len );
104 
105  /**
106  * Receive bytes
107  * @param buf The buffer to receive the data into.
108  * @param request The number of bytes to receive (request <= sizeof(buf) ).
109  * @param received The number of bytes actuially received
110  * @return The error code:
111  * - SystemError::ecOK
112  * - SystemError::ecEAGAIN
113  * - SystemError::ecENOTCONN
114  * - SystemError::ecECONNREFUSED
115  * On other errors returned by the internal call to recv, an exception is thrown.
116  */
117  virtual common::SystemError receive( void* buf, ssize_t request, ssize_t &received );
118 
119  /**
120  * Accepts a connection request and return a pointer to a new Socket for the new connection, the caller will
121  * own the new Socket object.
122  * @return The accepted socket.
123  */
124  virtual Socket* accept();
125 
126  /**
127  * An invalid SocketInvalid for comparison convenience - initialized to a Socket with socket fd=-1.
128  */
130 
131  protected:
132 
133 
134  };
135 
136 
137 }
138 
139 #endif
dodo::network::Socket::Socket
Socket()
Default constructor creates an invalid socket.
Definition: socket.hpp:57
dodo::network::Socket::Socket
Socket(int socket)
Construct from a socket descriptor.
Definition: socket.hpp:63
dodo::network::Address
Generic network Address, supporting ipv4 and ipv6 transparently.
Definition: address.hpp:90
dodo::network::Socket::receive
virtual common::SystemError receive(void *buf, ssize_t request, ssize_t &received)
Receive bytes.
Definition: socket.cpp:81
dodo::network::Socket::Socket
Socket(bool blocking)
Construct with default SocketParams.
Definition: socket.hpp:76
dodo::network::Socket::SocketInvalid
static Socket SocketInvalid
An invalid SocketInvalid for comparison convenience - initialized to a Socket with socket fd=-1.
Definition: socket.hpp:129
dodo::network::SocketParams
Socket parameters - the family (domain), socket type and protocol triplet.
Definition: socketparams.hpp:35
dodo::network::Socket::sendTo
virtual common::SystemError sendTo(const Address &address, const void *buf, ssize_t len)
Send raw packets to the given Address.
Definition: socket.cpp:59
address.hpp
dodo::network
Interface for network communication.
Definition: address.hpp:37
dodo::network::Socket
A Linux socket.
Definition: socket.hpp:50
dodo::network::Socket::~Socket
virtual ~Socket()
Destructs this Socket, but does not call close().
Definition: socket.hpp:81
dodo::network::Socket::Socket
Socket(bool blocking, SocketParams params)
Construct from SocketParams.
Definition: socket.hpp:70
dodo::network::Socket::accept
virtual Socket * accept()
Accepts a connection request and return a pointer to a new Socket for the new connection,...
Definition: socket.cpp:98
dodo::common::SystemError
Linux system error primitive to provide a consistent interface to Linux error codes.
Definition: systemerror.hpp:53
systemerror.hpp
basesocket.hpp
dodo::network::BaseSocket
Interface to and common implementation of concrete sockets (Socket, TLSSocket).
Definition: basesocket.hpp:36
dodo::network::Socket::send
virtual common::SystemError send(const void *buf, ssize_t len, bool more=false)
Send len bytes as a bytestream.
Definition: socket.cpp:36