|
| 1 | +// Copyright 2013 The Flutter 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 zircon; |
| 6 | + |
| 7 | +@pragma('vm:entry-point') |
| 8 | +class ZDChannel { |
| 9 | + static ZDChannel? create([int options = 0]) { |
| 10 | + final Pointer<zircon_dart_handle_pair_t>? channelPtr = |
| 11 | + zirconFFIBindings?.zircon_dart_channel_create(options); |
| 12 | + if (channelPtr == null || channelPtr.address == 0) { |
| 13 | + throw Exception('Unable to create a channel'); |
| 14 | + } |
| 15 | + return ZDChannel._(ZDHandlePair._(channelPtr)); |
| 16 | + } |
| 17 | + |
| 18 | + static int _write(ZDHandle channel, ByteData data, List<ZDHandle> handles) { |
| 19 | + final Pointer<zircon_dart_handle_list_t> handleList = |
| 20 | + zirconFFIBindings!.zircon_dart_handle_list_create(); |
| 21 | + handles.forEach((ZDHandle handle) { |
| 22 | + zirconFFIBindings! |
| 23 | + .zircon_dart_handle_list_append(handleList, handle._ptr); |
| 24 | + }); |
| 25 | + |
| 26 | + final Uint8List dataAsBytes = data.buffer.asUint8List(); |
| 27 | + final Pointer<zircon_dart_byte_array_t> byteArray = |
| 28 | + zirconFFIBindings!.zircon_dart_byte_array_create(dataAsBytes.length); |
| 29 | + for (int i = 0; i < dataAsBytes.length; i++) { |
| 30 | + zirconFFIBindings!.zircon_dart_byte_array_set_value( |
| 31 | + byteArray, i, dataAsBytes.elementAt(i)); |
| 32 | + } |
| 33 | + int ret = zirconFFIBindings! |
| 34 | + .zircon_dart_channel_write(channel._ptr, byteArray, handleList); |
| 35 | + |
| 36 | + zirconFFIBindings!.zircon_dart_byte_array_free(byteArray); |
| 37 | + zirconFFIBindings!.zircon_dart_handle_list_free(handleList); |
| 38 | + return ret; |
| 39 | + } |
| 40 | + |
| 41 | + int writeLeft(ByteData data, List<ZDHandle> handles) { |
| 42 | + return _write(handlePair.left, data, handles); |
| 43 | + } |
| 44 | + |
| 45 | + int writeRight(ByteData data, List<ZDHandle> handles) { |
| 46 | + return _write(handlePair.right, data, handles); |
| 47 | + } |
| 48 | + |
| 49 | + @pragma('vm:entry-point') |
| 50 | + ZDChannel._(this.handlePair); |
| 51 | + |
| 52 | + final ZDHandlePair handlePair; |
| 53 | + |
| 54 | + @override |
| 55 | + String toString() => 'Channel(handlePair=$handlePair)'; |
| 56 | +} |
0 commit comments