-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.hpp
More file actions
71 lines (57 loc) · 1.59 KB
/
Copy pathclient.hpp
File metadata and controls
71 lines (57 loc) · 1.59 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
/* File: /lib/socket/client.hpp
*
* 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]>
*/
#ifndef CPPLIB__lib__socket__client__hpp
#define CPPLIB__lib__socket__client__hpp
#include "../../lib/tl/listener.hpp"
#include "../../lib/utils/enum.hpp"
#include "../../lib/utils/string.hpp"
#include "./socket.hpp"
namespace lib::socket {
// DECLARATION lib::socket::client
/// @todo Fix callback recursion, if any. e.g. if close() failed it fires state change,
/// which could possibly lead to callback loop. Need to ensure.
class client
: public virtual socket
{
public:
LIB_UTILS_ENUM( State
, INITIAL
, CONNECTING
, CONNECTED
, DISCONNECTING
, DISCONNECTED
, FAILED
)
client() = default;
virtual ~client() = default;
State getState() const noexcept { return state; }
bool isFailed() const noexcept { return state == State::FAILED; }
bool isActive() const noexcept;
tl::listener< void, const client& > on_state_changed;
protected:
virtual void setState( State new_state );
virtual void onStateChanged();
private:
State state = State::INITIAL;
};
} // namespace lib::socket
LIB_UTILS_ENUM_NAMES( lib::socket::client::State
, "initial"
, "connecting"
, "connected"
, "disconnecting"
, "disconnected"
, "failed"
)
// SPECIALIZATION lib::str()
#include <string>
namespace lib {
inline ::std::string str( const lib::socket::client & client )
{ return str( client.getName() ); }
} // namespace lib
#endif // CPPLIB__lib__socket__client__hpp