Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option for flutter daemon to listen on a TCP port #95418

Merged
merged 3 commits into from Dec 22, 2021

Conversation

chingjun
Copy link
Contributor

Added a new class DaemonConnection to reuse the connection handling
between daemon server and client, and handle connection with different
medium (stdio, socket).

Added a new option listen-on-tcp-port to the flutter daemon command,
when passed, the daemon will accept commands on a port instead of stdio.

This will be used to create a device proxy.

Context: b/210724354

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I signed the CLA.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@flutter-dashboard flutter-dashboard bot added the tool Affects the "flutter" command-line tool. See also t: labels. label Dec 16, 2021
Added a new class DaemonConnection to reuse the connection handling
between daemon server and client, and handle connection with different
medium (stdio, socket).

Added a new option `listen-on-tcp-port` to the flutter daemon command,
when passed, the daemon will accept commands on a port instead of stdio.
Comment on lines 67 to 69
final String port = stringArg('listen-on-tcp-port');
await _DaemonServer(
port: int.parse(port),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be checked that it's a valid int.

Suggested change
final String port = stringArg('listen-on-tcp-port');
await _DaemonServer(
port: int.parse(port),
int port;
try {
port = int.parse(stringArg('listen-on-tcp-port'));
} on FormatException catch (error) {
throwToolExit('Invalid port for `--listen-on-tcp-port`: $error');
}
await _DaemonServer(
port: port,

this.notifyingLogger,
});

int port;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int port;
final int port;

int port;

/// Stdout logger used to print general server-related errors.
Logger logger;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Logger logger;
final Logger logger;

Logger logger;

// Logger that sends the message to the other end of daemon connection.
NotifyingLogger notifyingLogger;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
NotifyingLogger notifyingLogger;
final NotifyingLogger notifyingLogger;

if (!_onExitCompleter.isCompleted) {
_onExitCompleter.complete(0);
}
},
);
}

DaemonConnection connection;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DaemonConnection connection;
final DaemonConnection connection;

void send(Map<String, Object?> message) {
globals.stdio.stdoutWrite(
'[${json.encode(message)}]\n',
fallback: (String message, dynamic error, StackTrace stack) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fallback: (String message, dynamic error, StackTrace stack) {
fallback: (String message, Object? error, StackTrace stack) {

packages/flutter_tools/lib/src/daemon.dart Show resolved Hide resolved
map['params'] = _toJsonable(args);
}
_send(map);
daemon.connection.sendEvent(name, args != null ? _toJsonable(args) : null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_toJsonable returns null when args is null.

Suggested change
daemon.connection.sendEvent(name, args != null ? _toJsonable(args) : null);
daemon.connection.sendEvent(name, _toJsonable(args));

}

/// A [DaemonStream] that uses [Socket] as the underlying stream.
class TcpDaemonStreams extends DaemonStreams {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this tested anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a new test for this class

/// microtask queue. This avoids a deadlock when tests `await` a Future
/// which queues a microtask that will not be processed unless the queue
/// is flushed.
// Future<T> _runFakeAsync<T>(Future<T> Function(FakeAsync time) f) async {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete?

@chingjun
Copy link
Contributor Author

Thanks for the thorough review! I've just pushed the fix for the review feedbacks. PTAL, thanks!

Copy link
Member

@jmagman jmagman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nits

stdinCommandStream,
stdoutCommandResponse,
DaemonConnection(
daemonStreams: StdioDaemonStreams(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
daemonStreams: StdioDaemonStreams(),
daemonStreams: StdioDaemonStreams(globals.stdio),

globals.printStatus('Starting device daemon...');
final Daemon daemon = Daemon(
stdinCommandStream, stdoutCommandResponse,
DaemonConnection(
daemonStreams: StdioDaemonStreams(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
daemonStreams: StdioDaemonStreams(),
daemonStreams: StdioDaemonStreams(globals.stdio),

stdinCommandStream,
stdoutCommandResponse,
DaemonConnection(
daemonStreams: StdioDaemonStreams(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
daemonStreams: StdioDaemonStreams(),
daemonStreams: StdioDaemonStreams(globals.stdio),

Comment on lines 42 to 44
StdioDaemonStreams([Stdio? stdio]) :
_stdio = stdio ?? globals.stdio,
inputStream = _convertInputStream((stdio ?? globals.stdio).stdin);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make the injected globals required, ideally we should be able to remove import 'globals.dart' as globals; from almost everywhere but context_runner.dart. Unfortunately the commands don't have their dependencies injected yet, so the global context objects will need to be passed from there.

Suggested change
StdioDaemonStreams([Stdio? stdio]) :
_stdio = stdio ?? globals.stdio,
inputStream = _convertInputStream((stdio ?? globals.stdio).stdin);
StdioDaemonStreams(Stdio stdio) :
_stdio = stdio,
inputStream = _convertInputStream(stdio.stdin);

Comment on lines 67 to 68
Logger? logger,
}): _logger = logger ?? globals.logger {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Logger? logger,
}): _logger = logger ?? globals.logger {
required Logger logger,
}): _logger = logger {

Comment on lines 76 to 77
Logger? logger,
}) : _logger = logger ?? globals.logger {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Logger? logger,
}) : _logger = logger ?? globals.logger {
required Logger logger,
}) : _logger = logger {

});
final Daemon daemon = Daemon(
DaemonConnection(
daemonStreams: TcpDaemonStreams(socket),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
daemonStreams: TcpDaemonStreams(socket),
daemonStreams: TcpDaemonStreams(socket, logger: logger),

}

/// Connects to a remote host and creates a [DaemonStreams] from the socket.
TcpDaemonStreams.connect(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be used in my next PR, would you want me to remove it from here, and add the change in my next PR instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, you can leave it in this PR, wanted to make sure it wasn't leftover from something else you tried.

Comment on lines 4 to 5

// @dart = 2.8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// @dart = 2.8

Make this file null safe.

Comment on lines 42 to 44
BufferLogger bufferLogger;
FakeDaemonStreams daemonStreams;
DaemonConnection daemonConnection;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
BufferLogger bufferLogger;
FakeDaemonStreams daemonStreams;
DaemonConnection daemonConnection;
late BufferLogger bufferLogger;
late FakeDaemonStreams daemonStreams;
late DaemonConnection daemonConnection;

@chingjun
Copy link
Contributor Author

Just pushed the changes for the review feedback, PTAL, thanks!

Copy link
Member

@jmagman jmagman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@chingjun
Copy link
Contributor Author

Thanks for the review Jenn!

@chingjun chingjun merged commit 2b46ea4 into flutter:master Dec 22, 2021
zanderso added a commit that referenced this pull request Dec 22, 2021
zanderso added a commit that referenced this pull request Dec 22, 2021
chingjun added a commit to chingjun/flutter that referenced this pull request Dec 22, 2021
chingjun added a commit that referenced this pull request Dec 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tool Affects the "flutter" command-line tool. See also t: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants