-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpServer.cpp
48 lines (37 loc) · 1.22 KB
/
httpServer.cpp
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
#include "httpServer.h"
#include "utils.h"
#include "threadPool.h"
namespace http
{
HttpServer::HttpServer(Server &serverInstance, std::string servIp, int port) : Server(serverInstance)
{
httpServSockAddrLen = sizeof(httpServSockAddr);
httpServSock = runSocket(servIp, port, httpServSockAddr, httpServSockAddrLen, SOCK_STREAM);
}
HttpServer::~HttpServer()
{
}
void HttpServer::startListen()
{
if (listen(httpServSock, 20) < 0)
erroredExit("Could not listen on the socket");
log("Listening...");
while (true)
{
int connSock = acceptConnection();
pool->enqueueTask([this, &connSock]
{
std::thread thr(&Server::handleUserConnection, this, std::ref(connSock));
thr.join();
close(connSock);
});
}
}
int HttpServer::acceptConnection()
{
int connSock = accept(httpServSock, (sockaddr *)&httpServSockAddr, &httpServSockAddrLen);
if (connSock < 0)
erroredExit("Could not accept connection");
return connSock;
}
}