/
http_server.cpp
61 lines (48 loc) · 1.09 KB
/
http_server.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
49
50
51
52
53
54
55
56
57
58
59
60
#include "utils/socket.h"
#include "http_server.h"
#include "http_server_request_thread.h"
#include "utils/sqlite_db.h"
#include "http_server_db_helpers.h"
HTTPServer::HTTPServer(const std::string &webContentPath, SQLiteDB *pDB, int port) : m_port(port), m_webContentPath(webContentPath), m_pMainDB(pDB)
{
}
bool HTTPServer::start()
{
Socket::initWinsocks();
Socket mainSocket;
mainSocket.create();
if (!mainSocket.bind(m_port))
{
printf("Couldn't bind to port: %i\n", m_port);
return false;
}
if (!mainSocket.listen(20))
{
printf("Couldn't listen for connections.\n");
return false;
}
createNeededTables(m_pMainDB);
while (true)
{
Socket *newSock = new Socket();
if (mainSocket.accept(newSock))
{
HTTPServerRequestThread *newThread = new HTTPServerRequestThread(newSock, m_webContentPath, m_pMainDB);
if (newThread)
{
newThread->setAutodestruct(true);
newThread->start();
}
else
{
printf("Couldn't create new thread\n");
}
}
else
{
printf("Couldn't accept connection\n");
}
}
Socket::cleanupWinsocks();
return true;
}