forked from parihaaraka/cpp2tnt
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconnector.h
More file actions
126 lines (102 loc) · 3.13 KB
/
connector.h
File metadata and controls
126 lines (102 loc) · 3.13 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
#pragma once
#include <string_view>
#include <unordered_map>
#include "mp_writer.h"
#include "mp_reader.h"
#include "proto.h"
#include "connection.h"
using namespace std;
namespace tnt
{
class Stream
{
public:
};
class Header
{
public:
uint64_t sync;
uint64_t errCode;
};
//template<typename... Args>
//void write(){};
class Connector : public connection, public iproto_writer
{
public:
class FuncParamTuple
{
public:
template<typename... Args>
FuncParamTuple(Args... args)
{
//std::make_tuple(args...);
}
};
typedef function<void(const Header& header, const mp_map_reader& body, void* userData)> OnFuncResult;
typedef function<void()> SimpleEventCallbak;
Connector();
template<typename UserData = void*, typename... Args>
void Call(string_view name, OnFuncResult&& resultHundler, UserData userData = (void*)nullptr, Args... args)
{
if (isConnected_)
{
static_assert (sizeof (HandlerData::userData_) >= sizeof (userData), "User data too big.");
constexpr size_t countArgs = sizeof...(args);
begin_call(name);
begin_array(countArgs);
write(args...);
//if (countArgs)
finalize();
finalize();
HandlerData handler;
handler.handler_ = move(resultHundler);
*((decltype (userData)*)&handler.userData_) = move(userData);
handlers_[last_request_id()] = move(handler);
flush();
}
else
{
wtf_buffer buff;
mp_writer writer(buff);
writer.begin_map(1);
writer<<int(tnt::response_field::ERROR)<<"disconected";
writer.finalize();
Header header;
header.errCode = 77;
header.sync = 0;
mp_reader reader(buff);
auto body = reader.read<mp_map_reader>();
resultHundler(header, body, &userData);
}
}
void write()
{}
template<typename T, typename... Args>
void write(T& arg, Args... args)
{
*this<<arg;
write(args...);
}
void AddOnOpened(SimpleEventCallbak cb_);
void AddOnClosed(SimpleEventCallbak cb_);
void close(bool reconnect_soon = false) noexcept;
bool IsConnected();
protected:
class HandlerData
{
public:
OnFuncResult handler_;
uint64_t userData_[2];
};
unordered_map<uint64_t, HandlerData> handlers_;
vector<SimpleEventCallbak> onOpenedHandlers_;
vector<SimpleEventCallbak> onClosedHandlers_;
bool isConnected_ = false;
void OnResponse(wtf_buffer &buf);
void OnOpened();
void OnClosed();
bool isNeedsClose_ = false;
bool isNeedsReconnect_ = false;
bool isProcessingReply_ = false;
};
}