-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtcpk.cpp
203 lines (179 loc) · 6.01 KB
/
qtcpk.cpp
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
#include <string>
#include <map>
#include <stdexcept>
#include <iostream>
#include "qtcp.hpp"
#define KXVER 3
#include <k.h>
namespace {
inline K kerror(const char *err) {
return krr(const_cast<S>(err));
}
inline int k2int(K kobj) {
return kobj->t == -6?kobj->i:kobj->j;
}
std::string k2str(K kobj) {
return kobj->t == -11?std::string(kobj->s):std::string((char*)kC(kobj),kobj->n);
}
K pipeNotifyK(int handle) {
pipeNotify(handle);
return K(0);
}
}
void setPipeNotify(int handle) {
K result = sd1(-handle, pipeNotifyK);
if (result == nullptr) {
throw std::runtime_error("failed to install pipe notify handler");
}
r0(result);
}
void unsetPipeNotify(int handle) {
sd0x(handle, 0);
}
void reportConnFailed(const std::string &alias, const std::string &msg) {
K r = k(0, (char*)".tcp.connFailed", kp((char*)alias.c_str()), kp((char*)msg.c_str()), K(0));
if (r != nullptr) {
if (r->t == -128) {
std::cerr << ".tcp.connFailed threw error: " << r->s << std::endl;
}
r0(r);
}
}
void reportConnSuccess(const std::string &alias, int handle) {
K r = k(0, (char*)".tcp.connSuccess", kp((char*)alias.c_str()), ki(handle), K(0));
if (r != nullptr) {
if (r->t == -128) {
std::cerr << ".tcp.connSuccess threw error: " << r->s << std::endl;
}
r0(r);
}
}
void reportListenSuccess(const std::string &alias, int protocol, int handle) {
K r = k(0, (char*)".tcp.listenSuccess", kp((char*)alias.c_str()), ki(protocol), ki(handle), K(0));
if (r != nullptr) {
if (r->t == -128) {
std::cerr << ".tcp.listenSuccess threw error: " << r->s << std::endl;
}
r0(r);
}
}
void reportDisconnect(int handle) {
K r = k(0, (char*)".tcp.disconnect", ki(handle), K(0));
if (r != nullptr) {
if (r->t == -128) {
std::cerr << ".tcp.disconnect threw error: " << r->s << std::endl;
}
r0(r);
}
}
void reportMessage(int handle, const uint8_t *data, int len) {
K datak = ktn(KG, len);
memcpy(kG(datak), data, len);
K r = k(0, (char*)".tcp.receive", ki(handle), datak, K(0));
if (r != nullptr) {
if (r->t == -128) {
std::cerr << ".tcp.receive threw error: " << r->s << std::endl;
}
r0(r);
}
}
void reportNewClient(int handle, int newClient, const std::string &hostname) {
K r = k(0, (char*)".tcp.clientConnect", ki(handle), ki(newClient), kp((char*)hostname.c_str()), K(0));
if (r != nullptr) {
if (r->t == -128) {
std::cerr << ".tcp.clientConnect threw error: " << r->s << std::endl;
}
r0(r);
}
}
void reportUdpListenSuccess(const std::string &alias, int handle) {
K r = k(0, (char*)".udp.listenSuccess", kp((char*)alias.c_str()), ki(handle), K(0));
if (r != nullptr) {
if (r->t == -128) {
std::cerr << ".udp.listenSuccess threw error: " << r->s << std::endl;
}
r0(r);
}
}
void reportUdpMessage(int handle, int ip, int port, const uint8_t *data, int len) {
K datak = ktn(KG, len);
memcpy(kG(datak), data, len);
K r = k(0, (char*)".udp.receive", ki(handle), ki(ip), ki(port), datak, K(0));
if (r != nullptr) {
if (r->t == -128) {
std::cerr << ".udp.receive threw error: " << r->s << std::endl;
}
r0(r);
}
}
extern "C" {
K qtcp_connect(K alias, K host, K port) {
if (! (alias->t == -11 || alias->t == 10)) return kerror("wrong type for alias");
if (! (host->t == -11 || host->t == 10)) return kerror("wrong type for host");
if (! (port->t == -6 || port->t == -7)) return kerror("wrong type for port");
std::string aliasstr = k2str(alias);
std::string hoststr = k2str(host);
std::string portstr = std::to_string(k2int(port));
try {
addConnection(aliasstr, hoststr, portstr);
} catch(const std::exception &e) {
return kerror(e.what());
}
return K(0);
}
K qtcp_close(K handle) {
if (! (handle->t == -6 || handle->t == -7)) return kerror("wrong type for handle");
try {
closeConnection(k2int(handle));
} catch(const std::exception &e) {
return kerror(e.what());
}
return K(0);
}
K qtcp_send(K handle, K data) {
if (! (handle->t == -6 || handle->t == -7)) return kerror("wrong type for handle");
if (data->t != KG) return kerror("wrong type for data");
try {
sendData(k2int(handle), kG(data), data->n);
} catch(const std::exception &e) {
return kerror(e.what());
}
return K(0);
}
K qtcp_listen(K alias, K port) {
if (! (alias->t == -11 || alias->t == 10)) return kerror("wrong type for alias");
if (! (port->t == -6 || port->t == -7)) return kerror("wrong type for port");
std::string aliasstr = k2str(alias);
std::string portstr = std::to_string(k2int(port));
try {
std::string errs = addListener(aliasstr, portstr);
return kpn(&errs[0], errs.size());
} catch(const std::exception &e) {
return kerror(e.what());
}
}
K qudp_listen(K alias, K port) {
if (! (alias->t == -11 || alias->t == 10)) return kerror("wrong type for alias");
if (! (port->t == -6 || port->t == -7)) return kerror("wrong type for port");
std::string aliasstr = k2str(alias);
try {
addUdpListener(aliasstr, k2int(port));
} catch(const std::exception &e) {
return kerror(e.what());
}
return K(0);
}
K qudp_send(K handle, K host, K port, K data) {
if (! (handle->t == -6 || handle->t == -7)) return kerror("wrong type for handle");
if (! (host->t == -11 || host->t == 10)) return kerror("wrong type for host");
if (! (port->t == -6 || port->t == -7)) return kerror("wrong type for port");
std::string hoststr = k2str(host);
if (data->t != KG) return kerror("wrong type for data");
try {
sendUdpData(k2int(handle), hoststr, k2int(port), kG(data), data->n);
} catch(const std::exception &e) {
return kerror(e.what());
}
return K(0);
}
}