Skip to content

Commit

Permalink
Reset TCP/IP connection with a '+' prefix
Browse files Browse the repository at this point in the history
When running scrcpy with --tcpip=xx.xx.xx.xx, to make sure a new working
connection is established, it was first disconnected by a call to:

    adb disconnect <addr>

However, this caused all running instances connected to that address to
be killed. Running several instances of scrcpy on the same device is now
useful with virtual displays, so change the default behavior to NOT
disconnect.

To force a reconnection, a '+' prefix can be added:

    scrcpy --tcpip=+192.168.0.x

Fixes #5562 <#5562>
  • Loading branch information
rom1v committed Dec 4, 2024
1 parent b26b4fb commit 0e473eb
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
6 changes: 4 additions & 2 deletions app/scrcpy.1
Original file line number Diff line number Diff line change
Expand Up @@ -518,13 +518,15 @@ Enable "show touches" on start, restore the initial value on exit.
It only shows physical touches (not clicks from scrcpy).

.TP
.BI "\-\-tcpip\fR[=\fIip\fR[:\fIport\fR]]
Configure and reconnect the device over TCP/IP.
.BI "\-\-tcpip\fR[=[+]\fIip\fR[:\fIport\fR]]
Configure and connect the device over TCP/IP.

If a destination address is provided, then scrcpy connects to this address before starting. The device must listen on the given TCP port (default is 5555).

If no destination address is provided, then scrcpy attempts to find the IP address and adb port of the current device (typically connected over USB), enables TCP/IP mode if necessary, then connects to this address before starting.

Prefix the address with a '+' to force a reconnection.

.TP
.BI "\-\-time\-limit " seconds
Set the maximum mirroring time, in seconds.
Expand Down
5 changes: 3 additions & 2 deletions app/src/adb/adb.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ sc_adb_connect(struct sc_intr *intr, const char *ip_port, unsigned flags) {

// "adb connect" always returns successfully (with exit code 0), even in
// case of failure. As a workaround, check if its output starts with
// "connected".
// "connected" or "already connected".
char buf[128];
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf) - 1);
sc_pipe_close(pout);
Expand All @@ -429,7 +429,8 @@ sc_adb_connect(struct sc_intr *intr, const char *ip_port, unsigned flags) {
assert((size_t) r < sizeof(buf));
buf[r] = '\0';

ok = !strncmp("connected", buf, sizeof("connected") - 1);
ok = !strncmp("connected", buf, sizeof("connected") - 1)
|| !strncmp("already connected", buf, sizeof("already connected") - 1);
if (!ok && !(flags & SC_ADB_NO_STDERR)) {
// "adb connect" also prints errors to stdout. Since we capture it,
// re-print the error to stderr.
Expand Down
7 changes: 4 additions & 3 deletions app/src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -860,16 +860,17 @@ static const struct sc_option options[] = {
{
.longopt_id = OPT_TCPIP,
.longopt = "tcpip",
.argdesc = "ip[:port]",
.argdesc = "[+]ip[:port]",
.optional_arg = true,
.text = "Configure and reconnect the device over TCP/IP.\n"
.text = "Configure and connect the device over TCP/IP.\n"
"If a destination address is provided, then scrcpy connects to "
"this address before starting. The device must listen on the "
"given TCP port (default is 5555).\n"
"If no destination address is provided, then scrcpy attempts "
"to find the IP address of the current device (typically "
"connected over USB), enables TCP/IP mode, then connects to "
"this address before starting.",
"this address before starting.\n"
"Prefix the address with a '+' to force a reconnection.",
},
{
.longopt_id = OPT_TIME_LIMIT,
Expand Down
23 changes: 16 additions & 7 deletions app/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -829,11 +829,14 @@ sc_server_switch_to_tcpip(struct sc_server *server, const char *serial) {
}

static bool
sc_server_connect_to_tcpip(struct sc_server *server, const char *ip_port) {
sc_server_connect_to_tcpip(struct sc_server *server, const char *ip_port,
bool disconnect) {
struct sc_intr *intr = &server->intr;

// Error expected if not connected, do not report any error
sc_adb_disconnect(intr, ip_port, SC_ADB_SILENT);
if (disconnect) {
// Error expected if not connected, do not report any error
sc_adb_disconnect(intr, ip_port, SC_ADB_SILENT);
}

LOGI("Connecting to %s...", ip_port);

Expand All @@ -849,7 +852,7 @@ sc_server_connect_to_tcpip(struct sc_server *server, const char *ip_port) {

static bool
sc_server_configure_tcpip_known_address(struct sc_server *server,
const char *addr) {
const char *addr, bool disconnect) {
// Append ":5555" if no port is present
bool contains_port = strchr(addr, ':');
char *ip_port = contains_port ? strdup(addr)
Expand All @@ -860,7 +863,7 @@ sc_server_configure_tcpip_known_address(struct sc_server *server,
}

server->serial = ip_port;
return sc_server_connect_to_tcpip(server, ip_port);
return sc_server_connect_to_tcpip(server, ip_port, disconnect);
}

static bool
Expand All @@ -885,7 +888,7 @@ sc_server_configure_tcpip_unknown_address(struct sc_server *server,
}

server->serial = ip_port;
return sc_server_connect_to_tcpip(server, ip_port);
return sc_server_connect_to_tcpip(server, ip_port, false);
}

static void
Expand Down Expand Up @@ -972,7 +975,13 @@ run_server(void *data) {
sc_adb_device_destroy(&device);
}
} else {
ok = sc_server_configure_tcpip_known_address(server, params->tcpip_dst);
// If the user passed a '+' (--tcpip=+ip), then disconnect first
const char *tcpip_dst = params->tcpip_dst;
bool plus = tcpip_dst[0] == '+';
if (plus) {
++tcpip_dst;
}
ok = sc_server_configure_tcpip_known_address(server, tcpip_dst, plus);
if (!ok) {
goto error_connection_failed;
}
Expand Down
6 changes: 6 additions & 0 deletions doc/connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ scrcpy --tcpip=192.168.1.1 # default port is 5555
scrcpy --tcpip=192.168.1.1:5555
```

Prefix the address with a '+' to force a reconnection:

```bash
scrcpy --tcpip=+192.168.1.1
```


### Manual

Expand Down

0 comments on commit 0e473eb

Please sign in to comment.