mirror of
https://github.com/boostorg/asio.git
synced 2026-01-28 06:42:08 +00:00
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]
126 lines
2.8 KiB
C++
126 lines
2.8 KiB
C++
//
|
|
// udp_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/udp.hpp>
|
|
#include <boost/shared_ptr.hpp>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <vector>
|
|
#include "allocator.hpp"
|
|
|
|
using boost::asio::ip::udp;
|
|
|
|
#include "yield.hpp"
|
|
|
|
class udp_server : coroutine
|
|
{
|
|
public:
|
|
udp_server(boost::asio::io_service& io_service,
|
|
unsigned short port, std::size_t buf_size) :
|
|
socket_(io_service, udp::endpoint(udp::v4(), port)),
|
|
buffer_(buf_size)
|
|
{
|
|
}
|
|
|
|
void operator()(boost::system::error_code ec, std::size_t n = 0)
|
|
{
|
|
reenter (this) for (;;)
|
|
{
|
|
yield socket_.async_receive_from(
|
|
boost::asio::buffer(buffer_),
|
|
sender_, ref(this));
|
|
|
|
if (!ec)
|
|
{
|
|
for (std::size_t i = 0; i < n; ++i) buffer_[i] = ~buffer_[i];
|
|
socket_.send_to(boost::asio::buffer(buffer_, n), sender_, 0, ec);
|
|
}
|
|
}
|
|
}
|
|
|
|
friend void* asio_handler_allocate(std::size_t n, udp_server* s)
|
|
{
|
|
return s->allocator_.allocate(n);
|
|
}
|
|
|
|
friend void asio_handler_deallocate(void* p, std::size_t, udp_server* s)
|
|
{
|
|
s->allocator_.deallocate(p);
|
|
}
|
|
|
|
struct ref
|
|
{
|
|
explicit ref(udp_server* p)
|
|
: p_(p)
|
|
{
|
|
}
|
|
|
|
void operator()(boost::system::error_code ec, std::size_t n = 0)
|
|
{
|
|
(*p_)(ec, n);
|
|
}
|
|
|
|
private:
|
|
udp_server* p_;
|
|
|
|
friend void* asio_handler_allocate(std::size_t n, ref* r)
|
|
{
|
|
return asio_handler_allocate(n, r->p_);
|
|
}
|
|
|
|
friend void asio_handler_deallocate(void* p, std::size_t n, ref* r)
|
|
{
|
|
asio_handler_deallocate(p, n, r->p_);
|
|
}
|
|
};
|
|
|
|
private:
|
|
udp::socket socket_;
|
|
std::vector<unsigned char> buffer_;
|
|
udp::endpoint sender_;
|
|
allocator allocator_;
|
|
};
|
|
|
|
#include "unyield.hpp"
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
if (argc != 5)
|
|
{
|
|
std::fprintf(stderr,
|
|
"Usage: udp_server <port1> <nports> "
|
|
"<bufsize> {spin|block}\n");
|
|
return 1;
|
|
}
|
|
|
|
unsigned short first_port = static_cast<unsigned short>(std::atoi(argv[1]));
|
|
unsigned short num_ports = static_cast<unsigned short>(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);
|
|
std::vector<boost::shared_ptr<udp_server> > servers;
|
|
|
|
for (unsigned short i = 0; i < num_ports; ++i)
|
|
{
|
|
unsigned short port = first_port + i;
|
|
boost::shared_ptr<udp_server> s(new udp_server(io_service, port, buf_size));
|
|
servers.push_back(s);
|
|
(*s)(boost::system::error_code());
|
|
}
|
|
|
|
if (spin)
|
|
for (;;) io_service.poll();
|
|
else
|
|
io_service.run();
|
|
}
|