aboutsummaryrefslogtreecommitdiff
path: root/src/network.cpp
blob: 921ea033c6c6977d765502c6433f95b557d97707 (plain) (blame)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "salticidae/config.h"
#ifdef SALTICIDAE_CBINDINGS
#include "salticidae/network.h"

using namespace salticidae;

extern "C" {

// MsgNetwork

msgnetwork_config_t *msgnetwork_config_new() {
    return new msgnetwork_config_t();
}

void msgnetwork_config_free(const msgnetwork_config_t *self) { delete self; }

msgnetwork_t *msgnetwork_new(const eventcontext_t *ec, const msgnetwork_config_t *config, SalticidaeCError *cerror) {
    SALTICIDAE_CERROR_TRY(cerror)
    return new msgnetwork_t(*ec, *config);
    SALTICIDAE_CERROR_CATCH(cerror)
    return nullptr;
}

void msgnetwork_free(const msgnetwork_t *self) { delete self; }

void msgnetwork_config_max_msg_size(msgnetwork_config_t *self, size_t size) {
    self->max_msg_size(size);
}

void msgnetwork_config_max_msg_queue_size(msgnetwork_config_t *self, size_t size) {
    self->max_msg_queue_size(size);
}

void msgnetwork_config_burst_size(msgnetwork_config_t *self, size_t burst_size) {
    self->burst_size(burst_size);
}

void msgnetwork_config_max_listen_backlog(msgnetwork_config_t *self, int backlog) {
    self->max_listen_backlog(backlog);
}

void msgnetwork_config_conn_server_timeout(msgnetwork_config_t *self, double timeout) {
    self->conn_server_timeout(timeout);
}

void msgnetwork_config_recv_chunk_size(msgnetwork_config_t *self, size_t size) {
    self->recv_chunk_size(size);
}

void msgnetwork_config_nworker(msgnetwork_config_t *self, size_t nworker) {
    self->nworker(nworker);
}

void msgnetwork_config_max_recv_buff_size(msgnetwork_config_t *self, size_t size) {
    self->max_recv_buff_size(size);
}

void msgnetwork_config_max_send_buff_size(msgnetwork_config_t *self, size_t size) {
    self->max_send_buff_size(size);
}

void msgnetwork_config_enable_tls(msgnetwork_config_t *self, bool enabled) {
    self->enable_tls(enabled);
}

void msgnetwork_config_tls_key_file(msgnetwork_config_t *self, const char *pem_fname) {
    self->tls_key_file(pem_fname);
}

void msgnetwork_config_tls_cert_file(msgnetwork_config_t *self, const char *pem_fname) {
    self->tls_cert_file(pem_fname);
}

void msgnetwork_config_tls_key_by_move(msgnetwork_config_t *self, pkey_t *key) {
    self->tls_key(new pkey_t(std::move(*key)));
}

void msgnetwork_config_tls_cert_by_move(msgnetwork_config_t *self, x509_t *cert) {
    self->tls_cert(new x509_t(std::move(*cert)));
}

bool msgnetwork_send_msg(msgnetwork_t *self, const msg_t *msg, const msgnetwork_conn_t *conn) {
    return self->_send_msg(*msg, *conn);
}

int32_t msgnetwork_send_msg_deferred_by_move(msgnetwork_t *self,
                                        msg_t *_moved_msg, const msgnetwork_conn_t *conn) {
    return self->_send_msg_deferred(std::move(*_moved_msg), *conn);
}

msgnetwork_conn_t *msgnetwork_connect_sync(msgnetwork_t *self, const netaddr_t *addr, SalticidaeCError *cerror) {
    SALTICIDAE_CERROR_TRY(cerror)
    return new msgnetwork_conn_t(self->connect_sync(*addr));
    SALTICIDAE_CERROR_CATCH(cerror)
    return nullptr;
}

int32_t msgnetwork_connect(msgnetwork_t *self, const netaddr_t *addr) {
    return self->connect(*addr);
}

msgnetwork_conn_t *msgnetwork_conn_copy(const msgnetwork_conn_t *self) {
    return new msgnetwork_conn_t(*self);
}


void msgnetwork_conn_free(const msgnetwork_conn_t *self) { delete self; }

void msgnetwork_listen(msgnetwork_t *self, const netaddr_t *listen_addr, SalticidaeCError *cerror) {
    SALTICIDAE_CERROR_TRY(cerror)
    self->listen(*listen_addr);
    SALTICIDAE_CERROR_CATCH(cerror)
}

void msgnetwork_start(msgnetwork_t *self) { self->start(); }

void msgnetwork_stop(msgnetwork_t *self) { self->stop(); }

void msgnetwork_terminate(msgnetwork_t *self, const msgnetwork_conn_t *conn) {
    self->terminate(*conn);
}

void msgnetwork_reg_handler(msgnetwork_t *self,
                            _opcode_t opcode,
                            msgnetwork_msg_callback_t cb,
                            void *userdata) {
    self->set_handler(opcode,
        [=](const msgnetwork_t::Msg &msg, const msgnetwork_conn_t &conn) {
            cb(&msg, &conn, userdata);
        });
}

void msgnetwork_reg_conn_handler(msgnetwork_t *self,
                                msgnetwork_conn_callback_t cb,
                                void *userdata) {
    self->reg_conn_handler([=](const ConnPool::conn_t &_conn, bool connected) {
        auto conn = salticidae::static_pointer_cast<msgnetwork_t::Conn>(_conn);
        return cb(&conn, connected, userdata);
    });
}

void msgnetwork_reg_error_handler(msgnetwork_t *self,
                                msgnetwork_error_callback_t cb,
                                void *userdata) {
    self->reg_error_handler([=](const std::exception_ptr _err, bool fatal, int32_t async_id) {
        SalticidaeCError cerror;
        try {
            std::rethrow_exception(_err);
        } catch (SalticidaeError &err) {
            cerror = err.get_cerr();
        } catch (...) {
            cerror = salticidae_cerror_unknown();
        }
        cb(&cerror, fatal, async_id, userdata);
    });
}

msgnetwork_t *msgnetwork_conn_get_net(const msgnetwork_conn_t *conn) {
    return (*conn)->get_net();
}

msgnetwork_conn_mode_t msgnetwork_conn_get_mode(const msgnetwork_conn_t *conn) {
    return (msgnetwork_conn_mode_t)(*conn)->get_mode();
}

const netaddr_t *msgnetwork_conn_get_addr(const msgnetwork_conn_t *conn) {
    return &(*conn)->get_addr();
}

const x509_t *msgnetwork_conn_get_peer_cert(const msgnetwork_conn_t *conn) {
    return (*conn)->get_peer_cert();
}

bool msgnetwork_conn_is_terminated(const msgnetwork_conn_t *conn) {
    return (*conn)->is_terminated();
}

// PeerNetwork

peernetwork_config_t *peernetwork_config_new() {
    return new peernetwork_config_t();
}

void peernetwork_config_free(const peernetwork_config_t *self) { delete self; }

void peernetwork_config_ping_period(peernetwork_config_t *self, double t) {
    self->ping_period(t);
}

void peernetwork_config_conn_timeout(peernetwork_config_t *self, double t) {
    self->conn_timeout(t);
}

void peernetwork_config_id_mode(peernetwork_config_t *self, peernetwork_id_mode_t mode) {
    self->id_mode(peernetwork_t::IdentityMode(mode));
}

msgnetwork_config_t *peernetwork_config_as_msgnetwork_config(peernetwork_config_t *self) { return self; }

peernetwork_t *peernetwork_new(const eventcontext_t *ec, const peernetwork_config_t *config, SalticidaeCError *cerror) {
    SALTICIDAE_CERROR_TRY(cerror)
    return new peernetwork_t(*ec, *config);
    SALTICIDAE_CERROR_CATCH(cerror)
    return nullptr;
}

void peernetwork_free(const peernetwork_t *self) { delete self; }

int32_t peernetwork_add_peer(peernetwork_t *self, const netaddr_t *addr) {
    return self->add_peer(*addr);
}

int32_t peernetwork_del_peer(peernetwork_t *self, const netaddr_t *addr) {
    return self->del_peer(*addr);
}

bool peernetwork_has_peer(const peernetwork_t *self, const netaddr_t *addr) {
    return self->has_peer(*addr);
}

const peernetwork_conn_t *peernetwork_get_peer_conn(const peernetwork_t *self,
                                                    const netaddr_t *addr,
                                                    SalticidaeCError *cerror) {
    SALTICIDAE_CERROR_TRY(cerror)
    return new peernetwork_conn_t(self->get_peer_conn(*addr));
    SALTICIDAE_CERROR_CATCH(cerror)
    return nullptr;
}

msgnetwork_t *peernetwork_as_msgnetwork(peernetwork_t *self) { return self; }

peernetwork_t *msgnetwork_as_peernetwork_unsafe(msgnetwork_t *self) {
    return static_cast<peernetwork_t *>(self);
}

msgnetwork_conn_t *msgnetwork_conn_new_from_peernetwork_conn(const peernetwork_conn_t *conn) {
    return new msgnetwork_conn_t(*conn);
}

peernetwork_conn_t *peernetwork_conn_new_from_msgnetwork_conn_unsafe(const msgnetwork_conn_t *conn) {
    return new peernetwork_conn_t(salticidae::static_pointer_cast<peernetwork_t::Conn>(*conn));
}

peernetwork_conn_t *peernetwork_conn_copy(const peernetwork_conn_t *self) {
    return new peernetwork_conn_t(*self);
}

netaddr_t *peernetwork_conn_get_peer_addr(const peernetwork_conn_t *self) {
    return new netaddr_t((*self)->get_peer_addr());
}

void peernetwork_conn_free(const peernetwork_conn_t *self) { delete self; }

bool peernetwork_send_msg(peernetwork_t *self, const msg_t * msg, const netaddr_t *addr) {
    try {
        self->_send_msg(*msg, *addr);
        return true;
    } catch (...) {
        return false;
    }
}

int32_t peernetwork_send_msg_deferred_by_move(peernetwork_t *self,
                                        msg_t *_moved_msg, const netaddr_t *addr) {
    return self->_send_msg_deferred(std::move(*_moved_msg), *addr);
}

int32_t peernetwork_multicast_msg_by_move(peernetwork_t *self,
                                    msg_t *_moved_msg, const netaddr_array_t *addrs) {
    return self->_multicast_msg(std::move(*_moved_msg), *addrs);
}

void peernetwork_listen(peernetwork_t *self, const netaddr_t *listen_addr, SalticidaeCError *cerror) {
    SALTICIDAE_CERROR_TRY(cerror)
    self->listen(*listen_addr);
    SALTICIDAE_CERROR_CATCH(cerror)
}

void peernetwork_reg_peer_handler(peernetwork_t *self,
                                peernetwork_peer_callback_t cb,
                                void *userdata) {
    self->reg_peer_handler([=](const peernetwork_conn_t &conn, bool connected) {
        cb(&conn, connected, userdata);
    });
}

void peernetwork_reg_unknown_peer_handler(peernetwork_t *self,
                                        peernetwork_unknown_peer_callback_t cb,
                                        void *userdata) {
    self->reg_unknown_peer_handler([=](const NetAddr &claimed_addr, const salticidae::X509 *cert) {
        cb(&claimed_addr, cert, userdata);
    });
}

clientnetwork_t *clientnetwork_new(const eventcontext_t *ec, const msgnetwork_config_t *config, SalticidaeCError *cerror) {
    SALTICIDAE_CERROR_TRY(cerror)
    return new clientnetwork_t(*ec, *config);
    SALTICIDAE_CERROR_CATCH(cerror)
    return nullptr;
}

void clientnetwork_free(const clientnetwork_t *self) { delete self; }

msgnetwork_t *clientnetwork_as_msgnetwork(clientnetwork_t *self) { return self; }

clientnetwork_t *msgnetwork_as_clientnetwork_unsafe(msgnetwork_t *self) {
    return static_cast<clientnetwork_t *>(self);
}

msgnetwork_conn_t *msgnetwork_conn_new_from_clientnetwork_conn(const clientnetwork_conn_t *conn) {
    return new msgnetwork_conn_t(*conn);
}

clientnetwork_conn_t *clientnetwork_conn_new_from_msgnetwork_conn_unsafe(const msgnetwork_conn_t *conn) {
    return new clientnetwork_conn_t(salticidae::static_pointer_cast<clientnetwork_t::Conn>(*conn));
}

clientnetwork_conn_t *clientnetwork_conn_copy(const clientnetwork_conn_t *self) {
    return new clientnetwork_conn_t(*self);
}

void clientnetwork_conn_free(const clientnetwork_conn_t *self) { delete self; }

bool clientnetwork_send_msg(clientnetwork_t *self, const msg_t * msg, const netaddr_t *addr) {
    try {
        self->_send_msg(*msg, *addr);
        return true;
    } catch (...) {
        return false;
    }
}

int32_t clientnetwork_send_msg_deferred_by_move(clientnetwork_t *self, msg_t *_moved_msg, const netaddr_t *addr) {
    return self->_send_msg_deferred(std::move(*_moved_msg), *addr);
}

}

#endif