forked from alecthomas/gozmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zmq_2_x.go
88 lines (76 loc) · 2.33 KB
/
zmq_2_x.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
// +build !zmq_3_x
/*
Copyright 2010-2012 Alec Thomas
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gozmq
/*
#cgo CFLAGS: -I/usr/local/include
#cgo LDFLAGS: -L/usr/local/lib -lzmq
#include <zmq.h>
#include <stdlib.h>
#include <string.h>
*/
import "C"
import "unsafe"
var (
RECOVERY_IVL_MSEC = Int64SocketOption(C.ZMQ_RECOVERY_IVL_MSEC)
SWAP = Int64SocketOption(C.ZMQ_SWAP)
MCAST_LOOP = Int64SocketOption(C.ZMQ_MCAST_LOOP)
HWM = UInt64SocketOption(C.ZMQ_HWM)
NOBLOCK = SendRecvOption(C.ZMQ_NOBLOCK)
)
// Send a message to the socket.
// int zmq_send (void *s, zmq_msg_t *msg, int flags);
func (s *zmqSocket) Send(data []byte, flags SendRecvOption) error {
var m C.zmq_msg_t
// Copy data array into C-allocated buffer.
size := C.size_t(len(data))
if C.zmq_msg_init_size(&m, size) != 0 {
return errno()
}
if size > 0 {
// FIXME Ideally this wouldn't require a copy.
C.memcpy(unsafe.Pointer(C.zmq_msg_data(&m)), unsafe.Pointer(&data[0]), size) // XXX I hope this works...(seems to)
}
if C.zmq_send(s.s, &m, C.int(flags)) != 0 {
// zmq_send did not take ownership, free message
C.zmq_msg_close(&m)
return errno()
}
return nil
}
// Receive a message from the socket.
// int zmq_recv (void *s, zmq_msg_t *msg, int flags);
func (s *zmqSocket) Recv(flags SendRecvOption) (data []byte, err error) {
// Allocate and initialise a new zmq_msg_t
var m C.zmq_msg_t
if C.zmq_msg_init(&m) != 0 {
err = errno()
return
}
defer C.zmq_msg_close(&m)
// Receive into message
if C.zmq_recv(s.s, &m, C.int(flags)) != 0 {
err = errno()
return
}
// Copy message data into a byte array
// FIXME Ideally this wouldn't require a copy.
size := C.zmq_msg_size(&m)
if size > 0 {
data = make([]byte, int(size))
C.memcpy(unsafe.Pointer(&data[0]), C.zmq_msg_data(&m), size)
} else {
data = nil
}
return
}