| 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 core; | |
| 6 | |
| 7 class MojoDataPipeProducer { | |
| 8 static const int FLAG_NONE = 0; | |
| 9 static const int FLAG_ALL_OR_NONE = 1 << 0; | |
| 10 | |
| 11 MojoHandle handle; | |
| 12 MojoResult status; | |
| 13 final int elementBytes; | |
| 14 | |
| 15 MojoDataPipeProducer(this.handle, | |
| 16 [this.status = MojoResult.OK, this.elementBytes = 1]); | |
| 17 | |
| 18 int write(ByteData data, [int numBytes = -1, int flags = 0]) { | |
| 19 if (handle == null) { | |
| 20 status = MojoResult.INVALID_ARGUMENT; | |
| 21 return 0; | |
| 22 } | |
| 23 | |
| 24 int data_numBytes = (numBytes == -1) ? data.lengthInBytes : numBytes; | |
| 25 List result = | |
| 26 MojoDataPipeNatives.MojoWriteData(handle.h, data, data_numBytes, flags); | |
| 27 if (result == null) { | |
| 28 status = MojoResult.INVALID_ARGUMENT; | |
| 29 return 0; | |
| 30 } | |
| 31 | |
| 32 assert((result is List) && (result.length == 2)); | |
| 33 status = new MojoResult(result[0]); | |
| 34 return result[1]; | |
| 35 } | |
| 36 | |
| 37 ByteData beginWrite(int bufferBytes, [int flags = 0]) { | |
| 38 if (handle == null) { | |
| 39 status = MojoResult.INVALID_ARGUMENT; | |
| 40 return null; | |
| 41 } | |
| 42 | |
| 43 List result = | |
| 44 MojoDataPipeNatives.MojoBeginWriteData(handle.h, bufferBytes, flags); | |
| 45 if (result == null) { | |
| 46 status = MojoResult.INVALID_ARGUMENT; | |
| 47 return null; | |
| 48 } | |
| 49 | |
| 50 assert((result is List) && (result.length == 2)); | |
| 51 status = new MojoResult(result[0]); | |
| 52 return result[1]; | |
| 53 } | |
| 54 | |
| 55 MojoResult endWrite(int bytesWritten) { | |
| 56 if (handle == null) { | |
| 57 status = MojoResult.INVALID_ARGUMENT; | |
| 58 return status; | |
| 59 } | |
| 60 int result = MojoDataPipeNatives.MojoEndWriteData(handle.h, bytesWritten); | |
| 61 status = new MojoResult(result); | |
| 62 return status; | |
| 63 } | |
| 64 | |
| 65 String toString() => "MojoDataPipeProducer(handle: $handle, status: $status)"; | |
| 66 } | |
| 67 | |
| 68 class MojoDataPipeConsumer { | |
| 69 static const int FLAG_NONE = 0; | |
| 70 static const int FLAG_ALL_OR_NONE = 1 << 0; | |
| 71 static const int FLAG_DISCARD = 1 << 1; | |
| 72 static const int FLAG_QUERY = 1 << 2; | |
| 73 static const int FLAG_PEEK = 1 << 3; | |
| 74 | |
| 75 MojoHandle handle; | |
| 76 MojoResult status; | |
| 77 final int elementBytes; | |
| 78 | |
| 79 MojoDataPipeConsumer(this.handle, | |
| 80 [this.status = MojoResult.OK, this.elementBytes = 1]); | |
| 81 | |
| 82 int read(ByteData data, [int numBytes = -1, int flags = 0]) { | |
| 83 if (handle == null) { | |
| 84 status = MojoResult.INVALID_ARGUMENT; | |
| 85 return 0; | |
| 86 } | |
| 87 | |
| 88 int data_numBytes = (numBytes == -1) ? data.lengthInBytes : numBytes; | |
| 89 List result = | |
| 90 MojoDataPipeNatives.MojoReadData(handle.h, data, data_numBytes, flags); | |
| 91 if (result == null) { | |
| 92 status = MojoResult.INVALID_ARGUMENT; | |
| 93 return 0; | |
| 94 } | |
| 95 assert((result is List) && (result.length == 2)); | |
| 96 status = new MojoResult(result[0]); | |
| 97 return result[1]; | |
| 98 } | |
| 99 | |
| 100 ByteData beginRead([int bufferBytes = 0, int flags = 0]) { | |
| 101 if (handle == null) { | |
| 102 status = MojoResult.INVALID_ARGUMENT; | |
| 103 return null; | |
| 104 } | |
| 105 | |
| 106 List result = | |
| 107 MojoDataPipeNatives.MojoBeginReadData(handle.h, bufferBytes, flags); | |
| 108 if (result == null) { | |
| 109 status = MojoResult.INVALID_ARGUMENT; | |
| 110 return null; | |
| 111 } | |
| 112 | |
| 113 assert((result is List) && (result.length == 2)); | |
| 114 status = new MojoResult(result[0]); | |
| 115 return result[1]; | |
| 116 } | |
| 117 | |
| 118 MojoResult endRead(int bytesRead) { | |
| 119 if (handle == null) { | |
| 120 status = MojoResult.INVALID_ARGUMENT; | |
| 121 return status; | |
| 122 } | |
| 123 int result = MojoDataPipeNatives.MojoEndReadData(handle.h, bytesRead); | |
| 124 status = new MojoResult(result); | |
| 125 return status; | |
| 126 } | |
| 127 | |
| 128 int query() => read(null, 0, FLAG_QUERY); | |
| 129 | |
| 130 String toString() => "MojoDataPipeConsumer(" | |
| 131 "handle: $handle, status: $status, available: ${query()})"; | |
| 132 } | |
| 133 | |
| 134 class MojoDataPipe { | |
| 135 static const int FLAG_NONE = 0; | |
| 136 static const int DEFAULT_ELEMENT_SIZE = 1; | |
| 137 static const int DEFAULT_CAPACITY = 0; | |
| 138 | |
| 139 MojoDataPipeProducer producer; | |
| 140 MojoDataPipeConsumer consumer; | |
| 141 MojoResult status; | |
| 142 | |
| 143 MojoDataPipe._internal() { | |
| 144 producer = null; | |
| 145 consumer = null; | |
| 146 status = MojoResult.OK; | |
| 147 } | |
| 148 | |
| 149 factory MojoDataPipe([int elementBytes = DEFAULT_ELEMENT_SIZE, | |
| 150 int capacityBytes = DEFAULT_CAPACITY, int flags = FLAG_NONE]) { | |
| 151 List result = MojoDataPipeNatives.MojoCreateDataPipe( | |
| 152 elementBytes, capacityBytes, flags); | |
| 153 if (result == null) { | |
| 154 return null; | |
| 155 } | |
| 156 assert((result is List) && (result.length == 3)); | |
| 157 MojoHandle producerHandle = new MojoHandle(result[1]); | |
| 158 MojoHandle consumerHandle = new MojoHandle(result[2]); | |
| 159 MojoDataPipe pipe = new MojoDataPipe._internal(); | |
| 160 pipe.producer = new MojoDataPipeProducer( | |
| 161 producerHandle, new MojoResult(result[0]), elementBytes); | |
| 162 pipe.consumer = new MojoDataPipeConsumer( | |
| 163 consumerHandle, new MojoResult(result[0]), elementBytes); | |
| 164 pipe.status = new MojoResult(result[0]); | |
| 165 return pipe; | |
| 166 } | |
| 167 } | |
| OLD | NEW |