OLD | NEW |
---|---|
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library vmservice_io; | 5 library vmservice_io; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'dart:isolate'; | 10 import 'dart:isolate'; |
11 import 'dart:vmservice'; | 11 import 'dart:vmservice'; |
12 | 12 |
13 part 'loader.dart'; | 13 part 'loader.dart'; |
14 part 'resources.dart'; | 14 part 'resources.dart'; |
15 part 'server.dart'; | 15 part 'server.dart'; |
16 | 16 |
17 // The TCP ip/port that the HTTP server listens on. | 17 // The TCP ip/port that the HTTP server listens on. |
18 int _port; | 18 int _port; |
19 String _ip; | 19 String _ip; |
20 // Should the HTTP server auto start? | 20 // Should the HTTP server auto start? |
21 bool _autoStart; | 21 bool _autoStart; |
22 | 22 |
23 bool _isWindows = false; | 23 bool _isWindows = false; |
24 | 24 |
25 var _signalWatch; | 25 var _signalWatch; |
26 var _signalSubscription; | |
26 | 27 |
27 // HTTP servr. | 28 // HTTP servr. |
28 Server server; | 29 Server server; |
29 Future<Server> serverFuture; | 30 Future<Server> serverFuture; |
30 | 31 |
31 void _bootServer() { | 32 _onShutdown() { |
33 if (server != null) { | |
34 server.close(true).catchError((e, st) => assert(e)); | |
kasperl
2015/03/27 07:50:55
This is illegal syntax (assert isn't an expression
| |
35 } | |
36 if (_signalSubscription != null) { | |
37 _signalSubscription.cancel(); | |
38 _signalSubscription = null; | |
39 } | |
40 } | |
41 | |
42 _bootServer() { | |
32 // Load resources. | 43 // Load resources. |
33 _triggerResourceLoad(); | 44 _triggerResourceLoad(); |
34 // Lazily create service. | 45 // Lazily create service. |
35 var service = new VMService(); | 46 var service = new VMService(); |
47 service.onShutdown = _onShutdown; | |
36 // Lazily create server. | 48 // Lazily create server. |
37 server = new Server(service, _ip, _port); | 49 server = new Server(service, _ip, _port); |
38 } | 50 } |
39 | 51 |
40 void _clearFuture(_) { | 52 _clearFuture(_) { |
41 serverFuture = null; | 53 serverFuture = null; |
42 } | 54 } |
43 | 55 |
44 void _onSignal(ProcessSignal signal) { | 56 _onSignal(ProcessSignal signal) { |
45 if (serverFuture != null) { | 57 if (serverFuture != null) { |
46 // Still waiting. | 58 // Still waiting. |
47 return; | 59 return; |
48 } | 60 } |
49 if (server == null) { | 61 if (server == null) { |
50 _bootServer(); | 62 _bootServer(); |
51 } | 63 } |
52 // Toggle HTTP server. | 64 // Toggle HTTP server. |
53 if (server.running) { | 65 if (server.running) { |
54 serverFuture = server.shutdown(true).then(_clearFuture); | 66 serverFuture = server.shutdown(true).then(_clearFuture); |
55 } else { | 67 } else { |
56 serverFuture = server.startup().then(_clearFuture); | 68 serverFuture = server.startup().then(_clearFuture); |
57 } | 69 } |
58 } | 70 } |
59 | 71 |
60 void _registerSignalHandler() { | 72 _registerSignalHandler() { |
61 if (_isWindows) { | 73 if (_isWindows) { |
62 // Cannot register for signals on Windows. | 74 // Cannot register for signals on Windows. |
63 return; | 75 return; |
64 } | 76 } |
65 _signalWatch(ProcessSignal.SIGQUIT).listen(_onSignal); | 77 _signalSubscription = _signalWatch(ProcessSignal.SIGQUIT).listen(_onSignal); |
66 } | 78 } |
67 | 79 |
68 const _shortDelay = const Duration(milliseconds: 10); | 80 const _shortDelay = const Duration(milliseconds: 10); |
69 | 81 |
70 main() { | 82 main() { |
71 if (_autoStart) { | 83 if (_autoStart) { |
72 _bootServer(); | 84 _bootServer(); |
73 server.startup(); | 85 server.startup(); |
74 // It's just here to push an event on the event loop so that we invoke the | 86 // It's just here to push an event on the event loop so that we invoke the |
75 // scheduled microtasks. | 87 // scheduled microtasks. |
76 Timer.run(() {}); | 88 Timer.run(() {}); |
77 } | 89 } |
78 scriptLoadPort.handler = _processLoadRequest; | 90 scriptLoadPort.handler = _processLoadRequest; |
79 // Register signal handler after a small delay to avoid stalling main | 91 // Register signal handler after a small delay to avoid stalling main |
80 // isolate startup. | 92 // isolate startup. |
81 new Timer(_shortDelay, _registerSignalHandler); | 93 new Timer(_shortDelay, _registerSignalHandler); |
82 return scriptLoadPort; | 94 return scriptLoadPort; |
83 } | 95 } |
OLD | NEW |