-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserverlistener.h
63 lines (51 loc) · 1.82 KB
/
serverlistener.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
#ifndef SERVERLISTENER_H
#define SERVERLISTENER_H
/**
* @file serverlistener.h
* @brief File contains definition of ServerListener
*/
#include <stdexcept>
#include <string>
#include <functional>
#include <winsock2.h>
#include <ws2tcpip.h>
#include "serverexceptions.h"
/**
* @brief Creates an HTTP server instance and awaits requests
*
* Class uses WinSock library.
*
* @warning Server handles only HTTP GET requests (and shows some debug data in response)
* @warning Threads responsible for handling clients aren't really being taken care of
* @todo Handle more HTTP methods
* @todo Give the opportunity to handle client requests to the user (some kind of routing)
*/
class ServerListener {
int port;
size_t buffer_size;
SOCKET listen_socket = INVALID_SOCKET;
bool server_running;
static void clientHandler(SOCKET client_socket, size_t buffer_size);
public:
/**
* Initialize a new server instance.
*
* @param port Port on which the server will be listening for connections
* @param buffer_size Size of the buffer used to retrieve data from sockets
*/
ServerListener(int port=80, size_t buffer_size=255);
/**
* Start listening for connections.
*
* @param client_acceptation_error_callback The function receiving the ClientAcceptationException object when a problem with acceptation of new connection occurs
*/
void run(std::function<void(ClientAcceptationException)> client_acceptation_error_callback = [](ClientAcceptationException) {});
/**
* Stop listening for connections (close listening socket).
*
* @warning It could be run only from the different thread, as start() is blocking
*/
void stop();
virtual ~ServerListener();
};
#endif // SERVERLISTENER_H