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 package system | 5 package system |
6 | 6 |
7 //#include "c_allocators.h" | 7 //#include "c_allocators.h" |
8 //#include "mojo/public/c/system/core.h" | 8 //#include "mojo/public/c/system/core.h" |
9 import "C" | 9 import "C" |
10 import "unsafe" | 10 import "unsafe" |
11 | 11 |
12 // ConsumerHandle is a handle for the consumer part of a data pipe. | 12 // ConsumerHandle is a handle for the consumer part of a data pipe. |
13 type ConsumerHandle interface { | 13 type ConsumerHandle interface { |
14 Handle | 14 Handle |
15 | 15 |
16 // ReadData reads data from the data pipe consumer handle with the | 16 // ReadData reads data from the data pipe consumer handle with the |
17 // given flags. On success, returns the data that was read. | 17 // given flags. On success, returns the data that was read. |
18 ReadData(flags MojoReadDataFlags) (MojoResult, []byte) | 18 ReadData(flags MojoReadDataFlags) (MojoResult, []byte) |
| 19 |
| 20 // BeginReadData begins a two-phase read from the data pipe consumer. |
| 21 // On success, returns a slice from which the caller can read up to its |
| 22 // length bytes of data. If flags has |MOJO_READ_DATA_FLAG_ALL_OR_NONE| |
| 23 // set, then the slice length will be at least as large as |numBytes|, |
| 24 // which must also be a multiple of the element size (otherwise the |
| 25 // caller must check the length of the slice). |
| 26 // |
| 27 // During a two-phase read, this handle is *not* readable. E.g., read |
| 28 // from this handle will return |MOJO_RESULT_BUSY|. |
| 29 // |
| 30 // Once the caller has finished reading data from the slice, it should |
| 31 // call |EndReadData()| to specify the amount read and to complete the |
| 32 // two-phase read. |
| 33 BeginReadData(numBytes int, flags MojoReadDataFlags) (MojoResult, []byte
) |
| 34 |
| 35 // EndReadData ends a two-phase read from the data pipe consumer that |
| 36 // was begun by a call to |BeginReadData()| on the same handle. |
| 37 // |numBytesRead| should indicate the amount of data actually read; it |
| 38 // must be less than or equal to the length of the slice returned by |
| 39 // |BeginReadData()| and must be a multiple of the element size. |
| 40 // |
| 41 // On failure, the two-phase read (if any) is ended (so the handle may |
| 42 // become readable again) but no data is "removed" from the data pipe. |
| 43 EndReadData(numBytesRead int) MojoResult |
19 } | 44 } |
20 | 45 |
21 // ProducerHandle is a handle for the producer part of a data pipe. | 46 // ProducerHandle is a handle for the producer part of a data pipe. |
22 type ProducerHandle interface { | 47 type ProducerHandle interface { |
23 Handle | 48 Handle |
24 | 49 |
25 // WriteData writes data to the data pipe producer handle with the | 50 // WriteData writes data to the data pipe producer handle with the |
26 // given flags. On success, returns the number of bytes that were | 51 // given flags. On success, returns the number of bytes that were |
27 // actually written. | 52 // actually written. |
28 WriteData(data []byte, flags MojoWriteDataFlags) (MojoResult, int) | 53 WriteData(data []byte, flags MojoWriteDataFlags) (MojoResult, int) |
| 54 |
| 55 // BeginWriteData begins a two-phase write to the data pipe producer. |
| 56 // On success, returns a slice to which the caller can write. If flags |
| 57 // has |MOJO_READ_DATA_FLAG_ALL_OR_NONE| set, then the slice length will |
| 58 // be at least as large as |numBytes|, which must also be a multiple of |
| 59 // the element size (otherwise the caller must check the length of the |
| 60 // slice). |
| 61 // |
| 62 // During a two-phase write, this handle is *not* writable. E.g., write |
| 63 // to this handle will return |MOJO_RESULT_BUSY|. |
| 64 // |
| 65 // Once the caller has finished writing data to the buffer, it should |
| 66 // call |EndWriteData()| to specify the amount written and to complete |
| 67 // the two-phase write. |
| 68 BeginWriteData(numBytes int, flags MojoWriteDataFlags) (MojoResult, []by
te) |
| 69 |
| 70 // EndWriteData ends a two-phase write to the data pipe producer that |
| 71 // was begun by a call to |BeginWriteData()| on the same handle. |
| 72 // |numBytesWritten| should indicate the amount of data actually |
| 73 // written; it must be less than or equal to the length of the slice |
| 74 // returned by |BeginWriteData()| and must be a multiple of the element |
| 75 // size. The slice returned from |BeginWriteData()| must have been |
| 76 // filled with exactly |numBytesWritten| bytes of data. |
| 77 // |
| 78 // On failure, the two-phase write (if any) is ended (so the handle may |
| 79 // become writable again, if there's space available) but no data |
| 80 // written to the slice is "put into" the data pipe. |
| 81 EndWriteData(numBytesWritten int) MojoResult |
29 } | 82 } |
30 | 83 |
31 type dataPipeConsumer struct { | 84 type dataPipeConsumer struct { |
32 baseHandle | 85 baseHandle |
33 } | 86 } |
34 | 87 |
35 func (h *dataPipeConsumer) ReadData(flags MojoReadDataFlags) (MojoResult, []byte
) { | 88 func (h *dataPipeConsumer) ReadData(flags MojoReadDataFlags) (MojoResult, []byte
) { |
36 cParams := C.MallocReadDataParams() | 89 cParams := C.MallocReadDataParams() |
37 defer C.FreeReadDataParams(cParams) | 90 defer C.FreeReadDataParams(cParams) |
38 *cParams.num_bytes = 0 | 91 *cParams.num_bytes = 0 |
39 if result := C.MojoReadData(h.mojoHandle.cValue(), nil, cParams.num_byte
s, C.MOJO_READ_DATA_FLAG_QUERY); result != C.MOJO_RESULT_OK { | 92 if result := C.MojoReadData(h.mojoHandle.cValue(), nil, cParams.num_byte
s, C.MOJO_READ_DATA_FLAG_QUERY); result != C.MOJO_RESULT_OK { |
40 return MojoResult(result), nil | 93 return MojoResult(result), nil |
41 } | 94 } |
42 dataArray := C.MallocDataArray(*cParams.num_bytes) | 95 dataArray := C.MallocDataArray(*cParams.num_bytes) |
43 defer C.FreeDataArray(dataArray) | 96 defer C.FreeDataArray(dataArray) |
44 result := C.MojoReadData(h.mojoHandle.cValue(), dataArray.elements, cPar
ams.num_bytes, flags.cValue()) | 97 result := C.MojoReadData(h.mojoHandle.cValue(), dataArray.elements, cPar
ams.num_bytes, flags.cValue()) |
45 dataSize := int(*cParams.num_bytes) | 98 dataSize := int(*cParams.num_bytes) |
46 data := make([]byte, dataSize) | 99 data := make([]byte, dataSize) |
47 cData := unsafeByteSlice(unsafe.Pointer(dataArray.elements), dataSize) | 100 cData := unsafeByteSlice(unsafe.Pointer(dataArray.elements), dataSize) |
48 copy(data, cData) | 101 copy(data, cData) |
49 return MojoResult(result), data | 102 return MojoResult(result), data |
50 } | 103 } |
51 | 104 |
| 105 func (h *dataPipeConsumer) BeginReadData(numBytes int, flags MojoReadDataFlags)
(MojoResult, []byte) { |
| 106 cParams := C.MallocTwoPhaseActionParams() |
| 107 defer C.FreeTwoPhaseActionParams(cParams) |
| 108 *cParams.num_bytes = C.uint32_t(numBytes) |
| 109 result := C.MojoBeginReadData(h.mojoHandle.cValue(), cParams.buffer, cPa
rams.num_bytes, flags.cValue()) |
| 110 buffer := unsafeByteSlice(unsafe.Pointer(*cParams.buffer), int(*cParams.
num_bytes)) |
| 111 return MojoResult(result), buffer |
| 112 } |
| 113 |
| 114 func (h *dataPipeConsumer) EndReadData(numBytesRead int) MojoResult { |
| 115 return MojoResult(C.MojoEndReadData(h.mojoHandle.cValue(), C.uint32_t(nu
mBytesRead))) |
| 116 } |
| 117 |
52 type dataPipeProducer struct { | 118 type dataPipeProducer struct { |
53 baseHandle | 119 baseHandle |
54 } | 120 } |
55 | 121 |
56 func (h *dataPipeProducer) WriteData(data []byte, flags MojoWriteDataFlags) (Moj
oResult, int) { | 122 func (h *dataPipeProducer) WriteData(data []byte, flags MojoWriteDataFlags) (Moj
oResult, int) { |
57 cParams := C.MallocWriteDataParams(C.uint32_t(len(data))) | 123 cParams := C.MallocWriteDataParams(C.uint32_t(len(data))) |
58 defer C.FreeWriteDataParams(cParams) | 124 defer C.FreeWriteDataParams(cParams) |
59 *cParams.num_bytes = C.uint32_t(len(data)) | 125 *cParams.num_bytes = C.uint32_t(len(data)) |
60 cArray := unsafeByteSlice(unsafe.Pointer(cParams.elements), len(data)) | 126 cArray := unsafeByteSlice(unsafe.Pointer(cParams.elements), len(data)) |
61 copy(cArray, data) | 127 copy(cArray, data) |
62 result := C.MojoWriteData(h.mojoHandle.cValue(), cParams.elements, cPara
ms.num_bytes, flags.cValue()) | 128 result := C.MojoWriteData(h.mojoHandle.cValue(), cParams.elements, cPara
ms.num_bytes, flags.cValue()) |
63 return MojoResult(result), int(*cParams.num_bytes) | 129 return MojoResult(result), int(*cParams.num_bytes) |
64 } | 130 } |
| 131 |
| 132 func (h *dataPipeProducer) BeginWriteData(numBytes int, flags MojoWriteDataFlags
) (MojoResult, []byte) { |
| 133 cParams := C.MallocTwoPhaseActionParams() |
| 134 defer C.FreeTwoPhaseActionParams(cParams) |
| 135 *cParams.num_bytes = C.uint32_t(numBytes) |
| 136 result := C.MojoBeginWriteData(h.mojoHandle.cValue(), cParams.buffer, cP
arams.num_bytes, flags.cValue()) |
| 137 buffer := unsafeByteSlice(unsafe.Pointer(*cParams.buffer), int(*cParams.
num_bytes)) |
| 138 return MojoResult(result), buffer |
| 139 } |
| 140 |
| 141 func (h *dataPipeProducer) EndWriteData(numBytesWritten int) MojoResult { |
| 142 return MojoResult(C.MojoEndWriteData(h.mojoHandle.cValue(), C.uint32_t(n
umBytesWritten))) |
| 143 } |
OLD | NEW |