• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang mangos.Socket类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/kildevaeld/projects/Godeps/_workspace/src/github.com/gdamore/mangos.Socket的典型用法代码示例。如果您正苦于以下问题:Golang Socket类的具体用法?Golang Socket怎么用?Golang Socket使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Socket类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: recvName

func recvName(sock mangos.Socket, name string) {
	var msg []byte
	var err error
	if msg, err = sock.Recv(); err == nil {
		fmt.Printf("%s: RECEIVED: \"%s\"\n", name, string(msg))
	}
}
开发者ID:kildevaeld,项目名称:projects,代码行数:7,代码来源:pair.go


示例2: replyLoop

func replyLoop(sock mangos.Socket) {
	if sendData == nil {
		fatalf("No data to send!")
	}
	for {
		msg, err := sock.RecvMsg()
		switch err {
		case mangos.ErrRecvTimeout:
			return
		case nil:
		default:
			fatalf("RecvMsg failed: %v", err)
		}
		printMsg(msg)
		msg.Free()

		msg = mangos.NewMessage(len(sendData))
		msg.Body = append(msg.Body, sendData...)
		err = sock.SendMsg(msg)

		if err != nil {
			fatalf("SendMsg failed: %v", err)
		}
	}
}
开发者ID:kildevaeld,项目名称:projects,代码行数:25,代码来源:macat.go


示例3: NewListener

func (t *inprocTran) NewListener(addr string, sock mangos.Socket) (mangos.PipeListener, error) {
	if _, err := mangos.StripScheme(t, addr); err != nil {
		return nil, err
	}
	l := &listener{addr: addr, proto: sock.GetProtocol()}
	return l, nil
}
开发者ID:kildevaeld,项目名称:projects,代码行数:7,代码来源:inproc.go


示例4: sendRecv

func sendRecv(sock mangos.Socket, name string) {
	for {
		sock.SetOption(mangos.OptionRecvDeadline, 100*time.Millisecond)
		recvName(sock, name)
		time.Sleep(time.Second)
		sendName(sock, name)
	}
}
开发者ID:kildevaeld,项目名称:projects,代码行数:8,代码来源:pair.go


示例5: client

