forked from liveforeverx/chello
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransport_zmq.go
53 lines (43 loc) · 918 Bytes
/
transport_zmq.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
package main
import (
"net/url"
zmq "github.com/pebbe/zmq2"
)
func init() {
var gen = func() (helloTransport, error) {
return NewZmqSocket()
}
registerTransport("zmq+ipc", gen)
registerTransport("zmq+tcp", gen)
}
type zmqSocket struct {
*zmq.Socket
}
func NewZmqSocket() (*zmqSocket, error) {
var sock, err = zmq.NewSocket(zmq.REQ)
if err == nil {
return &zmqSocket{sock}, nil
}
return nil, err
}
func (s *zmqSocket) Connect(addr string) error {
var url, _ = url.Parse(addr)
url.Scheme = url.Scheme[4:] // "zmq+XYZ"
return s.Socket.Connect(url.String())
}
func (s *zmqSocket) Close() error {
return s.Socket.Close()
}
func (s *zmqSocket) Request(req string) (string, error) {
var (
reply []string
err error
)
if _, err = s.Socket.SendMessage(req); err != nil {
return "", err
}
if reply, err = s.Socket.RecvMessage(0); err != nil {
return "", err
}
return reply[0], nil
}