Skip to content

Commit a3da58d

Browse files
committed
Add receiveAll for ZSocket type
1 parent 65ddc18 commit a3da58d

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

examples/ex06_pollermultipart.nim

+1-8
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,6 @@ import ../zmq
66
const address = "tcp://127.0.0.1:5559"
77
const max_msg = 10
88

9-
proc receiveMultipart(socket: ZSocket, flags: ZSendRecvOptions): seq[string] =
10-
# Little trick to receive all multipart message no matter how many parts there is using getsockopt
11-
var hasMore: int = 1
12-
while hasMore > 0:
13-
result.add(socket.receive())
14-
hasMore = getsockopt[int](socket, RCVMORE)
15-
169

1710
proc client() =
1811
var d1 = connect(address, mode = DEALER)
@@ -35,7 +28,7 @@ proc client() =
3528
if res > 0:
3629
for i in 0..<len(poller):
3730
if events(poller[i]):
38-
let buf = receiveMultipart(poller[i].socket, NOFLAGS)
31+
let buf = receiveAll(poller[i].socket, NOFLAGS)
3932
for j, msg in buf.pairs:
4033
echo &"CLIENT> Socket{i} received \"{msg}\""
4134
else:

zmq/connections.nim

+15-8
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,19 @@ proc receive*(s: ZSocket, flags: ZSendRecvOptions = NOFLAGS): string =
321321
## Return an empty string on EAGAIN
322322
tryReceive(s, flags).msg
323323

324+
proc receiveAll*(s: ZSocket, flags: ZSendRecvOptions = NOFLAGS): seq[string] =
325+
## Receive all parts of a message
326+
##
327+
## If EAGAIN occurs without any data being received, it will be an empty seq
328+
var expectMessage = true
329+
while expectMessage:
330+
let (msgAvailable, moreAvailable, msg) = tryReceive(s, flags)
331+
if msgAvailable:
332+
result.add msg
333+
expectMessage = moreAvailable
334+
else:
335+
expectMessage = false
336+
324337
proc tryReceive*(c: ZConnection, flags: ZSendRecvOptions = NOFLAGS): tuple[msgAvailable: bool, moreAvailable: bool, msg: string] =
325338
## Receives a message from a connection.
326339
##
@@ -337,14 +350,8 @@ proc receiveAll*(c: ZConnection, flags: ZSendRecvOptions = NOFLAGS): seq[string]
337350
## Receive all parts of a message
338351
##
339352
## If EAGAIN occurs without any data being received, it will be an empty seq
340-
var expectMessage = true
341-
while expectMessage:
342-
let (msgAvailable, moreAvailable, msg) = tryReceive(c, flags)
343-
if msgAvailable:
344-
result.add msg
345-
expectMessage = moreAvailable
346-
else:
347-
expectMessage = false
353+
receiveAll(c.socket, flags)
354+
348355

349356
proc proxy*(frontend, backend: ZConnection) =
350357
## The proxy connects a frontend socket to a backend socket. Data flows from frontend to backend.

0 commit comments

Comments
 (0)