| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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 file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 class _ProcessStartStatus { | 5 class _ProcessStartStatus { |
| 6 int _errorCode; // Set to OS error code if process start failed. | 6 int _errorCode; // Set to OS error code if process start failed. |
| 7 String _errorMessage; // Set to OS error message if process start failed. | 7 String _errorMessage; // Set to OS error message if process start failed. |
| 8 } | 8 } |
| 9 | 9 |
| 10 | 10 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 void start() { | 32 void start() { |
| 33 var status = new _ProcessStartStatus(); | 33 var status = new _ProcessStartStatus(); |
| 34 bool success = _start( | 34 bool success = _start( |
| 35 _path, _arguments, _in, _out, _err, _exitHandler, status); | 35 _path, _arguments, _in, _out, _err, _exitHandler, status); |
| 36 if (!success) { | 36 if (!success) { |
| 37 close(); | 37 close(); |
| 38 throw new ProcessException(status._errorMessage, status._errorCode); | 38 throw new ProcessException(status._errorMessage, status._errorCode); |
| 39 } | 39 } |
| 40 _started = true; | 40 _started = true; |
| 41 if (_exitHandlerCallback !== null) { | 41 |
| 42 setExitHandler(_exitHandlerCallback); | 42 // Setup an exit handler to handle internal cleanup and possible |
| 43 } | 43 // callback when a process terminates. |
| 44 _exitHandler.setDataHandler(() { |
| 45 List<int> buffer = new List<int>(8); |
| 46 SocketInputStream input = _exitHandler.inputStream; |
| 47 |
| 48 int exitCode(List<int> ints) { |
| 49 return ints[4] + (ints[5] << 8) + (ints[6] << 16) + (ints[7] << 24); |
| 50 } |
| 51 |
| 52 int exitPid(List<int> ints) { |
| 53 return ints[0] + (ints[1] << 8) + (ints[2] << 16) + (ints[3] << 24); |
| 54 } |
| 55 |
| 56 void handleExit() { |
| 57 _processExit(exitPid(buffer)); |
| 58 if (_exitHandlerCallback != null) { |
| 59 _exitHandlerCallback(exitCode(buffer)); |
| 60 } |
| 61 } |
| 62 |
| 63 void readData() { |
| 64 handleExit(); |
| 65 } |
| 66 |
| 67 bool result = input.read(buffer, 0, 8, readData); |
| 68 if (result) { |
| 69 handleExit(); |
| 70 } |
| 71 }); |
| 44 } | 72 } |
| 45 | 73 |
| 46 bool _start(String path, | 74 bool _start(String path, |
| 47 List<String> arguments, | 75 List<String> arguments, |
| 48 Socket input, | 76 Socket input, |
| 49 Socket output, | 77 Socket output, |
| 50 Socket error, | 78 Socket error, |
| 51 Socket exitHandler, | 79 Socket exitHandler, |
| 52 _ProcessStartStatus status) native "Process_Start"; | 80 _ProcessStartStatus status) native "Process_Start"; |
| 53 | 81 |
| 82 void _processExit(int pid) native "Process_Exit"; |
| 83 |
| 54 InputStream get stdoutStream() { | 84 InputStream get stdoutStream() { |
| 55 if (_closed) { | 85 if (_closed) { |
| 56 throw new ProcessException("Process closed"); | 86 throw new ProcessException("Process closed"); |
| 57 } | 87 } |
| 58 return _in.inputStream; | 88 return _in.inputStream; |
| 59 } | 89 } |
| 60 | 90 |
| 61 InputStream get stderrStream() { | 91 InputStream get stderrStream() { |
| 62 if (_closed) { | 92 if (_closed) { |
| 63 throw new ProcessException("Process closed"); | 93 throw new ProcessException("Process closed"); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 _closed = true; | 129 _closed = true; |
| 100 } | 130 } |
| 101 | 131 |
| 102 void setExitHandler(void callback(int exitCode)) { | 132 void setExitHandler(void callback(int exitCode)) { |
| 103 if (_closed) { | 133 if (_closed) { |
| 104 throw new ProcessException("Process closed"); | 134 throw new ProcessException("Process closed"); |
| 105 } | 135 } |
| 106 if (_killed) { | 136 if (_killed) { |
| 107 throw new ProcessException("Process killed"); | 137 throw new ProcessException("Process killed"); |
| 108 } | 138 } |
| 109 if (_started) { | 139 _exitHandlerCallback = callback; |
| 110 _exitHandler.setDataHandler(() { | |
| 111 List<int> buffer = new List<int>(4); | |
| 112 SocketInputStream input = _exitHandler.inputStream; | |
| 113 | |
| 114 int getExitValue(List<int> ints) { | |
| 115 return ints[0] + (ints[1] << 8) + (ints[2] << 16) + (ints[3] << 24); | |
| 116 } | |
| 117 | |
| 118 void readData() { | |
| 119 callback(getExitValue(buffer)); | |
| 120 } | |
| 121 | |
| 122 bool result = input.read(buffer, 0, 4, readData); | |
| 123 if (result) { | |
| 124 callback(getExitValue(buffer)); | |
| 125 } | |
| 126 }); | |
| 127 } else { | |
| 128 _exitHandlerCallback = callback; | |
| 129 } | |
| 130 } | 140 } |
| 131 | 141 |
| 132 String _path; | 142 String _path; |
| 133 | |
| 134 ObjectArray<String> _arguments; | 143 ObjectArray<String> _arguments; |
| 135 | |
| 136 Socket _in; | 144 Socket _in; |
| 137 | |
| 138 Socket _out; | 145 Socket _out; |
| 139 | |
| 140 Socket _err; | 146 Socket _err; |
| 141 | |
| 142 Socket _exitHandler; | 147 Socket _exitHandler; |
| 143 | |
| 144 int _pid; | 148 int _pid; |
| 145 | |
| 146 bool _closed; | 149 bool _closed; |
| 147 | |
| 148 bool _killed; | 150 bool _killed; |
| 149 | |
| 150 bool _started; | 151 bool _started; |
| 151 | |
| 152 var _exitHandlerCallback; | 152 var _exitHandlerCallback; |
| 153 } | 153 } |
| OLD | NEW |