-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSession.cpp
More file actions
165 lines (145 loc) · 4.46 KB
/
Session.cpp
File metadata and controls
165 lines (145 loc) · 4.46 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
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
/*
* Session class - This represents a connection session between a client and the server
* Author: Adam Waggoner, Jaden Holladay, Logan Ropelato, Gray Marchese
*/
#include <cstdlib>
#include <iostream>
#include <memory>
#include <utility>
#include <vector>
#include <boost/asio.hpp>
#include <boost/regex.hpp>
#include "RegexUtils.h"
#include "Session.h"
#include "SpreadsheetManager.h"
int Session::_currentID = 0;
using boost::asio::ip::tcp;
Session::Session(tcp::socket socket)
: socket_(std::move(socket))
{
_currentSpreadsheet = NULL;
_ID = _currentID++;
}
void Session::Start()
{
DoRead();
}
// Reads a message from the client.
void Session::DoRead()
{
auto self(shared_from_this());
socket_.async_read_some(boost::asio::buffer(data_, max_length),
[this, self](boost::system::error_code ec, std::size_t BytesRead)
{
if (!ec)
{
// Ignore empty messages
if(BytesRead == 0 || (BytesRead == 1 && data_[0] == '\n'))
{
DoRead();
return;
}
data_[BytesRead] = '\0';
// Split incoming messages on newline character.
std::vector<std::string> messages = RegexUtils::Split(buffer_ + data_, '\n');
buffer_ = "";
// If we've received an incomplete message
if(data_[BytesRead-1] != '\n')
{
// Fill the buffer with the partial message
buffer_ = messages[messages.size()-1].c_str();
messages.pop_back();
}
// Iterate through received messages.
std::vector<std::string>::iterator itr = messages.begin();
while(itr != messages.end())
{
// std::cout << "Message Received: " << *itr << std::endl;
// Get a list of each field in the message (delimited by tabs)
std::vector<std::string> msg = RegexUtils::Split(*itr, '\t');
// If a connect message is received, we have to connect to a specific spreadsheet
// Format Connect\tSheetName
// Check for filename size < 255
if (msg[0] == "Connect" && msg.size() == 2 && msg[1] != "" && RegexUtils::IsValidFilename(msg[1]))
{
if(_currentSpreadsheet != NULL)
{
_currentSpreadsheet->UnsubscribeSession(_ID);
}
_currentSpreadsheet = SpreadsheetManager::GetInstance()->GetSpreadsheet(msg[1]);
_currentSpreadsheet->SubscribeSession(_ID, this);
}
else if(_currentSpreadsheet != NULL)
{
// If the message is corrupt of malformed, disconnect that user
if(!_currentSpreadsheet->ReceiveMessage(_ID, *itr))
{
_closeSocket(boost::system::errc::make_error_code(boost::system::errc::permission_denied));
}
}
else
{
_closeSocket(boost::system::errc::make_error_code(boost::system::errc::permission_denied));
}
++itr;
}
// Continue the read loop
DoRead();
}
else
{
// If an error is caught we close the socket
_closeSocket(ec);
}
});
}
// Asynchronously sends a message to the client in this session
void Session::DoWrite(std::string message)
{
auto self(shared_from_this());
// std::cout << "Message Sent: " << (message + "\n") << std::endl;
boost::asio::async_write(socket_, boost::asio::buffer(message + "\n", (message + "\n").length()),
[this, self](boost::system::error_code ec, std::size_t /*length*/)
{
if (ec)
{
_closeSocket(ec);
}
});
}
void Session::_closeSocket(boost::system::error_code ec)
{
// Remove from spreadsheets and disconnect socket.
try
{
std::string error;
std:: string eof1 ("End of file");
std:: string eof2 ("Connection reset by peer");
error = ec.message();
// std::cout<<"Error Message:" << error <<'\n';
if (ec)
{
// Remove from spreadsheets and disconnect socket.
if(socket_.is_open())
{
if (_currentSpreadsheet != NULL)
{
_currentSpreadsheet->UnsubscribeSession(_ID);
_currentSpreadsheet = NULL;
}
socket_.shutdown(socket_.shutdown_both);
socket_.close();
// std::cout << "Safely closed socket after client disconnect within try catch." << std::endl;
}
}
}
catch(boost::system::system_error)
{
if (_currentSpreadsheet != NULL)
{
_currentSpreadsheet->UnsubscribeSession(_ID);
_currentSpreadsheet = NULL;
}
// std:: cout << "Caught the system error: Connection has been closed. " << '\n';
}
}