-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cpp
71 lines (56 loc) · 1.49 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
#include "server.h"
#include <iostream>
#include "database.h"
#include "Config.h"
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include "databaseconfig.h"
#include <vector>
#include <algorithm>
namespace ba = boost::asio;
Server::Server() : m_dbConfig(),
m_database(m_dbConfig),
m_config(m_database),
m_service(),
m_acceptor(m_service, ba::ip::tcp::endpoint(ba::ip::tcp::v4(), m_config.listen_port()))
{
m_config.read();
}
Server::~Server()
{
}
const Config& Server::config()
{
return m_config;
}
Database& Server::database()
{
return m_database;
}
boost::asio::io_service& Server::service()
{
return m_service;
}
void Server::run()
{
listen();
m_service.run();
}
void Server::listen()
{
Connection::pointer connection = Connection::create(*this);
m_acceptor.async_accept(connection->socket(),
boost::bind(&Server::accept, this, connection,
ba::placeholders::error));
}
void Server::accept(Connection::pointer connection, const boost::system::error_code& error)
{
std::string ip = connection->socket().remote_endpoint().address().to_v4().to_string();
std::cout << "-----------------------------------------\n";
std::cout << "New request coming from " << ip << std::endl;
if (!error)
connection->start();
else
std::cout << "Server accept error : " << error.message() << std::endl;
listen();
}