aboutsummaryrefslogtreecommitdiff
path: root/network.go
blob: c30e3d306042335111f0d520b34d635fc3b1271a (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
package salticidae

// #include "salticidae/network.h"
import "C"
import "runtime"

type CMsgNetwork = *C.msgnetwork_t
type msgNetwork struct { inner CMsgNetwork }
type MsgNetwork = *msgNetwork

func MsgNetworkFromC(ptr CMsgNetwork) MsgNetwork {
    return &msgNetwork{ inner: ptr }
}

type CMsgNetworkConn = *C.msgnetwork_conn_t
type msgNetworkConn struct { inner CMsgNetworkConn }
type MsgNetworkConn = *msgNetworkConn

func MsgNetworkConnFromC(ptr CMsgNetworkConn) MsgNetworkConn {
    return &msgNetworkConn{ inner: ptr }
}

type MsgNetworkConnMode = C.msgnetwork_conn_mode_t

func (self MsgNetworkConn) free() { C.msgnetwork_conn_free(self.inner) }

func (self MsgNetworkConn) GetNet() MsgNetwork {
    return MsgNetworkFromC(C.msgnetwork_conn_get_net(self.inner))
}

var (
    CONN_MODE_ACTIVE = MsgNetworkConnMode(C.CONN_MODE_ACTIVE)
    CONN_MODE_PASSIVE = MsgNetworkConnMode(C.CONN_MODE_PASSIVE)
    CONN_MODE_DEAD = MsgNetworkConnMode(C.CONN_MODE_DEAD)
)

func (self MsgNetworkConn) GetMode() MsgNetworkConnMode {
    return C.msgnetwork_conn_get_mode(self.inner)
}

func (self MsgNetworkConn) GetAddr() NetAddr {
    return NetAddrFromC(C.msgnetwork_conn_get_addr(self.inner))
}

type CMsgNetworkConfig = *C.msgnetwork_config_t
type msgNetworkConfig struct { inner CMsgNetworkConfig }
type MsgNetworkConfig = *msgNetworkConfig

func MsgNetworkConfigFromC(ptr CMsgNetworkConfig) MsgNetworkConfig {
    return &msgNetworkConfig{ inner: ptr }
}

func NewMsgNetworkConfig() MsgNetworkConfig {
    res := MsgNetworkConfigFromC(C.msgnetwork_config_new())
    runtime.SetFinalizer(res, func(self MsgNetworkConfig) { self.free() })
    return res
}

func (self MsgNetworkConfig) free() { C.msgnetwork_config_free(self.inner) }

func (self MsgNetworkConfig) BurstSize(size int) {
    C.msgnetwork_config_burst_size(self.inner, C.size_t(size))
}

func (self MsgNetworkConfig) MaxListenBacklog(backlog int) {
    C.msgnetwork_config_max_listen_backlog(self.inner, C.int(backlog))
}

func (self MsgNetworkConfig) ConnServerTimeout(timeout float64) {
    C.msgnetwork_config_conn_server_timeout(self.inner, C.double(timeout))
}

func (self MsgNetworkConfig) SegBuffSize(size int) {
    C.msgnetwork_config_seg_buff_size(self.inner, C.size_t(size))
}

func (self MsgNetworkConfig) NWorker(nworker int) {
    C.msgnetwork_config_nworker(self.inner, C.size_t(nworker))
}

func (self MsgNetworkConfig) QueueCapacity(capacity int) {
    C.msgnetwork_config_queue_capacity(self.inner, C.size_t(capacity))
}

func NewMsgNetwork(ec EventContext, config MsgNetworkConfig) MsgNetwork {
    res := MsgNetworkFromC(C.msgnetwork_new(ec.inner, config.inner))
    ec.attach(rawptr_t(res.inner), res)
    runtime.SetFinalizer(res, func(self MsgNetwork) { self.free() })
    return res
}

func (self MsgNetwork) free() { C.msgnetwork_free(self.inner) }
func (self MsgNetwork) Listen(addr NetAddr, err *Error) { C.msgnetwork_listen(self.inner, addr.inner, err) }
func (self MsgNetwork) Start() { C.msgnetwork_start(self.inner) }
func (self MsgNetwork) Stop() { C.msgnetwork_stop(self.inner) }

func (self MsgNetwork) SendMsgByMove(msg Msg, conn MsgNetworkConn) { C.msgnetwork_send_msg_by_move(self.inner, msg.inner, conn.inner) }
func (self MsgNetwork) Connect(addr NetAddr, err *Error) MsgNetworkConn {
    res := MsgNetworkConnFromC(C.msgnetwork_connect(self.inner, addr.inner, err))
    runtime.SetFinalizer(res, func(self MsgNetworkConn) { self.free() })
    return res
}
func (self MsgNetwork) Terminate(conn MsgNetworkConn) { C.msgnetwork_terminate(self.inner, conn.inner) }


func (self MsgNetwork) RegHandler(opcode Opcode, callback MsgNetworkMsgCallback, userdata rawptr_t) {
    C.msgnetwork_reg_handler(self.inner, C._opcode_t(opcode), callback, userdata)
}

func (self MsgNetwork) RegConnHandler(callback MsgNetworkConnCallback, userdata rawptr_t) {
    C.msgnetwork_reg_conn_handler(self.inner, callback, userdata)
}

func (self MsgNetwork) RegErrorHandler(callback MsgNetworkErrorCallback, userdata rawptr_t) {
    C.msgnetwork_reg_error_handler(self.inner, callback, userdata)
}

func (self MsgNetworkConn) Copy() MsgNetworkConn {
    res := MsgNetworkConnFromC(C.msgnetwork_conn_copy(self.inner))
    runtime.SetFinalizer(res, func(self MsgNetworkConn) { self.free() })
    return res
}
func (self MsgNetworkConn) Free() { C.msgnetwork_conn_free(self.inner) }

type MsgNetworkMsgCallback = C.msgnetwork_msg_callback_t
type MsgNetworkConnCallback = C.msgnetwork_conn_callback_t
type MsgNetworkErrorCallback = C.msgnetwork_error_callback_t

type CPeerNetwork = *C.peernetwork_t
type peerNetwork struct { inner CPeerNetwork }
type PeerNetwork = *peerNetwork

func PeerNetworkFromC(ptr CPeerNetwork) PeerNetwork {
    return &peerNetwork{ inner: ptr }
}


type CPeerNetworkConn = *C.peernetwork_conn_t
type peerNetworkConn struct { inner CPeerNetworkConn }
type PeerNetworkConn = *peerNetworkConn

func PeerNetworkConnFromC(ptr CPeerNetworkConn) PeerNetworkConn {
    return &peerNetworkConn{ inner: ptr }
}

type PeerNetworkIdMode = C.peernetwork_id_mode_t

var (
    ID_MODE_IP_BASED = PeerNetworkIdMode(C.ID_MODE_IP_BASED)
    ID_MODE_IP_PORT_BASED = PeerNetworkIdMode(C.ID_MODE_IP_PORT_BASED)
)

type CPeerNetworkConfig = *C.peernetwork_config_t
type peerNetworkConfig struct { inner CPeerNetworkConfig }
type PeerNetworkConfig = *peerNetworkConfig

func PeerNetworkConfigFromC(ptr CPeerNetworkConfig) PeerNetworkConfig {
    return &peerNetworkConfig{ inner: ptr }
}

func NewPeerNetworkConfig() PeerNetworkConfig {
    res := PeerNetworkConfigFromC(C.peernetwork_config_new())
    runtime.SetFinalizer(res, func(self PeerNetworkConfig) { self.free() })
    return res
}

func (self PeerNetworkConfig) free() { C.peernetwork_config_free(self.inner) }

func (self PeerNetworkConfig) RetryConnDelay(t_sec float64) {
    C.peernetwork_config_retry_conn_delay(self.inner, C.double(t_sec))
}

func (self PeerNetworkConfig) PingPeriod(t_sec float64) {
    C.peernetwork_config_ping_period(self.inner, C.double(t_sec))
}

func (self PeerNetworkConfig) ConnTimeout(t_sec float64) {
    C.peernetwork_config_conn_timeout(self.inner, C.double(t_sec))
}

func (self PeerNetworkConfig) IdMode(mode PeerNetworkIdMode) {
    C.peernetwork_config_id_mode(self.inner, mode)
}

func (self PeerNetworkConfig) AsMsgNetworkConfig() MsgNetworkConfig {
    return MsgNetworkConfigFromC(C.peernetwork_config_as_msgnetwork_config(self.inner))
}

func NewPeerNetwork(ec EventContext, config PeerNetworkConfig) PeerNetwork {
    res := PeerNetworkFromC(C.peernetwork_new(ec.inner, config.inner))
    ec.attach(rawptr_t(res.inner), res)
    runtime.SetFinalizer(res, func(self PeerNetwork) { self.free() })
    return res
}

func (self PeerNetwork) free() { C.peernetwork_free(self.inner) }

func (self PeerNetwork) AddPeer(paddr NetAddr) { C.peernetwork_add_peer(self.inner, paddr.inner) }

func (self PeerNetwork) HasPeer(paddr NetAddr) bool { return bool(C.peernetwork_has_peer(self.inner, paddr.inner)) }

func (self PeerNetwork) GetPeerConn(paddr NetAddr, err *Error) PeerNetworkConn {
    res := PeerNetworkConnFromC(C.peernetwork_get_peer_conn(self.inner, paddr.inner, err))
    runtime.SetFinalizer(res, func(self PeerNetworkConn) { self.free() })
    return res
}

func (self PeerNetwork) AsMsgNetwork() MsgNetwork {
    return MsgNetworkFromC(C.peernetwork_as_msgnetwork(self.inner))
}

func (self MsgNetwork) AsPeerNetworkUnsafe() PeerNetwork {
    return PeerNetworkFromC(C.msgnetwork_as_peernetwork_unsafe(self.inner))
}

func NewMsgNetworkConnFromPeerNetWorkConn(conn PeerNetworkConn) MsgNetworkConn {
    res := MsgNetworkConnFromC(C.msgnetwork_conn_new_from_peernetwork_conn(conn.inner))
    runtime.SetFinalizer(res, func(self MsgNetworkConn) { self.free() })
    return res
}

func (self PeerNetworkConn) Copy() PeerNetworkConn {
    res := PeerNetworkConnFromC(C.peernetwork_conn_copy(self.inner))
    runtime.SetFinalizer(res, func(self PeerNetworkConn) { self.free() })
    return res
}

func (self PeerNetworkConn) free() { C.peernetwork_conn_free(self.inner) }

func (self PeerNetwork) SendMsgByMove(_moved_msg Msg, paddr NetAddr) {
    C.peernetwork_send_msg_by_move(self.inner, _moved_msg.inner, paddr.inner)
}

func (self PeerNetwork) MulticastMsgByMove(_moved_msg Msg, paddrs []NetAddr) {
    na := NewAddrArrayFromAddrs(paddrs)
    C.peernetwork_multicast_msg_by_move(self.inner, _moved_msg.inner, na.inner)
}

func (self PeerNetwork) Listen(listenAddr NetAddr, err *Error) {
    C.peernetwork_listen(self.inner, listenAddr.inner, err)
}

type MsgNetworkUnknownPeerCallback = C.msgnetwork_unknown_peer_callback_t

func (self PeerNetwork) RegUnknownPeerHandler(callback MsgNetworkUnknownPeerCallback, userdata rawptr_t) {
    C.peernetwork_reg_unknown_peer_handler(self.inner, callback, userdata)
}