0001    // TCPClientSocket.swift
0002    //
0003    // The MIT License (MIT)
0004    //
0005    // Copyright (c) 2015 Zewo
0006    //
0007    // Permission is hereby granted, free of charge, to any person obtaining a copy
0008    // of this software and associated documentation files (the "Software"), to deal
0009    // in the Software without restriction, including without limitation the rights
0010    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0011    // copies of the Software, and to permit persons to whom the Software is
0012    // furnished to do so, subject to the following conditions:
0013    //
0014    // The above copyright notice and this permission notice shall be included in all
0015    // copies or substantial portions of the Software.
0016    //
0017    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0018    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0019    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0020    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0021    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0022    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0023    // SOFTWARE.
0024    
0025    import CLibvenice
0026    @_exported import IP
0027    
0028    public final class TCPClientSocket
TCPClientSocket.swift:114
extension TCPClientSocket {
TCPServerSocket.swift:37
    public func accept(deadline: Deadline = never) throws -> TCPClientSocket {
TCPStream.swift:29
    private let socket: TCPClientSocket
TCPStream.swift:34
    public init(socket: TCPClientSocket, lowWaterMark: Int = 1, highWaterMark: Int = 4096) {
TCPStreamClient.swift:39
        let socket = try TCPClientSocket(ip: ip)
: TCPSocket { 0029 override init(socket: tcpsock) throws { 0030 try super.init(socket: socket) 0031 } 0032 0033 public init(ip: IP, deadline: Deadline = never) throws { 0034 try super.init(socket: tcpconnect(ip.address, deadline)) 0035 } 0036 0037 public init
TCPClientSocket.swift:33
    public init(ip: IP, deadline: Deadline = never) throws {
(fileDescriptor: FileDescriptor) throws { 0038 try super.init(socket: tcpattach(fileDescriptor, 0)) 0039 } 0040 0041 public var ip: IP { 0042 return try! IP(address: tcpaddr(socket)) 0043 } 0044 0045 public func send(data: Data, flush: Bool = true, deadline: Deadline = never) throws { 0046 try assertNotClosed() 0047 0048 let bytesProcessed = data.withUnsafeBufferPointer { 0049 tcpsend(socket, $0.baseAddress, $0.count, deadline) 0050 } 0051 0052 try TCPError.assertNoSendErrorWithData(data, bytesProcessed: bytesProcessed) 0053 0054 if flush { 0055 try self.flush() 0056 } 0057 } 0058 0059 public func flush(deadline: Deadline = never) throws { 0060 try assertNotClosed() 0061 tcpflush(socket, deadline) 0062 try TCPError.assertNoError() 0063 } 0064 0065 public func receive(length length: Int, deadline: Deadline = never) throws -> Data { 0066 try assertNotClosed() 0067 0068 var data = Data.bufferWithSize(length) 0069 let bytesProcessed = data.withUnsafeMutableBufferPointer { 0070 tcprecv(socket, $0.baseAddress, $0.count, deadline) 0071 } 0072 0073 try TCPError.assertNoReceiveErrorWithData(data, bytesProcessed: bytesProcessed) 0074 return data.prefix(bytesProcessed) 0075 } 0076 0077 public func receive(lowWaterMark lowWaterMark: Int, highWaterMark: Int, deadline: Deadline = never) throws -> Data { 0078 try assertNotClosed() 0079 0080 if lowWaterMark <= 0 || highWaterMark <= 0 { 0081 throw TCPError.Unknown(description: "Marks should be > 0") 0082 } 0083 0084 if lowWaterMark > highWaterMark { 0085 throw TCPError.Unknown(description: "loweWaterMark should be less than highWaterMark") 0086 } 0087 0088 var data = Data.bufferWithSize(highWaterMark) 0089 let bytesProcessed = data.withUnsafeMutableBufferPointer { 0090 tcprecvlh(socket, $0.baseAddress, lowWaterMark, highWaterMark, deadline) 0091 } 0092 0093 try TCPError.assertNoReceiveErrorWithData(data, bytesProcessed: bytesProcessed) 0094 return data.prefix(bytesProcessed) 0095 } 0096 0097 public func receive
TCPClientSocket.swift:65
    public func receive(length length: Int, deadline: Deadline = never) throws -> Data {
TCPClientSocket.swift:77
    public func receive(lowWaterMark lowWaterMark: Int, highWaterMark: Int, deadline: Deadline = never) throws -> Data {
(length length: Int, untilDelimiter delimiter: String, deadline: Deadline = never) throws -> Data { 0098 try assertNotClosed() 0099 0100 var data = Data.bufferWithSize(length) 0101 let bytesProcessed = data.withUnsafeMutableBufferPointer { 0102 tcprecvuntil(socket, $0.baseAddress, $0.count, delimiter, delimiter.utf8.count, deadline) 0103 } 0104 0105 try TCPError.assertNoReceiveErrorWithData(data, bytesProcessed: bytesProcessed) 0106 return data.prefix(bytesProcessed) 0107 } 0108 0109 public func attach(fileDescriptor: FileDescriptor) throws { 0110 try super.attach(fileDescriptor, isServer: false) 0111 } 0112 } 0113 0114 extension TCPClientSocket { 0115 public func send
TCPClientSocket.swift:45
    public func send(data: Data, flush: Bool = true, deadline: Deadline = never) throws {
(convertible: DataConvertible, deadline: Deadline = never) throws { 0116 try send(convertible.data, deadline: deadline) 0117 } 0118 0119 public func receiveString(length length: Int, deadline: Deadline = never) throws -> String { 0120 let result = try receive(length: length, deadline: deadline) 0121 return try String(data: result) 0122 } 0123 0124 public func receiveString
TCPClientSocket.swift:119
    public func receiveString(length length: Int, deadline: Deadline = never) throws -> String {
(length length: Int, untilDelimiter delimiter: String, deadline: Deadline = never) throws -> String { 0125 let result = try receive(length: length, untilDelimiter: delimiter, deadline: deadline) 0126 return try String(data: result) 0127 } 0128 }