Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
actor-incubator
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cpp-libs
actor-incubator
Commits
ec2fe898
Commit
ec2fe898
authored
Sep 17, 2019
by
Jakob Otto
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' into topic/transport-worker-dispatcher
parents
69ec093f
1fb4c58c
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
545 additions
and
0 deletions
+545
-0
libcaf_net/CMakeLists.txt
libcaf_net/CMakeLists.txt
+2
-0
libcaf_net/caf/detail/convert_ip_endpoint.hpp
libcaf_net/caf/detail/convert_ip_endpoint.hpp
+32
-0
libcaf_net/caf/net/udp_datagram_socket.hpp
libcaf_net/caf/net/udp_datagram_socket.hpp
+83
-0
libcaf_net/src/convert_ip_endpoint.cpp
libcaf_net/src/convert_ip_endpoint.cpp
+63
-0
libcaf_net/src/udp_datagram_socket.cpp
libcaf_net/src/udp_datagram_socket.cpp
+140
-0
libcaf_net/test/convert_ip_endpoint.cpp
libcaf_net/test/convert_ip_endpoint.cpp
+112
-0
libcaf_net/test/udp_datagram_socket.cpp
libcaf_net/test/udp_datagram_socket.cpp
+113
-0
No files found.
libcaf_net/CMakeLists.txt
View file @
ec2fe898
...
@@ -19,6 +19,8 @@ set(LIBCAF_NET_SRCS
...
@@ -19,6 +19,8 @@ set(LIBCAF_NET_SRCS
src/socket.cpp
src/socket.cpp
src/socket_manager.cpp
src/socket_manager.cpp
src/stream_socket.cpp
src/stream_socket.cpp
src/convert_ip_endpoint.cpp
src/udp_datagram_socket.cpp
src/tcp_stream_socket.cpp
src/tcp_stream_socket.cpp
src/tcp_accept_socket.cpp
src/tcp_accept_socket.cpp
)
)
...
...
libcaf_net/caf/detail/convert_ip_endpoint.hpp
0 → 100644
View file @
ec2fe898
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2019 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#pragma once
#include "caf/detail/socket_sys_includes.hpp"
#include "caf/fwd.hpp"
namespace
caf
{
namespace
detail
{
void
convert
(
const
ip_endpoint
&
src
,
sockaddr_storage
&
dst
);
error
convert
(
const
sockaddr_storage
&
src
,
ip_endpoint
&
dst
);
}
// namespace detail
}
// namespace caf
libcaf_net/caf/net/udp_datagram_socket.hpp
0 → 100644
View file @
ec2fe898
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2019 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#pragma once
#include "caf/fwd.hpp"
#include "caf/ip_endpoint.hpp"
#include "caf/net/network_socket.hpp"
namespace
caf
{
namespace
net
{
/// A datagram-oriented network communication endpoint for bidirectional
/// byte transmission.
struct
udp_datagram_socket
:
abstract_socket
<
udp_datagram_socket
>
{
using
super
=
abstract_socket
<
udp_datagram_socket
>
;
using
super
::
super
;
constexpr
operator
socket
()
const
noexcept
{
return
socket
{
id
};
}
constexpr
operator
network_socket
()
const
noexcept
{
return
network_socket
{
id
};
}
};
/// Creates a `udp_datagram_socket` bound to given port.
/// @param node ip_endpoint that contains the port to bind to. Pass port '0' to
/// bind to any unused port - The endpoint will be updated with the specific
/// port that was bound.
/// @returns The connected socket or an error.
/// @relates udp_datagram_socket
expected
<
std
::
pair
<
udp_datagram_socket
,
uint16_t
>>
make_udp_datagram_socket
(
ip_endpoint
ep
,
bool
reuse_addr
=
false
);
/// Enables or disables `SIO_UDP_CONNRESET` error on `x`.
/// @relates udp_datagram_socket
error
allow_connreset
(
udp_datagram_socket
x
,
bool
new_value
);
/// Receives the next datagram on socket `x`.
/// @param x The UDP socket for receiving datagrams.
/// @param buf Writable output buffer.
/// @returns The number of received bytes and the sender as `ip_endpoint` on
/// success, an error code otherwise.
/// @relates udp_datagram_socket
/// @post buf was modified and the resulting integer represents the length of
/// the received datagram, even if it did not fit into the given buffer.
variant
<
std
::
pair
<
size_t
,
ip_endpoint
>
,
sec
>
read
(
udp_datagram_socket
x
,
span
<
byte
>
buf
);
/// Sends the content of `buf` as a datagram to the endpoint `ep` on socket `x`.
/// @param x The UDP socket for sending datagrams.
/// @param buf The buffer to send.
/// @returns The number of written bytes on success, otherwise an error code.
/// @relates udp_datagram_socket
variant
<
size_t
,
sec
>
write
(
udp_datagram_socket
x
,
span
<
const
byte
>
buf
,
ip_endpoint
ep
);
/// Converts the result from I/O operation on a ::udp_datagram_socket to either
/// an error code or a non-zero positive integer.
/// @relates udp_datagram_socket
variant
<
size_t
,
sec
>
check_udp_datagram_socket_io_res
(
std
::
make_signed
<
size_t
>::
type
res
);
}
// namespace net
}
// namespace caf
libcaf_net/src/convert_ip_endpoint.cpp
0 → 100644
View file @
ec2fe898
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2019 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#include "caf/detail/convert_ip_endpoint.hpp"
#include "caf/error.hpp"
#include "caf/ipv4_endpoint.hpp"
#include "caf/ipv6_endpoint.hpp"
#include "caf/sec.hpp"
namespace
caf
{
namespace
detail
{
void
convert
(
const
ip_endpoint
&
src
,
sockaddr_storage
&
dst
)
{
if
(
src
.
address
().
embeds_v4
())
{
auto
sockaddr4
=
reinterpret_cast
<
sockaddr_in
*>
(
&
dst
);
sockaddr4
->
sin_family
=
AF_INET
;
sockaddr4
->
sin_port
=
ntohs
(
src
.
port
());
sockaddr4
->
sin_addr
.
s_addr
=
src
.
address
().
embedded_v4
().
bits
();
}
else
{
auto
sockaddr6
=
reinterpret_cast
<
sockaddr_in6
*>
(
&
dst
);
sockaddr6
->
sin6_family
=
AF_INET6
;
sockaddr6
->
sin6_port
=
ntohs
(
src
.
port
());
memcpy
(
&
sockaddr6
->
sin6_addr
,
src
.
address
().
bytes
().
data
(),
src
.
address
().
bytes
().
size
());
}
}
error
convert
(
const
sockaddr_storage
&
src
,
ip_endpoint
&
dst
)
{
if
(
src
.
ss_family
==
AF_INET
)
{
auto
sockaddr4
=
reinterpret_cast
<
const
sockaddr_in
&>
(
src
);
ipv4_address
ipv4_addr
;
memcpy
(
ipv4_addr
.
data
().
data
(),
&
sockaddr4
.
sin_addr
,
ipv4_addr
.
size
());
dst
=
ip_endpoint
{
ipv4_addr
,
htons
(
sockaddr4
.
sin_port
)};
}
else
if
(
src
.
ss_family
==
AF_INET6
)
{
auto
sockaddr6
=
reinterpret_cast
<
const
sockaddr_in6
&>
(
src
);
ipv6_address
ipv6_addr
;
memcpy
(
ipv6_addr
.
bytes
().
data
(),
&
sockaddr6
.
sin6_addr
,
ipv6_addr
.
bytes
().
size
());
dst
=
ip_endpoint
{
ipv6_addr
,
htons
(
sockaddr6
.
sin6_port
)};
}
else
{
return
sec
::
invalid_argument
;
}
return
none
;
}
}
// namespace detail
}
// namespace caf
libcaf_net/src/udp_datagram_socket.cpp
0 → 100644
View file @
ec2fe898
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2019 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#include "caf/net/udp_datagram_socket.hpp"
#include "caf/byte.hpp"
#include "caf/detail/convert_ip_endpoint.hpp"
#include "caf/detail/net_syscall.hpp"
#include "caf/detail/socket_sys_aliases.hpp"
#include "caf/detail/socket_sys_includes.hpp"
#include "caf/expected.hpp"
#include "caf/ip_endpoint.hpp"
#include "caf/logger.hpp"
#include "caf/net/socket_guard.hpp"
#include "caf/span.hpp"
namespace
caf
{
namespace
net
{
#ifdef CAF_WINDOWS
error
allow_connreset
(
udp_datagram_socket
x
,
bool
new_value
)
{
CAF_LOG_TRACE
(
CAF_ARG
(
x
)
<<
CAF_ARG
(
new_value
));
DWORD
bytes_returned
=
0
;
CAF_NET_SYSCALL
(
"WSAIoctl"
,
res
,
!=
,
0
,
WSAIoctl
(
x
.
id
,
_WSAIOW
(
IOC_VENDOR
,
12
),
&
new_value
,
sizeof
(
new_value
),
NULL
,
0
,
&
bytes_returned
,
NULL
,
NULL
));
return
none
;
}
#else // CAF_WINDOWS
error
allow_connreset
(
udp_datagram_socket
x
,
bool
)
{
if
(
socket_cast
<
net
::
socket
>
(
x
)
==
invalid_socket
)
return
sec
::
socket_invalid
;
// nop; SIO_UDP_CONNRESET only exists on Windows
return
none
;
}
#endif // CAF_WINDOWS
expected
<
std
::
pair
<
udp_datagram_socket
,
uint16_t
>>
make_udp_datagram_socket
(
ip_endpoint
ep
,
bool
reuse_addr
)
{
CAF_LOG_TRACE
(
CAF_ARG
(
ep
));
sockaddr_storage
addr
;
detail
::
convert
(
ep
,
addr
);
CAF_NET_SYSCALL
(
"socket"
,
fd
,
==
,
invalid_socket_id
,
::
socket
(
addr
.
ss_family
,
SOCK_DGRAM
,
0
));
udp_datagram_socket
sock
{
fd
};
auto
sguard
=
make_socket_guard
(
sock
);
socklen_t
len
=
(
addr
.
ss_family
==
AF_INET
)
?
sizeof
(
sockaddr_in
)
:
sizeof
(
sockaddr_in6
);
if
(
reuse_addr
)
{
int
on
=
1
;
CAF_NET_SYSCALL
(
"setsockopt"
,
tmp1
,
!=
,
0
,
setsockopt
(
fd
,
SOL_SOCKET
,
SO_REUSEADDR
,
reinterpret_cast
<
setsockopt_ptr
>
(
&
on
),
static_cast
<
socket_size_type
>
(
sizeof
(
on
))));
}
CAF_NET_SYSCALL
(
"bind"
,
err
,
!=
,
0
,
::
bind
(
sock
.
id
,
reinterpret_cast
<
sockaddr
*>
(
&
addr
),
len
));
CAF_NET_SYSCALL
(
"getsockname"
,
erro
,
!=
,
0
,
getsockname
(
sock
.
id
,
reinterpret_cast
<
sockaddr
*>
(
&
addr
),
&
len
));
CAF_LOG_DEBUG
(
CAF_ARG
(
sock
.
id
));
auto
port
=
addr
.
ss_family
==
AF_INET
?
reinterpret_cast
<
sockaddr_in
*>
(
&
addr
)
->
sin_port
:
reinterpret_cast
<
sockaddr_in6
*>
(
&
addr
)
->
sin6_port
;
return
std
::
make_pair
(
sguard
.
release
(),
port
);
}
variant
<
std
::
pair
<
size_t
,
ip_endpoint
>
,
sec
>
read
(
udp_datagram_socket
x
,
span
<
byte
>
buf
)
{
sockaddr_storage
addr
=
{};
socklen_t
len
=
sizeof
(
sockaddr_storage
);
auto
res
=
::
recvfrom
(
x
.
id
,
reinterpret_cast
<
socket_recv_ptr
>
(
buf
.
data
()),
buf
.
size
(),
0
,
reinterpret_cast
<
sockaddr
*>
(
&
addr
),
&
len
);
auto
ret
=
check_udp_datagram_socket_io_res
(
res
);
if
(
auto
num_bytes
=
get_if
<
size_t
>
(
&
ret
))
{
CAF_LOG_INFO_IF
(
*
num_bytes
==
0
,
"Received empty datagram"
);
CAF_LOG_WARNING_IF
(
*
num_bytes
>
buf
.
size
(),
"recvfrom cut of message, only received "
<<
CAF_ARG
(
buf
.
size
())
<<
" of "
<<
CAF_ARG
(
num_bytes
)
<<
" bytes"
);
ip_endpoint
ep
;
// TODO: how to properly pass error further?
if
(
auto
err
=
detail
::
convert
(
addr
,
ep
))
return
sec
::
runtime_error
;
return
std
::
pair
<
size_t
,
ip_endpoint
>
(
*
num_bytes
,
ep
);
}
else
{
return
get
<
sec
>
(
ret
);
}
}
variant
<
size_t
,
sec
>
write
(
udp_datagram_socket
x
,
span
<
const
byte
>
buf
,
ip_endpoint
ep
)
{
sockaddr_storage
addr
;
detail
::
convert
(
ep
,
addr
);
auto
res
=
::
sendto
(
x
.
id
,
reinterpret_cast
<
socket_send_ptr
>
(
buf
.
data
()),
buf
.
size
(),
0
,
reinterpret_cast
<
sockaddr
*>
(
&
addr
),
ep
.
address
().
embeds_v4
()
?
sizeof
(
sockaddr_in
)
:
sizeof
(
sockaddr_in6
));
auto
ret
=
check_udp_datagram_socket_io_res
(
res
);
if
(
auto
num_bytes
=
get_if
<
size_t
>
(
&
ret
))
return
*
num_bytes
;
else
return
get
<
sec
>
(
ret
);
}
variant
<
size_t
,
sec
>
check_udp_datagram_socket_io_res
(
std
::
make_signed
<
size_t
>::
type
res
)
{
if
(
res
<
0
)
{
auto
code
=
last_socket_error
();
if
(
code
==
std
::
errc
::
operation_would_block
||
code
==
std
::
errc
::
resource_unavailable_try_again
)
return
sec
::
unavailable_or_would_block
;
return
sec
::
socket_operation_failed
;
}
return
static_cast
<
size_t
>
(
res
);
}
}
// namespace net
}
// namespace caf
libcaf_net/test/convert_ip_endpoint.cpp
0 → 100644
View file @
ec2fe898
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2019 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#define CAF_SUITE convert_ip_endpoint
#include "caf/detail/convert_ip_endpoint.hpp"
#include "caf/test/dsl.hpp"
#include "host_fixture.hpp"
#include <cstring>
#include "caf/detail/socket_sys_includes.hpp"
#include "caf/ipv4_endpoint.hpp"
#include "caf/ipv6_endpoint.hpp"
using
namespace
caf
;
using
namespace
caf
::
detail
;
namespace
{
struct
fixture
:
host_fixture
{
fixture
()
:
host_fixture
()
{
memset
(
&
sockaddr6_src
,
0
,
sizeof
(
sockaddr_in6
));
memset
(
&
sockaddr6_dst
,
0
,
sizeof
(
sockaddr_in6
));
sockaddr6_src
.
sin6_family
=
AF_INET6
;
sockaddr6_src
.
sin6_port
=
htons
(
23
);
sockaddr6_src
.
sin6_addr
=
in6addr_loopback
;
memset
(
&
sockaddr4_src
,
0
,
sizeof
(
sockaddr_in
));
memset
(
&
sockaddr4_dst
,
0
,
sizeof
(
sockaddr_in
));
sockaddr4_src
.
sin_family
=
AF_INET
;
sockaddr4_src
.
sin_port
=
htons
(
23
);
sockaddr4_src
.
sin_addr
.
s_addr
=
INADDR_LOOPBACK
;
}
sockaddr_in6
sockaddr6_src
;
sockaddr_in6
sockaddr6_dst
;
sockaddr_in
sockaddr4_src
;
sockaddr_in
sockaddr4_dst
;
ip_endpoint
ep_src
;
ip_endpoint
ep_dst
;
};
}
// namespace
CAF_TEST_FIXTURE_SCOPE
(
convert_ip_endpoint_tests
,
fixture
)
CAF_TEST
(
sockaddr_in6
roundtrip
)
{
ip_endpoint
ep
;
CAF_MESSAGE
(
"converting sockaddr_in6 to ip_endpoint"
);
CAF_CHECK_EQUAL
(
convert
(
reinterpret_cast
<
sockaddr_storage
&>
(
sockaddr6_src
),
ep
),
none
);
CAF_MESSAGE
(
"converting ip_endpoint to sockaddr_in6"
);
convert
(
ep
,
reinterpret_cast
<
sockaddr_storage
&>
(
sockaddr6_dst
));
CAF_CHECK_EQUAL
(
memcmp
(
&
sockaddr6_src
,
&
sockaddr6_dst
,
sizeof
(
sockaddr_in6
)),
0
);
}
CAF_TEST
(
ipv6_endpoint
roundtrip
)
{
sockaddr_storage
addr
;
memset
(
&
addr
,
0
,
sizeof
(
sockaddr_storage
));
if
(
auto
err
=
detail
::
parse
(
"[::1]:55555"
,
ep_src
))
CAF_FAIL
(
"unable to parse input: "
<<
err
);
CAF_MESSAGE
(
"converting ip_endpoint to sockaddr_in6"
);
convert
(
ep_src
,
addr
);
CAF_MESSAGE
(
"converting sockaddr_in6 to ip_endpoint"
);
CAF_CHECK_EQUAL
(
convert
(
addr
,
ep_dst
),
none
);
CAF_CHECK_EQUAL
(
ep_src
,
ep_dst
);
}
CAF_TEST
(
sockaddr_in4
roundtrip
)
{
ip_endpoint
ep
;
CAF_MESSAGE
(
"converting sockaddr_in to ip_endpoint"
);
CAF_CHECK_EQUAL
(
convert
(
reinterpret_cast
<
sockaddr_storage
&>
(
sockaddr4_src
),
ep
),
none
);
CAF_MESSAGE
(
"converting ip_endpoint to sockaddr_in"
);
convert
(
ep
,
reinterpret_cast
<
sockaddr_storage
&>
(
sockaddr4_dst
));
CAF_CHECK_EQUAL
(
memcmp
(
&
sockaddr4_src
,
&
sockaddr4_dst
,
sizeof
(
sockaddr_in
)),
0
);
}
CAF_TEST
(
ipv4_endpoint
roundtrip
)
{
sockaddr_storage
addr
;
memset
(
&
addr
,
0
,
sizeof
(
sockaddr_storage
));
if
(
auto
err
=
detail
::
parse
(
"127.0.0.1:55555"
,
ep_src
))
CAF_FAIL
(
"unable to parse input: "
<<
err
);
CAF_MESSAGE
(
"converting ip_endpoint to sockaddr_in"
);
convert
(
ep_src
,
addr
);
CAF_MESSAGE
(
"converting sockaddr_in to ip_endpoint"
);
CAF_CHECK_EQUAL
(
convert
(
addr
,
ep_dst
),
none
);
CAF_CHECK_EQUAL
(
ep_src
,
ep_dst
);
}
CAF_TEST_FIXTURE_SCOPE_END
();
libcaf_net/test/udp_datagram_socket.cpp
0 → 100644
View file @
ec2fe898
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2019 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#define CAF_SUITE udp_datagram_socket
#include "caf/net/udp_datagram_socket.hpp"
#include "caf/test/dsl.hpp"
#include "host_fixture.hpp"
#include "caf/detail/net_syscall.hpp"
#include "caf/detail/socket_sys_includes.hpp"
#include "caf/ip_address.hpp"
#include "caf/ipv4_address.hpp"
#include "caf/net/ip.hpp"
#include "caf/net/socket_guard.hpp"
using
namespace
caf
;
using
namespace
caf
::
net
;
namespace
{
constexpr
string_view
hello_test
=
"Hello test!"
;
struct
fixture
:
host_fixture
{
fixture
()
:
host_fixture
(),
v4_local
{
make_ipv4_address
(
127
,
0
,
0
,
1
)},
v6_local
{{
0
},
{
0x1
}},
// TODO: use local_addresses() when merged
addrs
{
net
::
ip
::
resolve
(
"localhost"
)}
{
}
bool
contains
(
ip_address
x
)
{
return
std
::
count
(
addrs
.
begin
(),
addrs
.
end
(),
x
)
>
0
;
}
void
test_send_receive
(
ip_address
addr
)
{
std
::
vector
<
byte
>
buf
(
1024
);
ip_endpoint
ep
(
addr
,
0
);
auto
send_pair
=
unbox
(
make_udp_datagram_socket
(
ep
));
auto
send_socket
=
send_pair
.
first
;
auto
receive_pair
=
unbox
(
make_udp_datagram_socket
(
ep
));
auto
receive_socket
=
receive_pair
.
first
;
ep
.
port
(
ntohs
(
receive_pair
.
second
));
CAF_MESSAGE
(
"sending data to: "
<<
to_string
(
ep
));
auto
send_guard
=
make_socket_guard
(
send_socket
);
auto
receive_guard
=
make_socket_guard
(
receive_socket
);
if
(
auto
err
=
nonblocking
(
socket_cast
<
net
::
socket
>
(
receive_socket
),
true
))
CAF_FAIL
(
"nonblocking returned an error"
<<
err
);
auto
test_read_res
=
read
(
receive_socket
,
make_span
(
buf
));
if
(
auto
err
=
get_if
<
sec
>
(
&
test_read_res
))
CAF_CHECK_EQUAL
(
*
err
,
sec
::
unavailable_or_would_block
);
else
CAF_FAIL
(
"read should have failed"
);
auto
write_ret
=
write
(
send_socket
,
as_bytes
(
make_span
(
hello_test
)),
ep
);
if
(
auto
num_bytes
=
get_if
<
size_t
>
(
&
write_ret
))
CAF_CHECK_EQUAL
(
*
num_bytes
,
hello_test
.
size
());
else
CAF_FAIL
(
"write returned an error: "
<<
sys
.
render
(
get
<
sec
>
(
write_ret
)));
auto
read_ret
=
read
(
receive_socket
,
make_span
(
buf
));
if
(
auto
read_res
=
get_if
<
std
::
pair
<
size_t
,
ip_endpoint
>>
(
&
read_ret
))
{
CAF_CHECK_EQUAL
(
read_res
->
first
,
hello_test
.
size
());
buf
.
resize
(
read_res
->
first
);
}
else
{
CAF_FAIL
(
"write returned an error: "
<<
sys
.
render
(
get
<
sec
>
(
read_ret
)));
}
string_view
buf_view
{
reinterpret_cast
<
const
char
*>
(
buf
.
data
()),
buf
.
size
()};
CAF_CHECK_EQUAL
(
buf_view
,
hello_test
);
}
actor_system_config
cfg
;
actor_system
sys
{
cfg
};
ip_address
v4_local
;
ip_address
v6_local
;
std
::
vector
<
ip_address
>
addrs
;
};
}
// namespace
CAF_TEST_FIXTURE_SCOPE
(
udp_datagram_socket_test
,
fixture
)
CAF_TEST
(
send
and
receive
)
{
// TODO: check which versions exist and test existing versions accordingly
// -> local_addresses()
if
(
contains
(
v4_local
))
{
test_send_receive
(
v4_local
);
}
else
if
(
contains
(
v6_local
))
{
test_send_receive
(
v6_local
);
}
else
{
CAF_FAIL
(
"could not resolve 'localhost'"
);
}
}
CAF_TEST_FIXTURE_SCOPE_END
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment