-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.cpp
More file actions
65 lines (51 loc) · 1.79 KB
/
Server.cpp
File metadata and controls
65 lines (51 loc) · 1.79 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
#include "Server.h"
namespace securityServer {
using boost::asio::ip::tcp;
Server::Server(boost::asio::io_service& io_service,
const tcp::endpoint& endpoint) :
io_service_(io_service), acceptor_(io_service, endpoint) {
remote_station_ptr newRemoteStation(new RemoteStation(io_service));
acceptor_.async_accept(newRemoteStation->getSocket(),
boost::bind(&Server::handle_accept, this, newRemoteStation,
boost::asio::placeholders::error));
}
void Server::handle_accept(remote_station_ptr remoteStation,
boost::system::error_code& error) {
// read header
size_t header;
boost::asio::read(remoteStation->getSocket(),
boost::asio::buffer(&header, sizeof(header)));
std::cout << "body is " << header << " bytes" << std::endl;
// read body
boost::asio::streambuf buf;
const size_t rc = boost::asio::read(remoteStation->getSocket(),
buf.prepare(header));
buf.commit(header);
std::cout << "read " << rc << " bytes" << std::endl;
// deserialize
std::istream is(&buf);
boost::archive::text_iarchive ar(is);
securityClient::Packet packet;
ar & packet;
if(remote_station_map_.find(packet.client_id_)!=std::map::end)//if the id of new client
{
remote_station_map_[packet.client_id_]=remoteStation;
remoteStation->startListening();
}//if the id already exist something is wrong
else
{
remoteStation->~RemoteStation();
}
// remote_station_ptr newRemoteStation(new RemoteStation(io_service));
// acceptor_.async_accept(newRemoteStation->getSocket(),
// boost::bind(&Server::handle_accept, this, newRemoteStation,
// boost::asio::placeholders::error));
}
void Server::sendTo(int remote_station_id, securityClient::Packet& packet)
{
remote_station_map_[remote_station_id]->send(packet);
}
Server::~Server() {
// TODO Auto-generated destructor stub
}
} /* namespace securityServer */