Esempio n. 1
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)
}
Esempio n. 2
0
func manetlib() {
	addr, err := ma.NewMultiaddr("/ip4/127.0.0.1/udp/5556/udt")
	if err != nil {
		panic(err)
	}

	list, err := manet.Listen(addr)
	if err != nil {
		panic(err)
	}

	defer list.Close()

	runtest(
		func() (net.Conn, error) {
			return list.Accept()
		},
		func() (net.Conn, error) {
			return manet.Dial(addr)
		},
	)
}
Esempio n. 3
0
func apiServer(port int) error {
	m := BuildAPIAddr(port)
	ln, err := manet.Listen(m)
	if err != nil {
		return err
	}

	fmt.Printf("API server listening on %v\n", m)

	// Go accept API requests.
	go func() {
		defer ln.Close()

		for {
			conn, err := ln.Accept()
			if err != nil {
				fmt.Println(err)
				return
			}

			// Go handle the new connection.
			go func(con manet.Conn) {
				defer con.Close()

				// Decode arguments.
				var args []string
				mx := mux.StandardMux()
				err = mx.Decoder(con).Decode(&args)
				if err != nil {
					fmt.Println(err)
					return
				}

				cmdName := args[0]
				args = args[1:]

				// Get the command object
				v, ok := (*commandsPtr)[cmdName]
				if !ok {
					fmt.Printf("Unknown command: %s\n", cmdName)
					return
				}
				cmd, ok := v.(*RemoteCommand)
				if !ok {
					fmt.Printf("%s is not a remote command\n", cmdName)
					return
				}

				// Multiplex connection
				remoteStdio, err := WithErrorCode(con)
				if err != nil {
					fmt.Println(err)
					return
				}

				// Run cmd. Provide remote connection as stdin/stdout.
				ec := cmd.Run(args, remoteStdio)
				remoteStdio.Close(ec)
			}(conn)
		}
	}()

	return nil
}