-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsession.cpp
476 lines (409 loc) · 13.1 KB
/
session.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#include "session.hpp"
#include "model-sys/users.hpp"
#include "data-sys/users_dao.hpp"
#include "state_manager.hpp"
#include "async_io.hpp"
#include "socket_handler.hpp"
#include "session_manager.hpp"
#include "telnet_decoder.hpp"
#include "menu_system.hpp"
#include "logging.hpp"
#include "encoding.hpp"
#include "libSqliteWrapped.h"
#include "mods/mod_prelogon.hpp"
#include <memory>
#include <list>
#include <string>
#include <cassert>
#include <thread>
/**
* @brief Session Constructor
* @param tcp_connection
* @param session_manager
* @return
*/
Session::Session(async_io_ptr my_async_io, session_manager_ptr my_session_manager)
: m_log(Logging::getInstance())
, m_async_io(my_async_io)
, m_session_manager(my_session_manager)
, m_state_manager(nullptr)
, m_telnet_decoder(nullptr)
, m_user_record(nullptr)
, m_node_number(0)
, m_is_leaving(false)
, m_parsed_data("")
, m_encoding_text(Encoding::ENCODING_TEXT_UTF8)
, m_encoding(Encoding::ENCODE_UTF8)
, m_is_use_ansi(true)
, m_is_esc_timer(false)
, m_is_session_authorized(false)
, m_user_database(USERS_DATABASE, &m_database_log)
{
// Setup Shared Pointers
m_state_manager = std::make_shared<StateManager>();
m_telnet_decoder = std::make_shared<TelnetDecoder>(my_async_io);
m_user_record = std::make_shared<Users>();
if(m_async_io->isActive())
{
try
{
m_log.write<Logging::DEBUG_LOG>("New Session Accepted", __LINE__, __FILE__);
}
catch(std::exception &ex)
{
m_log.write<Logging::ERROR_LOG>("New Session Exception=", ex.what(), __LINE__, __FILE__);
}
}
}
Session::~Session()
{
m_log.write<Logging::INFO_LOG>("~Session() Closed.");
// Free the menu system state and modules when session closes.
m_state_manager->clean();
m_async_io.reset();
m_telnet_decoder.reset();
m_user_record.reset();
m_session_manager.reset();
std::vector<unsigned char>().swap(m_in_data_vector);
}
/**
* @brief Telnet Option Negoiation Timer
*/
void Session::startTelnetOptionNegoiation()
{
//m_deadline_timer->setWaitInMilliseconds(2000);
// Deprecated bind, look at replacing with lambda and std::function
//m_deadline_timer->asyncWait(
// std::bind(&Session::handleTelnetOptionNegoiation, shared_from_this())
//);
auto callback_function = std::bind(&Session::handleTelnetOptionNegoiation, shared_from_this());
asyncWait(2000, callback_function);
}
/**
* @brief Timer Handler, When Called Starts up the Login State
*
* @param timer
*/
void Session::handleTelnetOptionNegoiation()
{
// Setup Node Number for Separate Thread.
m_log.setUserInfo(m_node_number);
// Starts Up the Menu System Then Loads up the PreLogin Sequence.
state_ptr new_state = std::make_shared<MenuSystem>(shared_from_this());
m_state_manager->changeState(new_state);
}
/**
* @brief Deadline Detection Timer for Negotiation
* @param timer
*/
void Session::handlePyBind11State()
{
// Work in Progress, not yet in use for main develop branch
// Also requries Dependencies and Extra setup on Make Files!
#ifdef HAVE_PYHON
// Detection Completed, start ip the Pre-Logon Sequence State.
state_ptr new_state = std::make_shared<PythonSystem>(m_session_data);
m_state_manager->changeState(new_state);
#endif // HAVE_PYTHON
}
/**
* @brief Callback from The Broadcaster to write data to the active sessions.
* @param msg
*/
void Session::deliver(const std::string &msg, bool is_disconnection)
{
if(msg.size() == 0 || msg[0] == '\0')
{
return;
}
std::string outputBuffer = "";
// On Output, We have internal UTF8 now, translate to CP437
if(m_encoding == Encoding::ENCODE_CP437)
{
outputBuffer = Encoding::getInstance().utf8Decode(msg);
}
else
{
outputBuffer = msg;
}
if(m_async_io->isActive()) // && TheCommunicator::instance()->isActive())
{
// Deprecated bind, look at replacing with lambda and std::function
if (!is_disconnection)
{
m_async_io->asyncWrite(outputBuffer,
std::bind(
&Session::handleWrite,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2));
}
else
{
m_async_io->asyncWrite(outputBuffer,
std::bind(
&Session::handleWriteThenDisconnect,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2));
}
}
}
/**
* @brief Callback after Writing Data, If error/hangup notifies
* Everyone this person has left.
* @param error
*/
void Session::handleWrite(const std::error_code& error, socket_handler_ptr)
{
// Setup All Mods for Proper Node Logging by Session.
m_log.setUserInfo(m_node_number);
if(error)
{
m_log.write<Logging::ERROR_LOG>("Async_HandleWrite Error Session Closed() msg=", error.message());
logoff();
}
}
/**
* @brief Callback after Writing Data, If error/hangup notifies
* Everyone this person has left.
* @param error
*/
void Session::handleWriteThenDisconnect(const std::error_code& error, socket_handler_ptr ptr)
{
m_log.setUserInfo(m_node_number);
handleWrite(error, ptr);
disconnectUser();
}
template <class Callback>
void Session::asyncWait(int expires_on, const Callback &callback_method)
{
m_log.setUserInfo(m_node_number);
m_async_io->asyncWait(expires_on, callback_method);
}
// Previous SessionData Methods
/**
* @brief Data Handler for incoming Data (From Client)
*/
void Session::waitingForData()
{
// Important, clear out buffer before each read.
std::vector<unsigned char>().swap(m_in_data_vector);
if(m_async_io->isActive()) // && TheCommunicator::instance()->isActive())
{
// Deprecated bind, look at replacing with lambda and std::function
m_async_io->asyncRead(m_in_data_vector,
std::bind(
&Session::handleRead,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2));
}
}
/**
* @brief Passed data Though the State, and Checks ESC Timer
*/
void Session::updateState()
{
// Setup Logging per Current Node.
m_log.setUserInfo(m_node_number);
// Last Character Received is ESC, then Check for
// ESC Sequence, or Lone ESC Key.
if(m_parsed_data[m_parsed_data.size()-1] == '\x1b')
{
startEscapeTimer();
m_is_esc_timer = true;
}
else if(!m_is_esc_timer)
{
m_state_manager->update();
}
}
/**
* @brief ESC Sequence Timer for Determining Single Key vs. ESC Sequence
*/
void Session::startEscapeTimer()
{
// Add Deadline Timer for .400 milliseconds for complete ESC Sequences.
// Is no other input or part of ESC Sequences ie.. [A following the ESC
// Then it's an ESC key, otherwise capture the rest of the sequence.
//m_esc_input_timer->setWaitInMilliseconds(400);
//m_esc_input_timer->asyncWait(
// std::bind(&Session::handleEscTimer, shared_from_this())
//);
auto callback_function = std::bind(&Session::handleEscTimer, shared_from_this());
asyncWait(400, callback_function);
}
/**
* @brief Deadline Input Timer for ESC vs ESC Sequence.
* @param timer
*/
void Session::handleEscTimer()
{
m_log.setUserInfo(m_node_number);
// Move text to State Machine, Timer has passed, or remainder of Sequence caught up!
m_state_manager->update();
m_is_esc_timer = false;
}
/**
* @brief Callback after data received. handles telnet options
* Then parses out normal text data from client to server.
* @param error
* @param bytes_transferred
*/
void Session::handleRead(const std::error_code& error, socket_handler_ptr)
{
m_log.setUserInfo(m_node_number);
m_log.write<Logging::DEBUG_LOG>("handleRead - After Incoming Data.", __FILE__, __LINE__);
session_manager_ptr session_manager = m_session_manager.lock();
if(!session_manager)
{
m_log.write<Logging::ERROR_LOG>("handleRead - Unable to load session_manager", __FILE__, __LINE__);
return;
}
if (error)
{
// Disconnect the session.
m_is_leaving = true;
session_manager->leave(shared_from_this());
return;
}
// Part I: Parse Out Telnet Options and handle responses back to client.
handleTeloptCodes();
if(m_parsed_data.size() > 0)
{
// Windows Console Telnet sends [CR\LF] for ENTER!
// search and replace input buffer we only need one!
std::string::size_type id1 = 0;
do
{
// Convert CR\LF to LF!
id1 = m_parsed_data.find("\r\n", 0);
if(id1 != std::string::npos)
{
m_parsed_data.erase(id1, 1);
id1 = m_parsed_data.find("\r\n", 0);
}
}
while(id1 != std::string::npos);
// If were in a process, skip stat and ESC timer.
/*
if(m_is_process_running)
{
if(m_processes.size() > 0)
{
m_processes.back()->update();
}
else
{
// If no processes then reset.
m_is_process_running = false;
updateState();
}
}
else
{
// Check for state cleanup
if(m_processes.size() > 0)
{
clearProcess();
}
updateState();
}*/
updateState();
}
// Restart Callback to wait for more data.
// If this step is skipped, then the node will exit
// since io_service will have no more work!
if(m_async_io->isActive())
{
waitingForData();
}
else
{
m_log.write<Logging::WARN_LOG>("handleRead - m_async_io no longer active");
logoff();
}
}
/**
* @brief Handle Telnet Options in incoming data
* raw data is read in from socket
* m_parsed_data is filled with parsed out options.
*/
void Session::handleTeloptCodes()
{
m_log.setUserInfo(m_node_number);
std::string incoming_data = "";
if (m_in_data_vector.size() == 0)
{
return;
}
for(auto c : m_in_data_vector)
{
try
{
// Enter the Telnet_State and handle parsing options.
unsigned char ch = m_telnet_decoder->telnetOptionParse(c);
// Skip any incoming nulls, nulls are also return on Telnet options received
// So we know that there is no valid text data to send to the client.
if(ch == '\0')
{
continue;
}
// Incoming Buffer is filled and Telnet options are parsed out.
incoming_data += ch;
}
catch(std::exception& e)
{
m_log.write<Logging::ERROR_LOG>("Exception telnet_process_char", e.what(), __LINE__, __FILE__);
}
}
// Encode all incoming data as UTF8 unless we are not utf8
// Updated, append Incoming to Parsed Data so we don't miss double ESC Sequences.
// This catches items still in Buffer where sequences happen quickly. like Double ESC's.
if(m_encoding != Encoding::ENCODE_UTF8)
{
m_parsed_data += Encoding::getInstance().utf8Encode(incoming_data);
}
else
{
m_parsed_data += incoming_data;
}
}
/**
* @brief User Logoff
*/
void Session::logoff()
{
m_log.setUserInfo(m_node_number);
// This might be doing double the deallocation
if(m_async_io && m_async_io->getSocketHandle()->isActive())
{
try
{
m_log.write<Logging::INFO_LOG>("m_async_io->getSocketHandle() is ACTIVE! -> INACTIVE", __LINE__, __FILE__);
m_async_io->getSocketHandle()->setInactive();
}
catch(std::exception &e)
{
// Sometime this doesn't close when it's already existed, just extra checking here.
m_log.write<Logging::ERROR_LOG>("Exception connection shutdown()", e.what(), __LINE__, __FILE__);
}
}
session_manager_ptr session_manager = m_session_manager.lock();
if(session_manager)
{
m_is_leaving = true;
m_log.write<Logging::INFO_LOG>("Logoff Session Manager", __LINE__, __FILE__);
session_manager->leave(shared_from_this());
session_manager.reset();
}
}
/**
* @brief Shutdown Socket Connections by User Rquests / Logoff.
*/
void Session::disconnectUser()
{
m_log.setUserInfo(m_node_number);
m_async_io->getSocketHandle()->disconnectUser();
}