OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // | 5 // |
6 // Implementation of Socket and RawSocket for Mojo. | 6 // Implementation of Socket and RawSocket for Mojo. |
7 // | 7 // |
8 | 8 |
9 patch class Socket { | 9 patch class Socket { |
10 /* patch */ static Future<Socket> connect(host, int port, {sourceAddress}) { | 10 /* patch */ static Future<Socket> connect(host, int port, {sourceAddress}) { |
11 return RawSocket.connect(host, port, sourceAddress: sourceAddress).then( | 11 return RawSocket.connect(host, port, sourceAddress: sourceAddress).then( |
12 (socket) => new _MojoSocket(socket)); | 12 (socket) => new _MojoSocket(socket)); |
13 } | 13 } |
14 } | 14 } |
15 | 15 |
16 patch class RawSocket { | 16 patch class RawSocket { |
17 /* patch */ static Future<RawSocket> connect( | 17 /* patch */ static Future<RawSocket> connect( |
18 host, int port, {sourceAddress}) { | 18 host, int port, {sourceAddress}) { |
19 return _MojoRawSocket.connect(host, port, sourceAddress); | 19 return _MojoRawSocket.connect(host, port, sourceAddress); |
20 } | 20 } |
21 } | 21 } |
22 | 22 |
23 class _MojoRawSocket extends Stream<RawSocketEvent> implements RawSocket { | 23 class _MojoRawSocket extends Stream<RawSocketEvent> implements RawSocket { |
24 StreamController<RawSocketEvent> _controller; | 24 StreamController<RawSocketEvent> _controller; |
25 final _tcpBoundSocket = new TcpBoundSocketProxy.unbound(); | 25 final _tcpBoundSocket = new _TcpBoundSocketProxy.unbound(); |
26 final _tcpConnectedSocket = new TcpConnectedSocketProxy.unbound(); | 26 final _tcpConnectedSocket = new TcpConnectedSocketProxy.unbound(); |
27 // Constructing a new MojoDataPipe allocates two handles. All failure paths | 27 // Constructing a new MojoDataPipe allocates two handles. All failure paths |
28 // must be sure that these handles are closed so we do not leak any handles. | 28 // must be sure that these handles are closed so we do not leak any handles. |
29 final _pipeOut = new MojoDataPipe(); | 29 final _pipeOut = new MojoDataPipe(); |
30 bool _outClosed = false; | 30 bool _outClosed = false; |
31 // Constructing a new MojoDataPipe allocates two handles. All failure paths | 31 // Constructing a new MojoDataPipe allocates two handles. All failure paths |
32 // must be sure that these handles are closed so we do not leak any handles. | 32 // must be sure that these handles are closed so we do not leak any handles. |
33 final _pipeIn = new MojoDataPipe(); | 33 final _pipeIn = new MojoDataPipe(); |
34 bool _inClosed = false; | 34 bool _inClosed = false; |
35 bool _readEventsEnabled = true; | 35 bool _readEventsEnabled = true; |
(...skipping 11 matching lines...) Expand all Loading... |
47 | 47 |
48 _tracePrint(String message) { | 48 _tracePrint(String message) { |
49 assert(_trace); | 49 assert(_trace); |
50 print('${_traceId}: $message'); | 50 print('${_traceId}: $message'); |
51 } | 51 } |
52 | 52 |
53 _traceProxies() { | 53 _traceProxies() { |
54 if (!_trace) { | 54 if (!_trace) { |
55 return; | 55 return; |
56 } | 56 } |
57 _tracePrint('_tcpBoundSocket handle = ${_tcpBoundSocket.handle}'); | 57 _tracePrint( |
58 _tracePrint('_tcpConnectedSocket handle = ${_tcpConnectedSocket.handle}'); | 58 '_tcpBoundSocket handle = ${_tcpBoundSocket.proxy.handle}'); |
| 59 _tracePrint( |
| 60 '_tcpConnectedSocket handle = ${_tcpConnectedSocket.handle}'); |
59 } | 61 } |
60 | 62 |
61 _tracePipeIn() { | 63 _tracePipeIn() { |
62 if (!_trace) { | 64 if (!_trace) { |
63 return; | 65 return; |
64 } | 66 } |
65 if (_pipeInEvents != null) { | 67 if (_pipeInEvents != null) { |
66 _tracePrint('pipe in consumer handle = ${_pipeInEvents.handle}'); | 68 _tracePrint('pipe in consumer handle = ${_pipeInEvents.handle}'); |
67 } else { | 69 } else { |
68 _tracePrint('pipe in consumer handle ${_pipeIn.consumer.handle}'); | 70 _tracePrint('pipe in consumer handle ${_pipeIn.consumer.handle}'); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 if (!_trace) { | 119 if (!_trace) { |
118 return; | 120 return; |
119 } | 121 } |
120 _tracePrint(_tcpConnectedSocket.toString()); | 122 _tracePrint(_tcpConnectedSocket.toString()); |
121 } | 123 } |
122 | 124 |
123 _traceBoundSocket() { | 125 _traceBoundSocket() { |
124 if (!_trace) { | 126 if (!_trace) { |
125 return; | 127 return; |
126 } | 128 } |
127 _tracePrint(_tcpBoundSocket.toString()); | 129 _tracePrint(_tcpBoundSocket.proxy.toString()); |
128 } | 130 } |
129 | 131 |
130 static int _traceIdGenerator = 0; | 132 static int _traceIdGenerator = 0; |
131 static _enableTrace(_MojoRawSocket rawSocket) { | 133 static _enableTrace(_MojoRawSocket rawSocket) { |
132 if (rawSocket._trace) { | 134 if (rawSocket._trace) { |
133 return; | 135 return; |
134 } | 136 } |
135 rawSocket._trace = true; | 137 rawSocket._trace = true; |
136 rawSocket._traceId = _traceIdGenerator++; | 138 rawSocket._traceId = _traceIdGenerator++; |
137 rawSocket._tracePrint('Tracing enabled for ${rawSocket._traceId}'); | 139 rawSocket._tracePrint('Tracing enabled for ${rawSocket._traceId}'); |
138 rawSocket._traceLocalAddress(); | 140 rawSocket._traceLocalAddress(); |
139 rawSocket._traceRemoteAddress(); | 141 rawSocket._traceRemoteAddress(); |
140 } | 142 } |
141 | 143 |
142 _MojoRawSocket() { | 144 _MojoRawSocket() { |
143 _controller = new StreamController(sync: true, | 145 _controller = new StreamController(sync: true, |
144 onListen: _onSubscriptionStateChange, | 146 onListen: _onSubscriptionStateChange, |
145 onCancel: _onSubscriptionStateChange, | 147 onCancel: _onSubscriptionStateChange, |
146 onPause: _onPauseStateChange, | 148 onPause: _onPauseStateChange, |
147 onResume: _onPauseStateChange); | 149 onResume: _onPauseStateChange); |
148 } | 150 } |
149 | 151 |
150 static Future<_MojoRawSocket> _connect(NetAddress source, | 152 static Future<_MojoRawSocket> _connect(NetAddress source, |
151 NetAddress dest) async { | 153 NetAddress dest) async { |
152 var rawSocket = new _MojoRawSocket(); | 154 var rawSocket = new _MojoRawSocket(); |
153 var networkService = _getNetworkService(); | 155 var networkService = _getNetworkService(); |
154 assert(networkService != null); | 156 assert(networkService != null); |
155 var response = | 157 var response = await networkService.createTcpBoundSocket( |
156 await networkService.createTcpBoundSocket(source, | 158 source, rawSocket._tcpBoundSocket.proxy); |
157 rawSocket._tcpBoundSocket); | |
158 if (!_NetworkService._okay(response.result)) { | 159 if (!_NetworkService._okay(response.result)) { |
159 rawSocket.close(); | 160 rawSocket.close(); |
160 _NetworkService._throwOnError(response.result); | 161 _NetworkService._throwOnError(response.result); |
161 } | 162 } |
162 | 163 |
163 rawSocket._traceBoundSocket(); | 164 rawSocket._traceBoundSocket(); |
164 | 165 |
165 rawSocket._localAddress = | 166 rawSocket._localAddress = |
166 _NetworkServiceCodec._fromNetAddress(response.boundTo); | 167 _NetworkServiceCodec._fromNetAddress(response.boundTo); |
167 rawSocket._localPort = | 168 rawSocket._localPort = |
(...skipping 640 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
808 if (_raw != null) { | 809 if (_raw != null) { |
809 _raw.shutdown(SocketDirection.SEND); | 810 _raw.shutdown(SocketDirection.SEND); |
810 _disableWriteEvent(); | 811 _disableWriteEvent(); |
811 } | 812 } |
812 } | 813 } |
813 } | 814 } |
814 | 815 |
815 Map _toJSON(bool ref) => _raw._toJSON(ref); | 816 Map _toJSON(bool ref) => _raw._toJSON(ref); |
816 void set _owner(owner) { _raw._owner = owner; } | 817 void set _owner(owner) { _raw._owner = owner; } |
817 } | 818 } |
OLD | NEW |