diff options
| author | Lorry Tar Creator <lorry-tar-importer@baserock.org> | 2013-06-25 22:59:01 +0000 |
|---|---|---|
| committer | <> | 2013-09-27 11:49:28 +0000 |
| commit | 8c4528713d907ee2cfd3bfcbbad272c749867f84 (patch) | |
| tree | c09e2ce80f47b90c85cc720f5139089ad9c8cfff /libs/asio/example/cpp03/buffers | |
| download | boost-tarball-baserock/morph.tar.gz | |
Imported from /home/lorry/working-area/delta_boost-tarball/boost_1_54_0.tar.bz2.boost_1_54_0baserock/morph
Diffstat (limited to 'libs/asio/example/cpp03/buffers')
| -rw-r--r-- | libs/asio/example/cpp03/buffers/Jamfile | 33 | ||||
| -rw-r--r-- | libs/asio/example/cpp03/buffers/Jamfile.v2 | 38 | ||||
| -rw-r--r-- | libs/asio/example/cpp03/buffers/reference_counted.cpp | 131 |
3 files changed, 202 insertions, 0 deletions
diff --git a/libs/asio/example/cpp03/buffers/Jamfile b/libs/asio/example/cpp03/buffers/Jamfile new file mode 100644 index 000000000..7a0fb31ad --- /dev/null +++ b/libs/asio/example/cpp03/buffers/Jamfile @@ -0,0 +1,33 @@ +# +# Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com) +# +# Distributed under the Boost Software License, Version 1.0. (See accompanying +# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +# + +subproject libs/asio/example/buffers ; + +project boost : $(BOOST_ROOT) ; + +if $(UNIX) +{ + switch $(JAMUNAME) + { + case SunOS* : + { + SOCKET_LIBS = <find-library>socket <find-library>nsl ; + } + } +} + +exe server + : <lib>@boost/libs/system/build/boost_system + reference_counted.cpp + : <include>$(BOOST_ROOT) + <include>../../../.. + <define>BOOST_ALL_NO_LIB=1 + <threading>multi + <mingw><*><find-library>ws2_32 + <mingw><*><find-library>mswsock + $(SOCKET_LIBS) + ; diff --git a/libs/asio/example/cpp03/buffers/Jamfile.v2 b/libs/asio/example/cpp03/buffers/Jamfile.v2 new file mode 100644 index 000000000..1fcf5effe --- /dev/null +++ b/libs/asio/example/cpp03/buffers/Jamfile.v2 @@ -0,0 +1,38 @@ +# +# Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com) +# +# Distributed under the Boost Software License, Version 1.0. (See accompanying +# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +# + +import os ; + +if [ os.name ] = SOLARIS +{ + lib socket ; + lib nsl ; +} +else if [ os.name ] = NT +{ + lib ws2_32 ; + lib mswsock ; +} +else if [ os.name ] = HPUX +{ + lib ipv6 ; +} + +exe server + : reference_counted.cpp + /boost/system//boost_system + : <define>BOOST_ALL_NO_LIB=1 + <threading>multi + <os>SOLARIS:<library>socket + <os>SOLARIS:<library>nsl + <os>NT:<define>_WIN32_WINNT=0x0501 + <os>NT,<toolset>gcc:<library>ws2_32 + <os>NT,<toolset>gcc:<library>mswsock + <os>NT,<toolset>gcc-cygwin:<define>__USE_W32_SOCKETS + <os>HPUX,<toolset>gcc:<define>_XOPEN_SOURCE_EXTENDED + <os>HPUX:<library>ipv6 + ; diff --git a/libs/asio/example/cpp03/buffers/reference_counted.cpp b/libs/asio/example/cpp03/buffers/reference_counted.cpp new file mode 100644 index 000000000..5a46e4dc5 --- /dev/null +++ b/libs/asio/example/cpp03/buffers/reference_counted.cpp @@ -0,0 +1,131 @@ +// +// reference_counted.cpp +// ~~~~~~~~~~~~~~~~~~~~~ +// +// Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com) +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// + +#include <boost/asio.hpp> +#include <boost/bind.hpp> +#include <boost/enable_shared_from_this.hpp> +#include <boost/shared_ptr.hpp> +#include <iostream> +#include <vector> + +using boost::asio::ip::tcp; + +// A reference-counted non-modifiable buffer class. +class shared_const_buffer +{ +public: + // Construct from a std::string. + explicit shared_const_buffer(const std::string& data) + : data_(new std::vector<char>(data.begin(), data.end())), + buffer_(boost::asio::buffer(*data_)) + { + } + + // Implement the ConstBufferSequence requirements. + typedef boost::asio::const_buffer value_type; + typedef const boost::asio::const_buffer* const_iterator; + const boost::asio::const_buffer* begin() const { return &buffer_; } + const boost::asio::const_buffer* end() const { return &buffer_ + 1; } + +private: + boost::shared_ptr<std::vector<char> > data_; + boost::asio::const_buffer buffer_; +}; + +class session + : public boost::enable_shared_from_this<session> +{ +public: + session(boost::asio::io_service& io_service) + : socket_(io_service) + { + } + + tcp::socket& socket() + { + return socket_; + } + + void start() + { + using namespace std; // For time_t, time and ctime. + time_t now = time(0); + shared_const_buffer buffer(ctime(&now)); + boost::asio::async_write(socket_, buffer, + boost::bind(&session::handle_write, shared_from_this())); + } + + void handle_write() + { + } + +private: + // The socket used to communicate with the client. + tcp::socket socket_; +}; + +typedef boost::shared_ptr<session> session_ptr; + +class server +{ +public: + server(boost::asio::io_service& io_service, short port) + : io_service_(io_service), + acceptor_(io_service, tcp::endpoint(tcp::v4(), port)) + { + session_ptr new_session(new session(io_service_)); + acceptor_.async_accept(new_session->socket(), + boost::bind(&server::handle_accept, this, new_session, + boost::asio::placeholders::error)); + } + + void handle_accept(session_ptr new_session, + const boost::system::error_code& error) + { + if (!error) + { + new_session->start(); + } + + new_session.reset(new session(io_service_)); + acceptor_.async_accept(new_session->socket(), + boost::bind(&server::handle_accept, this, new_session, + boost::asio::placeholders::error)); + } + +private: + boost::asio::io_service& io_service_; + tcp::acceptor acceptor_; +}; + +int main(int argc, char* argv[]) +{ + try + { + if (argc != 2) + { + std::cerr << "Usage: reference_counted <port>\n"; + return 1; + } + + boost::asio::io_service io_service; + + using namespace std; // For atoi. + server s(io_service, atoi(argv[1])); + + io_service.run(); + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << "\n"; + } + + return 0; +} |
