示例#1
0
文件: rwc.go 项目: Gaboose/go-pubsub
func server(whole net.Conn) (ErrorCodeRWC, error) {

	mainPeelCh := make(chan io.ReadWriteCloser)
	ecPeelCh := make(chan io.ReadWriteCloser)
	ecCh := make(chan byte)

	rt := sroute.NewMultistreamMuxer()
	rt.AddHandler("/main", func(mainPeel io.ReadWriteCloser) error {
		mainPeelCh <- mainPeel
		return nil
	})
	rt.AddHandler("/ec", func(ecPeel io.ReadWriteCloser) error {
		ecPeelCh <- ecPeel
		go readToCh(ecPeel, ecCh)
		return nil
	})

	sc, err := transport.NewConn(whole, true)
	if err != nil {
		return nil, err
	}

	go sc.Serve(func(s smux.Stream) {
		rt.Handle(s)
	})

	return &errorCodeRWC{whole, <-mainPeelCh, <-ecPeelCh, ecCh}, nil
}
示例#2
0
func NewGateway() *Gateway {
	router := ms.NewMultistreamMuxer()

	conns := ps.NewSwarm(transport)
	conns.SetStreamHandler(func(s *ps.Stream) {
		go func() {
			router.Handle(s)
		}()
	})

	return &Gateway{
		conns:  conns,
		router: router,
	}
}
示例#3
0
func NewTransport() pst.Transport {
	mux := mss.NewMultistreamMuxer()
	mux.AddHandler("/multiplex", nil)
	mux.AddHandler("/spdystream", nil)
	mux.AddHandler("/yamux", nil)

	tpts := map[string]pst.Transport{
		"/multiplex":  multiplex.DefaultTransport,
		"/spdystream": spdy.Transport,
		"/yamux":      yamux.DefaultTransport,
	}

	return &transport{
		mux:  mux,
		tpts: tpts,
	}
}
示例#4
0
// New constructs and sets up a new *BasicHost with given Network
func New(net inet.Network, opts ...interface{}) *BasicHost {
	h := &BasicHost{
		network:          net,
		mux:              msmux.NewMultistreamMuxer(),
		NegotiateTimeout: NegotiateTimeout,
	}

	h.proc = goprocess.WithTeardown(func() error {
		if h.natmgr != nil {
			h.natmgr.Close()
		}

		return h.Network().Close()
	})

	// setup host services
	h.ids = identify.NewIDService(h)

	muxh := h.Mux().Handle
	handle := func(s inet.Stream) {
		muxh(s)
	}
	h.relay = relay.NewRelayService(h, handle)

	for _, o := range opts {
		switch o := o.(type) {
		case Option:
			switch o {
			case NATPortMap:
				h.natmgr = newNatManager(h)
			}
		case metrics.Reporter:
			h.bwc = o
		}
	}

	h.ids.Reporter = h.bwc

	net.SetConnHandler(h.newConnHandler)
	net.SetStreamHandler(h.newStreamHandler)

	return h
}
示例#5
0
func Server(listen string, protos []string) error {
	list, err := net.Listen("tcp", listen)
	if err != nil {
		return err
	}

	mux := mss.NewMultistreamMuxer()
	for _, p := range protos {
		mux.AddHandler(p, EchoHandler)
	}

	for {
		con, err := list.Accept()
		if err != nil {
			return err
		}

		err = mux.Handle(con)
		if err != nil {
			return err
		}
	}
}