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 class MessageHeader { | |
8 static const int kSimpleMessageSize = 16; | |
9 static const int kSimpleMessageNumFields = 2; | |
10 static const int kMessageWithRequestIdSize = 24; | |
11 static const int kMessageWithRequestIdNumFields = 3; | |
12 static const int kMessageTypeOffset = DataHeader.kHeaderSize; | |
13 static const int kMessageFlagsOffset = kMessageTypeOffset + 4; | |
14 static const int kMessageRequestIdOffset = kMessageFlagsOffset + 4; | |
15 static const int kMessageExpectsResponse = 1 << 0; | |
16 static const int kMessageIsResponse = 1 << 1; | |
17 | |
18 DataHeader _header; | |
19 int type; | |
20 int flags; | |
21 int requestId; | |
22 | |
23 static bool mustHaveRequestId(int flags) => | |
24 (flags & (kMessageExpectsResponse | kMessageIsResponse)) != 0; | |
25 | |
26 MessageHeader(this.type) : | |
27 _header = new DataHeader(kSimpleMessageSize, kSimpleMessageNumFields), | |
28 flags = 0, | |
29 requestId = 0; | |
30 | |
31 MessageHeader.withRequestId(this.type, this.flags, this.requestId) : | |
32 _header = new DataHeader( | |
33 kMessageWithRequestIdSize, kMessageWithRequestIdNumFields); | |
34 | |
35 MessageHeader.fromMessage(Message message) { | |
36 var decoder = new Decoder(message); | |
37 _header = decoder.decodeDataHeader(); | |
38 type = decoder.decodeUint32(kMessageTypeOffset); | |
39 flags = decoder.decodeUint32(kMessageFlagsOffset); | |
40 if (mustHaveRequestId(flags)) { | |
41 if (_header.size < kMessageWithRequestIdSize) { | |
42 throw 'Incorrect message size. Got: ${_header.size} ' + | |
43 'wanted $kMessageWithRequestIdSize'; | |
44 } | |
45 requestId = decoder.decodeUint64(kMessageRequestIdOffset); | |
46 } else { | |
47 requestId = 0; | |
48 } | |
49 } | |
50 | |
51 int get size => _header.size; | |
52 bool get hasRequestId => mustHaveRequestId(flags); | |
53 | |
54 void encode(Encoder encoder) { | |
55 encoder.encodeDataHeader(_header); | |
56 encoder.encodeUint32(type, kMessageTypeOffset); | |
57 encoder.encodeUint32(flags, kMessageFlagsOffset); | |
58 if (hasRequestId) { | |
59 encoder.encodeUint64(requestId, kMessageRequestIdOffset); | |
60 } | |
61 } | |
62 | |
63 ServiceMessage get serviceMessage => new ServiceMessage(this); | |
64 | |
65 String toString() => "MessageHeader($_header, $type, $flags, $requestId)"; | |
66 } | |
67 | |
68 | |
69 class Message { | |
70 final ByteData buffer; | |
71 final List<core.MojoHandle> handles; | |
72 Message(this.buffer, this.handles); | |
73 String toString() => | |
74 "Message(numBytes=${buffer.lengthInBytes}, numHandles=${handles.length})"; | |
75 } | |
76 | |
77 | |
78 class ServiceMessage extends Message { | |
79 final MessageHeader header; | |
80 Message _payload; | |
81 | |
82 ServiceMessage(Message message, this.header) | |
83 : super(message.buffer, message.handles); | |
84 | |
85 ServiceMessage.fromMessage(Message message) | |
86 : this(message, new MessageHeader.fromMessage(message)); | |
87 | |
88 Message get payload { | |
89 if (_payload == null) { | |
90 var truncatedBuffer = new ByteData.view(buffer.buffer, header.size); | |
91 _payload = new Message(truncatedBuffer, handles); | |
92 } | |
93 return _payload; | |
94 } | |
95 | |
96 String toString() => "ServiceMessage($header, $_payload)"; | |
97 } | |
OLD | NEW |