mirror of
https://github.com/boostorg/asio.git
synced 2026-01-28 06:42:08 +00:00
........
r48414 | chris_kohlhoff | 2008-08-28 22:03:46 +1000 (Thu, 28 Aug 2008) | 2 lines
Fix uninitialised event handles in asio::detail::thread in external mode.
........
r48415 | chris_kohlhoff | 2008-08-28 22:04:21 +1000 (Thu, 28 Aug 2008) | 2 lines
Fix duplicate variable warning.
........
r48416 | chris_kohlhoff | 2008-08-28 22:06:42 +1000 (Thu, 28 Aug 2008) | 3 lines
Fix duplicate variable warning. Use get_io_service() rather than
deprecated io_service() function.
........
r48417 | chris_kohlhoff | 2008-08-28 22:09:09 +1000 (Thu, 28 Aug 2008) | 2 lines
Use get_io_service() rather than deprecated io_service() function.
........
r48418 | chris_kohlhoff | 2008-08-28 22:11:47 +1000 (Thu, 28 Aug 2008) | 9 lines
Change the CompletionCondition concept so that:
- It is now evaluated before the first call to the underlying
*_some() operation, as well as after every operation.
- The return value is a number of bytes, which indicates the maximum
length to be transferred on the subsequent *_some() operation. If
the return value is 0 then the composed operation completes.
Add missing unit tests for read_at and write_at.
........
r48490 | chris_kohlhoff | 2008-08-31 18:58:49 +1000 (Sun, 31 Aug 2008) | 2 lines
Don't build serial port test code when serial ports are not available.
........
r48491 | chris_kohlhoff | 2008-08-31 19:01:59 +1000 (Sun, 31 Aug 2008) | 3 lines
Refactor reactive socket implementation so that synchronous read, write,
accept and connect operations don't modify data associated with the socket.
........
r48495 | chris_kohlhoff | 2008-08-31 21:38:52 +1000 (Sun, 31 Aug 2008) | 2 lines
Add class to allow use of arbitrary Windows overlapped I/O operations.
........
r48524 | chris_kohlhoff | 2008-09-02 09:04:35 +1000 (Tue, 02 Sep 2008) | 3 lines
Add support for using an eventfd descriptor on linux to interrupt a blocked
epoll/select reactor.
........
r48525 | chris_kohlhoff | 2008-09-02 09:05:05 +1000 (Tue, 02 Sep 2008) | 3 lines
Use templates to handle iovec structures that have an iov_base member of
type char*.
........
r48526 | chris_kohlhoff | 2008-09-02 09:07:29 +1000 (Tue, 02 Sep 2008) | 2 lines
Fix error in comment.
........
r48527 | chris_kohlhoff | 2008-09-02 18:46:46 +1000 (Tue, 02 Sep 2008) | 2 lines
Fix to compile with MSVC 7.1.
........
r48535 | chris_kohlhoff | 2008-09-02 21:34:18 +1000 (Tue, 02 Sep 2008) | 2 lines
Add const overloads of the lowest_layer member functions.
........
r48558 | chris_kohlhoff | 2008-09-03 18:56:43 +1000 (Wed, 03 Sep 2008) | 2 lines
Oops, changeset 48535 accidentally reverted 48527's fix.
........
r48559 | chris_kohlhoff | 2008-09-03 18:57:48 +1000 (Wed, 03 Sep 2008) | 2 lines
Add a workaround for a possible gcc 4.3 optimiser bug.
........
r48560 | chris_kohlhoff | 2008-09-03 19:06:10 +1000 (Wed, 03 Sep 2008) | 2 lines
Explicitly specify return type with bind to keep some compilers happy.
........
r48561 | chris_kohlhoff | 2008-09-03 19:08:04 +1000 (Wed, 03 Sep 2008) | 2 lines
Update completion_condition documentation to match new signature.
........
r48562 | chris_kohlhoff | 2008-09-03 21:36:43 +1000 (Wed, 03 Sep 2008) | 2 lines
Add windows/transmit_file example.
........
r48563 | chris_kohlhoff | 2008-09-03 21:46:02 +1000 (Wed, 03 Sep 2008) | 2 lines
Document BOOST_ASIO_DISABLE_EVENTFD macro.
........
r48564 | chris_kohlhoff | 2008-09-03 21:54:06 +1000 (Wed, 03 Sep 2008) | 2 lines
Add windows::overlapped_ptr to the reference index page.
........
r48565 | chris_kohlhoff | 2008-09-03 22:28:07 +1000 (Wed, 03 Sep 2008) | 2 lines
Regenerate documentation.
........
r48683 | chris_kohlhoff | 2008-09-09 22:59:43 +1000 (Tue, 09 Sep 2008) | 2 lines
Add windows example directory.
........
r48684 | chris_kohlhoff | 2008-09-09 23:00:07 +1000 (Tue, 09 Sep 2008) | 2 lines
Remove generated tags file.
........
r48685 | chris_kohlhoff | 2008-09-09 23:01:16 +1000 (Tue, 09 Sep 2008) | 2 lines
Version bump.
........
r48686 | chris_kohlhoff | 2008-09-09 23:02:22 +1000 (Tue, 09 Sep 2008) | 2 lines
Handle SSL library errors.
........
r48715 | chris_kohlhoff | 2008-09-11 10:05:57 +1000 (Thu, 11 Sep 2008) | 2 lines
Clarify which resolve functions perform forward or reverse resolution.
........
[SVN r49194]
342 lines
12 KiB
C++
342 lines
12 KiB
C++
//
|
|
// udp.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)
|
|
//
|
|
|
|
// Disable autolinking for unit tests.
|
|
#if !defined(BOOST_ALL_NO_LIB)
|
|
#define BOOST_ALL_NO_LIB 1
|
|
#endif // !defined(BOOST_ALL_NO_LIB)
|
|
|
|
// Test that header file is self-contained.
|
|
#include <boost/asio/ip/udp.hpp>
|
|
|
|
#include <boost/bind.hpp>
|
|
#include <cstring>
|
|
#include <boost/asio.hpp>
|
|
#include "../unit_test.hpp"
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// ip_udp_socket_compile test
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
// The following test checks that all public member functions on the class
|
|
// ip::udp::socket compile and link correctly. Runtime failures are ignored.
|
|
|
|
namespace ip_udp_socket_compile {
|
|
|
|
void connect_handler(const boost::system::error_code&)
|
|
{
|
|
}
|
|
|
|
void send_handler(const boost::system::error_code&, std::size_t)
|
|
{
|
|
}
|
|
|
|
void receive_handler(const boost::system::error_code&, std::size_t)
|
|
{
|
|
}
|
|
|
|
void test()
|
|
{
|
|
using namespace boost::asio;
|
|
namespace ip = boost::asio::ip;
|
|
|
|
try
|
|
{
|
|
io_service ios;
|
|
char mutable_char_buffer[128] = "";
|
|
const char const_char_buffer[128] = "";
|
|
socket_base::message_flags in_flags = 0;
|
|
socket_base::keep_alive socket_option;
|
|
socket_base::bytes_readable io_control_command;
|
|
boost::system::error_code ec;
|
|
|
|
// basic_datagram_socket constructors.
|
|
|
|
ip::udp::socket socket1(ios);
|
|
ip::udp::socket socket2(ios, ip::udp::v4());
|
|
ip::udp::socket socket3(ios, ip::udp::v6());
|
|
ip::udp::socket socket4(ios, ip::udp::endpoint(ip::udp::v4(), 0));
|
|
ip::udp::socket socket5(ios, ip::udp::endpoint(ip::udp::v6(), 0));
|
|
int native_socket1 = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
ip::udp::socket socket6(ios, ip::udp::v4(), native_socket1);
|
|
|
|
// basic_io_object functions.
|
|
|
|
io_service& ios_ref = socket1.io_service();
|
|
(void)ios_ref;
|
|
|
|
// basic_socket functions.
|
|
|
|
ip::udp::socket::lowest_layer_type& lowest_layer = socket1.lowest_layer();
|
|
(void)lowest_layer;
|
|
|
|
const ip::udp::socket& socket7 = socket1;
|
|
const ip::udp::socket::lowest_layer_type& lowest_layer2
|
|
= socket7.lowest_layer();
|
|
(void)lowest_layer2;
|
|
|
|
socket1.open(ip::udp::v4());
|
|
socket1.open(ip::udp::v6());
|
|
socket1.open(ip::udp::v4(), ec);
|
|
socket1.open(ip::udp::v6(), ec);
|
|
|
|
int native_socket2 = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
socket1.assign(ip::udp::v4(), native_socket2);
|
|
int native_socket3 = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
socket1.assign(ip::udp::v4(), native_socket3, ec);
|
|
|
|
bool is_open = socket1.is_open();
|
|
(void)is_open;
|
|
|
|
socket1.close();
|
|
socket1.close(ec);
|
|
|
|
ip::udp::socket::native_type native_socket4 = socket1.native();
|
|
(void)native_socket4;
|
|
|
|
socket1.cancel();
|
|
socket1.cancel(ec);
|
|
|
|
bool at_mark1 = socket1.at_mark();
|
|
(void)at_mark1;
|
|
bool at_mark2 = socket1.at_mark(ec);
|
|
(void)at_mark2;
|
|
|
|
std::size_t available1 = socket1.available();
|
|
(void)available1;
|
|
std::size_t available2 = socket1.available(ec);
|
|
(void)available2;
|
|
|
|
socket1.bind(ip::udp::endpoint(ip::udp::v4(), 0));
|
|
socket1.bind(ip::udp::endpoint(ip::udp::v6(), 0));
|
|
socket1.bind(ip::udp::endpoint(ip::udp::v4(), 0), ec);
|
|
socket1.bind(ip::udp::endpoint(ip::udp::v6(), 0), ec);
|
|
|
|
socket1.connect(ip::udp::endpoint(ip::udp::v4(), 0));
|
|
socket1.connect(ip::udp::endpoint(ip::udp::v6(), 0));
|
|
socket1.connect(ip::udp::endpoint(ip::udp::v4(), 0), ec);
|
|
socket1.connect(ip::udp::endpoint(ip::udp::v6(), 0), ec);
|
|
|
|
socket1.async_connect(ip::udp::endpoint(ip::udp::v4(), 0), connect_handler);
|
|
socket1.async_connect(ip::udp::endpoint(ip::udp::v6(), 0), connect_handler);
|
|
|
|
socket1.set_option(socket_option);
|
|
socket1.set_option(socket_option, ec);
|
|
|
|
socket1.get_option(socket_option);
|
|
socket1.get_option(socket_option, ec);
|
|
|
|
socket1.io_control(io_control_command);
|
|
socket1.io_control(io_control_command, ec);
|
|
|
|
ip::udp::endpoint endpoint1 = socket1.local_endpoint();
|
|
ip::udp::endpoint endpoint2 = socket1.local_endpoint(ec);
|
|
|
|
ip::udp::endpoint endpoint3 = socket1.remote_endpoint();
|
|
ip::udp::endpoint endpoint4 = socket1.remote_endpoint(ec);
|
|
|
|
socket1.shutdown(socket_base::shutdown_both);
|
|
socket1.shutdown(socket_base::shutdown_both, ec);
|
|
|
|
// basic_datagram_socket functions.
|
|
|
|
socket1.send(buffer(mutable_char_buffer));
|
|
socket1.send(buffer(const_char_buffer));
|
|
socket1.send(null_buffers());
|
|
socket1.send(buffer(mutable_char_buffer), in_flags);
|
|
socket1.send(buffer(const_char_buffer), in_flags);
|
|
socket1.send(null_buffers(), in_flags);
|
|
socket1.send(buffer(mutable_char_buffer), in_flags, ec);
|
|
socket1.send(buffer(const_char_buffer), in_flags, ec);
|
|
socket1.send(null_buffers(), in_flags, ec);
|
|
|
|
socket1.async_send(buffer(mutable_char_buffer), send_handler);
|
|
socket1.async_send(buffer(const_char_buffer), send_handler);
|
|
socket1.async_send(null_buffers(), send_handler);
|
|
socket1.async_send(buffer(mutable_char_buffer), in_flags, send_handler);
|
|
socket1.async_send(buffer(const_char_buffer), in_flags, send_handler);
|
|
socket1.async_send(null_buffers(), in_flags, send_handler);
|
|
|
|
socket1.send_to(buffer(mutable_char_buffer),
|
|
ip::udp::endpoint(ip::udp::v4(), 0));
|
|
socket1.send_to(buffer(mutable_char_buffer),
|
|
ip::udp::endpoint(ip::udp::v6(), 0));
|
|
socket1.send_to(buffer(const_char_buffer),
|
|
ip::udp::endpoint(ip::udp::v4(), 0));
|
|
socket1.send_to(buffer(const_char_buffer),
|
|
ip::udp::endpoint(ip::udp::v6(), 0));
|
|
socket1.send_to(null_buffers(),
|
|
ip::udp::endpoint(ip::udp::v4(), 0));
|
|
socket1.send_to(null_buffers(),
|
|
ip::udp::endpoint(ip::udp::v6(), 0));
|
|
socket1.send_to(buffer(mutable_char_buffer),
|
|
ip::udp::endpoint(ip::udp::v4(), 0), in_flags);
|
|
socket1.send_to(buffer(mutable_char_buffer),
|
|
ip::udp::endpoint(ip::udp::v6(), 0), in_flags);
|
|
socket1.send_to(buffer(const_char_buffer),
|
|
ip::udp::endpoint(ip::udp::v4(), 0), in_flags);
|
|
socket1.send_to(buffer(const_char_buffer),
|
|
ip::udp::endpoint(ip::udp::v6(), 0), in_flags);
|
|
socket1.send_to(null_buffers(),
|
|
ip::udp::endpoint(ip::udp::v4(), 0), in_flags);
|
|
socket1.send_to(null_buffers(),
|
|
ip::udp::endpoint(ip::udp::v6(), 0), in_flags);
|
|
socket1.send_to(buffer(mutable_char_buffer),
|
|
ip::udp::endpoint(ip::udp::v4(), 0), in_flags, ec);
|
|
socket1.send_to(buffer(mutable_char_buffer),
|
|
ip::udp::endpoint(ip::udp::v6(), 0), in_flags, ec);
|
|
socket1.send_to(buffer(const_char_buffer),
|
|
ip::udp::endpoint(ip::udp::v4(), 0), in_flags, ec);
|
|
socket1.send_to(buffer(const_char_buffer),
|
|
ip::udp::endpoint(ip::udp::v6(), 0), in_flags, ec);
|
|
socket1.send_to(null_buffers(),
|
|
ip::udp::endpoint(ip::udp::v4(), 0), in_flags, ec);
|
|
socket1.send_to(null_buffers(),
|
|
ip::udp::endpoint(ip::udp::v6(), 0), in_flags, ec);
|
|
|
|
socket1.async_send_to(buffer(mutable_char_buffer),
|
|
ip::udp::endpoint(ip::udp::v4(), 0), send_handler);
|
|
socket1.async_send_to(buffer(mutable_char_buffer),
|
|
ip::udp::endpoint(ip::udp::v6(), 0), send_handler);
|
|
socket1.async_send_to(buffer(const_char_buffer),
|
|
ip::udp::endpoint(ip::udp::v4(), 0), send_handler);
|
|
socket1.async_send_to(buffer(const_char_buffer),
|
|
ip::udp::endpoint(ip::udp::v6(), 0), send_handler);
|
|
socket1.async_send_to(null_buffers(),
|
|
ip::udp::endpoint(ip::udp::v4(), 0), send_handler);
|
|
socket1.async_send_to(null_buffers(),
|
|
ip::udp::endpoint(ip::udp::v6(), 0), send_handler);
|
|
socket1.async_send_to(buffer(mutable_char_buffer),
|
|
ip::udp::endpoint(ip::udp::v4(), 0), in_flags, send_handler);
|
|
socket1.async_send_to(buffer(mutable_char_buffer),
|
|
ip::udp::endpoint(ip::udp::v6(), 0), in_flags, send_handler);
|
|
socket1.async_send_to(buffer(const_char_buffer),
|
|
ip::udp::endpoint(ip::udp::v4(), 0), in_flags, send_handler);
|
|
socket1.async_send_to(buffer(const_char_buffer),
|
|
ip::udp::endpoint(ip::udp::v6(), 0), in_flags, send_handler);
|
|
socket1.async_send_to(null_buffers(),
|
|
ip::udp::endpoint(ip::udp::v4(), 0), in_flags, send_handler);
|
|
socket1.async_send_to(null_buffers(),
|
|
ip::udp::endpoint(ip::udp::v6(), 0), in_flags, send_handler);
|
|
|
|
socket1.receive(buffer(mutable_char_buffer));
|
|
socket1.receive(null_buffers());
|
|
socket1.receive(buffer(mutable_char_buffer), in_flags);
|
|
socket1.receive(null_buffers(), in_flags);
|
|
socket1.receive(buffer(mutable_char_buffer), in_flags, ec);
|
|
socket1.receive(null_buffers(), in_flags, ec);
|
|
|
|
socket1.async_receive(buffer(mutable_char_buffer), receive_handler);
|
|
socket1.async_receive(null_buffers(), receive_handler);
|
|
socket1.async_receive(buffer(mutable_char_buffer), in_flags,
|
|
receive_handler);
|
|
socket1.async_receive(null_buffers(), in_flags, receive_handler);
|
|
|
|
ip::udp::endpoint endpoint;
|
|
socket1.receive_from(buffer(mutable_char_buffer), endpoint);
|
|
socket1.receive_from(null_buffers(), endpoint);
|
|
socket1.receive_from(buffer(mutable_char_buffer), endpoint, in_flags);
|
|
socket1.receive_from(null_buffers(), endpoint, in_flags);
|
|
socket1.receive_from(buffer(mutable_char_buffer), endpoint, in_flags, ec);
|
|
socket1.receive_from(null_buffers(), endpoint, in_flags, ec);
|
|
|
|
socket1.async_receive_from(buffer(mutable_char_buffer),
|
|
endpoint, receive_handler);
|
|
socket1.async_receive_from(null_buffers(),
|
|
endpoint, receive_handler);
|
|
socket1.async_receive_from(buffer(mutable_char_buffer),
|
|
endpoint, in_flags, receive_handler);
|
|
socket1.async_receive_from(null_buffers(),
|
|
endpoint, in_flags, receive_handler);
|
|
}
|
|
catch (std::exception&)
|
|
{
|
|
}
|
|
}
|
|
|
|
} // namespace ip_udp_socket_compile
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// ip_udp_socket_runtime test
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
// The following test checks the runtime operation of the ip::udp::socket class.
|
|
|
|
namespace ip_udp_socket_runtime {
|
|
|
|
void handle_send(size_t expected_bytes_sent,
|
|
const boost::system::error_code& err, size_t bytes_sent)
|
|
{
|
|
BOOST_CHECK(!err);
|
|
BOOST_CHECK(expected_bytes_sent == bytes_sent);
|
|
}
|
|
|
|
void handle_recv(size_t expected_bytes_recvd,
|
|
const boost::system::error_code& err, size_t bytes_recvd)
|
|
{
|
|
BOOST_CHECK(!err);
|
|
BOOST_CHECK(expected_bytes_recvd == bytes_recvd);
|
|
}
|
|
|
|
void test()
|
|
{
|
|
using namespace std; // For memcmp and memset.
|
|
using namespace boost::asio;
|
|
namespace ip = boost::asio::ip;
|
|
|
|
io_service ios;
|
|
|
|
ip::udp::socket s1(ios, ip::udp::endpoint(ip::udp::v4(), 0));
|
|
ip::udp::endpoint target_endpoint = s1.local_endpoint();
|
|
target_endpoint.address(ip::address_v4::loopback());
|
|
|
|
ip::udp::socket s2(ios);
|
|
s2.open(ip::udp::v4());
|
|
s2.bind(ip::udp::endpoint(ip::udp::v4(), 0));
|
|
char send_msg[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
s2.send_to(buffer(send_msg, sizeof(send_msg)), target_endpoint);
|
|
|
|
char recv_msg[sizeof(send_msg)];
|
|
ip::udp::endpoint sender_endpoint;
|
|
size_t bytes_recvd = s1.receive_from(buffer(recv_msg, sizeof(recv_msg)),
|
|
sender_endpoint);
|
|
|
|
BOOST_CHECK(bytes_recvd == sizeof(send_msg));
|
|
BOOST_CHECK(memcmp(send_msg, recv_msg, sizeof(send_msg)) == 0);
|
|
|
|
memset(recv_msg, 0, sizeof(recv_msg));
|
|
|
|
target_endpoint = sender_endpoint;
|
|
s1.async_send_to(buffer(send_msg, sizeof(send_msg)), target_endpoint,
|
|
boost::bind(handle_send, sizeof(send_msg),
|
|
boost::asio::placeholders::error,
|
|
boost::asio::placeholders::bytes_transferred));
|
|
s2.async_receive_from(buffer(recv_msg, sizeof(recv_msg)), sender_endpoint,
|
|
boost::bind(handle_recv, sizeof(recv_msg),
|
|
boost::asio::placeholders::error,
|
|
boost::asio::placeholders::bytes_transferred));
|
|
|
|
ios.run();
|
|
|
|
BOOST_CHECK(memcmp(send_msg, recv_msg, sizeof(send_msg)) == 0);
|
|
}
|
|
|
|
} // namespace ip_udp_socket_runtime
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
test_suite* init_unit_test_suite(int, char*[])
|
|
{
|
|
test_suite* test = BOOST_TEST_SUITE("ip/udp");
|
|
test->add(BOOST_TEST_CASE(&ip_udp_socket_compile::test));
|
|
test->add(BOOST_TEST_CASE(&ip_udp_socket_runtime::test));
|
|
return test;
|
|
}
|