-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp.cpp
More file actions
169 lines (136 loc) · 5.39 KB
/
Copy pathtcp.cpp
File metadata and controls
169 lines (136 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* File: /test/lib/impl/socket/tcp.cpp
*
* This file is a part of cpplib project which is distributed under MIT License.
* See file LICENSE for full license details.
*
* Copyright (c) 2020-present Nikita Zuev (V.Slavski!) <[email protected]>
*/
#include <lib/debug/features.hpp>
#include <cstring>
CPPLIB_MSVC_WARNING(disable:4355)
CPPLIB_MSVC_WARNING(disable:5204)
#include <future>
CPPLIB_MSVC_WARNING(default:5204)
CPPLIB_MSVC_WARNING(default:4355)
#include <string>
#include <cpp/lib_scope>
#include <lib/data/serialize.hpp>
#include <lib/impl/serialize/std_contiguous_container.hpp>
#include <lib/impl/socket/tcp.hpp>
#include "./tcp.hpp"
namespace test::lib::socket::impl {
void Tcp::test_execute() noexcept/* override*/ {
using namespace ::std::literals::chrono_literals;
static constexpr auto sleep = []( auto ms ) { ::std::this_thread::sleep_for( ms ); };
constexpr ::lib::u32 SOCKET_ADDR = 0x7F000001;
constexpr ::lib::u16 SOCKET_PORT = 32001;
const ::std::wstring FOO( L"server -> client" );
constexpr char BAR[] = "client -> server";
constexpr int CLIENT_STOP = 313373;
::std::atomic<bool> emergency_client_stop = false;
auto client_future = ::std::async( ::std::launch::async, [&, this]() {
// Give server some time to get ready.
sleep( 10ms );
::lib::socket::impl::tcp::client client;
const auto error_message = ::cpp::scope_exit {[&]() {
test_error( client.error() );
}};
CPPLIB__TEST__TRUE( client.update() );
CPPLIB__TEST__TRUE( client.connect( SOCKET_ADDR, SOCKET_PORT ) );
CPPLIB__TEST__TRUE( client.update() );
auto count = client.write({ BAR, ::std::strlen( BAR ) + 1 });
CPPLIB__TEST__EQ( count, sizeof(BAR) );
CPPLIB__TEST__TRUE( client.update() );
::std::wstring foo;
while ( not emergency_client_stop
and client.update()
and not ::lib::data::can_deserialize( client, foo ) )
sleep( 10ms );
CPPLIB__TEST__TRUE( client.update() );
auto deserialize_result = ::lib::data::deserialize( client, foo );
CPPLIB__TEST__EQ( deserialize_result, ::lib::data::serialized_size( client, foo ) );
CPPLIB__TEST__EQ( foo, FOO );
CPPLIB__TEST__TRUE( client.update() );
while ( not emergency_client_stop
and client.update()
and not ::lib::data::can_deserialize( client, CLIENT_STOP ) )
sleep( 100ms );
CPPLIB__TEST__TRUE( client.update() );
int client_stop;
deserialize_result = ::lib::data::deserialize( client, client_stop );
CPPLIB__TEST__EQ( deserialize_result, ::lib::data::serialized_size( client, client_stop ) );
CPPLIB__TEST__EQ( client_stop, CLIENT_STOP );
CPPLIB__TEST__TRUE( client.update() );
CPPLIB__TEST__TRUE( client.close() );
CPPLIB__TEST__TRUE( client.update() );
} ); // client_future
struct Client : ::lib::tag_tl_listener< Client > {
using SockSrv = ::lib::socket::server;
void onNew( const SockSrv&, SockSrv::NewClientAction & action ) {
action = SockSrv::NewClientAction::ACCEPT;
}
void onStateChanged( const SockSrv&, const ::lib::SPtr<SockSrv::client> & client_ ) {
obj = client_;
}
::lib::SPtr<::lib::socket::socket> obj;
} client;
::lib::socket::impl::tcp::server server( SOCKET_ADDR, SOCKET_PORT );
const auto error_message = ::cpp::scope_exit {[&]() {
test_error( server.error() );
if ( client.obj )
test_error( client.obj->error() );
}};
server.on_new_client = { client, &Client::onNew };
server.on_client_state_changed = { client, &Client::onStateChanged };
bool server_failed = true;
[&, this](){
CPPLIB__TEST__EQ( server.getState(), ::lib::socket::server::State::BOUND );
CPPLIB__TEST__TRUE( server.listen() );
for ( auto i = 0; i < 10; ++i ) {
CPPLIB__TEST__LOOP_NEXT();
CPPLIB__TEST__TRUE( server.update() );
sleep( 10ms );
}
CPPLIB__TEST__LOOP_RESET();
CPPLIB__TEST__TRUE( client.obj );
CPPLIB__TEST__TRUE( server.update() );
auto serialize_result = ::lib::data::serialize( *client.obj, FOO );
CPPLIB__TEST__EQ( serialize_result, ::lib::data::serialized_size( *client.obj, FOO ) );
::std::string bar( sizeof(BAR) - 1, '\0' );
{
::lib::usize count = 0;
while ( count < bar.size() ) {
CPPLIB__TEST__LOOP_NEXT();
const auto & peek_result = client.obj->peek( bar );
CPPLIB__TEST__TRUE( peek_result.success() );
count = peek_result.value();
CPPLIB_MSVC_WARNING(disable:4130)
CPPLIB__TEST__EQ( ::std::strstr( BAR, bar.c_str() ), BAR );
CPPLIB_MSVC_WARNING(default:4130)
sleep( 10ms );
}
CPPLIB__TEST__LOOP_RESET();
}
CPPLIB__TEST__EQ( bar, BAR );
const auto & read_size = client.obj->read_size();
CPPLIB__TEST__TRUE( read_size.success() );
CPPLIB__TEST__GE( read_size, bar.size() );
CPPLIB__TEST__EQ( client.obj->read( bar ), bar.size() );
CPPLIB__TEST__EQ( BAR, bar );
serialize_result = ::lib::data::serialize( *client.obj, CLIENT_STOP );
CPPLIB__TEST__EQ( serialize_result, ::lib::data::serialized_size( *client.obj, CLIENT_STOP ) );
CPPLIB__TEST__TRUE( client.obj->shutdown() );
CPPLIB__TEST__FALSE( client.obj->is_open() );
client.obj.reset();
CPPLIB__TEST__TRUE( server.shutdown() );
CPPLIB__TEST__FALSE( server.is_open() );
server_failed = false;
CPPLIB_MSVC_WARNING(disable:4130) /**< See above. For some reason warning appears here if lambda used. */
}();
CPPLIB_MSVC_WARNING(default:4130)
emergency_client_stop = true;
CPPLIB__TEST__FALSE( server_failed );
CPPLIB__TEST__TRUE( not client.obj or client.obj->close() );
CPPLIB__TEST__TRUE( server.close() );
}
} // namespace test::lib::socket::impl