本文整理匯總了Golang中github.com/gdamore/mangos/protocol/req.NewSocket函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewSocket函數的具體用法?Golang NewSocket怎麽用?Golang NewSocket使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewSocket函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: NewSocket
// NewSocket allocates a new Socket. The Socket is the handle used to
// access the underlying library.
func NewSocket(d Domain, p Protocol) (*Socket, error) {
var s Socket
var err error
s.proto = p
s.dom = d
switch p {
case PUB:
s.sock, err = pub.NewSocket()
case SUB:
s.sock, err = sub.NewSocket()
case PUSH:
s.sock, err = push.NewSocket()
case PULL:
s.sock, err = pull.NewSocket()
case REQ:
s.sock, err = req.NewSocket()
case REP:
s.sock, err = rep.NewSocket()
case SURVEYOR:
s.sock, err = surveyor.NewSocket()
case RESPONDENT:
s.sock, err = respondent.NewSocket()
case PAIR:
s.sock, err = pair.NewSocket()
case BUS:
s.sock, err = bus.NewSocket()
default:
err = mangos.ErrBadProto
}
if err != nil {
return nil, err
}
switch d {
case AF_SP:
case AF_SP_RAW:
err = s.sock.SetOption(mangos.OptionRaw, true)
default:
err = errBadDomain
}
if err != nil {
s.sock.Close()
return nil, err
}
// Compat mode sockets should timeout on send if we don't have any pipes
if err = s.sock.SetOption(mangos.OptionWriteQLen, 0); err != nil {
s.sock.Close()
return nil, err
}
s.rto = -1
s.sto = -1
all.AddTransports(s.sock)
return &s, nil
}
示例2: TestDeviceBadPair
func TestDeviceBadPair(t *testing.T) {
s1, err := req.NewSocket()
if err != nil {
t.Errorf("Failed to open S1: %v", err)
return
}
defer s1.Close()
s2, err := pair.NewSocket()
if err != nil {
t.Errorf("Failed to open S2: %v", err)
return
}
defer s2.Close()
switch err := mangos.Device(s1, s2); err {
case mangos.ErrBadProto:
t.Logf("Got expected err: %v", err)
return
case nil:
t.Errorf("Matching incompatible types succeeded")
return
default:
t.Errorf("Got unexpected err: %v", err)
return
}
}
示例3: StartNanomsgJsonTest
func StartNanomsgJsonTest(address string, clients int, requestsPerClient int, messageSize int, timer metrics.Timer, requestSize *int) func(wg *sync.WaitGroup) {
return func(wg *sync.WaitGroup) {
var socket mangos.Socket
var err error
if socket, err = req.NewSocket(); err != nil {
log.Fatal(err)
}
defer socket.Close()
defer wg.Done()
socket.AddTransport(tcp.NewTransport())
if err = socket.Dial("tcp://" + address); err != nil {
log.Fatal(err)
}
request, _ := json.Marshal(Request{Method: "TEST", Payload: strings.Repeat("a", messageSize)})
*requestSize = len(request)
for i := 0; i < requestsPerClient; i++ {
timer.Time(func() {
if err = socket.Send(request); err != nil {
log.Fatal(err)
}
socket.Recv()
})
}
}
}
示例4: newCmdClient
func newCmdClient(addr string) (mangos.Socket, error) {
socket, err := req.NewSocket()
if err != nil {
return nil, err
}
if err := socket.SetOption(mangos.OptionSendDeadline, time.Second*3); err != nil {
socket.Close()
return nil, err
}
if err := socket.SetOption(mangos.OptionRecvDeadline, time.Second*3); err != nil {
socket.Close()
return nil, err
}
//socket.AddTransport(ipc.NewTransport())
socket.AddTransport(tcp.NewTransport())
if err := socket.Dial(addr); err != nil {
socket.Close()
return nil, err
}
return socket, nil
}
示例5: TestWebsockMux
func TestWebsockMux(t *testing.T) {
sockReq, _ := req.NewSocket()
sockRep, _ := rep.NewSocket()
tran := NewTransport()
l, e := tran.NewListener("ws://127.0.0.1:3336/mysock", sockReq)
if e != nil {
t.Errorf("Failed new Listener: %v", e)
return
}
muxi, e := l.GetOption(OptionWebSocketMux)
if e != nil {
t.Errorf("Failed get mux: %v", e)
}
mux := muxi.(*http.ServeMux)
mux.HandleFunc("/bogus", bogusHandler)
d, e := tran.NewDialer("ws://127.0.0.1:3336/bogus", sockRep)
if e != nil {
t.Errorf("Failed new Dialer: %v", e)
return
}
if e = l.Listen(); e != nil {
t.Errorf("Listen failed")
return
}
defer l.Close()
p, e := d.Dial()
if p != nil {
defer p.Close()
}
if e == nil {
t.Errorf("Dial passed, when should not have!")
return
}
t.Logf("Got expected error %v", e)
// Now let's try to use http client.
resp, err := http.Get("http://127.0.0.1:3336/bogus")
if err != nil {
t.Errorf("Get of boguspath failed: %v", err)
return
}
if resp.StatusCode != 200 {
t.Errorf("Response code wrong: %d", resp.StatusCode)
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("ReadAll Failed: %v", err)
return
}
if string(body) != bogusstr {
t.Errorf("Results mismatch: %s != %s", string(body), bogusstr)
}
t.Logf("Got body: %s", string(body))
}
示例6: TestWebsockPath
func TestWebsockPath(t *testing.T) {
sockReq, _ := req.NewSocket()
sockRep, _ := rep.NewSocket()
tran := NewTransport()
l, e := tran.NewListener("ws://127.0.0.1:3335/mysock", sockReq)
if e != nil {
t.Errorf("Failed new Listener: %v", e)
return
}
d, e := tran.NewDialer("ws://127.0.0.1:3335/boguspath", sockRep)
if e != nil {
t.Errorf("Failed new Dialer: %v", e)
return
}
if e = l.Listen(); e != nil {
t.Errorf("Listen failed")
return
}
defer l.Close()
p, e := d.Dial()
if p != nil {
defer p.Close()
}
if e == nil {
t.Errorf("Dial passed, when should not have!")
return
}
t.Logf("Got expected error %v", e)
}
示例7: Init
func (rt *reqTest) Init(t *testing.T, addr string) bool {
var err error
if rt.Sock, err = req.NewSocket(); err != nil {
rt.Errorf("NewSocket(): %v", err)
return false
}
rt.cur = 0
rt.tot = 0
return rt.T.Init(t, addr)
}
示例8: NewTranTest
func NewTranTest(tran mangos.Transport, addr string) *TranTest {
tt := &TranTest{addr: addr, tran: tran}
if strings.HasPrefix(tt.addr, "tls+tcp://") || strings.HasPrefix(tt.addr, "wss://") {
tt.cliCfg, _ = GetTlsConfig(false)
tt.srvCfg, _ = GetTlsConfig(true)
}
tt.sockRep, _ = rep.NewSocket()
tt.sockReq, _ = req.NewSocket()
return tt
}
示例9: NewClient
// NewClient creates and returns a new Client from the provided Benchmark
// configuration. It returns an error if the Benchmark is not valid or it
// can't communicate with any of the specified peers.
func NewClient(b *Benchmark) (*Client, error) {
if err := b.validate(); err != nil {
return nil, err
}
brokerd, err := req.NewSocket()
if err != nil {
return nil, err
}
brokerd.AddTransport(tcp.NewTransport())
brokerd.SetOption(mangos.OptionSendDeadline, time.Duration(b.DaemonTimeout)*time.Second)
brokerd.SetOption(mangos.OptionRecvDeadline, time.Duration(b.DaemonTimeout)*time.Second)
if err := brokerd.Dial(fmt.Sprintf("tcp://%s", b.BrokerdHost)); err != nil {
return nil, err
}
peerd := make(map[string]mangos.Socket, len(b.PeerHosts))
for _, peer := range b.PeerHosts {
s, err := req.NewSocket()
if err != nil {
return nil, err
}
s.AddTransport(tcp.NewTransport())
s.SetOption(mangos.OptionSendDeadline, time.Duration(b.DaemonTimeout)*time.Second)
s.SetOption(mangos.OptionRecvDeadline, time.Duration(b.DaemonTimeout)*time.Second)
if err := s.Dial(fmt.Sprintf("tcp://%s", peer)); err != nil {
return nil, err
}
peerd[peer] = s
}
return &Client{
brokerd: brokerd,
peerd: peerd,
Benchmark: b,
}, nil
}
示例10: clientWorker
func clientWorker(url string, id int) {
var sock mangos.Socket
var m *mangos.Message
var err error
if sock, err = req.NewSocket(); err != nil {
die("can't get new req socket: %s", err.Error())
}
// Leave this in Cooked mode!
sock.AddTransport(ipc.NewTransport())
sock.AddTransport(tcp.NewTransport())
if err = sock.Dial(url); err != nil {
die("can't dial on req socket: %s", err.Error())
}
// send an empty messsage
m = mangos.NewMessage(1)
if err = sock.SendMsg(m); err != nil {
die("can't send request: %s", err.Error())
}
if m, err = sock.RecvMsg(); err != nil {
die("can't recv reply: %s", err.Error())
}
sock.Close()
if len(m.Body) != 4 {
die("bad response len: %d", len(m.Body))
}
worker := binary.BigEndian.Uint32(m.Body[0:])
L.Lock()
fmt.Printf("Client: %4d Server: %4d\n", id, worker)
L.Unlock()
}
示例11: node1
func node1(url string) {
var sock mangos.Socket
var err error
var msg []byte
if sock, err = req.NewSocket(); err != nil {
die("can't get new req socket: %s", err.Error())
}
sock.AddTransport(ipc.NewTransport())
sock.AddTransport(tcp.NewTransport())
if err = sock.Dial(url); err != nil {
die("can't dial on req socket: %s", err.Error())
}
fmt.Printf("NODE1: SENDING DATE REQUEST %s\n", "DATE")
if err = sock.Send([]byte("DATE")); err != nil {
die("can't send message on push socket: %s", err.Error())
}
if msg, err = sock.Recv(); err != nil {
die("can't receive date: %s", err.Error())
}
fmt.Printf("NODE1: RECEIVED DATE %s\n", string(msg))
sock.Close()
}
示例12: TestDeviceReqRep
func TestDeviceReqRep(t *testing.T) {
s1, err := req.NewSocket()
if err != nil {
t.Errorf("Failed to open S1: %v", err)
return
}
defer s1.Close()
s2, err := rep.NewSocket()
if err != nil {
t.Errorf("Failed to open S2: %v", err)
return
}
defer s2.Close()
switch err := mangos.Device(s1, s2); err {
case nil:
t.Logf("Matching req/rep ok!")
return
default:
t.Errorf("Got unexpected err: %v", err)
return
}
}
示例13: TestPortHook
func TestPortHook(t *testing.T) {
t.Logf("Testing Add Hook")
srvtest := &hooktest{allow: true, t: t}
clitest := &hooktest{allow: true, t: t}
addr := AddrTestTCP
srvtest.expect = []hookinfo{{
action: mangos.PortActionAdd,
addr: addr,
server: true,
isopen: true,
}, {
action: mangos.PortActionRemove,
addr: addr,
server: true,
isopen: false,
}}
clitest.expect = []hookinfo{{
action: mangos.PortActionAdd,
addr: addr,
server: false,
isopen: true,
}, {
action: mangos.PortActionRemove,
addr: addr,
server: false,
isopen: false,
}}
sockreq, err := req.NewSocket()
if err != nil {
t.Errorf("NewSocket failed: %v", err)
return
}
defer sockreq.Close()
sockreq.AddTransport(tcp.NewTransport())
if sockreq.SetPortHook(clitest.Hook) != nil {
t.Errorf("SetPortHook result not nil!")
return
}
d, err := sockreq.NewDialer(addr, nil)
if err != nil {
t.Errorf("NewDialer failed: %v", err)
return
}
sockrep, err := rep.NewSocket()
if err != nil {
t.Errorf("NewSocket failed: %v", err)
return
}
defer sockrep.Close()
sockrep.AddTransport(tcp.NewTransport())
if sockrep.SetPortHook(srvtest.Hook) != nil {
t.Errorf("SetPortHook result not nil!")
return
}
l, err := sockrep.NewListener(addr, nil)
if err != nil {
t.Errorf("NewListener failed: %v", err)
return
}
if err := l.Listen(); err != nil {
t.Errorf("Listen failed: %v", err)
return
}
if err := d.Dial(); err != nil {
t.Errorf("Dial failed: %v", err)
return
}
// wait a second for connection to establish
// could also issue a req/rep...
t.Logf("Waiting a bit...")
time.Sleep(100 * time.Millisecond)
d.Close()
l.Close()
// shut down the server
sockrep.Close()
sockreq.Close()
clitest.Lock()
defer clitest.Unlock()
srvtest.Lock()
defer srvtest.Unlock()
for i, info := range clitest.expect {
t.Logf("Exp C[%d]: %s", i, info.String())
}
for i, info := range clitest.calls {
t.Logf("Got C[%d]: %s", i, info.String())
}
//.........這裏部分代碼省略.........
示例14: testMaxRx
func testMaxRx(t *testing.T, addr string, tran mangos.Transport) {
maxrx := 100
rp, err := rep.NewSocket()
if err != nil {
t.Errorf("Failed to make REP: %v", err)
return
}
defer rp.Close()
rp.AddTransport(tran)
// Now try setting the option
err = rp.SetOption(mangos.OptionMaxRecvSize, maxrx)
if err != nil {
t.Errorf("Failed set MaxRecvSize: %v", err)
return
}
// At this point, we can issue requests on rq, and read them from rp.
if err = rp.SetOption(mangos.OptionRecvDeadline, time.Millisecond*20); err != nil {
t.Errorf("Failed set recv deadline")
return
}
if err = rp.Listen(addr); err != nil {
t.Errorf("Failed listen: %v", err)
return
}
rq, err := req.NewSocket()
if err != nil {
t.Errorf("Failed to make REQ: %v", err)
return
}
defer rq.Close()
rq.AddTransport(tran)
if err = rq.Dial(addr); err != nil {
t.Errorf("Failed dial: %v", err)
return
}
time.Sleep(time.Millisecond * 10)
msg1 := make([]byte, maxrx+1)
msg2 := make([]byte, maxrx+1)
for i := 0; i < len(msg1); i++ {
msg1[i] = 'A' + byte(i%26)
msg2[i] = 'A' + byte(i%26)
}
// NB: maxrx *includes* the header.
if err = rq.Send(msg1[:maxrx-8]); err != nil {
t.Errorf("Failed first send: %v", err)
return
}
v, err := rp.Recv()
if err != nil {
t.Errorf("Failed first recv: %v", err)
return
} else if !bytes.Equal(v, msg2[:maxrx-8]) {
t.Errorf("Got wrong message: %v", v)
return
} else {
t.Logf("Got good message")
}
if err = rq.Send(msg1); err != nil {
t.Errorf("Failed send drop: %v", err)
return
}
v, err = rp.Recv()
switch err {
case mangos.ErrRecvTimeout: // expected
t.Logf("MaxRx honored")
case nil:
t.Errorf("Message not dropped: %v", v)
default:
t.Errorf("Got unexpected error: %v", err)
}
}
示例15: TestTCPListenAndAccept
package tcp
import (
"bytes"
"testing"
"time"
"github.com/gdamore/mangos"
"github.com/gdamore/mangos/protocol/rep"
"github.com/gdamore/mangos/protocol/req"
)
var tran = NewTransport()
var sockRep, _ = rep.NewSocket()
var sockReq, _ = req.NewSocket()
func TestTCPListenAndAccept(t *testing.T) {
addr := "tcp://127.0.0.1:3333"
t.Logf("Establishing accepter")
l, err := tran.NewListener(addr, sockRep)
if err != nil {
t.Errorf("NewListener failed: %v", err)
return
}
defer l.Close()
if err = l.Listen(); err != nil {
t.Errorf("Listen failed: %v", err)
return
}