-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.cpp
More file actions
243 lines (198 loc) · 7.31 KB
/
Copy pathhandler.cpp
File metadata and controls
243 lines (198 loc) · 7.31 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* File: /test/lib/packets/handler.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 <string>
#include <vector>
CPPLIB_MSVC_WARNING(disable:4355)
CPPLIB_MSVC_WARNING(disable:5204)
#include <future>
CPPLIB_MSVC_WARNING(default:5204)
CPPLIB_MSVC_WARNING(default:4355)
#include <cpp/lib_debug>
#include <cpp/lib_scope>
#include <lib/packets/handler.hpp>
#include <lib/impl/packets/ping_counter.hpp>
#include <lib/tl/listener.hpp>
#include <lib/types.hpp>
#include <lib/literals.hpp>
#include <lib/ptr.hpp>
#include <lib/data/serialize.hpp>
#include <lib/impl/serialize/std_contiguous_container.hpp>
#include <lib/impl/serialize/ptr.hpp>
#include <lib/impl/serialize/std_tuple.hpp>
#include <lib/impl/socket/tcp.hpp>
#include <lib/impl/stream/buffer.hpp>
#include "./handler.hpp"
namespace test::lib::packets {
namespace {
struct PacketBase : ::lib::tag_serializeable {};
struct Message : PacketBase {
static constexpr auto ID = 1;
Message() = default;
Message( const ::std::string & message, const ::std::string & title )
: title{ title }, message{ message } {}
::std::string title;
::std::string message;
::std::tuple<int, char> tuple;
inline ::lib::data::result_t serialized_size( ::lib::data::wstream_t & stream ) const override
{ return ::lib::data::serialized_size( stream, title, message ); }
inline bool can_deserialize( ::lib::data::rstream_t & stream ) const override
{ return ::lib::data::can_deserialize( stream, title, message ); }
inline ::lib::data::result_t serialize( ::lib::data::wstream_t & stream ) const override
{ return ::lib::data::serialize( stream, title, message ); }
inline ::lib::data::result_t deserialize( ::lib::data::rstream_t & stream ) override
{ return ::lib::data::deserialize( stream, title, message ); }
using PacketBase::deserialize;
};
class PacketsHandler
: public ::lib::packets::ReadHandler
, public ::lib::tag_tl_listener< PacketsHandler >
{
public:
PacketsHandler( ::lib::data::rwstream_t & stream_ )
: stream{ 128 }
, ping_counter{ 33, 42 }
{
listen( Message::ID, &message_packet, { *this, &PacketsHandler::onMessage } );
stream.reset( &stream_ );
reset( &stream );
ping_counter.attach( *this );
ping_counter.on_ping = { *this, &PacketsHandler::onPing };
}
~PacketsHandler() {
ping_counter.detach();
reset();
stream.reset();
}
bool update() { return stream.read_flush() and receive() and stream.write_flush(); }
::lib::u32 ping_count = 0;
::std::vector< ::std::string > messages;
private:
void onPing()
{ ++ping_count; }
bool onMessage( const ::lib::tag_serializeable & message_ ) {
const auto & message = static_cast<const Message&>( message_ );
CPP_ASSERT( &message_packet == &message );
messages.push_back( message.message );
return message.title == "client" or message.title == "server";
}
::lib::stream::impl::buffer stream;
Message message_packet;
::lib::packets::impl::PingCounter ping_counter;
};
} // namespace
void Handler::test_execute() noexcept/* override*/ {
static constexpr auto sleep = []( auto ms ) { ::std::this_thread::sleep_for( ms ); };
using namespace ::std::literals::chrono_literals;
constexpr ::lib::u32 SOCKET_ADDR = 0x7F000001;
constexpr ::lib::u16 SOCKET_PORT = 32002;
constexpr auto MSG_CLIENT_TO_SERVER = "This message has been sent from client to server.";
constexpr auto MSG_SERVER_TO_CLIENT = "This message has been sent from server to client.";
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.connect( SOCKET_ADDR, SOCKET_PORT ) );
PacketsHandler handler( client );
CPPLIB__TEST__TRUE( client.update() );
auto wait_ping_count = handler.ping_count + 2;
while ( wait_ping_count >= handler.ping_count ) {
CPPLIB__TEST__TRUE( client.update() );
CPPLIB__TEST__TRUE( handler.update() );
sleep( 5ms );
}
wait_ping_count = handler.ping_count + 2;
CPPLIB__TEST__TRUE( handler.send( Message{ MSG_CLIENT_TO_SERVER, "client" } ) );
while ( wait_ping_count >= handler.ping_count ) {
CPPLIB__TEST__TRUE( client.update() );
CPPLIB__TEST__TRUE( handler.update() );
sleep( 5ms );
}
wait_ping_count = handler.ping_count;
CPPLIB__TEST__EQ( handler.messages.size(), 1 );
CPPLIB__TEST__EQ( handler.messages.front(), MSG_SERVER_TO_CLIENT );
while ( wait_ping_count == handler.ping_count ) {
CPPLIB__TEST__TRUE( client.update() );
CPPLIB__TEST__TRUE( handler.update() );
sleep( 5ms );
}
} ); // 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() );
PacketsHandler handler( *client.obj );
CPPLIB__TEST__TRUE( server.update() or not client.obj->update() );
auto wait_ping_count = handler.ping_count + 2;
while ( wait_ping_count >= handler.ping_count ) {
CPPLIB__TEST__TRUE( server.update() );
CPPLIB__TEST__TRUE( client.obj->update() );
CPPLIB__TEST__TRUE( handler.update() );
sleep( 5ms );
}
CPPLIB__TEST__LOOP_RESET();
wait_ping_count = handler.ping_count + 2;
CPPLIB__TEST__TRUE( handler.send( Message{ MSG_SERVER_TO_CLIENT, "server" } ) );
while ( wait_ping_count >= handler.ping_count ) {
CPPLIB__TEST__TRUE( server.update() );
CPPLIB__TEST__TRUE( client.obj->update() );
CPPLIB__TEST__TRUE( handler.update() );
sleep( 5ms );
}
wait_ping_count = handler.ping_count;
CPPLIB__TEST__EQ( handler.messages.size(), 1 );
CPPLIB__TEST__EQ( handler.messages.front(), MSG_CLIENT_TO_SERVER );
while ( wait_ping_count == handler.ping_count ) {
CPPLIB__TEST__TRUE( server.update() );
CPPLIB__TEST__TRUE( client.obj->update() );
CPPLIB__TEST__TRUE( handler.update() );
sleep( 5ms );
}
client.obj.reset();
for ( auto i = 0; i < 10; ++i ) {
CPPLIB__TEST__LOOP_NEXT();
CPPLIB__TEST__TRUE( server.update() );
sleep( 10ms );
}
CPPLIB__TEST__LOOP_RESET();
server_failed = false;
}();
CPPLIB__TEST__FALSE( server_failed );
CPPLIB__TEST__TRUE( not client.obj or client.obj->close() );
CPPLIB__TEST__TRUE( server.close() );
}
} // namespace test::lib::packets