2
0
mirror of https://github.com/boostorg/asio.git synced 2026-01-30 19:32:09 +00:00
Files
asio/example/local/connect_pair.cpp
Christopher Kohlhoff 2c41e180c1 Merge from trunk. Fixes #3095, #3216, #3098, #3107, #1341, #2754, #3157, #2620, #2618.
........
  r54373 | chris_kohlhoff | 2009-06-26 21:03:14 +1000 (Fri, 26 Jun 2009) | 2 lines
  
  Fix doc generation for array reference parameters.
........
  r54376 | chris_kohlhoff | 2009-06-26 23:35:04 +1000 (Fri, 26 Jun 2009) | 2 lines
  
  Fix bug in hash resize. Ref #3095.
........
  r54377 | chris_kohlhoff | 2009-06-26 23:55:24 +1000 (Fri, 26 Jun 2009) | 3 lines
  
  Remove a local variable that was hiding the ec parameter and preventing
  error codes from being correctly propagated. Ref #3216.
........
  r54390 | chris_kohlhoff | 2009-06-27 12:17:49 +1000 (Sat, 27 Jun 2009) | 2 lines
  
  Fix failures reported when the tests are built with _GLIBCXX_DEBUG. Ref #3098.
........
  r54392 | chris_kohlhoff | 2009-06-27 15:24:16 +1000 (Sat, 27 Jun 2009) | 2 lines
  
  Fix custom memory allocation for timers. Ref #3107.
........
  r54393 | chris_kohlhoff | 2009-06-27 17:07:40 +1000 (Sat, 27 Jun 2009) | 2 lines
  
  Fix various g++ warnings. Ref #1341.
........
  r54400 | chris_kohlhoff | 2009-06-27 17:52:11 +1000 (Sat, 27 Jun 2009) | 4 lines
  
  Use boost::throw_exception() rather than throw keyword to allow asio to be
  used when exception support is disabled. Note that the SSL wrappers still
  require exception support. Refs #2754.
........
  r54407 | chris_kohlhoff | 2009-06-27 19:13:24 +1000 (Sat, 27 Jun 2009) | 2 lines
  
  Make links to function overloads more obvious.
........
  r54466 | chris_kohlhoff | 2009-06-28 23:07:43 +1000 (Sun, 28 Jun 2009) | 2 lines
  
  Add header file information to reference docs. Refs #3157.
........
  r54467 | chris_kohlhoff | 2009-06-28 23:20:17 +1000 (Sun, 28 Jun 2009) | 4 lines
  
  Treat 0-byte reads and writes as no-ops to comply with the documented type
  requirements for SyncReadStream, AsyncReadStream, SyncWriteStream and
  AsyncWriteStream.
........
  r54498 | chris_kohlhoff | 2009-06-29 19:32:41 +1000 (Mon, 29 Jun 2009) | 2 lines
  
  Add enum values to doc index. Refs #2620.
........


[SVN r54499]
2009-06-29 13:36:06 +00:00

143 lines
3.3 KiB
C++

//
// connect_pair.cpp
// ~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2008 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 <iostream>
#include <string>
#include <cctype>
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <boost/array.hpp>
#include <boost/bind.hpp>
#if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
using boost::asio::local::stream_protocol;
class uppercase_filter
{
public:
uppercase_filter(boost::asio::io_service& io_service)
: socket_(io_service)
{
}
stream_protocol::socket& socket()
{
return socket_;
}
void start()
{
// Wait for request.
socket_.async_read_some(boost::asio::buffer(data_),
boost::bind(&uppercase_filter::handle_read,
this, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
private:
void handle_read(const boost::system::error_code& ec, std::size_t size)
{
if (!ec)
{
// Compute result.
for (std::size_t i = 0; i < size; ++i)
data_[i] = std::toupper(data_[i]);
// Send result.
boost::asio::async_write(socket_, boost::asio::buffer(data_, size),
boost::bind(&uppercase_filter::handle_write,
this, boost::asio::placeholders::error));
}
else
{
throw boost::system::system_error(ec);
}
}
void handle_write(const boost::system::error_code& ec)
{
if (!ec)
{
// Wait for request.
socket_.async_read_some(boost::asio::buffer(data_),
boost::bind(&uppercase_filter::handle_read,
this, boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
}
else
{
throw boost::system::system_error(ec);
}
}
stream_protocol::socket socket_;
boost::array<char, 512> data_;
};
void run(boost::asio::io_service* io_service)
{
try
{
io_service->run();
}
catch (std::exception& e)
{
std::cerr << "Exception in thread: " << e.what() << "\n";
std::exit(1);
}
}
int main()
{
try
{
boost::asio::io_service io_service;
// Create filter and establish a connection to it.
uppercase_filter filter(io_service);
stream_protocol::socket socket(io_service);
boost::asio::local::connect_pair(socket, filter.socket());
filter.start();
// The io_service runs in a background thread to perform filtering.
boost::thread thread(boost::bind(run, &io_service));
for (;;)
{
// Collect request from user.
std::cout << "Enter a string: ";
std::string request;
std::getline(std::cin, request);
// Send request to filter.
boost::asio::write(socket, boost::asio::buffer(request));
// Wait for reply from filter.
std::vector<char> reply(request.size());
boost::asio::read(socket, boost::asio::buffer(reply));
// Show reply to user.
std::cout << "Result: ";
std::cout.write(&reply[0], request.size());
std::cout << std::endl;
}
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
std::exit(1);
}
}
#else // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
# error Local sockets not available on this platform.
#endif // defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)