OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE.md file. | |
4 | |
5 library fletchc.client_commands; | |
6 | |
7 import 'dart:io' show | |
8 Socket; | |
9 | |
10 import 'dart:async' show | |
11 StreamSubscription; | |
12 | |
13 import 'dart:typed_data' show | |
14 Uint8List; | |
15 | |
16 import 'dart:convert' show | |
17 UTF8; | |
18 | |
19 import '../console_print.dart' show | |
20 printToConsole; | |
21 | |
22 import '../please_report_crash.dart' show | |
23 stringifyError; | |
24 | |
25 enum ClientCommandCode { | |
26 // Note: if you modify this enum, please modify src/tools/driver/connection.h | |
27 // as well. | |
28 | |
29 /// Data on stdin. | |
30 Stdin, | |
31 | |
32 /// Data on stdout. | |
33 Stdout, | |
34 | |
35 /// Data on stderr. | |
36 Stderr, | |
37 | |
38 /// Command-line arguments. | |
39 Arguments, | |
40 | |
41 /// Unix process signal received. | |
42 Signal, | |
43 | |
44 /// Set process exit code. | |
45 ExitCode, | |
46 | |
47 /// Tell the receiver that commands will be processed immediatly. | |
48 EventLoopStarted, | |
49 | |
50 /// Tell receiver to close the port this was sent to. | |
51 ClosePort, | |
52 | |
53 /// A SendPort that the receiver can use to communicate with the sender. | |
54 SendPort, | |
55 | |
56 /// The the receiver to perform a task. | |
57 PerformTask, | |
58 | |
59 /// Error in connection. | |
60 ClientConnectionError, | |
61 | |
62 /// Connection closed. | |
63 ClientConnectionClosed, | |
64 } | |
65 | |
66 class ClientCommand { | |
67 final ClientCommandCode code; | |
68 final data; | |
69 | |
70 ClientCommand(this.code, this.data); | |
71 | |
72 String toString() => 'ClientCommand($code, $data)'; | |
73 } | |
74 | |
75 abstract class CommandSender { | |
76 void sendExitCode(int exitCode); | |
77 | |
78 void sendStdout(String data) { | |
79 sendStdoutBytes(new Uint8List.fromList(UTF8.encode(data))); | |
80 } | |
81 | |
82 void sendStdoutBytes(List<int> data) { | |
83 sendDataCommand(ClientCommandCode.Stdout, data); | |
84 } | |
85 | |
86 void sendStderr(String data) { | |
87 sendStderrBytes(new Uint8List.fromList(UTF8.encode(data))); | |
88 } | |
89 | |
90 void sendStderrBytes(List<int> data) { | |
91 sendDataCommand(ClientCommandCode.Stderr, data); | |
92 } | |
93 | |
94 void sendDataCommand(ClientCommandCode code, List<int> data); | |
95 | |
96 void sendClose(); | |
97 | |
98 void sendEventLoopStarted(); | |
99 } | |
100 | |
101 Function makeErrorHandler(String info) { | |
102 return (error, StackTrace stackTrace) { | |
103 printToConsole("Error on $info: ${stringifyError(error, stackTrace)}"); | |
104 }; | |
105 } | |
106 | |
107 Socket handleSocketErrors(Socket socket, String name, {void log(String info)}) { | |
108 String host = "?"; | |
109 String remotePort = "?"; | |
110 try { | |
111 host = "${socket.remoteAddress.host}"; | |
112 remotePort = "${socket.remotePort}"; | |
113 } catch (_) { | |
114 // Ignored, socket.remotePort may fail if the socket was closed on the | |
115 // other side. | |
116 } | |
117 String info = "$name ${socket.port} -> $host:$remotePort"; | |
118 if (log != null) { | |
119 log(info); | |
120 } | |
121 socket.done.catchError(makeErrorHandler(info)); | |
122 return socket; | |
123 } | |
OLD | NEW |