當前位置: 首頁>>代碼示例>>Golang>>正文


Golang tcp.NewTransport函數代碼示例

本文整理匯總了Golang中github.com/go-mangos/mangos/transport/tcp.NewTransport函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewTransport函數的具體用法?Golang NewTransport怎麽用?Golang NewTransport使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewTransport函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: main

func main() {
	var iaddr string
	flag.StringVar(&iaddr, "iaddr", "tcp://localhost:5555", "input data port")

	var oaddr string
	flag.StringVar(&oaddr, "oaddr", "tcp://localhost:5556", "output data port")

	flag.Parse()

	isck, err := pull.NewSocket()
	if err != nil {
		log.Fatalf("error creating a nanomsg socket: %v\n", err)
	}
	defer isck.Close()

	isck.AddTransport(ipc.NewTransport())
	isck.AddTransport(tcp.NewTransport())

	osck, err := push.NewSocket()
	if err != nil {
		log.Fatalf("error creating output port: %v\n", err)
	}
	defer osck.Close()

	osck.AddTransport(ipc.NewTransport())
	osck.AddTransport(tcp.NewTransport())

	log.Printf("dialing %s ...\n", iaddr)
	err = isck.Dial(iaddr)
	if err != nil {
		log.Fatalf("error dialing: %v\n", err)
	}
	log.Printf("dialing %s ... [done]\n", iaddr)

	log.Printf("dialing %s ...\n", oaddr)
	err = osck.Dial(oaddr)
	if err != nil {
		log.Fatalf("error dialing: %v\n", err)
	}
	log.Printf("dialing %s ... [done]\n", oaddr)

	for {
		msg, err := isck.Recv()
		if err != nil {
			if err == io.EOF || err == mangos.ErrClosed {
				log.Printf("received EOF: %v\n", err)
				break
			}
			log.Fatalf("error receiving data: %v\n", err)
		}
		log.Printf("recv: %v\n", string(msg))

		omsg := bytes.Repeat(msg, 2)
		err = osck.Send(omsg)
		if err != nil {
			log.Fatalf("error sending data: %v\n", err)
		}
	}

}
開發者ID:mkrzewic,項目名稱:FairRoot,代碼行數:60,代碼來源:main.go

示例2: TestSimpleCorrect

func TestSimpleCorrect(t *testing.T) {
	tx, e := pair.NewSocket()
	if e != nil {
		t.Fatalf("NewSocket: %v", e)
		return
	}
	rx, e := pair.NewSocket()
	if e != nil {
		t.Fatalf("NewSocket: %v", e)
		return
	}
	tx.AddTransport(tcp.NewTransport())
	rx.AddTransport(tcp.NewTransport())

	if e = rx.Listen(AddrTestTCP); e != nil {
		t.Fatalf("Listen: %v", e)
		return
	}
	if e = tx.Dial(AddrTestTCP); e != nil {
		t.Fatalf("Dial: %v", e)
		return
	}

	wg := &sync.WaitGroup{}
	wg.Add(2)
	go simpleSend(t, tx, wg)
	go simpleRecv(t, rx, wg)
	wg.Wait()
}
開發者ID:laszlo-kiss,項目名稱:mangos,代碼行數:29,代碼來源:simple_test.go

示例3: 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:laszlo-kiss,項目名稱:mangos,代碼行數:26,代碼來源:reqrep.go

示例4: testDevLoop

func testDevLoop(t *testing.T, addr string) {
	s1, err := pair.NewSocket()
	if err != nil {
		t.Errorf("Failed to open S1: %v", err)
		return
	}
	defer s1.Close()
	s1.AddTransport(tcp.NewTransport())
	s1.AddTransport(inproc.NewTransport())
	s1.AddTransport(ipc.NewTransport())
	s1.AddTransport(tlstcp.NewTransport())
	s1.AddTransport(ws.NewTransport())
	s1.AddTransport(wss.NewTransport())

	options := make(map[string]interface{})
	if strings.HasPrefix(addr, "wss://") || strings.HasPrefix(addr, "tls+tcp://") {
		options[mangos.OptionTLSConfig] = srvCfg
	}

	if err := s1.ListenOptions(addr, options); err != nil {
		t.Errorf("Failed listening to %s: %v", addr, err)
		return
	}

	if err := mangos.Device(s1, s1); err != nil {
		t.Errorf("Device failed: %v", err)
		return
	}

	RunTests(t, addr, deviceCaseClient())
}
開發者ID:laszlo-kiss,項目名稱:mangos,代碼行數:31,代碼來源:device_test.go

