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" | |
8 //#include "mojo/public/c/system/core.h" | |
9 import "C" | |
10 import "unsafe" | |
11 | |
12 // ConsumerHandle is a handle for the consumer part of a data pipe. | 7 // ConsumerHandle is a handle for the consumer part of a data pipe. |
13 type ConsumerHandle interface { | 8 type ConsumerHandle interface { |
14 Handle | 9 Handle |
15 | 10 |
16 // ReadData reads data from the data pipe consumer handle with the | 11 // ReadData reads data from the data pipe consumer handle with the |
17 // given flags. On success, returns the data that was read. | 12 // given flags. On success, returns the data that was read. |
18 ReadData(flags MojoReadDataFlags) (MojoResult, []byte) | 13 ReadData(flags MojoReadDataFlags) (MojoResult, []byte) |
19 | 14 |
20 // BeginReadData begins a two-phase read from the data pipe consumer. | 15 // 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 | 16 // On success, returns a slice from which the caller can read up to its |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 // size. The slice returned from |BeginWriteData()| must have been | 70 // size. The slice returned from |BeginWriteData()| must have been |
76 // filled with exactly |numBytesWritten| bytes of data. | 71 // filled with exactly |numBytesWritten| bytes of data. |
77 // | 72 // |
78 // On failure, the two-phase write (if any) is ended (so the handle may | 73 // 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 | 74 // become writable again, if there's space available) but no data |
80 // written to the slice is "put into" the data pipe. | 75 // written to the slice is "put into" the data pipe. |
81 EndWriteData(numBytesWritten int) MojoResult | 76 EndWriteData(numBytesWritten int) MojoResult |
82 } | 77 } |
83 | 78 |
84 type dataPipeConsumer struct { | 79 type dataPipeConsumer struct { |
| 80 // baseHandle should always be the first component of this struct, |
| 81 // see |finalizeHandle()| for more details. |
85 baseHandle | 82 baseHandle |
86 } | 83 } |
87 | 84 |
88 func (h *dataPipeConsumer) ReadData(flags MojoReadDataFlags) (MojoResult, []byte
) { | 85 func (h *dataPipeConsumer) ReadData(flags MojoReadDataFlags) (MojoResult, []byte
) { |
89 h.core.mu.Lock() | 86 h.core.mu.Lock() |
90 » defer h.core.mu.Unlock() | 87 » r, buf := sysImpl.ReadData(uint32(h.mojoHandle), uint32(flags)) |
91 | 88 » h.core.mu.Unlock() |
92 » cParams := C.MallocReadDataParams() | 89 » return MojoResult(r), buf |
93 » defer C.FreeReadDataParams(cParams) | |
94 » *cParams.num_bytes = 0 | |
95 » if result := C.MojoReadData(h.mojoHandle.cValue(), nil, cParams.num_byte
s, C.MOJO_READ_DATA_FLAG_QUERY); result != C.MOJO_RESULT_OK { | |
96 » » return MojoResult(result), nil | |
97 » } | |
98 » dataArray := C.MallocDataArray(*cParams.num_bytes) | |
99 » defer C.FreeDataArray(dataArray) | |
100 » result := C.MojoReadData(h.mojoHandle.cValue(), dataArray.elements, cPar
ams.num_bytes, flags.cValue()) | |
101 » dataSize := int(*cParams.num_bytes) | |
102 » data := make([]byte, dataSize) | |
103 » cData := unsafeByteSlice(unsafe.Pointer(dataArray.elements), dataSize) | |
104 » copy(data, cData) | |
105 » return MojoResult(result), data | |
106 } | 90 } |
107 | 91 |
108 func (h *dataPipeConsumer) BeginReadData(numBytes int, flags MojoReadDataFlags)
(MojoResult, []byte) { | 92 func (h *dataPipeConsumer) BeginReadData(numBytes int, flags MojoReadDataFlags)
(MojoResult, []byte) { |
109 h.core.mu.Lock() | 93 h.core.mu.Lock() |
110 » defer h.core.mu.Unlock() | 94 » r, buf := sysImpl.BeginReadData(uint32(h.mojoHandle), uint32(numBytes),
uint32(flags)) |
111 | 95 » h.core.mu.Unlock() |
112 » cParams := C.MallocTwoPhaseActionParams() | 96 » return MojoResult(r), buf |
113 » defer C.FreeTwoPhaseActionParams(cParams) | |
114 » *cParams.num_bytes = C.uint32_t(numBytes) | |
115 » result := C.MojoBeginReadData(h.mojoHandle.cValue(), cParams.buffer, cPa
rams.num_bytes, flags.cValue()) | |
116 » buffer := unsafeByteSlice(unsafe.Pointer(*cParams.buffer), int(*cParams.
num_bytes)) | |
117 » return MojoResult(result), buffer | |
118 } | 97 } |
119 | 98 |
120 func (h *dataPipeConsumer) EndReadData(numBytesRead int) MojoResult { | 99 func (h *dataPipeConsumer) EndReadData(numBytesRead int) MojoResult { |
121 h.core.mu.Lock() | 100 h.core.mu.Lock() |
122 » defer h.core.mu.Unlock() | 101 » r := sysImpl.EndReadData(uint32(h.mojoHandle), uint32(numBytesRead)) |
123 | 102 » h.core.mu.Unlock() |
124 » return MojoResult(C.MojoEndReadData(h.mojoHandle.cValue(), C.uint32_t(nu
mBytesRead))) | 103 » return MojoResult(r) |
125 } | 104 } |
126 | 105 |
127 type dataPipeProducer struct { | 106 type dataPipeProducer struct { |
| 107 // baseHandle should always be the first component of this struct, |
| 108 // see |finalizeHandle()| for more details. |
128 baseHandle | 109 baseHandle |
129 } | 110 } |
130 | 111 |
131 func (h *dataPipeProducer) WriteData(data []byte, flags MojoWriteDataFlags) (Moj
oResult, int) { | 112 func (h *dataPipeProducer) WriteData(data []byte, flags MojoWriteDataFlags) (Moj
oResult, int) { |
132 h.core.mu.Lock() | 113 h.core.mu.Lock() |
133 » defer h.core.mu.Unlock() | 114 » r, bytesWritten := sysImpl.WriteData(uint32(h.mojoHandle), data, uint32(
flags)) |
134 | 115 » h.core.mu.Unlock() |
135 » cParams := C.MallocWriteDataParams(C.uint32_t(len(data))) | 116 » return MojoResult(r), int(bytesWritten) |
136 » defer C.FreeWriteDataParams(cParams) | |
137 » *cParams.num_bytes = C.uint32_t(len(data)) | |
138 » cArray := unsafeByteSlice(unsafe.Pointer(cParams.elements), len(data)) | |
139 » copy(cArray, data) | |
140 » result := C.MojoWriteData(h.mojoHandle.cValue(), cParams.elements, cPara
ms.num_bytes, flags.cValue()) | |
141 » return MojoResult(result), int(*cParams.num_bytes) | |
142 } | 117 } |
143 | 118 |
144 func (h *dataPipeProducer) BeginWriteData(numBytes int, flags MojoWriteDataFlags
) (MojoResult, []byte) { | 119 func (h *dataPipeProducer) BeginWriteData(numBytes int, flags MojoWriteDataFlags
) (MojoResult, []byte) { |
145 h.core.mu.Lock() | 120 h.core.mu.Lock() |
146 » defer h.core.mu.Unlock() | 121 » r, buf := sysImpl.BeginWriteData(uint32(h.mojoHandle), uint32(numBytes),
uint32(flags)) |
147 | 122 » h.core.mu.Unlock() |
148 » cParams := C.MallocTwoPhaseActionParams() | 123 » return MojoResult(r), buf |
149 » defer C.FreeTwoPhaseActionParams(cParams) | |
150 » *cParams.num_bytes = C.uint32_t(numBytes) | |
151 » result := C.MojoBeginWriteData(h.mojoHandle.cValue(), cParams.buffer, cP
arams.num_bytes, flags.cValue()) | |
152 » buffer := unsafeByteSlice(unsafe.Pointer(*cParams.buffer), int(*cParams.
num_bytes)) | |
153 » return MojoResult(result), buffer | |
154 } | 124 } |
155 | 125 |
156 func (h *dataPipeProducer) EndWriteData(numBytesWritten int) MojoResult { | 126 func (h *dataPipeProducer) EndWriteData(numBytesWritten int) MojoResult { |
157 h.core.mu.Lock() | 127 h.core.mu.Lock() |
158 » defer h.core.mu.Unlock() | 128 » r := sysImpl.EndWriteData(uint32(h.mojoHandle), uint32(numBytesWritten)) |
159 | 129 » h.core.mu.Unlock() |
160 » return MojoResult(C.MojoEndWriteData(h.mojoHandle.cValue(), C.uint32_t(n
umBytesWritten))) | 130 » return MojoResult(r) |
161 } | 131 } |
OLD | NEW |