aboutsummaryrefslogtreecommitdiff
path: root/src/network.cpp
blob: c14531c364d7085cf665745d97bf17c2fa112cff (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
#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) {
    return new msgnetwork_t(*ec, *config);
}

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

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

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

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

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);
        cb(&conn, connected, userdata);
    });
}

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

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();
}

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

// PeerNetwork

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

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

peernetwork_t *peernetwork_new(const eventcontext_t *ec, const peernetwork_config_t *config) {
    return new peernetwork_t(*ec, *config);
}

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

void peernetwork_add_peer(peernetwork_t *self, const netaddr_t *paddr) {
    self->add_peer(*paddr);
}

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

const peernetwork_conn_t *peernetwork_get_peer_conn(const peernetwork_t *self,
                                        const netaddr_t *paddr) {
    return new peernetwork_conn_t(self->get_peer_conn(*paddr));
}

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

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

void peernetwork_send_msg(peernetwork_t *self,
                        msg_t * _moved_msg, const netaddr_t *paddr) {
    self->_send_msg(std::move(*_moved_msg), *paddr);
    delete _moved_msg;
}

void peernetwork_multicast_msg(peernetwork_t *self,
                                msg_t *_moved_msg,
                                const netaddr_t *paddrs, size_t npaddrs) {
    self->_multicast_msg(std::move(*_moved_msg), paddrs, npaddrs);
    delete _moved_msg;
}

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

}

#endif