2015-07-11 21:32:04 +00:00
|
|
|
package goadb
|
2015-04-12 20:34:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-12-30 08:00:03 +00:00
|
|
|
"io"
|
2015-04-12 20:34:20 +00:00
|
|
|
"net"
|
|
|
|
"runtime"
|
2015-07-11 21:32:04 +00:00
|
|
|
|
2015-07-12 06:18:58 +00:00
|
|
|
"github.com/zach-klippenstein/goadb/util"
|
2015-07-11 21:32:04 +00:00
|
|
|
"github.com/zach-klippenstein/goadb/wire"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Default port the adb server listens on.
|
|
|
|
AdbPort = 5037
|
2015-04-12 20:34:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
Dialer knows how to create connections to an adb server.
|
|
|
|
*/
|
|
|
|
type Dialer interface {
|
2015-07-11 21:32:04 +00:00
|
|
|
Dial() (*wire.Conn, error)
|
2015-04-12 20:34:20 +00:00
|
|
|
}
|
|
|
|
|
2015-07-11 21:32:04 +00:00
|
|
|
/*
|
|
|
|
NewDialer creates a new Dialer.
|
2015-04-12 20:34:20 +00:00
|
|
|
|
2015-07-11 21:32:04 +00:00
|
|
|
If host is "" or port is 0, "localhost:5037" is used.
|
|
|
|
*/
|
2015-04-12 20:34:20 +00:00
|
|
|
func NewDialer(host string, port int) Dialer {
|
2015-07-11 21:32:04 +00:00
|
|
|
if host == "" {
|
|
|
|
host = "localhost"
|
|
|
|
}
|
|
|
|
if port == 0 {
|
|
|
|
port = AdbPort
|
|
|
|
}
|
2015-04-12 20:34:20 +00:00
|
|
|
return &netDialer{host, port}
|
|
|
|
}
|
|
|
|
|
2015-07-11 21:32:04 +00:00
|
|
|
type netDialer struct {
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
}
|
|
|
|
|
2015-05-04 01:07:03 +00:00
|
|
|
func (d *netDialer) String() string {
|
|
|
|
return fmt.Sprintf("netDialer(%s:%d)", d.Host, d.Port)
|
|
|
|
}
|
|
|
|
|
2015-04-12 20:34:20 +00:00
|
|
|
// Dial connects to the adb server on the host and port set on the netDialer.
|
|
|
|
// The zero-value will connect to the default, localhost:5037.
|
2015-07-11 21:32:04 +00:00
|
|
|
func (d *netDialer) Dial() (*wire.Conn, error) {
|
2015-04-12 20:34:20 +00:00
|
|
|
host := d.Host
|
2015-05-03 13:11:09 +00:00
|
|
|
port := d.Port
|
|
|
|
|
2015-07-12 06:18:58 +00:00
|
|
|
address := fmt.Sprintf("%s:%d", host, port)
|
|
|
|
netConn, err := net.Dial("tcp", address)
|
2015-04-12 20:34:20 +00:00
|
|
|
if err != nil {
|
2015-09-07 23:06:20 +00:00
|
|
|
// Attempt to start the server and try again.
|
|
|
|
if err = StartServer(); err != nil {
|
|
|
|
return nil, util.WrapErrorf(err, util.ServerNotAvailable, "error starting server")
|
|
|
|
}
|
|
|
|
|
|
|
|
address = fmt.Sprintf("%s:%d", host, port)
|
|
|
|
netConn, err = net.Dial("tcp", address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, util.WrapErrorf(err, util.ServerNotAvailable, "error dialing %s", address)
|
|
|
|
}
|
2015-04-12 20:34:20 +00:00
|
|
|
}
|
|
|
|
|
2015-12-30 08:00:03 +00:00
|
|
|
// net.Conn can't be closed more than once, but wire.Conn will try to close both sender and scanner
|
|
|
|
// so we need to wrap it to make it safe.
|
|
|
|
safeConn := wire.MultiCloseable(netConn)
|
2015-04-12 20:34:20 +00:00
|
|
|
|
|
|
|
// Prevent leaking the network connection, not sure if TCPConn does this itself.
|
2015-12-30 08:00:03 +00:00
|
|
|
// Note that the network connection may still be in use after the conn isn't (scanners/senders
|
|
|
|
// can give their underlying connections to other scanner/sender types), so we can't
|
|
|
|
// set the finalizer on conn.
|
|
|
|
runtime.SetFinalizer(safeConn, func(conn io.ReadWriteCloser) {
|
2015-04-12 20:34:20 +00:00
|
|
|
conn.Close()
|
|
|
|
})
|
|
|
|
|
2015-12-30 08:00:03 +00:00
|
|
|
return &wire.Conn{
|
|
|
|
Scanner: wire.NewScanner(safeConn),
|
|
|
|
Sender: wire.NewSender(safeConn),
|
|
|
|
}, nil
|
2015-04-12 20:34:20 +00:00
|
|
|
}
|
|
|
|
|
2015-07-11 21:32:04 +00:00
|
|
|
func roundTripSingleResponse(d Dialer, req string) ([]byte, error) {
|
2015-04-12 20:34:20 +00:00
|
|
|
conn, err := d.Dial()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
return conn.RoundTripSingleResponse([]byte(req))
|
|
|
|
}
|