forked from pebbe/zmq4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
206 lines (184 loc) · 3.84 KB
/
utils.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
package zmq4
import (
"fmt"
)
/*
Send multi-part message on socket.
Any `[]string' or `[][]byte' is split into separate `string's or `[]byte's
Any other part that isn't a `string' or `[]byte' is converted
to `string' with `fmt.Sprintf("%v", part)'.
Returns total bytes sent.
*/
func (soc *Socket) SendMessage(parts ...interface{}) (total int, err error) {
return soc.sendMessage(0, parts...)
}
/*
Like SendMessage(), but adding the DONTWAIT flag.
*/
func (soc *Socket) SendMessageDontwait(parts ...interface{}) (total int, err error) {
return soc.sendMessage(DONTWAIT, parts...)
}
func (soc *Socket) sendMessage(dontwait Flag, parts ...interface{}) (total int, err error) {
var last int
PARTS:
for last = len(parts) - 1; last >= 0; last-- {
switch t := parts[last].(type) {
case []string:
if len(t) > 0 {
break PARTS
}
case [][]byte:
if len(t) > 0 {
break PARTS
}
default:
break PARTS
}
}
opt := SNDMORE | dontwait
for i := 0; i <= last; i++ {
if i == last {
opt = dontwait
}
switch t := parts[i].(type) {
case []string:
opt = SNDMORE | dontwait
n := len(t) - 1
for j, s := range t {
if j == n && i == last {
opt = dontwait
}
c, e := soc.Send(s, opt)
if e == nil {
total += c
} else {
return -1, e
}
}
case [][]byte:
opt = SNDMORE | dontwait
n := len(t) - 1
for j, b := range t {
if j == n && i == last {
opt = dontwait
}
c, e := soc.SendBytes(b, opt)
if e == nil {
total += c
} else {
return -1, e
}
}
case string:
c, e := soc.Send(t, opt)
if e == nil {
total += c
} else {
return -1, e
}
case []byte:
c, e := soc.SendBytes(t, opt)
if e == nil {
total += c
} else {
return -1, e
}
default:
c, e := soc.Send(fmt.Sprintf("%v", t), opt)
if e == nil {
total += c
} else {
return -1, e
}
}
}
return
}
/*
Receive parts as message from socket.
Returns last non-nil error code.
*/
func (soc *Socket) RecvMessage(flags Flag) (msg []string, err error) {
msg = make([]string, 0)
for {
s, e := soc.Recv(flags)
if e == nil {
msg = append(msg, s)
} else {
return msg[0:0], e
}
more, e := soc.GetRcvmore()
if e == nil {
if !more {
break
}
} else {
return msg[0:0], e
}
}
return
}
/*
Receive parts as message from socket.
Returns last non-nil error code.
*/
func (soc *Socket) RecvMessageBytes(flags Flag) (msg [][]byte, err error) {
msg = make([][]byte, 0)
for {
b, e := soc.RecvBytes(flags)
if e == nil {
msg = append(msg, b)
} else {
return msg[0:0], e
}
more, e := soc.GetRcvmore()
if e == nil {
if !more {
break
}
} else {
return msg[0:0], e
}
}
return
}
/*
Receive parts as message from socket, including metadata.
Metadata is picked from the first message part.
For details about metadata, see RecvWithMetadata().
Returns last non-nil error code.
*/
func (soc *Socket) RecvMessageWithMetadata(flags Flag, properties ...string) (msg []string, metadata map[string]string, err error) {
b, p, err := soc.RecvMessageBytesWithMetadata(flags, properties...)
m := make([]string, len(b))
for i, bt := range b {
m[i] = string(bt)
}
return m, p, err
}
/*
Receive parts as message from socket, including metadata.
Metadata is picked from the first message part.
For details about metadata, see RecvBytesWithMetadata().
Returns last non-nil error code.
*/
func (soc *Socket) RecvMessageBytesWithMetadata(flags Flag, properties ...string) (msg [][]byte, metadata map[string]string, err error) {
bb := make([][]byte, 0)
b, p, err := soc.RecvBytesWithMetadata(flags, properties...)
if err != nil {
return bb, p, err
}
for {
bb = append(bb, b)
var more bool
more, err = soc.GetRcvmore()
if err != nil || !more {
break
}
b, err = soc.RecvBytes(flags)
if err != nil {
break
}
}
return bb, p, err
}