Skip to content

Commit

Permalink
add boost asio
Browse files Browse the repository at this point in the history
  • Loading branch information
ggeorgiev committed Jun 11, 2016
1 parent 39e4bcc commit 20d8d01
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 4 deletions.
3 changes: 2 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ clang++ -D NDEBUG -I src/ -O3 -c src/parser/dbs/dbs_parser.cpp \
if [ ! -e build/release/src/rpc/client/ ]; then mkdir build/release/src/rpc/client/; fi
echo Compile src/rpc/client/client.cpp
clang++ -D NDEBUG -I src/ -O3 -c src/rpc/client/client.cpp \
-isystem protobuf/include/ -isystem src/system/ \
-isystem /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/ \
-isystem boost/include/ -isystem protobuf/include/ -isystem src/system/ \
-o build/release/src/rpc/client/client.cpp.o -std=c++14 &
echo Compile src/rpc/rpc.pb.cc
clang++ -D NDEBUG -I src/ -O3 -c src/rpc/rpc.pb.cc -isystem protobuf/include/ \
Expand Down
5 changes: 4 additions & 1 deletion prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ then

git submodule update --init tools/build || exit 1
git submodule update --init tools/inspect || exit 1

git submodule update --init libs/align || exit 1
git submodule update --init libs/algorithm || exit 1
git submodule update --init libs/array || exit 1
git submodule update --init libs/asio || exit 1
git submodule update --init libs/assert || exit 1
git submodule update --init libs/atomic || exit 1
git submodule update --init libs/bind || exit 1
Expand All @@ -93,7 +96,6 @@ then
git submodule update --init libs/filesystem || exit 1
git submodule update --init libs/function || exit 1
git submodule update --init libs/functional || exit 1
git submodule update --init libs/fusion || exit 1
git submodule update --init libs/hana || exit 1
git submodule update --init libs/heap || exit 1
git submodule update --init libs/integer || exit 1
Expand All @@ -111,6 +113,7 @@ then
git submodule update --init libs/preprocessor || exit 1
git submodule update --init libs/range || exit 1
git submodule update --init libs/ratio || exit 1
git submodule update --init libs/regex || exit 1
git submodule update --init libs/throw_exception || exit 1
git submodule update --init libs/thread || exit 1
git submodule update --init libs/tuple || exit 1
Expand Down
4 changes: 3 additions & 1 deletion src/3rdparty/boost.dbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ cxx_library /boost_template
@visibility=public
@directory=../../boost/include:

boost/asio.hpp
boost/crc.hpp
boost/functional/hash.hpp
boost/functional/hash/hash.hpp
Expand All @@ -15,9 +16,10 @@ cxx_library /boost_template
boost/hana.hpp
boost/hana/ext/std/tuple.hpp
boost/hana/for_each.hpp
boost/regex_fwd.hpp
boost/type_index.hpp
boost/variant/variant.hpp
boost/variant/get.hpp
boost/type_index.hpp
;

cxx_header
Expand Down
67 changes: 66 additions & 1 deletion src/rpc/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,72 @@
//

#include "rpc/client/client.h"
#include <boost/asio.hpp>
#include <cstdlib>
#include <iostream>
#include <thread>
#include <utility>

namespace rpc1
namespace rpc
{
using boost::asio::ip::tcp;

const int max_length = 1024;

void session(tcp::socket sock)
{
try
{
for (;;)
{
char data[max_length];

boost::system::error_code error;
size_t length = sock.read_some(boost::asio::buffer(data), error);
if (error == boost::asio::error::eof)
break; // Connection closed cleanly by peer.
else if (error)
throw boost::system::system_error(error); // Some other error.

boost::asio::write(sock, boost::asio::buffer(data, length));
}
}
catch (std::exception& e)
{
std::cerr << "Exception in thread: " << e.what() << "\n";
}
}

void server(boost::asio::io_service& io_service, unsigned short port)
{
tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port));
for (;;)
{
tcp::socket sock(io_service);
a.accept(sock);
std::thread(session, std::move(sock)).detach();
}
}

int main(int argc, char* argv[])
{
try
{
if (argc != 2)
{
std::cerr << "Usage: blocking_tcp_echo_server <port>\n";
return 1;
}

boost::asio::io_service io_service;

server(io_service, std::atoi(argv[1]));
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}

return 0;
}
}
25 changes: 25 additions & 0 deletions src/rpc/rpc.dbs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,31 @@ cxx_library rpc_client
;

cxx_library:
/cstd
/boost_template

rpc
;
;

cxx_library rpc_server
@type=user:

cxx_header
@visibility=public
@directory=..:

rpc/server/server.h
;

cxx_file:
server/server.cpp
;

cxx_library:
/cstd
/boost_template

rpc
;
;
13 changes: 13 additions & 0 deletions src/rpc/server/server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright © 2016 George Georgiev. All rights reserved.
//

#include "rpc/server/server.h"
#include <boost/asio.hpp>
#include <cstdlib>
#include <iostream>
#include <thread>
#include <utility>

