-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.cpp
107 lines (98 loc) · 3.15 KB
/
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
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
// Threading modeled after https://github.com/UCLA-CS130/AAAAA/blob/master/server.cpp
#include <ctime>
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include "server.h"
const int low_invalid_port = 1023;
const int high_invalid_port = 65536;
std::string make_daytime_string()
{
using namespace std; // For time_t, time and ctime;
time_t now = time(0);
return ctime(&now);
}
namespace Team15 {
namespace server {
server::server(const std::string& address, const std::string& port, const NginxConfig& config):
io_service_(),socket_(io_service_), acceptor_(io_service_),requestMgr_(config){
if (!this->is_valid(address, port)) {
fprintf(stderr, "Error: Invalid port input");
exit(1);
}
boost::asio::ip::tcp::resolver resolver(io_service_);
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve({address, port});
acceptor_.open(endpoint.protocol());
acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
acceptor_.bind(endpoint);
acceptor_.listen();
start_accepting();
}
bool server::is_valid(const std::string& address, const std::string& port) {
int port_num = atoi(port.c_str());
if (port == "") {
printf("Empty port input.\n");
return false;
}
if (!(port_num > low_invalid_port && port_num < high_invalid_port)) {
printf("Invalid port input.\n");
return false;
}
return true;
}
void server::start_accepting() {
std::cout << "Accepting connections." << std::endl;
acceptor_.async_accept(socket_,
[this](boost::system::error_code ec)
{
if (!acceptor_.is_open()) { std::cerr << "Failed to open" << std::endl; return; }
if (!ec) {
connection* c_p = new connection(std::move(socket_),this);
connections_.insert(c_p);
std::cout << "Socket opened, Starting connection." << std::endl;
c_p->start();
}
else {
std::cerr << "Found error" << std::endl;
}
start_accepting();
});
}
void server::run() {
// create thread pool
std::vector<boost::shared_ptr<boost::thread>> threads;
for (std::size_t i = 0; i < num_threads_; i++) {
// run io_service for async operations in each thread
boost::shared_ptr<boost::thread> thread(new boost::thread(
boost::bind(&boost::asio::io_service::run, &io_service_)));
threads.push_back(thread);
}
// wait for all threads to exit
for (std::size_t i = 0; i < threads.size(); i++) {
threads[i]->join();
}
}
void server::connection_done(connection* connection) {
connections_.erase(connection);
connection->stop();
free(connection);
}
server::~server() {
for (auto connection : connections_) {
connection->stop();
free(connection);
}
}
boost::asio::io_service& server::getService() {
return io_service_;
}
// const std::vector<requestconfig>& server::getRequestConfigVector() const {
// return requestConfigVector_;
//}
}
}