-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboost_server_framework.hh
192 lines (172 loc) · 6.01 KB
/
boost_server_framework.hh
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
// Desctiption:
// This program's a framework of sync mode server to having some children sessions which are written in separated classes.
// At the bottom of thie file, you can see a sample code for using this program.
//
// Author : Masaru Shimizu
// E-Mail : [email protected]
// Date : 4.2015
//
#ifndef BSTSVRFRMWK_HH
#define BSTSVRFRMWK_HH
/*
Realized points of this server framework.
a. Server function
b. Separated Server Framework and Child Session
c. Realized the separation with template of C++
*/
#include <set>
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
using boost::asio::ip::tcp;
//////////////////////////////////////////////////////////////////
// Server_Framework
template <typename Child_Session> class Server_Framework
{
//////////////////////////////////////////////////////////////////
// Server_Framework.Variables
private:
public:
gazebo::physics::WorldPtr _world;
boost::asio::io_service _ioservice;
boost::asio::ip::tcp::acceptor _acceptor;
std::set<Child_Session*> _Child_Session_list;
Child_Session *_new_Child_Sessionp;
boost::thread _thread;
//////////////////////////////////////////////////////////////////
// Server_Framework.Make_A_Child_Session_and_Accept_Loop
void Make_A_Child_Session_and_Accept_Loop(void)
{
while(1)
{
_new_Child_Sessionp = new Child_Session(*this);
_Child_Session_list.insert(_new_Child_Sessionp);
//std::cout << "Made a child session [" << _new_Child_Sessionp<<"]"<<std::endl;
boost::system::error_code err;
_acceptor.accept(_new_Child_Sessionp->_socket, err);
//std::cout << "After accept\n";
_new_Child_Sessionp->_thread = boost::thread(
boost::bind(&Child_Session::Accept_Process
, _new_Child_Sessionp));
//std::cout << "After thread\n";
}
}
//////////////////////////////////////////////////////////////////
// Server_Framework.Remove_Child_Session
void Remove_Child_Session(Child_Session *Child_Sessionp)
{
delete Child_Sessionp;
_Child_Session_list.erase(Child_Sessionp);
}
//////////////////////////////////////////////////////////////////
// Server_Framework.Constractor
Server_Framework(unsigned short port
, gazebo::physics::WorldPtr world) try
:_acceptor(_ioservice
, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port))
, _new_Child_Sessionp(NULL)
{
_world = world;
_thread = boost::thread(
boost::bind(&Server_Framework::Make_A_Child_Session_and_Accept_Loop
, this));
}
catch(std::exception const &err)
{
std::cerr << err.what() << std::endl;
}
//////////////////////////////////////////////////////////////////
// Server_Framework.Destractor
~Server_Framework()
{
_thread.join();
for(typename std::set<Child_Session*>::iterator
i = _Child_Session_list.begin(); i != _Child_Session_list.end(); i++)
delete *i;
}
};
#ifdef __SAMPLE_MAIN__
/*
# How to compile this sample code.
gcc -o a boost_server.cc -D__SAMPLE_MAIN__ -lboost_system-mt -lboost_thread-mt
# Realized points of this server framework.
a. Server function
b. Separated Server Framework and Child Session
c. Realized the separation with template of C++
*/
//////////////////////////////////////////////////////////////////
// Sample_Child_Session :
// COPY THIS CLASS and MODIFY to create your own child_session
struct Sample_Child_Session
{
//////////////////////////////////////////////////////////////////
// Sample_Child_Session.Variables
Server_Framework<Sample_Child_Session> &_parent;
boost::asio::io_service _ioservice;
boost::asio::ip::tcp::socket _socket;
boost::asio::streambuf _buffer;
boost::thread _thread;
// Add your own variables here
//////////////////////////////////////////////////////////////////
// Sample_Child_Session.Constructor
Sample_Child_Session(Server_Framework<Sample_Child_Session>&parent)
: _parent(parent), _socket(_ioservice) {}
// CAUTION:_socket needs local member _ioservice to be initialized,
// not by parent._ioservice.
// _parent is for using _parent.Remove_Child_Session()
//////////////////////////////////////////////////////////////////
// Sample_Child_Session.Child_Session_Loop_Core
void Child_Session_Loop_Core(void)
{
boost::system::error_code err;
//std::cout << "Child_Session_Loop a [" << this << "]" << std::endl;
boost::asio::read_until(_socket, _buffer , "\r\n", err);
//std::cout << "Child_Session_Loop b [" << this << "]" << std::endl;
// SAMPLE CODE : Sendback received data for debug
std::istream is(&_buffer);
std::string line;
for(;1;)
{
std::getline(is, line);
if(0 < line.length())
{
// THIS IS WRONG => boost::asio::write(_socket, line);
// SAMPLE CODE : Display received data for debug
// std::cout << line.c_str() << std::endl;
}
else
break;
}
// boost::asio::write(_socket, _buffer);
//
//std::cout << "Child_Session_Loop c [" << this << "]" << std::endl;
//std::cout << "Child_Session_Loop d [" << this << "]" << std::endl;
}
//////////////////////////////////////////////////////////////////
// Sample_Child_Session.Accept_Process
void Accept_Process(void)
{
//std::cout << "Accept_Process a [" << this << "]" << std::endl;
/* SAMPLE CODE : Sendback Accepted Acknowledgment for debug
boost::asio::streambuf ack_comment;
std::iostream st(&ack_comment);
st << "+ -- Accepted [" << this << "]" << std::endl;;
boost::asio::write(_socket, ack_comment);
*/
//std::cout << "Accept_Process b [" << this << "]" << std::endl;
while(1)
Child_Session_Loop_Core();
//std::cout << "Accept_Process c [" << this << "]" << std::endl;
}
// Add your own functions here
};
int main(void)
{
Server_Framework<Sample_Child_Session> sf(3000);
while(1)
sleep(10);
return 0;
}
#endif
#endif