/
socketset.go
230 lines (206 loc) · 5.81 KB
/
socketset.go
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
package zmq2
/*
#include <zmq.h>
#include "zmq2.h"
#include <stdint.h>
#include <stdlib.h>
*/
import "C"
import (
"time"
"unsafe"
)
func (soc *Socket) setString(opt C.int, s string) error {
if !soc.opened {
return ErrorSocketClosed
}
cs := C.CString(s)
defer C.free(unsafe.Pointer(cs))
if i, err := C.zmq_setsockopt(soc.soc, opt, unsafe.Pointer(cs), C.size_t(len(s))); i != 0 {
return errget(err)
}
return nil
}
func (soc *Socket) setInt(opt C.int, value int) error {
if !soc.opened {
return ErrorSocketClosed
}
val := C.int(value)
if i, err := C.zmq_setsockopt(soc.soc, opt, unsafe.Pointer(&val), C.size_t(unsafe.Sizeof(val))); i != 0 {
return errget(err)
}
return nil
}
func (soc *Socket) setInt64(opt C.int, value int64) error {
if !soc.opened {
return ErrorSocketClosed
}
val := C.int64_t(value)
if i, err := C.zmq_setsockopt(soc.soc, opt, unsafe.Pointer(&val), C.size_t(unsafe.Sizeof(val))); i != 0 {
return errget(err)
}
return nil
}
func (soc *Socket) setUInt64(opt C.int, value uint64) error {
if !soc.opened {
return ErrorSocketClosed
}
val := C.uint64_t(value)
if i, err := C.zmq_setsockopt(soc.soc, opt, unsafe.Pointer(&val), C.size_t(unsafe.Sizeof(val))); i != 0 {
return errget(err)
}
return nil
}
// ZMQ_HWM: Set high water mark
//
// See: http://api.zeromq.org/2-2:zmq-setsockopt#toc3
func (soc *Socket) SetHwm(value uint64) error {
return soc.setUInt64(C.ZMQ_HWM, value)
}
// ZMQ_SWAP: Set disk offload size
//
// See: http://api.zeromq.org/2-2:zmq-setsockopt#toc4
func (soc *Socket) SetSwap(value int64) error {
return soc.setInt64(C.ZMQ_SWAP, value)
}
// ZMQ_AFFINITY: Set I/O thread affinity
//
// See: http://api.zeromq.org/2-2:zmq-setsockopt#toc5
func (soc *Socket) SetAffinity(value uint64) error {
return soc.setUInt64(C.ZMQ_AFFINITY, value)
}
// ZMQ_IDENTITY: Set socket identity
//
// See: http://api.zeromq.org/2-2:zmq-setsockopt#toc6
func (soc *Socket) SetIdentity(value string) error {
return soc.setString(C.ZMQ_IDENTITY, value)
}
// ZMQ_SUBSCRIBE: Establish message filter
//
// See: http://api.zeromq.org/2-2:zmq-setsockopt#toc7
func (soc *Socket) SetSubscribe(filter string) error {
return soc.setString(C.ZMQ_SUBSCRIBE, filter)
}
// ZMQ_UNSUBSCRIBE: Remove message filter
//
// See: http://api.zeromq.org/2-2:zmq-setsockopt#toc8
func (soc *Socket) SetUnsubscribe(filter string) error {
return soc.setString(C.ZMQ_UNSUBSCRIBE, filter)
}
// ZMQ_RCVTIMEO: Maximum time before a recv operation returns with EAGAIN
//
// Use -1 for infinite
//
// Returns ErrorNotImplemented in 0MQ version 2.1
//
// See: http://api.zeromq.org/2-2:zmq-setsockopt#toc9
func (soc *Socket) SetRcvtimeo(value time.Duration) error {
major, minor, _ := Version()
if major == 2 && minor < 2 {
return ErrorNotImplemented
}
val := int(value / time.Millisecond)
if value == -1 {
val = -1
}
return soc.setInt(C.ZMQ_RCVTIMEO, val)
}
// ZMQ_SNDTIMEO: Maximum time before a send operation returns with EAGAIN
//
// Use -1 for infinite
//
// Returns ErrorNotImplemented in 0MQ version 2.1
//
// See: http://api.zeromq.org/2-2:zmq-setsockopt#toc10
func (soc *Socket) SetSndtimeo(value time.Duration) error {
major, minor, _ := Version()
if major == 2 && minor < 2 {
return ErrorNotImplemented
}
val := int(value / time.Millisecond)
if value == -1 {
val = -1
}
return soc.setInt(C.ZMQ_SNDTIMEO, val)
}
// ZMQ_RATE: Set multicast data rate
//
// See: http://api.zeromq.org/2-2:zmq-setsockopt#toc11
func (soc *Socket) SetRate(value int64) error {
return soc.setInt64(C.ZMQ_RATE, value)
}
// ZMQ_RECOVERY_IVL: Set multicast recovery interval
//
// Note: value is of type time.Duration
//
// See: http://api.zeromq.org/2-2:zmq-setsockopt#toc12
func (soc *Socket) SetRecoveryIvl(value time.Duration) error {
val := int64(value / time.Second)
return soc.setInt64(C.ZMQ_RECOVERY_IVL, val)
}
// ZMQ_RECOVERY_IVL_MSEC: Set multicast recovery interval in milliseconds
//
// Note: value is of type time.Duration
//
// See: http://api.zeromq.org/2-2:zmq-setsockopt#toc13
func (soc *Socket) SetRecoveryIvlMsec(value time.Duration) error {
val := int64(value / time.Millisecond)
if value < 0 {
val = -1
}
return soc.setInt64(C.ZMQ_RECOVERY_IVL_MSEC, val)
}
// ZMQ_MCAST_LOOP: Control multicast loop-back
//
// See: http://api.zeromq.org/2-2:zmq-setsockopt#toc14
func (soc *Socket) SetMcastLoop(value bool) error {
val := int64(0)
if value {
val = 1
}
return soc.setInt64(C.ZMQ_MCAST_LOOP, val)
}
// ZMQ_SNDBUF: Set kernel transmit buffer size
//
// See: http://api.zeromq.org/2-2:zmq-setsockopt#toc15
func (soc *Socket) SetSndbuf(value uint64) error {
return soc.setUInt64(C.ZMQ_SNDBUF, value)
}
// ZMQ_RCVBUF: Set kernel receive buffer size
//
// See: http://api.zeromq.org/2-2:zmq-setsockopt#toc16
func (soc *Socket) SetRcvbuf(value uint64) error {
return soc.setUInt64(C.ZMQ_RCVBUF, value)
}
// ZMQ_LINGER: Set linger period for socket shutdown
//
// Use -1 for infinite
//
// See: http://api.zeromq.org/2-2:zmq-setsockopt#toc17
func (soc *Socket) SetLinger(value time.Duration) error {
val := int(value / time.Millisecond)
if value == -1 {
val = -1
}
return soc.setInt(C.ZMQ_LINGER, val)
}
// ZMQ_RECONNECT_IVL: Set reconnection interval
//
// See: http://api.zeromq.org/2-2:zmq-setsockopt#toc18
func (soc *Socket) SetReconnectIvl(value time.Duration) error {
val := int(value / time.Millisecond)
return soc.setInt(C.ZMQ_RECONNECT_IVL, val)
}
// ZMQ_RECONNECT_IVL_MAX: Set maximum reconnection interval
//
// See: http://api.zeromq.org/2-2:zmq-setsockopt#toc19
func (soc *Socket) SetReconnectIvlMax(value time.Duration) error {
val := int(value / time.Millisecond)
return soc.setInt(C.ZMQ_RECONNECT_IVL_MAX, val)
}
// ZMQ_BACKLOG: Set maximum length of the queue of outstanding connections
//
// See: http://api.zeromq.org/2-2:zmq-setsockopt#toc20
func (soc *Socket) SetBacklog(value int) error {
return soc.setInt(C.ZMQ_BACKLOG, value)
}