Skip to content

Commit 1ea4052

Browse files
committed
mild cleanup of source
1 parent 77a7892 commit 1ea4052

File tree

6 files changed

+137
-139
lines changed

6 files changed

+137
-139
lines changed

CPP/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ all: $(BINS)
1616
$(BINS): %: $(BINDIR)/%
1717

1818
$(OUTBINS): $(BINDIR)/%: src/%.cc gen/msg.pb.cc src/asio_server.h | $(BINDIR)
19-
clang++ $(CPP_FLAGS) src/$*.cc gen/msg.pb.cc -o bin/$* $(CPP_LIBS)
19+
g++ $(CPP_FLAGS) src/$*.cc gen/msg.pb.cc -o bin/$* $(CPP_LIBS)
2020

2121
$(BINDIR):
2222
mkdir -p $(BINDIR)

CPP/src/asio_server.h

+30-31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
2-
This seemed a sufficiently common pattern so as to be worth abstracting.
3-
2+
This seemed a sufficiently common pattern so as to be worth abstracting. Basically the simplest sensible server and handler, where the server dispatches a new handler per connection. Using this class makes setting up a server almost as easy as in Python: just defining a handle() method and inheriting appropriately.
43
*/
54

65
#ifndef ASIO_SERVER_H
@@ -18,56 +17,56 @@ This seemed a sufficiently common pattern so as to be worth abstracting.
1817
/*
1918
class handler: public asio_handler<handler> {
2019
public:
21-
handler(boost::asio::io_service& io_service) : asio_handler(io_service) {}
22-
void handle() {
23-
// do something like read from socket_
24-
}
20+
handler(boost::asio::io_service& io_service) : asio_handler(io_service) {}
21+
void handle() {
22+
// do something like read from socket_
23+
}
2524
}
2625
2726
*/
2827
template<typename T>
2928
class asio_handler : public std::enable_shared_from_this<T> {
3029
public:
31-
asio_handler(boost::asio::io_service& io_service)
30+
asio_handler(boost::asio::io_service& io_service)
3231
: socket_(io_service) {}
3332

3433
boost::asio::local::stream_protocol::socket& socket() {
35-
return socket_;
34+
return socket_;
3635
}
3736

38-
void handle() {
39-
40-
}
41-
37+
void handle() {
38+
39+
}
40+
4241
protected:
43-
boost::asio::local::stream_protocol::socket socket_;
42+
boost::asio::local::stream_protocol::socket socket_;
4443
};
4544

4645

4746
// HANDLER must have a constructor with a 'boost::asio::io_service&' as its sole parameter,
4847
// must implement a 'handle' fn taking no parameters.
4948
// and must implement a 'socket' fn taking no parameters and returning a 'boost::asio::local::stream_protocol::socket'
5049
// this being the socket it wants to talk on.
51-
// In practice, just derive from asio_handler.
50+
// In practice, just ensure HANDLER derives from asio_handler.
5251
template<typename HANDLER>
5352
class asio_server {
54-
public:
55-
boost::asio::io_service& io_service_;
56-
boost::asio::local::stream_protocol::acceptor acceptor_;
57-
58-
asio_server(boost::asio::io_service& io_service, boost::asio::local::stream_protocol::endpoint endpoint)
59-
: io_service_(io_service), acceptor_(io_service, endpoint) {
60-
auto the_session = std::make_shared<HANDLER>(io_service_);
61-
acceptor_.async_accept(the_session->socket(), std::bind(&asio_server::handle_accept, this, the_session, std::placeholders::_1));
62-
}
63-
64-
void handle_accept(std::shared_ptr<HANDLER> session, const boost::system::error_code& error) {
65-
if(!error)
66-
session->handle();
67-
68-
session.reset(new HANDLER(io_service_));
69-
acceptor_.async_accept(session->socket(), std::bind(&asio_server::handle_accept, this, session, std::placeholders::_1));
70-
}
53+
public:
54+
boost::asio::io_service& io_service_;
55+
boost::asio::local::stream_protocol::acceptor acceptor_;
56+
57+
asio_server(boost::asio::io_service& io_service, boost::asio::local::stream_protocol::endpoint endpoint)
58+
: io_service_(io_service), acceptor_(io_service, endpoint) {
59+
auto the_session = std::make_shared<HANDLER>(io_service_);
60+
acceptor_.async_accept(the_session->socket(), std::bind(&asio_server::handle_accept, this, the_session, std::placeholders::_1));
61+
}
62+
63+
void handle_accept(std::shared_ptr<HANDLER> session, const boost::system::error_code& error) {
64+
if(!error)
65+
session->handle();
66+
67+
session.reset(new HANDLER(io_service_));
68+
acceptor_.async_accept(session->socket(), std::bind(&asio_server::handle_accept, this, session, std::placeholders::_1));
69+
}
7170
};
7271

