-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnection.cpp
More file actions
81 lines (73 loc) · 1.68 KB
/
Connection.cpp
File metadata and controls
81 lines (73 loc) · 1.68 KB
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
#include "Connection.hpp"
#include "Processor.hpp"
#include "Logger.hpp"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
Connection::Connection(MyServer &serv, int fd, const sockaddr_in &peer) : serv(serv)
{
this->fd = fd;
this->addr = peer.sin_addr;
this->port = peer.sin_port;
this->addrStr = inet_ntoa(addr);
this->closed = false;
}
Connection::~Connection()
{
}
in_addr Connection::getAddr() const
{
return addr;
}
uint Connection::getPort() const
{
return port;
}
int Connection::getFd() const
{
return fd;
}
const string &Connection::getAddrStr() const
{
return addrStr;
}
bool Connection::isClosed()
{
return (closed);
}
void Connection::processRequest(bool closeConnection)
{
try
{
Reader reader(fd, BUFF_SIZE);
if (!reader.empty())
{
Request request(reader);
request.setInAddr(addrStr);
logger.log(LogType::DEBUG, request);
Processor processor(serv);
Response response = processor.processRequest(request);
if (closeConnection)
response.setHeader("Connection", "close");
Writer writer(fd);
logger.log(LogType::DEBUG, response);
response.send(writer);
}
if (closeConnection)
{
close(fd);
closed = true;
}
}
catch (const MyServer::Exception &e)
{
Response response(serv);
response.setStatus("400");
Writer writer(fd, 1024);
response.send(writer);
close(fd);
closed = true;
logger.log(LogType::DEBUG, "caught exception " + e.GetMessage());
}
}