Beispiel #1
0
func main() {

	l1, err := reuse.Listen("tcp", "0.0.0.0:11111")
	maybeDie(err)
	fmt.Printf("listening on %s\n", l1.Addr())

	l2, err := reuse.Listen("tcp", "0.0.0.0:22222")
	maybeDie(err)
	fmt.Printf("listening on %s\n", l2.Addr())

	a1, err := resolve.ResolveAddr("dial", "tcp", "127.0.0.1:11111")
	maybeDie(err)

	a3, err := resolve.ResolveAddr("dial", "tcp", "127.0.0.1:33333")
	maybeDie(err)

	d1 := reuse.Dialer{net.Dialer{LocalAddr: a1}}
	d2 := reuse.Dialer{net.Dialer{LocalAddr: a3}}

	go func() {
		l2to1foo, err := l2.Accept()
		maybeDie(err)
		fmt.Printf("%s accepted conn from %s\n", addrStr(l2.Addr()), addrStr(l2to1foo.RemoteAddr()))

		fmt.Println("safe")

		l1to2bar, err := l1.Accept()
		maybeDie(err)
		fmt.Printf("%s accepted conn from %s\n", addrStr(l1.Addr()), addrStr(l1to2bar.RemoteAddr()))

		io.Copy(l1to2bar, l2to1foo)
	}()

	d1to2foo, err := d1.Dial("tcp4", "127.0.0.1:22222")
	maybeDie(err)
	fmt.Printf("dialing from %s to %s\n", d1.D.LocalAddr, "127.0.0.1:22222")

	d2to1bar, err := d2.Dial("tcp4", "127.0.0.1:11111")
	maybeDie(err)
	fmt.Printf("dialing from %s to %s\n", d2.D.LocalAddr, "127.0.0.1:11111")

	go io.Copy(d1to2foo, os.Stdin)
	io.Copy(os.Stdout, d2to1bar)
}
Beispiel #2
0
func (p *protoServer) Start() error {
	var err error
	if p.FnInst == nil {
		log.Tag("server", "gormethods").Errorf("FnInst function is nil!")
		return e.New("failed to init server")
	}
	if p.TlsConfig != nil {
		p.listener, err = tls.Listen(p.Proto, p.Addr, p.TlsConfig)
	} else {
		p.listener, err = reuse.Listen(p.Proto, p.Addr)
	}
	if err != nil {
		log.Tag("server", "gormethods").Errorf("Can't listen on interface %v proto %v: %v", p.Addr, p.Proto, e.Forward(err))
		return e.Forward(err)
	}
	err = p.chmodSocket(0660)
	if err != nil {
		log.Tag("server", "gormethods", "proto").Errorf("Fail to chmod socket %v: %v", p.listener.Addr(), e.Forward(err))
		return e.Forward(err)
	}
	p.closedListener = make(chan struct{})
	go func() {
		defer func() {
			log.Tag("server", "gormethods", "proto").Println("Listener accept close.")
			p.closedListener <- struct{}{}
		}()
		for {
			// Ok1
			conn, err := p.listener.Accept()
			if e.Contains(err, "use of closed network connection") {
				p.listener.Close()
				return
			} else if e.Contains(err, "too many open files") {
				p.listener.Close()
				log.Tag("server", "gormethods").Error(err)
				return
			} else if err != nil {
				p.listener.Close()
				log.Tag("server", "gormethods", "proto").Errorf("Accept for %v failed: %v", p.listener.Addr(), e.Forward(err))
				return
			}
			go p.accept(conn)
		}
	}()
	return nil
}
Beispiel #3
0
func manetListen(addr ma.Multiaddr) (manet.Listener, error) {
	network, naddr, err := manet.DialArgs(addr)
	if err != nil {
		return nil, err
	}

	if ReuseportIsAvailable() {
		nl, err := reuseport.Listen(network, naddr)
		if err == nil {
			// hey, it worked!
			return manet.WrapNetListener(nl)
		}
		// reuseport is available, but we failed to listen. log debug, and retry normally.
		log.Debugf("reuseport available, but failed to listen: %s %s, %s", network, naddr, err)
	}

	// either reuseport not available, or it failed. try normally.
	return manet.Listen(addr)
}