7372

CPP/src/client_sync.cc

+24-24
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,28 @@ using namespace std;
88
#define ADDRESS "./socket"
99

1010
int main() {
11-
try {
12-
boost::asio::io_service io_service;
13-
boost::asio::local::stream_protocol::socket socket(io_service);
14-
15-
socket.connect(boost::asio::local::stream_protocol::endpoint(ADDRESS));
16-
17-
example_mess::Boring msg;
18-
msg.set_cont("Greetings from CPP-land!");
19-
20-
21-
boost::asio::streambuf sbuf;
22-
ostream serialized(&sbuf);
23-
msg.SerializeToOstream(&serialized);
24-
25-
uint32_t message_length = sbuf.size();
26-
27-
message_length = htonl(message_length); // endianness!
28-
29-
boost::asio::write(socket, boost::asio::buffer(&message_length, 4));
30-
boost::asio::write(socket, sbuf);
31-
}
32-
catch(exception& e) {
33-
cout << e.what() << endl;
34-
}
11+
try {
12+
boost::asio::io_service io_service;
13+
boost::asio::local::stream_protocol::socket socket(io_service);
14+
15+
socket.connect(boost::asio::local::stream_protocol::endpoint(ADDRESS));
16+
17+
example_mess::Boring msg;
18+
msg.set_cont("Greetings from CPP-land!");
19+
20+
21+
boost::asio::streambuf sbuf;
22+
ostream serialized(&sbuf);
23+
msg.SerializeToOstream(&serialized);
24+
25+
uint32_t message_length = sbuf.size();
26+
27+
message_length = htonl(message_length); // endianness!
28+
29+
boost::asio::write(socket, boost::asio::buffer(&message_length, 4));
30+
boost::asio::write(socket, sbuf);
31+
}
32+
catch(exception& e) {
33+
cout << e.what() << endl;
34+
}
3535
}

CPP/src/server_async.cc

+41-43
Original file line numberDiff line numberDiff line change
@@ -15,60 +15,58 @@ using boost::asio::local::stream_protocol;
1515

