-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTcpConnection.cpp
149 lines (130 loc) · 3.57 KB
/
TcpConnection.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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <boost/algorithm/string.hpp>
#include "TcpConnection.h"
const long TIMEOUT = 60; // seconds
// Constructor
CTcpConnection::CTcpConnection(boost::asio::io_service& aIoService, CEmailAnalyser& aEmailAnalyser)
: mSocket(aIoService)
, mTimer(aIoService)
{
mConnection = NewMail.connect(
[&aEmailAnalyser] (const std::string& aMessage) {aEmailAnalyser.NewEmail(aMessage);}
);
}
// Init connection (start listening)
void CTcpConnection::Init ()
{
mClientAsString = mSocket.remote_endpoint().address().to_string() + ":" + std::to_string(mSocket.remote_endpoint().port());
// Configure timeout
ResetTimeout();
Send("220 SMTP Ready\r\n"); // Service ready
StartListening();
}
// Start listening from client
void CTcpConnection::StartListening ()
{
// Start listening for message.
mSocket.async_read_some(boost::asio::buffer(mBuffer),
[self = shared_from_this()] (auto error, size_t bytes_transferred){self->HandleRead(error, bytes_transferred);}
);
}
// Callback on data received from client
void CTcpConnection::HandleRead(const boost::system::error_code& error, size_t bytes_transferred)
{
bool bCloseConnexion = error;
if (!error)
{
// Relauch timeout timer
ResetTimeout();
std::string message(mBuffer.begin(), mBuffer.begin()+bytes_transferred);
// Display received message
// std::cout << "\033[1;36m" << message << "\033[0m";
// Append to email
mEmail.append(message);
boost::algorithm::to_upper(message);
// Limit email size
if (MAX_EMAIL_SIZE < mEmail.size())
{
bCloseConnexion = true;
}
else if (!mbDataSeen && std::string::npos != message.find("DATA"))
{
mbDataSeen = true;
Send("354 Enter mail, end with \".\" on a line by itself\r\n");
}
else if (std::string::npos != message.find("QUIT"))
{
bCloseConnexion = true;
}
else if (!mbDataSeen || std::string::npos != message.find("\r\n.\r\n") || std::string::npos != message.rfind(".\r\n", 0))
{
Send("250 Ok\r\n");
}
}
if (error)
{
// std::cout << "ERROR: " << error.message() << std::endl;
}
if (bCloseConnexion)
{
Send("221 Closing connection\r\n");
CloseConnection();
}
else
{
// Start listening for message again
StartListening();
}
}
// Send message to client
void CTcpConnection::Send (std::string aMessage)
{
// std::cout << "\033[1;35m" << aMessage << "\033[0m";
if (mSocket.is_open())
{
// Uncomment to see server response
// cout << "\033[1;31m" << aMessage << "\033[0m";
boost::asio::async_write(mSocket, boost::asio::buffer(aMessage),
[self = shared_from_this()] (auto error, auto bytes_transferred) {self->HandleWrite(error, bytes_transferred);}
);
}
}
// Callback at each write on socket
void CTcpConnection::HandleWrite(const boost::system::error_code& error, size_t bytes_transferred)
{
if (error)
{
std::cout << error.message() << std::endl;
CloseConnection();
}
}
// Reset timeout to initial value
void CTcpConnection::ResetTimeout ()
{
mTimer.expires_from_now(boost::posix_time::seconds(TIMEOUT));
// Changing expiration time cancel the timer, so we have to start a new asynchronous wait
mTimer.async_wait(
[self = shared_from_this()] (const boost::system::error_code& error_code)
{
if (boost::asio::error::operation_aborted != error_code)
{
std::cout << "Connection timeout" << std::endl;
self->CloseConnection();
}
}
);
}
// Close connection
void CTcpConnection::CloseConnection()
{
mTimer.cancel();
if (mSocket.is_open())
{
std::cout << "Close client connection with " << mClientAsString << std::endl;
mSocket.close();
}
if (mEmail.size())
{
NewMail(mEmail);
}
mConnection.disconnect();
}