0001    // UDP.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    @_exported import Data
0028    
0029    public final class UDPSocket {
0030        private var socket
UDP.swift:34
        return Int(udpport(socket))
UDP.swift:38
        self.socket = socket
UDP.swift:51
        if !closed && socket != nil {
UDP.swift:52
            udpclose(socket)
UDP.swift:102
        udpclose(socket)
: udpsock 0031 public private(set) var closed
UDP.swift:51
        if !closed && socket != nil {
UDP.swift:84
        if !closed {
UDP.swift:90
        closed = false
UDP.swift:95
        closed = true
UDP.swift:101
        closed = true
UDP.swift:106
        if closed {
= false 0032 0033 public var port: Int { 0034 return Int(udpport(socket)) 0035 } 0036 0037 public init(socket: udpsock) throws { 0038 self.socket = socket 0039 try UDPError.assertNoError() 0040 } 0041 0042 public convenience init(ip: IP) throws { 0043 try self.init(socket: udplisten(ip.address)) 0044 } 0045 0046 public convenience init(fileDescriptor: Int32) throws { 0047 try self.init(socket: udpattach(fileDescriptor)) 0048 } 0049 0050 deinit { 0051 if !closed && socket != nil { 0052 udpclose(socket) 0053 } 0054 } 0055 0056 public func send(ip ip: IP, data: Data, deadline: Deadline = never) throws { 0057 try assertNotClosed() 0058 0059 data.withUnsafeBufferPointer { 0060 udpsend(socket, ip.address, $0.baseAddress, $0.count) 0061 } 0062 0063 try UDPError.assertNoError() 0064 } 0065 0066 public func receive(length length: Int, deadline: Deadline = never) throws -> (Data, IP) { 0067 try assertNotClosed() 0068 0069 var address = ipaddr() 0070 var data = Data.bufferWithSize(length) 0071 0072 let bytesProcessed = data.withUnsafeMutableBufferPointer { 0073 udprecv(socket, &address, $0.baseAddress, $0.count, deadline) 0074 } 0075 0076 try UDPError.assertNoReceiveErrorWithData(data, bytesProcessed: bytesProcessed) 0077 0078 let processedData = data.prefix(bytesProcessed) 0079 let ip = try IP(address: address) 0080 return (processedData, ip) 0081 } 0082 0083 public func attach(fileDescriptor: FileDescriptor) throws { 0084 if !closed { 0085 try close() 0086 } 0087 0088 socket = udpattach(fileDescriptor) 0089 try UDPError.assertNoError() 0090 closed = false 0091 } 0092 0093 public func detach() throws -> FileDescriptor { 0094 try assertNotClosed() 0095 closed = true 0096 return udpdetach(socket) 0097 } 0098 0099 public func close
UDP.swift:85
            try close()
() throws { 0100 try assertNotClosed() 0101 closed = true 0102 udpclose(socket) 0103 } 0104 0105 func assertNotClosed
UDP.swift:57
        try assertNotClosed()
UDP.swift:67
        try assertNotClosed()
UDP.swift:94
        try assertNotClosed()
UDP.swift:100
        try assertNotClosed()
() throws { 0106 if closed { 0107 throw UDPError.closedSocketError 0108 } 0109 } 0110 }