namespace rpc
{
}
8 changes: 8 additions & 0 deletions src/rpc/server/server.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright © 2016 George Georgiev. All rights reserved.
//

#pragma once

namespace rpc
{
}
1 change: 1 addition & 0 deletions src/system/cstd.dbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ cxx_library /cstd
cstddef
cstdio
cstdint
cstdlib
stddef.h
stdio.h
stdlib.h
Expand Down
16 changes: 16 additions & 0 deletions xcode/dbs-build.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@
5E8079AC1D093CF900BB6C1C /* hex.h in Headers */ = {isa = PBXBuildFile; fileRef = 5E8079AB1D093CF900BB6C1C /* hex.h */; };
5E8079AE1D09CF8B00BB6C1C /* hex-utest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5E8079AD1D09CF8B00BB6C1C /* hex-utest.cpp */; };
5E8079B01D09D18800BB6C1C /* hex.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5E8079AF1D09D18800BB6C1C /* hex.cpp */; };
5E8079B61D0C693F00BB6C1C /* server.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5E8079B41D0C693F00BB6C1C /* server.cpp */; };
5E8079B71D0C693F00BB6C1C /* server.h in Headers */ = {isa = PBXBuildFile; fileRef = 5E8079B51D0C693F00BB6C1C /* server.h */; };
5E83145F1CDC5A7F00344DB5 /* framework.h in Headers */ = {isa = PBXBuildFile; fileRef = 5E83145E1CDC5A7F00344DB5 /* framework.h */; };
5E874FAA1CEC175F00C7C123 /* tool_command.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5E874FA81CEC175F00C7C123 /* tool_command.cpp */; };
5E874FAB1CEC175F00C7C123 /* tool_command.h in Headers */ = {isa = PBXBuildFile; fileRef = 5E874FA91CEC175F00C7C123 /* tool_command.h */; };
Expand Down Expand Up @@ -395,6 +397,8 @@
5E8079AB1D093CF900BB6C1C /* hex.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = hex.h; sourceTree = "<group>"; };
5E8079AD1D09CF8B00BB6C1C /* hex-utest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = "hex-utest.cpp"; sourceTree = "<group>"; };
5E8079AF1D09D18800BB6C1C /* hex.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = hex.cpp; sourceTree = "<group>"; };
5E8079B41D0C693F00BB6C1C /* server.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = server.cpp; sourceTree = "<group>"; };
5E8079B51D0C693F00BB6C1C /* server.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = server.h; sourceTree = "<group>"; };
5E83145E1CDC5A7F00344DB5 /* framework.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = framework.h; sourceTree = "<group>"; };
5E874FA81CEC175F00C7C123 /* tool_command.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = tool_command.cpp; sourceTree = "<group>"; };
5E874FA91CEC175F00C7C123 /* tool_command.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tool_command.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1123,6 +1127,7 @@
isa = PBXGroup;
children = (
5E0AEFC21D027FBD0094AD34 /* client */,
5E8079B31D0C686D00BB6C1C /* server */,
5E5A050D1CFA835D00DA4443 /* rpc.dbs */,
5E5A050E1CFA835D00DA4443 /* rpc.proto */,
);
Expand All @@ -1148,6 +1153,15 @@
path = logex/gtest;
sourceTree = "<group>";
};
5E8079B31D0C686D00BB6C1C /* server */ = {
isa = PBXGroup;
children = (
5E8079B41D0C693F00BB6C1C /* server.cpp */,
5E8079B51D0C693F00BB6C1C /* server.h */,
);
path = server;
sourceTree = "<group>";
};
5E874FA31CEC173D00C7C123 /* tool */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1316,6 +1330,7 @@
5E875BBB1CFAC7EB008AC423 /* element_manager.hpp in Headers */,
5E283C1A1CF23BC600852F24 /* element_manager.hpp in Headers */,
5E290B391CBAB92500F10210 /* tpool.h in Headers */,
5E8079B71D0C693F00BB6C1C /* server.h in Headers */,
5E10B3E51C70F272009C73E4 /* log.h in Headers */,
5E875BC81CFBBDE8008AC423 /* cxx_iwyu.h in Headers */,
5EABCABC1CE057FD00E79FFD /* sys_executable.h in Headers */,
Expand Down Expand Up @@ -1514,6 +1529,7 @@
5E290B2F1CB9F4FC00F10210 /* verbose-utest.cpp in Sources */,
5E217B681C713CDC00F082A1 /* cxx_program_crc_task.cpp in Sources */,
5E10B3D51C70F272009C73E4 /* err-utest.cpp in Sources */,
5E8079B61D0C693F00BB6C1C /* server.cpp in Sources */,
5EAE67781C72AFE4005B3F8D /* cxx_include_directory.cpp in Sources */,
5E10B4041C70F272009C73E4 /* priority-utest.cpp in Sources */,
5E5A050F1CFA835D00DA4443 /* rpc.proto in Sources */,
Expand Down

0 comments on commit 20d8d01

Please sign in to comment.