Newer
Older
#ifndef _TCPLISTENER_HPP
#define _TCPLISTENER_HPP
#include "sockaddr.hpp"
#include "tcpstream.hpp"
#include <filesystem>
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
77
78
79
80
81
82
83
84
85
86
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
/**
* @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;
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) = default;
TcpListener& operator=(TcpListener &&other) = default;
/**
* @brief Copying TcpListener is not allowed.
*/
TcpListener(const TcpListener &other) = delete;
/**
* @brief Copying TcpListener is not allowed.
*/
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();
};