2
0
mirror of https://github.com/boostorg/asio.git synced 2026-01-28 06:42:08 +00:00
Files
asio/test/latency/tcp_server.cpp
Christopher Kohlhoff 77dec8e703 Merge from trunk...
Fix compile error in regex overload of async_read_until.hpp. Fixes #5688

Explicitly specify the signal() function from the global namespace. Fixes #5722

Don't read the clock unless the heap is non-empty.

Change the SSL buffers sizes so that they're large enough to hold a complete TLS record. Fixes #5854

Make sure the synchronous null_buffers operations obey the user's non_blocking setting. Fixes #5756

Set size of select fd_set at runtime when using Windows.

Disable warning due to const qualifier being applied to function type.

Fix crash due to gcc_x86_fenced_block that shows up when using the Intel C++ compiler. Fixes #5763

Specialise operations for buffer sequences that are arrays of exactly two buffers.

Initialise all OpenSSL algorithms.

Fix error mapping when session is gracefully shut down.

Various performance improvements:

* Split the task_io_service's run and poll code.

* Use thread-local operation queues in single-threaded use cases (i.e. concurrency_hint is 1) to eliminate a lock/unlock pair.

* Only fence block exit when a handler is being run directly out of the io_service.

* Prefer x86 mfence-based fenced block when available.

* Use a plain ol' long for the atomic_count when all thread support is disabled.

* Allow some epoll_reactor speculative operations to be performed without holding the lock.

* Improve locality of reference by performing an epoll_reactor's I/O operation immediately before the corresponding handler is called. This also improves scalability across CPUs when multiple threads are running the io_service.

* Pass same error_code variable through to each operation's complete() function.

* Optimise creation of and access to the io_service implementation.

Remove unused state in HTTP server examples.

Add latency test programs.


[SVN r74863]
2011-10-09 21:59:57 +00:00

115 lines
2.5 KiB
C++

//
// tcp_server.cpp
// ~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2011 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/io_service.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/read.hpp>
#include <boost/asio/write.hpp>
#include <boost/shared_ptr.hpp>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
using boost::asio::ip::tcp;
#include "yield.hpp"
class tcp_server : coroutine
{
public:
tcp_server(tcp::acceptor& acceptor, std::size_t buf_size) :
acceptor_(acceptor),
socket_(acceptor_.get_io_service()),
buffer_(buf_size)
{
}
void operator()(boost::system::error_code ec, std::size_t n = 0)
{
reenter (this) for (;;)
{
yield acceptor_.async_accept(socket_, ref(this));
while (!ec)
{
yield boost::asio::async_read(socket_,
boost::asio::buffer(buffer_), ref(this));
if (!ec)
{
for (std::size_t i = 0; i < n; ++i) buffer_[i] = ~buffer_[i];
yield boost::asio::async_write(socket_,
boost::asio::buffer(buffer_), ref(this));
}
}
socket_.close();
}
}
struct ref
{
explicit ref(tcp_server* p)
: p_(p)
{
}
void operator()(boost::system::error_code ec, std::size_t n = 0)
{
(*p_)(ec, n);
}
private:
tcp_server* p_;
};
private:
tcp::acceptor& acceptor_;
tcp::socket socket_;
std::vector<unsigned char> buffer_;
tcp::endpoint sender_;
};
#include "unyield.hpp"
int main(int argc, char* argv[])
{
if (argc != 5)
{
std::fprintf(stderr,
"Usage: tcp_server <port> <nconns> "
"<bufsize> {spin|block}\n");
return 1;
}
unsigned short port = static_cast<unsigned short>(std::atoi(argv[1]));
int max_connections = std::atoi(argv[2]);
std::size_t buf_size = std::atoi(argv[3]);
bool spin = (std::strcmp(argv[4], "spin") == 0);
boost::asio::io_service io_service(1);
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), port));
std::vector<boost::shared_ptr<tcp_server> > servers;
for (int i = 0; i < max_connections; ++i)
{
boost::shared_ptr<tcp_server> s(new tcp_server(acceptor, buf_size));
servers.push_back(s);
(*s)(boost::system::error_code());
}
if (spin)
for (;;) io_service.poll();
else
io_service.run();
}