示例5: 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:laszlo-kiss,項目名稱:mangos,代碼行數:28,代碼來源:server.go

示例6: newEvtChannel

func newEvtChannel(addr string) (mangos.Socket, error) {
	socket, err := sub.NewSocket()
	if err != nil {
		return nil, err
	}

	if err := socket.SetOption(mangos.OptionRecvDeadline, time.Second*120); 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
	}

	err = socket.SetOption(mangos.OptionSubscribe, []byte(""))
	if err != nil {
		return nil, err
	}

	return socket, nil
}
開發者ID:BenHall,項目名稱:docker-slim,代碼行數:25,代碼來源:ipc.go

示例7: 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
}
開發者ID:BenHall,項目名稱:docker-slim,代碼行數:25,代碼來源:ipc.go

示例8: 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:laszlo-kiss,項目名稱:mangos,代碼行數:27,代碼來源:survey.go

示例9: newBusNode

func newBusNode(addr string) (busNode, error) {
	var err error
	var node busNode

	sock, err := bus.NewSocket()
	if err != nil {
		return node, err
	}

	node.sock = sock
	node.sock.AddTransport(ipc.NewTransport())
	node.sock.AddTransport(tcp.NewTransport())

	err = node.sock.Listen("tcp://127.0.0.1:0")
	if err != nil {
		return node, err
	}

	err = node.sock.Dial(addr)
	if err != nil {
		return node, err
	}

	return node, err
}
開發者ID:go-lsst,項目名稱:ncs,代碼行數:25,代碼來源:bus.go

示例10: server

func server(url string) {
	var sock mangos.Socket
	var err error
	var msg []byte
	if sock, err = surveyor.NewSocket(); err != nil {
		die("can't get new surveyor socket: %s", err)
	}
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tcp.NewTransport())
	if err = sock.Listen(url); err != nil {
		die("can't listen on surveyor socket: %s", err.Error())
	}
	err = sock.SetOption(mangos.OptionSurveyTime, time.Second/2)
	if err != nil {
		die("SetOption(): %s", err.Error())
	}
	for {
		time.Sleep(time.Second)
		fmt.Println("SERVER: SENDING DATE SURVEY REQUEST")
		if err = sock.Send([]byte("DATE")); err != nil {
			die("Failed sending survey: %s", err.Error())
		}
		for {
			if msg, err = sock.Recv(); err != nil {
				break
			}
			fmt.Printf("SERVER: RECEIVED \"%s\" SURVEY RESPONSE\n",
				string(msg))
		}
		fmt.Println("SERVER: SURVEY OVER")
	}
}
開發者ID:laszlo-kiss,項目名稱:mangos,代碼行數:32,代碼來源:survey.go

示例11: 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:laszlo-kiss,項目名稱:mangos,代碼行數:25,代碼來源:pubsub.go

示例12: NewDaemon

// NewDaemon creates and returns a new Daemon from the provided Config. An
// error is returned if the Daemon cannot be created.
func NewDaemon(config *Config) (*Daemon, error) {
	rep, err := rep.NewSocket()
	if err != nil {
		return nil, err
	}
	rep.AddTransport(tcp.NewTransport())
	return &Daemon{rep, nil, []*publisher{}, []*subscriber{}, config}, nil
}
開發者ID:huikang,項目名稱:Flotilla,代碼行數:10,代碼來源:daemon.go

示例13: AddTransports

// AddTransports adds all known transports to the given socket.
func AddTransports(sock mangos.Socket) {
	sock.AddTransport(tcp.NewTransport())
	sock.AddTransport(inproc.NewTransport())
	sock.AddTransport(ipc.NewTransport())
	sock.AddTransport(tlstcp.NewTransport())
	sock.AddTransport(ws.NewTransport())
	sock.AddTransport(wss.NewTransport())
}
開發者ID:laszlo-kiss,項目名稱:mangos,代碼行數:9,代碼來源:all.go

示例14: 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
}
開發者ID:huikang,項目名稱:Flotilla,代碼行數:45,代碼來源:client.go

示例15: newSocket

func newSocket() mangos.Socket {
	socket, err := pair.NewSocket()
	if err != nil {
		log.Fatalf("Node %s: Cannot create socket: %s\n", node, err.Error())
	}

	socket.AddTransport(ipc.NewTransport())
	socket.AddTransport(tcp.NewTransport())

	socket.SetOption(mangos.OptionRecvDeadline, 10*time.Second)
	return socket
}
開發者ID:exu,項目名稱:go-workshops,代碼行數:12,代碼來源:mangos.go


注:本文中的github.com/go-mangos/mangos/transport/tcp.NewTransport函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。