1616
class handler: public asio_handler<handler> {
1717
public:
18-
handler(boost::asio::io_service& io_service) : asio_handler(io_service) {}
18+
handler(boost::asio::io_service& io_service) : asio_handler(io_service) {}
1919

20-
void handle_initial_read(const boost::system::error_code& error, size_t bytes_transferred) {
21-
if(error) {cerr << error.message() << endl; return;}
20+
void handle_initial_read(const boost::system::error_code& error, size_t bytes_transferred) {
21+
if(error) {cerr << error.message() << endl; return;}
2222

23-
message_length = ntohl(message_length);
24-
25-
cout << message_length << endl;
26-
27-
message.resize(message_length);
28-
boost::asio::async_read(socket_, boost::asio::buffer(message),
29-
bind(&handler::handle_message, shared_from_this(), std::placeholders::_1));
30-
}
23+
message_length = ntohl(message_length);
24+
25+
cout << message_length << endl;
26+
27+
message.resize(message_length);
28+
boost::asio::async_read(socket_, boost::asio::buffer(message),
29+
bind(&handler::handle_message, shared_from_this(), std::placeholders::_1));
30+
}
31+
32+
void handle_message(const boost::system::error_code& error) {
33+
if(error) {cerr << error.message() << endl; return;}
3134

32-
void handle_message(const boost::system::error_code& error) {
33-
if(error) {cerr << error.message() << endl; return;}
34-
35-
example_mess::Boring msg;
36-
bool r = msg.ParseFromArray(message.data(), message.size());
37-
if(!r) {
38-
cerr << "Failed parsing" << endl;
39-
exit(1);
40-
}
41-
cout << "Message: " << msg.cont() << endl;
35+
example_mess::Boring msg;
36+
bool r = msg.ParseFromArray(message.data(), message.size());
37+
if(!r) {
38+
cerr << "Failed parsing" << endl;
39+
exit(1);
4240
}
41+
cout << "Message: " << msg.cont() << endl;
42+
}
4343

44-
void handle() {
45-
auto handler = bind(&handler::handle_initial_read, shared_from_this(), std::placeholders::_1, std::placeholders::_2);
46-
47-
boost::asio::async_read(socket_, boost::asio::buffer(&message_length, 4), handler);
48-
}
44+
void handle() {
45+
auto handler = bind(&handler::handle_initial_read, shared_from_this(), std::placeholders::_1, std::placeholders::_2);
46+
47+
boost::asio::async_read(socket_, boost::asio::buffer(&message_length, 4), handler);
48+
}
4949

5050
private:
51-
vector<uint8_t> message;
52-
53-
uint32_t message_length = 0;
51+
vector<uint8_t> message;
52+
53+
uint32_t message_length = 0;
5454
};
5555

5656

5757

58-
58+
5959

6060
int main() {
61-
try {
62-
boost::asio::io_service io_service;
63-
unlink(ADDRESS);
64-
stream_protocol::endpoint ep(ADDRESS);
65-
asio_server<handler> s(io_service, ep);
66-
67-
io_service.run();
68-
69-
} catch (exception& e) {
70-
cerr << e.what() << endl;
71-
}
72-
73-
return 0;
61+
try {
62+
boost::asio::io_service io_service;
63+
unlink(ADDRESS);
64+
stream_protocol::endpoint ep(ADDRESS);
65+
asio_server<handler> s(io_service, ep);
66+
io_service.run();
67+
} catch (exception& e) {
68+
cerr << e.what() << endl;
69+
}
70+
71+
return 0;
7472
}

CPP/src/server_sync.cc

+38-36
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,47 @@ using namespace std;
1515

1616
class handler: public asio_handler<handler> {
1717
public:
18-
handler(boost::asio::io_service& io_service) : asio_handler(io_service) {}
19-
20-
void handle() {
21-
vector<uint8_t> message;
22-
23-
uint32_t message_length = 0;
24-
25-
boost::asio::read(socket_, boost::asio::buffer(&message_length, 4));
26-
message_length = ntohl(message_length);
27-
28-
cout << message_length << endl;
29-
30-
message.resize(message_length);
31-
boost::asio::read(socket_, boost::asio::buffer(message));
32-
33-
example_mess::Boring msg;
34-
bool r = msg.ParseFromArray(message.data(), message.size());
35-
if(!r) {
36-
cerr << "Failed parsing" << endl;
37-
exit(1);
38-
}
39-
cout << "Message: " << msg.cont() << endl;
40-
}
18+
handler(boost::asio::io_service& io_service) : asio_handler(io_service) {}
19+
20+
void handle() {
21+
vector<uint8_t> message;
22+
23+
uint32_t message_length = 0;
24+
25+
// Read header
26+
boost::asio::read(socket_, boost::asio::buffer(&message_length, 4));
27+
message_length = ntohl(message_length);
28+
29+
cout << message_length << endl;
30+
31+
// Read message
32+
message.resize(message_length);
33+
boost::asio::read(socket_, boost::asio::buffer(message));
34+
35+
example_mess::Boring msg;
36+
bool r = msg.ParseFromArray(message.data(), message.size());
37+
if(!r) {
38+
cerr << "Failed parsing" << endl;
39+
exit(1);
40+
}
41+
cout << "Message: " << msg.cont() << endl;
42+
}
4143
};
4244

4345

4446

4547
int main() {
46-
try {
47-
boost::asio::io_service io_service;
48-
unlink(ADDRESS);
49-
boost::asio::local::stream_protocol::endpoint ep(ADDRESS);
50-
asio_server<handler> s(io_service, ep);
51-
52-
io_service.run();
53-
54-
} catch (exception& e) {
55-
cerr << e.what() << endl;
56-
}
57-
58-
return 0;
48+
try {
49+
boost::asio::io_service io_service;
50+
unlink(ADDRESS);
51+
boost::asio::local::stream_protocol::endpoint ep(ADDRESS);
52+
asio_server<handler> s(io_service, ep);
53+
54+
io_service.run();
55+
56+
} catch (exception& e) {
57+
cerr << e.what() << endl;
58+
}
59+
60+
return 0;
5961
}

Python/client.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66
address = './socket'
77

88

9-
109
msg = msg_pb2.Boring()
11-
msg.cont = "awww yisss!"
10+
msg.cont = "A very Pythonic hello to you!"
1211
encoded = msg.SerializeToString()
1312

1413
try:
1514
sock.connect(address)
1615
except socket.error as e:
17-
print(e)
18-
exit(1)
16+
print(e)
17+
exit(1)
1918

2019
try:
2120
x = pack('>I', len(encoded))

0 commit comments

Comments
 (0)