forked from eantcal/thttpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TcpListener.h
executable file
·125 lines (95 loc) · 2.95 KB
/
TcpListener.h
1
2
3
4
5
6
7
8
9
10
11
12
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
120
121
122
123
124
//
// This file is part of thttpd
// Copyright (c) Antonino Calderone ([email protected])
// All rights reserved.
// Licensed under the MIT License.
// See COPYING file in the project root for full license information.
//
/* -------------------------------------------------------------------------- */
#ifndef __TCP_LISTENER_H__
#define __TCP_LISTENER_H__
/* -------------------------------------------------------------------------- */
#include "TcpSocket.h"
#include "OsSocketSupport.h"
#include <atomic>
#include <chrono>
#include <cstdint>
#include <fstream>
#include <memory>
/* -------------------------------------------------------------------------- */
/**
* Listens for connections from TCP network clients.
*/
class TcpListener : public TransportSocket {
public:
using TranspPort = TcpSocket::TranspPort;
using Handle = std::unique_ptr<TcpListener>;
enum class Status { INVALID, VALID };
/**
* Returns the current state of this connection.
*
* @return State::VALID if connection is valid,
* State::INVALID otherwise
*/
Status getStatus() const {
return _status;
}
/**
* Returns the current state of this connection.
* @return true if connection is valid,
* false otherwise
*/
operator bool() const {
return getStatus() != Status::INVALID;
}
/**
* Returns a handle to a new listener object.
*
* @return the handle to a new listenr object instance
*/
static Handle create() {
return Handle(new TcpListener());
}
/**
* Associates a local IPv4 address and TCP port with this
* connection.
*
* @param ip The IPv4 address of local interface to bind to
* @param port The port to bind to
* @return false if operation fails, true otherwise
*/
bool bind(const std::string& ip, const TranspPort& port);
/**
* Associates a local TCP port with this connection.
*
* @param port The port to bind to
* @return false if operation fails, true otherwise
*/
bool bind(const TranspPort& port) {
return bind("", port);
}
/**
* Enables the listening mode, to listen for incoming
* connection attempts.
*
* @param backlog The maximum length of the pending connections queue.
* @return true if operation successfully completed, false otherwise
*/
bool listen(int backlog = SOMAXCONN) {
return ::listen(getSocketFd(), backlog) == 0;
}
/**
* Extracts the first connection on the queue of pending connections,
* and creates a new tcp connection handle
*
* @return an handle to a new tcp connection
*/
TcpSocket::Handle accept();
private:
std::atomic<Status> _status;
TranspPort _port = 0;
sockaddr_in _local_ip_port_sa_in;
TcpListener();
};
/* -------------------------------------------------------------------------- */
#endif // __TCP_LISTENER_H__