OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package system | |
6 | |
7 // MessagePipeHandle is a handle for a bidirectional communication channel for | |
8 // framed data (i.e., messages). Messages can contain plain data and/or Mojo | |
9 // handles. | |
10 type MessagePipeHandle interface { | |
11 Handle | |
12 | |
13 // ReadMessage reads a message from the message pipe endpoint with the | |
14 // specified flags. Returns the message data and attached handles that w
ere | |
15 // received in the "next" message. | |
16 ReadMessage(flags MojoReadMessageFlags) (MojoResult, []byte, []UntypedHa
ndle) | |
17 | |
18 // WriteMessage writes message data and optional attached handles to | |
19 // the message pipe endpoint given by handle. On success the attached | |
20 // handles will no longer be valid (i.e.: the receiver will receive | |
21 // equivalent but logically different handles). | |
22 WriteMessage(bytes []byte, handles []UntypedHandle, flags MojoWriteMessa
geFlags) MojoResult | |
23 } | |
24 | |
25 type messagePipe struct { | |
26 // baseHandle should always be the first component of this struct, | |
27 // see |finalizeHandle()| for more details. | |
28 baseHandle | |
29 } | |
30 | |
31 func (h *messagePipe) ReadMessage(flags MojoReadMessageFlags) (MojoResult, []byt
e, []UntypedHandle) { | |
32 h.core.mu.Lock() | |
33 r, buf, rawHandles := sysImpl.ReadMessage(uint32(h.mojoHandle), uint32(f
lags)) | |
34 h.core.mu.Unlock() | |
35 if r != 0 { | |
36 return MojoResult(r), nil, nil | |
37 } | |
38 | |
39 handles := make([]UntypedHandle, len(rawHandles)) | |
40 for i := 0; i < len(handles); i++ { | |
41 handles[i] = h.core.AcquireNativeHandle(MojoHandle(rawHandles[i]
)) | |
42 } | |
43 return MojoResult(r), buf, handles | |
44 } | |
45 | |
46 func (h *messagePipe) WriteMessage(bytes []byte, handles []UntypedHandle, flags
MojoWriteMessageFlags) MojoResult { | |
47 | |
48 var rawHandles []uint32 | |
49 if len(handles) != 0 { | |
50 rawHandles = make([]uint32, len(handles)) | |
51 for i := 0; i < len(handles); i++ { | |
52 rawHandles[i] = uint32(handles[i].ReleaseNativeHandle()) | |
53 } | |
54 } | |
55 h.core.mu.Lock() | |
56 r := sysImpl.WriteMessage(uint32(h.mojoHandle), bytes, rawHandles, uint3
2(flags)) | |
57 h.core.mu.Unlock() | |
58 return MojoResult(r) | |
59 } | |
OLD | NEW |