func client(url string, name string) {
	var sock mangos.Socket
	var err error
	var msg []byte

	if sock, err = sub.NewSocket(); err != nil {
		die("can't get new sub socket: %s", err.Error())
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Dial(url); err != nil {
		die("can't dial on sub socket: %s", err.Error())
	}
	// Empty byte array effectively subscribes to everything
	err = sock.SetOption(mangos.OptionSubscribe, []byte(""))
	if err != nil {
		die("cannot subscribe: %s", err.Error())
	}
	for {
		if msg, err = sock.Recv(); err != nil {
			die("Cannot recv: %s", err.Error())
		}
		fmt.Printf("CLIENT(%s): RECEIVED %s\n", name, string(msg))
	}
}
开发者ID:kildevaeld,项目名称:projects,代码行数:25,代码来源:pubsub.go


示例6: client

func client(url string, name string) {
	var sock mangos.Socket
	var err error
	var msg []byte

	if sock, err = respondent.NewSocket(); err != nil {
		die("can't get new respondent socket: %s", err.Error())
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Dial(url); err != nil {
		die("can't dial on respondent socket: %s", err.Error())
	}
	for {
		if msg, err = sock.Recv(); err != nil {
			die("Cannot recv: %s", err.Error())
		}
		fmt.Printf("CLIENT(%s): RECEIVED \"%s\" SURVEY REQUEST\n",
			name, string(msg))

		d := date()
		fmt.Printf("CLIENT(%s): SENDING DATE SURVEY RESPONSE\n", name)
		if err = sock.Send([]byte(d)); err != nil {
			die("Cannot send: %s", err.Error())
		}
	}
}
开发者ID:kildevaeld,项目名称:projects,代码行数:27,代码来源:survey.go


示例7: node0

func node0(url string) {
	var sock mangos.Socket
	var err error
	var msg []byte
	if sock, err = rep.NewSocket(); err != nil {
		die("can't get new rep socket: %s", err)
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Listen(url); err != nil {
		die("can't listen on rep socket: %s", err.Error())
	}
	for {
		// Could also use sock.RecvMsg to get header
		msg, err = sock.Recv()
		if string(msg) == "DATE" { // no need to terminate
			fmt.Println("NODE0: RECEIVED DATE REQUEST")
			d := date()
			fmt.Printf("NODE0: SENDING DATE %s\n", d)
			err = sock.Send([]byte(d))
			if err != nil {
				die("can't send reply: %s", err.Error())
			}
		}
	}
}
开发者ID:kildevaeld,项目名称:projects,代码行数:26,代码来源:reqrep.go


示例8: NewListener

func (t wsTran) NewListener(addr string, sock mangos.Socket) (mangos.PipeListener, error) {
	proto := sock.GetProtocol()
	l, e := t.listener(addr, proto)
	if e == nil {
		if v, e := sock.GetOption(mangos.OptionMaxRecvSize); e == nil {
			l.maxrx = v.(int)
		}
		l.mux.Handle(l.url.Path, l)
	}
	return l, e
}
开发者ID:kildevaeld,项目名称:projects,代码行数:11,代码来源:ws.go


示例9: node0

func node0(url string) {
	var sock mangos.Socket
	var err error
	if sock, err = pair.NewSocket(); err != nil {
		die("can't get new pair socket: %s", err)
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Listen(url); err != nil {
		die("can't listen on pair socket: %s", err.Error())
	}
	sendRecv(sock, "node0")
}
开发者ID:kildevaeld,项目名称:projects,代码行数:13,代码来源:pair.go


示例10: NewDialer

func (wsTran) NewDialer(addr string, sock mangos.Socket) (mangos.PipeDialer, error) {
	iswss := strings.HasPrefix(addr, "wss://")
	opts := make(map[string]interface{})

	opts[mangos.OptionNoDelay] = true
	opts[mangos.OptionKeepAlive] = true
	proto := sock.GetProtocol()
	maxrx := 0
	if v, e := sock.GetOption(mangos.OptionMaxRecvSize); e == nil {
		maxrx = v.(int)
	}

	return &dialer{addr: addr, proto: proto, iswss: iswss, opts: opts, maxrx: maxrx}, nil
}
开发者ID:kildevaeld,项目名称:projects,代码行数:14,代码来源:ws.go


示例11: recvLoop

func recvLoop(sock mangos.Socket) {
	for {
		msg, err := sock.RecvMsg()
		switch err {
		case mangos.ErrProtoState:
			return
		case mangos.ErrRecvTimeout:
			return
		case nil:
		default:
			fatalf("RecvMsg failed: %v", err)
		}
		printMsg(msg)
		msg.Free()
	}
}
开发者ID:kildevaeld,项目名称:projects,代码行数:16,代码来源:macat.go


示例12: simpleSend

func simpleSend(t *testing.T, tx mangos.Socket, wg *sync.WaitGroup) {
	var buf [256]byte
	i := 0
	for i = 0; i < 10000; i++ {
		l := rand.Intn(255) + 1
		buf[0] = uint8(l)
		if e := tx.Send(buf[:l]); e != nil {
			t.Fatalf("Send: %v", e)
			break
		}
	}
	t.Logf("Sent %d Msgs", i)
	if e := tx.Close(); e != nil {
		t.Fatalf("Tx Close: %v", e)
	}
	wg.Done()
}
开发者ID:kildevaeld,项目名称:projects,代码行数:17,代码来源:simple_test.go


示例13: server

func server(url string, nworkers int) {
	var sock mangos.Socket
	var err error
	var wg sync.WaitGroup

	rand.Seed(time.Now().UnixNano())

	if sock, err = rep.NewSocket(); err != nil {
		die("can't get new rep socket: %s", err)
	}
	if err = sock.SetOption(mangos.OptionRaw, true); err != nil {
		die("can't set raw mode: %s", err)
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Listen(url); err != nil {
		die("can't listen on rep socket: %s", err.Error())
	}
	wg.Add(nworkers)
	fmt.Printf("Starting %d workers\n", nworkers)
	for id := 0; id < nworkers; id++ {
		go func(id int) {
			defer wg.Done()
			serverWorker(sock, id)
		}(id)
	}
	wg.Wait()
}
开发者ID:kildevaeld,项目名称:projects,代码行数:28,代码来源:server.go


示例14: sendLoop

func sendLoop(sock mangos.Socket) {
	if sendData == nil {
		fatalf("No data to send!")
	}
	for {
		msg := mangos.NewMessage(len(sendData))
		msg.Body = append(msg.Body, sendData...)
		err := sock.SendMsg(msg)

		if err != nil {
			fatalf("SendMsg failed: %v", err)
		}

		if sendInterval >= 0 {
			time.Sleep(time.Duration(sendInterval) * time.Second)
		} else {
			break
		}
	}
}
开发者ID:kildevaeld,项目名称:projects,代码行数:20,代码来源:macat.go


示例15: node1

func node1(url string, msg string) {
	var sock mangos.Socket
	var err error

	if sock, err = push.NewSocket(); err != nil {
		die("can't get new push socket: %s", err.Error())
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Dial(url); err != nil {
		die("can't dial on push socket: %s", err.Error())
	}
	fmt.Printf("NODE1: SENDING \"%s\"\n", msg)
	if err = sock.Send([]byte(msg)); err != nil {
		die("can't send message on push socket: %s", err.Error())
	}
	sock.Close()
}
开发者ID:kildevaeld,项目名称:projects,代码行数:18,代码来源:pipeline.go


示例16: serverWorker

func serverWorker(sock mangos.Socket, id int) {
	var err error

	delay := rand.Intn(int(time.Second))

	for {
		var m *mangos.Message

		if m, err = sock.RecvMsg(); err != nil {
			return
		}

		m.Body = make([]byte, 4)

		time.Sleep(time.Duration(delay))

		binary.BigEndian.PutUint32(m.Body[0:], uint32(id))

		if err = sock.SendMsg(m); err != nil {
			return
		}
	}
}
开发者ID:kildevaeld,项目名称:projects,代码行数:23,代码来源:server.go


示例17: simpleRecv

func simpleRecv(t *testing.T, rx mangos.Socket, wg *sync.WaitGroup) {
	i := 0
	for i = 0; i < 10000; i++ {
		buf, e := rx.Recv()
		if e != nil {
			t.Fatalf("Recv: %v", e)
			break
		}
		if len(buf) < 1 {
			t.Fatalf("Recv: empty buf")
			break
		}
		if len(buf) != int(buf[0]) {
			t.Fatalf("Recv: length %d != expected %d",
				len(buf), buf[0])
			break
		}
	}
	t.Logf("Recvd %d Msgs", i)
	if e := rx.Close(); e != nil {
		t.Fatalf("Rx Close: %v", e)
	}
	wg.Done()
}
开发者ID:kildevaeld,项目名称:projects,代码行数:24,代码来源:simple_test.go


示例18: node0

func node0(url string) {
	var sock mangos.Socket
	var err error
	var msg []byte
	if sock, err = pull.NewSocket(); err != nil {
		die("can't get new pull socket: %s", err)
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Listen(url); err != nil {
		die("can't listen on pull socket: %s", err.Error())
	}
	for {
		// Could also use sock.RecvMsg to get header
		msg, err = sock.Recv()
		fmt.Printf("NODE0: RECEIVED \"%s\"\n", msg)
	}
}
开发者ID:kildevaeld,项目名称:projects,代码行数:18,代码来源:pipeline.go


示例19: server

func server(url string) {
	var sock mangos.Socket
	var err error
	if sock, err = pub.NewSocket(); err != nil {
		die("can't get new pub socket: %s", err)
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Listen(url); err != nil {
		die("can't listen on pub socket: %s", err.Error())
	}
	for {
		// Could also use sock.RecvMsg to get header
		d := date()
		fmt.Printf("SERVER: PUBLISHING DATE %s\n", d)
		if err = sock.Send([]byte(d)); err != nil {
			die("Failed publishing: %s", err.Error())
		}
		time.Sleep(time.Second)
	}
}
开发者ID:kildevaeld,项目名称:projects,代码行数:21,代码来源:pubsub.go


示例20: sendRecvLoop

func sendRecvLoop(sock mangos.Socket) {
	for {
		msg := mangos.NewMessage(len(sendData))
		msg.Body = append(msg.Body, sendData...)
		err := sock.SendMsg(msg)

		if err != nil {
			fatalf("SendMsg failed: %v", err)
		}

		if sendInterval < 0 {
			recvLoop(sock)
			return
		}

		now := time.Now()

		// maximum wait time is upper bound of recvTimeout and
		// sendInterval

		if recvTimeout < 0 || recvTimeout > sendInterval {
			sock.SetOption(mangos.OptionRecvDeadline,
				time.Second*time.Duration(sendInterval))
		}
		msg, err = sock.RecvMsg()
		switch err {
		case mangos.ErrRecvTimeout:
		case nil:
			printMsg(msg)
			msg.Free()
		default:
			fatalf("RecvMsg failed: %v", err)
		}
		time.Sleep((time.Second * time.Duration(sendInterval)) -
			time.Now().Sub(now))
	}
}
开发者ID:kildevaeld,项目名称:projects,代码行数:37,代码来源:macat.go



注:本文中的github.com/kildevaeld/projects/Godeps/_workspace/src/github.com/gdamore/mangos.Socket类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang proto.CompactTextString函数代码示例发布时间:2022-05-23
下一篇:
Golang mangos.Message类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap