Newer
Older
#ifndef _TCPLISTENER_HPP
#define _TCPLISTENER_HPP
#include "sockaddr.hpp"
#include "tcpstream.hpp"
#include <filesystem>
/**
* @brief Listen to a local ip address + port and accept incomming connections
* as TcpStreams.
*/
class TcpListener
{
private:
/**
* @brief The local SockAddr that is used to bind to and listen.
*/
SockAddr local;
/**
* @brief The filedescriptor of the current socket. If this is 0, the socket
* is closed.
*/
int sockfd;
/**
* @brief If set to true, the socket is automatically closed on destruction
*/
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
public:
/**
* @brief Create a TcpListener that will listen to the local address and
* port that is specified in the SockAddr.
*
* @param local The SockAddr that will be listened to.
*/
TcpListener(SockAddr local);
/**
* @brief Same as TcpListener(SockAddr) and the parameters are passed to
* SockAddr constructor.
*
* @see SockAddr
*/
TcpListener(IpAddr localAddress, uint16_t port);
/**
* @brief Same as TcpListener(SockAddr) and the parameters are passed to
* SockAddr constructor.
*
* @see SockAddr
*/
TcpListener(const std::string &localAddress, uint16_t port);
/**
* @brief Same as TcpListener(SockAddr) and the parameters are passed to
* SockAddr constructor.
*
* @see SockAddr
*/
TcpListener(const std::string &localAddressPort);
/**
* @brief The socket is automatically closed when the TcpListener is
* destroyed.
*/
~TcpListener();
TcpListener(TcpListener &&other);
TcpListener& operator=(TcpListener &&other);
* @brief Copying TcpListener is not allowed. See clone() for explicit copies.
*/
TcpListener(const TcpListener &other) = delete;
/**
* @brief Copying TcpListener is not allowed. See clone() for explicit copies.
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
*/
TcpListener& operator=(const TcpListener &other) = delete;
/**
* @brief Start listening on the address and port specified at the creation.
* This does not yet block and accept clients.
*
* This will create the socket, bind it to the socket address and listen.
*
* @param connectionQueue The number of connections that will be queued
* before refusing new connections.
*/
void listen(int connectionQueue = 10);
/**
* @brief Block until a tcp connection is accepted.
*
* @return The TcpStream associated with the accepted connection.
*/
TcpStream accept();
/**
* @brief Check if the listener socket is closed or open. Open in this case
* means bound and listening.
*
* @return True if the socket was not yet opened, or if it has been closed.
*/
bool isClosed() const;
/**
* @brief Close the listening socket. After this, the TcpListener can no
* longer be used without calling listen again.
*/
void close();
/**
* @brief Set the behavior for when the TcpListener is destroyed. If autoclose
* is enabled, the socket is closed on destruct. If autoclose is disabled, the
* socket will not be closed automatically.
*
* @param autoclose Enable or disable the autoclose functionality.
*/
void setAutoclose(bool autoclose);
/**
* @brief Create a clone of this socket wrapper object. The clone and the
* original will share the same underlying socket and file descriptor.
* If one of the instances closes the socket, the socket will be closed
* for both. The other instance will not be notified about this, but instead
* socket operations will just fail. Due to this, it might be a good idea to
* disabel autoclose and manually close the socket.
*
* @return A clone of this TcpListener that shares the same underlying socket.
*/
TcpListener clone();