1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 import Foundation 21 22 public protocol TTransport { 23 24 // Required readnull25 func read(size: Int) throws -> Data 26 func write(data: Data) throws 27 func flush() throws 28 29 // Optional (default provided) 30 func readAll(size: Int) throws -> Data 31 func isOpen() throws -> Bool 32 func open() throws 33 func close() throws 34 } 35 36 public extension TTransport { 37 func isOpen() throws -> Bool { return true } 38 func open() throws { } 39 func close() throws { } 40 41 func readAll(size: Int) throws -> Data { 42 var buff = Data() 43 var have = 0 44 while have < size { 45 let chunk = try self.read(size: size - have) 46 have += chunk.count 47 buff.append(chunk) 48 if chunk.count == 0 { 49 throw TTransportError(error: .endOfFile) 50 } 51 } 52 return buff 53 } 54 } 55 56 public protocol TAsyncTransport : TTransport { 57 // Factory 58 func flush(_ completion: @escaping (TAsyncTransport, Error?) ->()) 59 } 60 61 public protocol TAsyncTransportFactory { 62 associatedtype Transport : TAsyncTransport newTransportnull63 func newTransport() -> Transport 64 } 65