| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 part of bindings; | |
| 6 | |
| 7 abstract class Interface extends core.MojoEventStreamListener { | |
| 8 int _outstandingResponseFutures = 0; | |
| 9 bool _isClosing = false; | |
| 10 | |
| 11 Interface(core.MojoMessagePipeEndpoint endpoint) : super(endpoint); | |
| 12 | |
| 13 Interface.fromHandle(core.MojoHandle handle) : super.fromHandle(handle); | |
| 14 | |
| 15 Interface.unbound() : super.unbound(); | |
| 16 | |
| 17 Future<Message> handleMessage(ServiceMessage message); | |
| 18 | |
| 19 void handleRead() { | |
| 20 // Query how many bytes are available. | |
| 21 var result = endpoint.query(); | |
| 22 assert(result.status.isOk || result.status.isResourceExhausted); | |
| 23 | |
| 24 // Read the data and view as a message. | |
| 25 var bytes = new ByteData(result.bytesRead); | |
| 26 var handles = new List<core.MojoHandle>(result.handlesRead); | |
| 27 result = endpoint.read(bytes, result.bytesRead, handles); | |
| 28 assert(result.status.isOk || result.status.isResourceExhausted); | |
| 29 | |
| 30 // Prepare the response. | |
| 31 var message = new ServiceMessage.fromMessage(new Message(bytes, handles)); | |
| 32 var responseFuture = _isClosing ? null : handleMessage(message); | |
| 33 | |
| 34 // If there's a response, send it. | |
| 35 if (responseFuture != null) { | |
| 36 _outstandingResponseFutures++; | |
| 37 responseFuture.then((response) { | |
| 38 _outstandingResponseFutures--; | |
| 39 if (isOpen) { | |
| 40 endpoint.write(response.buffer, | |
| 41 response.buffer.lengthInBytes, | |
| 42 response.handles); | |
| 43 if (!endpoint.status.isOk) { | |
| 44 throw "message pipe write failed: ${endpoint.status}"; | |
| 45 } | |
| 46 if (_isClosing && (_outstandingResponseFutures == 0)) { | |
| 47 // This was the final response future for which we needed to send | |
| 48 // a response. It is safe to close. | |
| 49 super.close(); | |
| 50 _isClosing = false; | |
| 51 } | |
| 52 } | |
| 53 }); | |
| 54 } else if (_isClosing && (_outstandingResponseFutures == 0)) { | |
| 55 // We are closing, there is no response to send for this message, and | |
| 56 // there are no outstanding response futures. Do the close now. | |
| 57 super.close(); | |
| 58 _isClosing = false; | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 void handleWrite() { | |
| 63 throw 'Unexpected write signal in client.'; | |
| 64 } | |
| 65 | |
| 66 void close() { | |
| 67 if (!isOpen) return; | |
| 68 if (isInHandler || (_outstandingResponseFutures > 0)) { | |
| 69 // Either close() is being called from within handleRead() or | |
| 70 // handleWrite(), or close() is being called while there are outstanding | |
| 71 // response futures. Defer the actual close until all response futures | |
| 72 // have been resolved. | |
| 73 _isClosing = true; | |
| 74 } else { | |
| 75 super.close(); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 Message buildResponse(Struct response, int name) { | |
| 80 var header = new MessageHeader(name); | |
| 81 return response.serializeWithHeader(header); | |
| 82 } | |
| 83 | |
| 84 Message buildResponseWithId(Struct response, int name, int id, int flags) { | |
| 85 var header = new MessageHeader.withRequestId(name, flags, id); | |
| 86 return response.serializeWithHeader(header); | |
| 87 } | |
| 88 | |
| 89 void sendMessage(Struct message, int name) { | |
| 90 var header = new MessageHeader(name); | |
| 91 var serviceMessage = message.serializeWithHeader(header); | |
| 92 endpoint.write(serviceMessage.buffer, | |
| 93 serviceMessage.buffer.lengthInBytes, | |
| 94 serviceMessage.handles); | |
| 95 if (!endpoint.status.isOk) { | |
| 96 throw "message pipe write failed: ${endpoint.status}"; | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 Future sendMessageWithRequestId(Struct response, int name, int id) { | |
| 101 throw "The client interface should not expect a response"; | |
| 102 } | |
| 103 } | |
| OLD | NEW |