0001 // 0002 // BlueSocket.swift 0003 // BlueSocket 0004 // 0005 // Created by Bill Abt on 11/9/15. 0006 // Copyright © 2016 IBM. All rights reserved. 0007 // 0008 // Licensed under the Apache License, Version 2.0 (the "License"); 0009 // you may not use this file except in compliance with the License. 0010 // You may obtain a copy of the License at 0011 // 0012 // http://www.apache.org/licenses/LICENSE-2.0 0013 // 0014 // Unless required by applicable law or agreed to in writing, software 0015 // distributed under the License is distributed on an "AS IS" BASIS, 0016 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0017 // See the License for the specific language governing permissions and 0018 // limitations under the License. 0019 // 0020 0021 #if os(OSX) || os(iOS) || os(tvOS) || os(watchOS) 0022 import Darwin 0023 import Foundation 0024 #elseif os(Linux) 0025 import Foundation 0026 import Glibc 0027 #endif 0028 0029 // MARK: BlueSocketError 0030 0031 public class BlueSocketError: ErrorType, CustomStringConvertible { 0032 0033 /// 0034 /// The error code: **see BlueSocket for possible errors** 0035 /// 0036 public var errorCode
BlueSocket.swift:380 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_UNABLE_TO_CREATE_SOCKET, reason: self.lastError())BlueSocket.swift:432 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:437 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ALREADY_CONNECTED, reason: nil)BlueSocket.swift:442 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_LISTENING, reason: nil)BlueSocket.swift:453 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ACCEPT_FAILED, reason: self.lastError())BlueSocket.swift:469 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:474 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ALREADY_CONNECTED, reason: nil)BlueSocket.swift:479 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_LISTENING, reason: nil)BlueSocket.swift:490 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ACCEPT_FAILED, reason: self.lastError())BlueSocket.swift:548 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:553 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ALREADY_CONNECTED, reason: nil)BlueSocket.swift:558 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INVALID_HOSTNAME, reason: nil)BlueSocket.swift:566 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_GETHOSTBYNAME_FAILED, reason: self.lastError())BlueSocket.swift:581 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_CONNECT_FAILED, reason: self.lastError())BlueSocket.swift:599 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:604 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil)BlueSocket.swift:625 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_SELECT_FAILED, reason: self.lastError())BlueSocket.swift:655 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_SETSOCKOPT_FAILED, reason: self.lastError())BlueSocket.swift:671 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BIND_FAILED, reason: self.lastError())BlueSocket.swift:677 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_LISTEN_FAILED, reason: self.lastError())BlueSocket.swift:700 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INVALID_BUFFER, reason: nil)BlueSocket.swift:706 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:711 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil)BlueSocket.swift:719 throw BlueSocketError(bufferSize: self.readStorage.length)BlueSocket.swift:750 throw BlueSocketError(bufferSize: self.readStorage.length)BlueSocket.swift:774 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INTERNAL, reason: "Unable to create temporary NSData...")BlueSocket.swift:781 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INTERNAL, reason: "Unable to convert data to NSString.")BlueSocket.swift:805 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:810 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil)BlueSocket.swift:849 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INVALID_BUFFER, reason: nil)BlueSocket.swift:855 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:860 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil)BlueSocket.swift:869 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_WRITE_FAILED, reason: self.lastError())BlueSocket.swift:885 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:890 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil)BlueSocket.swift:905 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_WRITE_FAILED, reason: self.lastError())BlueSocket.swift:935 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_GET_FCNTL_FAILED, reason: self.lastError())BlueSocket.swift:950 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_SET_FCNTL_FAILED, reason: self.lastError())BlueSocket.swift:990 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_RECV_FAILED, reason: self.lastError()): Int32 0037 0038 /// 0039 /// The reason for the error **(if available)** 0040 /// 0041 public var errorReason
BlueSocket.swift:49 return "Error code: \(self.errorCode), Reason: \(reason)"BlueSocket.swift:51 return "Error code: \(self.errorCode), Reason: Unavailable"BlueSocket.swift:69 self.errorCode = Int32(code): String? 0042 0043 /// 0044 /// Returns a string description of the error. 0045 /// 0046 public var description: String { 0047 0048 if let reason = self.errorReason { 0049 return "Error code: \(self.errorCode), Reason: \(reason)" 0050 } 0051 return "Error code: \(self.errorCode), Reason: Unavailable" 0052 } 0053 0054 /// 0055 /// The buffer size needed to complete the read. 0056 /// 0057 public var bufferSizeNeeded
BlueSocket.swift:48 if let reason = self.errorReason {BlueSocket.swift:70 self.errorReason = reason: Int32 0058 0059 /// 0060 /// Initializes an BlueSocketError Instance 0061 /// 0062 /// - Parameter code: Error code 0063 /// - Parameter reason: Optional Error Reason 0064 /// 0065 /// - Returns: BlueSocketError instance 0066 /// 0067 init
BlueSocket.swift:71 self.bufferSizeNeeded = 0BlueSocket.swift:84 self.bufferSizeNeeded = Int32(bufferSize)(code: Int, reason: String?) { 0068 0069 self.errorCode = Int32(code) 0070 self.errorReason = reason 0071 self.bufferSizeNeeded = 0 0072 } 0073 0074 /// 0075 /// Initializes an BlueSocketError Instance for a too small receive buffer error. 0076 /// 0077 /// - Parameter bufferSize: Required buffer size 0078 /// 0079 /// - Returns: BlueSocketError Instance 0080 /// 0081 convenience init
BlueSocket.swift:83 self.init(code: BlueSocket.SOCKET_ERR_RECV_BUFFER_TOO_SMALL, reason: nil)BlueSocket.swift:380 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_UNABLE_TO_CREATE_SOCKET, reason: self.lastError())BlueSocket.swift:432 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:437 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ALREADY_CONNECTED, reason: nil)BlueSocket.swift:442 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_LISTENING, reason: nil)BlueSocket.swift:453 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ACCEPT_FAILED, reason: self.lastError())BlueSocket.swift:469 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:474 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ALREADY_CONNECTED, reason: nil)BlueSocket.swift:479 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_LISTENING, reason: nil)BlueSocket.swift:490 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ACCEPT_FAILED, reason: self.lastError())BlueSocket.swift:548 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:553 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ALREADY_CONNECTED, reason: nil)BlueSocket.swift:558 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INVALID_HOSTNAME, reason: nil)BlueSocket.swift:566 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_GETHOSTBYNAME_FAILED, reason: self.lastError())BlueSocket.swift:581 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_CONNECT_FAILED, reason: self.lastError())BlueSocket.swift:599 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:604 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil)BlueSocket.swift:625 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_SELECT_FAILED, reason: self.lastError())BlueSocket.swift:655 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_SETSOCKOPT_FAILED, reason: self.lastError())BlueSocket.swift:671 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BIND_FAILED, reason: self.lastError())BlueSocket.swift:677 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_LISTEN_FAILED, reason: self.lastError())BlueSocket.swift:700 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INVALID_BUFFER, reason: nil)BlueSocket.swift:706 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:711 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil)BlueSocket.swift:774 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INTERNAL, reason: "Unable to create temporary NSData...")BlueSocket.swift:781 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INTERNAL, reason: "Unable to convert data to NSString.")BlueSocket.swift:805 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:810 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil)BlueSocket.swift:849 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INVALID_BUFFER, reason: nil)BlueSocket.swift:855 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:860 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil)BlueSocket.swift:869 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_WRITE_FAILED, reason: self.lastError())BlueSocket.swift:885 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:890 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil)BlueSocket.swift:905 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_WRITE_FAILED, reason: self.lastError())BlueSocket.swift:935 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_GET_FCNTL_FAILED, reason: self.lastError())BlueSocket.swift:950 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_SET_FCNTL_FAILED, reason: self.lastError())BlueSocket.swift:990 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_RECV_FAILED, reason: self.lastError())(bufferSize: Int) { 0082 0083 self.init(code: BlueSocket.SOCKET_ERR_RECV_BUFFER_TOO_SMALL, reason: nil) 0084 self.bufferSizeNeeded = Int32(bufferSize) 0085 } 0086 } 0087 0088 public class BlueSocket
BlueSocket.swift:719 throw BlueSocketError(bufferSize: self.readStorage.length)BlueSocket.swift:750 throw BlueSocketError(bufferSize: self.readStorage.length): BlueSocketReader, BlueSocketWriter { 0089 0090 // MARK: Constants 0091 0092 public static let BlueSocket_DOMAIN = "BlueSocket.ErrorDomain" 0093 0094 public static let SOCKET_MINIMUM_READ_BUFFER_SIZE
BlueSocket.swift:83 self.init(code: BlueSocket.SOCKET_ERR_RECV_BUFFER_TOO_SMALL, reason: nil)BlueSocket.swift:211 var readBuffer: UnsafeMutablePointer<CChar> = UnsafeMutablePointer<CChar>.alloc(BlueSocket.SOCKET_DEFAULT_READ_BUFFER_SIZE)BlueSocket.swift:216 var readStorage: NSMutableData = NSMutableData(capacity: BlueSocket.SOCKET_DEFAULT_READ_BUFFER_SIZE)!BlueSocket.swift:234 if readBufferSize < BlueSocket.SOCKET_MINIMUM_READ_BUFFER_SIZE {BlueSocket.swift:236 readBufferSize = BlueSocket.SOCKET_MINIMUM_READ_BUFFER_SIZEBlueSocket.swift:228 public var readBufferSize: Int = BlueSocket.SOCKET_DEFAULT_READ_BUFFER_SIZE {BlueSocket.swift:256 public var maxPendingConnections: Int = BlueSocket.SOCKET_DEFAULT_MAX_CONNECTIONSBlueSocket.swift:276 public private(set) var remoteHostName: String = BlueSocket.NO_HOSTNAMEBlueSocket.swift:281 public private(set) var remotePort: Int = SOCKET_INVALID_PORTBlueSocket.swift:286 public private(set) var socketfd: Int32 = Int32(SOCKET_INVALID_DESCRIPTOR)BlueSocket.swift:297 public class func defaultConfigured() throws -> BlueSocket {BlueSocket.swift:299 return try BlueSocket(family: .INET, type: .STREAM, proto: .TCP)BlueSocket.swift:311 public class func customConfigured(family: BlueSocketProtocolFamily, type: BlueSocketType, proto: BlueSocketProtocol) throws -> BlueSocket {BlueSocket.swift:313 return try BlueSocket(family: family, type: type, proto: proto)BlueSocket.swift:336 public class func checkStatus(sockets: [BlueSocket]) throws -> (readables: [BlueSocket], writables: [BlueSocket]) {BlueSocket.swift:336 public class func checkStatus(sockets: [BlueSocket]) throws -> (readables: [BlueSocket], writables: [BlueSocket]) {BlueSocket.swift:336 public class func checkStatus(sockets: [BlueSocket]) throws -> (readables: [BlueSocket], writables: [BlueSocket]) {BlueSocket.swift:338 var readables: [BlueSocket] = []BlueSocket.swift:339 var writables: [BlueSocket] = []BlueSocket.swift:379 self.socketfd = Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR)BlueSocket.swift:380 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_UNABLE_TO_CREATE_SOCKET, reason: self.lastError())BlueSocket.swift:400 if let hostname = BlueSocket.dottedIP(remoteAddress.sin_addr) {BlueSocket.swift:427 public func acceptConnectionAndKeepListening() throws -> BlueSocket {BlueSocket.swift:430 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:432 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:437 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ALREADY_CONNECTED, reason: nil)BlueSocket.swift:442 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_LISTENING, reason: nil)BlueSocket.swift:453 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ACCEPT_FAILED, reason: self.lastError())BlueSocket.swift:458 return try BlueSocket(fd: socketfd2, remoteAddress: acceptAddr)BlueSocket.swift:467 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:469 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:474 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ALREADY_CONNECTED, reason: nil)BlueSocket.swift:479 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_LISTENING, reason: nil)BlueSocket.swift:490 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ACCEPT_FAILED, reason: self.lastError())BlueSocket.swift:499 if let hostname = BlueSocket.dottedIP(acceptAddr.sin_addr) {BlueSocket.swift:513 if self.socketfd != Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:529 self.socketfd = Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR)BlueSocket.swift:532 self.remoteHostName = BlueSocket.NO_HOSTNAMEBlueSocket.swift:546 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:548 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:553 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ALREADY_CONNECTED, reason: nil)BlueSocket.swift:558 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INVALID_HOSTNAME, reason: nil)BlueSocket.swift:566 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_GETHOSTBYNAME_FAILED, reason: self.lastError())BlueSocket.swift:581 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_CONNECT_FAILED, reason: self.lastError())BlueSocket.swift:597 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:599 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:604 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil)BlueSocket.swift:625 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_SELECT_FAILED, reason: self.lastError())BlueSocket.swift:655 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_SETSOCKOPT_FAILED, reason: self.lastError())BlueSocket.swift:661 localAddr.sin_addr.s_addr = BlueSocket.INADDR_ANYBlueSocket.swift:671 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BIND_FAILED, reason: self.lastError())BlueSocket.swift:677 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_LISTEN_FAILED, reason: self.lastError())BlueSocket.swift:700 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INVALID_BUFFER, reason: nil)BlueSocket.swift:704 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:706 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:711 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil)BlueSocket.swift:774 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INTERNAL, reason: "Unable to create temporary NSData...")BlueSocket.swift:781 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INTERNAL, reason: "Unable to convert data to NSString.")BlueSocket.swift:803 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:805 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:810 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil)BlueSocket.swift:849 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INVALID_BUFFER, reason: nil)BlueSocket.swift:853 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:855 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:860 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil)BlueSocket.swift:869 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_WRITE_FAILED, reason: self.lastError())BlueSocket.swift:883 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:885 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:890 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil)BlueSocket.swift:905 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_WRITE_FAILED, reason: self.lastError())BlueSocket.swift:935 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_GET_FCNTL_FAILED, reason: self.lastError())BlueSocket.swift:950 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_SET_FCNTL_FAILED, reason: self.lastError())BlueSocket.swift:990 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_RECV_FAILED, reason: self.lastError())= 1024 0095 public static let SOCKET_DEFAULT_READ_BUFFER_SIZE
BlueSocket.swift:234 if readBufferSize < BlueSocket.SOCKET_MINIMUM_READ_BUFFER_SIZE {BlueSocket.swift:236 readBufferSize = BlueSocket.SOCKET_MINIMUM_READ_BUFFER_SIZE= 4096 0096 public static let SOCKET_DEFAULT_MAX_CONNECTIONS
BlueSocket.swift:211 var readBuffer: UnsafeMutablePointer<CChar> = UnsafeMutablePointer<CChar>.alloc(BlueSocket.SOCKET_DEFAULT_READ_BUFFER_SIZE)BlueSocket.swift:216 var readStorage: NSMutableData = NSMutableData(capacity: BlueSocket.SOCKET_DEFAULT_READ_BUFFER_SIZE)!BlueSocket.swift:228 public var readBufferSize: Int = BlueSocket.SOCKET_DEFAULT_READ_BUFFER_SIZE {= 5 0097 0098 public static let SOCKET_INVALID_PORT
BlueSocket.swift:256 public var maxPendingConnections: Int = BlueSocket.SOCKET_DEFAULT_MAX_CONNECTIONS= 0 0099 public static let SOCKET_INVALID_DESCRIPTOR
BlueSocket.swift:281 public private(set) var remotePort: Int = SOCKET_INVALID_PORT= -1 0100 0101 public static let INADDR_ANY
BlueSocket.swift:286 public private(set) var socketfd: Int32 = Int32(SOCKET_INVALID_DESCRIPTOR)BlueSocket.swift:379 self.socketfd = Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR)BlueSocket.swift:430 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:467 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:513 if self.socketfd != Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:529 self.socketfd = Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR)BlueSocket.swift:546 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:597 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:704 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:803 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:853 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:883 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {= in_addr_t(0) 0102 0103 public static let NO_HOSTNAME
BlueSocket.swift:661 localAddr.sin_addr.s_addr = BlueSocket.INADDR_ANY= "No hostname" 0104 0105 // MARK: - Error Codes 0106 0107 public static let SOCKET_ERR_UNABLE_TO_CREATE_SOCKET
BlueSocket.swift:276 public private(set) var remoteHostName: String = BlueSocket.NO_HOSTNAMEBlueSocket.swift:532 self.remoteHostName = BlueSocket.NO_HOSTNAME= -9999 0108 public static let SOCKET_ERR_BAD_DESCRIPTOR
BlueSocket.swift:380 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_UNABLE_TO_CREATE_SOCKET, reason: self.lastError())= -9998 0109 public static let SOCKET_ERR_ALREADY_CONNECTED
BlueSocket.swift:432 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:469 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:548 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:599 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:706 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:805 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:855 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)BlueSocket.swift:885 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)= -9997 0110 public static let SOCKET_ERR_NOT_CONNECTED
BlueSocket.swift:437 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ALREADY_CONNECTED, reason: nil)BlueSocket.swift:474 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ALREADY_CONNECTED, reason: nil)BlueSocket.swift:553 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ALREADY_CONNECTED, reason: nil)= -9996 0111 public static let SOCKET_ERR_NOT_LISTENING
BlueSocket.swift:604 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil)BlueSocket.swift:711 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil)BlueSocket.swift:810 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil)BlueSocket.swift:860 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil)BlueSocket.swift:890 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil)= -9995 0112 public static let SOCKET_ERR_ACCEPT_FAILED
BlueSocket.swift:442 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_LISTENING, reason: nil)BlueSocket.swift:479 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_LISTENING, reason: nil)= -9994 0113 public static let SOCKET_ERR_SETSOCKOPT_FAILED
BlueSocket.swift:453 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ACCEPT_FAILED, reason: self.lastError())BlueSocket.swift:490 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ACCEPT_FAILED, reason: self.lastError())= -9993 0114 public static let SOCKET_ERR_BIND_FAILED
BlueSocket.swift:655 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_SETSOCKOPT_FAILED, reason: self.lastError())= -9992 0115 public static let SOCKET_ERR_INVALID_HOSTNAME
BlueSocket.swift:671 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BIND_FAILED, reason: self.lastError())= -9991 0116 public static let SOCKET_ERR_GETHOSTBYNAME_FAILED
BlueSocket.swift:558 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INVALID_HOSTNAME, reason: nil)= -9990 0117 public static let SOCKET_ERR_CONNECT_FAILED
BlueSocket.swift:566 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_GETHOSTBYNAME_FAILED, reason: self.lastError())= -9989 0118 public static let SOCKET_ERR_SELECT_FAILED
BlueSocket.swift:581 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_CONNECT_FAILED, reason: self.lastError())= -9988 0119 public static let SOCKET_ERR_LISTEN_FAILED
BlueSocket.swift:625 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_SELECT_FAILED, reason: self.lastError())= -9987 0120 public static let SOCKET_ERR_INVALID_BUFFER
BlueSocket.swift:677 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_LISTEN_FAILED, reason: self.lastError())= -9986 0121 public static let SOCKET_ERR_INVALID_BUFFER_SIZE = -9985 0122 public static let SOCKET_ERR_RECV_FAILED
BlueSocket.swift:700 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INVALID_BUFFER, reason: nil)BlueSocket.swift:849 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INVALID_BUFFER, reason: nil)= -9984 0123 public static let SOCKET_ERR_RECV_BUFFER_TOO_SMALL
BlueSocket.swift:990 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_RECV_FAILED, reason: self.lastError())= -9983 0124 public static let SOCKET_ERR_WRITE_FAILED
BlueSocket.swift:83 self.init(code: BlueSocket.SOCKET_ERR_RECV_BUFFER_TOO_SMALL, reason: nil)= -9982 0125 public static let SOCKET_ERR_GET_FCNTL_FAILED
BlueSocket.swift:869 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_WRITE_FAILED, reason: self.lastError())BlueSocket.swift:905 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_WRITE_FAILED, reason: self.lastError())= -9981 0126 public static let SOCKET_ERR_SET_FCNTL_FAILED
BlueSocket.swift:935 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_GET_FCNTL_FAILED, reason: self.lastError())= -9980 0127 public static let SOCKET_ERR_NOT_IMPLEMENTED = -9979 0128 public static let SOCKET_ERR_INTERNAL
BlueSocket.swift:950 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_SET_FCNTL_FAILED, reason: self.lastError())= -9978 0129 0130 // MARK: Enums 0131 0132 /// 0133 /// Socket Protocol Family Values 0134 /// 0135 public enum BlueSocketProtocolFamily
BlueSocket.swift:774 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INTERNAL, reason: "Unable to create temporary NSData...")BlueSocket.swift:781 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INTERNAL, reason: "Unable to convert data to NSString."){ 0136 0137 case INET
BlueSocket.swift:311 public class func customConfigured(family: BlueSocketProtocolFamily, type: BlueSocketType, proto: BlueSocketProtocol) throws -> BlueSocket {BlueSocket.swift:368 private init(family: BlueSocketProtocolFamily, type: BlueSocketType, proto: BlueSocketProtocol) throws {, INET6
BlueSocket.swift:146 case .INET:BlueSocket.swift:299 return try BlueSocket(family: .INET, type: .STREAM, proto: .TCP)0138 0139 /// 0140 /// Return the value for a particular case 0141 /// 0142 /// - Returns: Int32 containing the value for specific case. 0143 /// 0144 func valueOf
BlueSocket.swift:149 case .INET6:() -> Int32 { 0145 switch(self) { 0146 case .INET: 0147 return Int32(AF_INET) 0148 0149 case .INET6: 0150 return Int32(AF_INET6) 0151 } 0152 } 0153 } 0154 0155 /// 0156 /// Socket Type Values 0157 /// 0158 /// **Note:** Only `STREAM`, i.e. `SOCK_STREAM`, supported at this time. 0159 /// 0160 public enum BlueSocketType
BlueSocket.swift:374 self.socketfd = socket(family.valueOf(), type.valueOf(), proto.valueOf()){ 0161 0162 case STREAM
BlueSocket.swift:311 public class func customConfigured(family: BlueSocketProtocolFamily, type: BlueSocketType, proto: BlueSocketProtocol) throws -> BlueSocket {BlueSocket.swift:368 private init(family: BlueSocketProtocolFamily, type: BlueSocketType, proto: BlueSocketProtocol) throws {0163 0164 /// 0165 /// Return the value for a particular case 0166 /// 0167 /// - Returns: Int32 containing the value for specific case. 0168 /// 0169 func valueOf
BlueSocket.swift:171 case .STREAM:BlueSocket.swift:299 return try BlueSocket(family: .INET, type: .STREAM, proto: .TCP)() -> Int32 { 0170 switch(self) { 0171 case .STREAM: 0172 #if os(Linux) 0173 return Int32(SOCK_STREAM.rawValue) 0174 #else 0175 return SOCK_STREAM 0176 #endif 0177 } 0178 } 0179 } 0180 0181 /// 0182 /// Socket Protocol Values 0183 /// 0184 /// **Note:** Only `TCP`, i.e. `IPROTO_TCP`, supported at this time. 0185 /// 0186 public enum BlueSocketProtocol
BlueSocket.swift:374 self.socketfd = socket(family.valueOf(), type.valueOf(), proto.valueOf()){ 0187 case TCP
BlueSocket.swift:311 public class func customConfigured(family: BlueSocketProtocolFamily, type: BlueSocketType, proto: BlueSocketProtocol) throws -> BlueSocket {BlueSocket.swift:368 private init(family: BlueSocketProtocolFamily, type: BlueSocketType, proto: BlueSocketProtocol) throws {0188 0189 /// 0190 /// Return the value for a particular case 0191 /// 0192 /// - Returns: Int32 containing the value for specific case. 0193 /// 0194 func valueOf
BlueSocket.swift:196 case .TCP:BlueSocket.swift:299 return try BlueSocket(family: .INET, type: .STREAM, proto: .TCP)() -> Int32 { 0195 switch(self) { 0196 case .TCP: 0197 return Int32(IPPROTO_TCP) 0198 } 0199 } 0200 } 0201 0202 // MARK: Properties 0203 0204 // MARK: -- Private 0205 0206 /// 0207 /// Internal read buffer. 0208 /// **Note:** The readBuffer is actually allocating unmanaged memory that'll 0209 /// be deallocated when we're done with it. 0210 /// 0211 var readBuffer
BlueSocket.swift:374 self.socketfd = socket(family.valueOf(), type.valueOf(), proto.valueOf()): UnsafeMutablePointer<CChar> = UnsafeMutablePointer<CChar>.alloc(BlueSocket.SOCKET_DEFAULT_READ_BUFFER_SIZE) 0212 0213 /// 0214 /// Internal Storage Buffer initially created with `BlueSocket.SOCKET_DEFAULT_READ_BUFFER_SIZE`. 0215 /// 0216 var readStorage
BlueSocket.swift:242 if readBuffer != nil {BlueSocket.swift:243 readBuffer.destroy()BlueSocket.swift:244 readBuffer.dealloc(oldValue)BlueSocket.swift:246 readBuffer = UnsafeMutablePointer<CChar>.alloc(readBufferSize)BlueSocket.swift:247 readBuffer.initialize(0)BlueSocket.swift:371 self.readBuffer.initialize(0)BlueSocket.swift:398 self.readBuffer.initialize(0)BlueSocket.swift:416 self.readBuffer.destroy(0)BlueSocket.swift:417 self.readBuffer.dealloc(self.readBufferSize)BlueSocket.swift:966 if self.readBuffer != nil {BlueSocket.swift:968 self.readBuffer.destroy()BlueSocket.swift:969 self.readBuffer.initialize(0x0)BlueSocket.swift:970 memset(self.readBuffer, 0x0, self.readBufferSize)BlueSocket.swift:977 count = recv(self.socketfd, self.readBuffer, self.readBufferSize, 0)BlueSocket.swift:995 self.readStorage.appendBytes(self.readBuffer, length: count): NSMutableData = NSMutableData(capacity: BlueSocket.SOCKET_DEFAULT_READ_BUFFER_SIZE)! 0217 0218 // MARK: -- Public 0219 0220 /// 0221 /// Internal Read buffer size for all open sockets. 0222 /// **Note:** Changing this value will cause the internal read buffer to 0223 /// be discarded and reallocated with the new size. The value must be 0224 /// set to at least `BlueSocket.SOCKET_MINIMUM_READ_BUFFER_SIZE`. If set 0225 /// to something smaller, it will be automatically set to the minimum 0226 /// size as defined by `BlueSocket.SOCKET_MINIMUM_READ_BUFFER_SIZE`. 0227 /// 0228 public var readBufferSize
BlueSocket.swift:715 if self.readStorage.length > 0 {BlueSocket.swift:717 if bufSize < self.readStorage.length {BlueSocket.swift:719 throw BlueSocketError(bufferSize: self.readStorage.length)BlueSocket.swift:722 let returnCount = self.readStorage.lengthBlueSocket.swift:725 memcpy(buffer, self.readStorage.bytes, self.readStorage.length)BlueSocket.swift:725 memcpy(buffer, self.readStorage.bytes, self.readStorage.length)BlueSocket.swift:728 self.readStorage.length = 0BlueSocket.swift:744 if self.readStorage.length > 0 {BlueSocket.swift:747 if bufSize < self.readStorage.length {BlueSocket.swift:750 throw BlueSocketError(bufferSize: self.readStorage.length)BlueSocket.swift:754 memcpy(buffer, self.readStorage.bytes, self.readStorage.length)BlueSocket.swift:754 memcpy(buffer, self.readStorage.bytes, self.readStorage.length)BlueSocket.swift:756 returnCount = self.readStorage.lengthBlueSocket.swift:759 self.readStorage.length = 0BlueSocket.swift:827 data.appendData(self.readStorage)BlueSocket.swift:829 returnCount = self.readStorage.lengthBlueSocket.swift:832 self.readStorage.length = 0BlueSocket.swift:995 self.readStorage.appendBytes(self.readBuffer, length: count)BlueSocket.swift:1006 return self.readStorage.length: Int = BlueSocket.SOCKET_DEFAULT_READ_BUFFER_SIZE { 0229 0230 // If the buffer size changes we need to reallocate the buffer... 0231 didSet { 0232 0233 // Ensure minimum buffer size... 0234 if readBufferSize < BlueSocket.SOCKET_MINIMUM_READ_BUFFER_SIZE { 0235 0236 readBufferSize = BlueSocket.SOCKET_MINIMUM_READ_BUFFER_SIZE 0237 } 0238 0239 print("Creating read buffer of size: \(readBufferSize)") 0240 if readBufferSize != oldValue { 0241 0242 if readBuffer != nil { 0243 readBuffer.destroy() 0244 readBuffer.dealloc(oldValue) 0245 } 0246 readBuffer = UnsafeMutablePointer<CChar>.alloc(readBufferSize) 0247 readBuffer.initialize(0) 0248 } 0249 } 0250 } 0251 0252 /// 0253 /// Maximum number of pending connections per listening socket. 0254 /// **Note:** Default value is `BlueSocket.SOCKET_DEFAULT_MAX_CONNECTIONS` 0255 /// 0256 public var maxPendingConnections
BlueSocket.swift:234 if readBufferSize < BlueSocket.SOCKET_MINIMUM_READ_BUFFER_SIZE {BlueSocket.swift:236 readBufferSize = BlueSocket.SOCKET_MINIMUM_READ_BUFFER_SIZEBlueSocket.swift:239 print("Creating read buffer of size: \(readBufferSize)")BlueSocket.swift:240 if readBufferSize != oldValue {BlueSocket.swift:246 readBuffer = UnsafeMutablePointer<CChar>.alloc(readBufferSize)BlueSocket.swift:417 self.readBuffer.dealloc(self.readBufferSize)BlueSocket.swift:970 memset(self.readBuffer, 0x0, self.readBufferSize)BlueSocket.swift:977 count = recv(self.socketfd, self.readBuffer, self.readBufferSize, 0)BlueSocket.swift:999 if count < self.readBufferSize {: Int = BlueSocket.SOCKET_DEFAULT_MAX_CONNECTIONS 0257 0258 /// 0259 /// True if this socket is connected. False otherwise. (Readonly) 0260 /// 0261 public private(set) var connected
BlueSocket.swift:639 return try self.listenOn(port, maxPendingConnections: self.maxPendingConnections): Bool = false 0262 0263 /// 0264 /// True if this socket is blocking. False otherwise. (Readonly) 0265 /// 0266 public private(set) var isBlocking
BlueSocket.swift:396 self.connected = trueBlueSocket.swift:435 if self.connected {BlueSocket.swift:472 if self.connected {BlueSocket.swift:504 self.connected = trueBlueSocket.swift:533 self.connected = falseBlueSocket.swift:551 if self.connected {BlueSocket.swift:586 self.connected = trueBlueSocket.swift:602 if !self.connected {BlueSocket.swift:709 if !self.connected {BlueSocket.swift:808 if !self.connected {BlueSocket.swift:858 if !self.connected {BlueSocket.swift:888 if !self.connected {: Bool = true 0267 0268 /// 0269 /// True if this socket is listening. False otherwise. (Readonly) 0270 /// 0271 public private(set) var listening
BlueSocket.swift:953 self.isBlocking = shouldBlock: Bool = false 0272 0273 /// 0274 /// The remote host name this socket is connected to. (Readonly) 0275 /// 0276 public private(set) var remoteHostName
BlueSocket.swift:397 self.listening = falseBlueSocket.swift:440 if !self.listening {BlueSocket.swift:477 if !self.listening {BlueSocket.swift:505 self.listening = falseBlueSocket.swift:523 if self.listening {BlueSocket.swift:534 self.listening = falseBlueSocket.swift:680 self.listening = true: String = BlueSocket.NO_HOSTNAME 0277 0278 /// 0279 /// The remote port this socket is connected to. (Readonly) 0280 /// 0281 public private(set) var remotePort
BlueSocket.swift:401 self.remoteHostName = hostnameBlueSocket.swift:500 self.remoteHostName = hostnameBlueSocket.swift:532 self.remoteHostName = BlueSocket.NO_HOSTNAMEBlueSocket.swift:562 self.remoteHostName = hostBlueSocket.swift:563 let remoteHost: UnsafeMutablePointer<hostent> = gethostbyname(self.remoteHostName)BlueSocket.swift:584 self.remoteHostName = host: Int = SOCKET_INVALID_PORT 0282 0283 /// 0284 /// The file descriptor representing this socket. (Readonly) 0285 /// 0286 public private(set) var socketfd
BlueSocket.swift:404 self.remotePort = Int(remoteAddress.sin_port)BlueSocket.swift:498 self.remotePort = Int(acceptAddr.sin_port)BlueSocket.swift:585 self.remotePort = Int(port): Int32 = Int32(SOCKET_INVALID_DESCRIPTOR) 0287 0288 0289 // MARK: Class Methods 0290 0291 /// 0292 /// Creates a default pre-configured BlueSocket instance. 0293 /// Default socket created with family: .INET, type: .STREAM, proto: .TCP 0294 /// 0295 /// - Returns: New BlueSocket instance 0296 /// 0297 public class func defaultConfigured() throws -> BlueSocket { 0298 0299 return try BlueSocket(family: .INET, type: .STREAM, proto: .TCP) 0300 } 0301 0302 /// 0303 /// Create a configured BlueSocket instance. 0304 /// 0305 /// - Parameter family: The family of the socket to create. 0306 /// - Parameter type: The type of socket to create. 0307 /// - Parameter proto: The protocool to use for the socket. 0308 /// 0309 /// - Returns: New BlueSocket instance 0310 /// 0311 public class func customConfigured(family: BlueSocketProtocolFamily, type: BlueSocketType, proto: BlueSocketProtocol) throws -> BlueSocket { 0312 0313 return try BlueSocket(family: family, type: type, proto: proto) 0314 } 0315 0316 /// 0317 /// Extract the dotted IP address from an in_addr struct. 0318 /// 0319 /// - Parameter fromAddress: The in_addr struct. 0320 /// 0321 /// - Returns: Optional String containing the dotted IP address or nil if not available. 0322 /// 0323 public class func dottedIP
BlueSocket.swift:374 self.socketfd = socket(family.valueOf(), type.valueOf(), proto.valueOf())BlueSocket.swift:377 if self.socketfd < 0 {BlueSocket.swift:379 self.socketfd = Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR)BlueSocket.swift:405 self.socketfd = fdBlueSocket.swift:410 if self.socketfd > 0 {BlueSocket.swift:430 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:449 accept(self.socketfd, UnsafeMutablePointer($0), &addrSize)BlueSocket.swift:467 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:486 accept(self.socketfd, UnsafeMutablePointer($0), &addrSize)BlueSocket.swift:497 self.socketfd = socketfd2BlueSocket.swift:513 if self.socketfd != Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:524 Darwin.shutdown(self.socketfd, Int32(SHUT_RDWR))BlueSocket.swift:526 Darwin.close(self.socketfd)BlueSocket.swift:529 self.socketfd = Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR)BlueSocket.swift:546 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:577 connect(self.socketfd, UnsafeMutablePointer($0), socklen_t(sizeof(sockaddr_in)))BlueSocket.swift:597 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:610 fdSet(self.socketfd, set: &readfds)BlueSocket.swift:614 fdSet(self.socketfd, set: &writefds)BlueSocket.swift:620 let count = select(self.socketfd + 1, &readfds, &writefds, nil, &timeout)BlueSocket.swift:629 return (fdIsSet(self.socketfd, set: &readfds), fdIsSet(self.socketfd, set: &writefds))BlueSocket.swift:629 return (fdIsSet(self.socketfd, set: &readfds), fdIsSet(self.socketfd, set: &writefds))BlueSocket.swift:653 if setsockopt(self.socketfd, SOL_SOCKET, SO_REUSEADDR, &on, socklen_t(sizeof(Int32))) < 0 {BlueSocket.swift:669 if bind(self.socketfd, &bindAddr, addrSize) < 0 {BlueSocket.swift:675 if listen(self.socketfd, Int32(maxPendingConnections)) < 0 {BlueSocket.swift:704 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:803 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:853 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:866 let s = write(self.socketfd, buffer + sent, Int(bufSize - sent))BlueSocket.swift:883 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) {BlueSocket.swift:902 let s = write(self.socketfd, buffer + sent, Int(data.length - sent))BlueSocket.swift:932 let flags = fcntl(self.socketfd, F_GETFL)BlueSocket.swift:941 result = fcntl(self.socketfd, F_SETFL, flags & ~O_NONBLOCK)BlueSocket.swift:945 result = fcntl(self.socketfd, F_SETFL, flags | O_NONBLOCK)BlueSocket.swift:977 count = recv(self.socketfd, self.readBuffer, self.readBufferSize, 0)(fromAddress: in_addr) -> String? { 0324 0325 let cString = inet_ntoa(fromAddress) 0326 return String.fromCString(cString) 0327 } 0328 0329 /// 0330 /// Check whether one or more sockets are available for reading and/or writing 0331 /// 0332 /// - Parameter sockets: Array of BlueSockets to be tested. 0333 /// 0334 /// - Returns: Tuple containing two arrays of BlueSockets, one each representing readable and writable sockets. 0335 /// 0336 public class func checkStatus(sockets: [BlueSocket]) throws -> (readables: [BlueSocket], writables: [BlueSocket]) { 0337 0338 var readables: [BlueSocket] = [] 0339 var writables: [BlueSocket] = [] 0340 0341 for socket in sockets { 0342 0343 let result = try socket.isReadableOrWritable() 0344 if result.readable { 0345 readables.append(socket) 0346 } 0347 if result.writable { 0348 writables.append(socket) 0349 } 0350 } 0351 0352 return (readables, writables) 0353 } 0354 0355 // MARK: Lifecycle Methods 0356 0357 // MARK: -- Public 0358 0359 /// 0360 /// Internal initializer to create a configured BlueSocket instance. 0361 /// 0362 /// - Parameter family: The family of the socket to create. 0363 /// - Parameter type: The type of socket to create. 0364 /// - Parameter proto: The protocool to use for the socket. 0365 /// 0366 /// - Returns: New BlueSocket instance 0367 /// 0368 private init
BlueSocket.swift:400 if let hostname = BlueSocket.dottedIP(remoteAddress.sin_addr) {BlueSocket.swift:499 if let hostname = BlueSocket.dottedIP(acceptAddr.sin_addr) {(family: BlueSocketProtocolFamily, type: BlueSocketType, proto: BlueSocketProtocol) throws { 0369 0370 // Initialize the read buffer... 0371 self.readBuffer.initialize(0) 0372 0373 // Create the socket... 0374 self.socketfd = socket(family.valueOf(), type.valueOf(), proto.valueOf()) 0375 0376 // If error, throw an appropriate exception... 0377 if self.socketfd < 0 { 0378 0379 self.socketfd = Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) 0380 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_UNABLE_TO_CREATE_SOCKET, reason: self.lastError()) 0381 } 0382 } 0383 0384 // MARK: -- Private 0385 0386 /// 0387 /// Private constructor to create an instance for existing open socket fd. 0388 /// 0389 /// - Parameter fd: Open file descriptor. 0390 /// - Parameter remoteAddress: The sockaddr_in associated with the open fd. 0391 /// 0392 /// - Returns: New BlueSocket instance 0393 /// 0394 private init
BlueSocket.swift:299 return try BlueSocket(family: .INET, type: .STREAM, proto: .TCP)BlueSocket.swift:313 return try BlueSocket(family: family, type: type, proto: proto)(fd: Int32, remoteAddress: sockaddr_in) throws { 0395 0396 self.connected = true 0397 self.listening = false 0398 self.readBuffer.initialize(0) 0399 0400 if let hostname = BlueSocket.dottedIP(remoteAddress.sin_addr) { 0401 self.remoteHostName = hostname 0402 } 0403 0404 self.remotePort = Int(remoteAddress.sin_port) 0405 self.socketfd = fd 0406 } 0407 0408 deinit { 0409 0410 if self.socketfd > 0 { 0411 0412 self.close() 0413 } 0414 0415 // Destroy and free the readBuffer... 0416 self.readBuffer.destroy(0) 0417 self.readBuffer.dealloc(self.readBufferSize) 0418 } 0419 0420 // MARK: Public Methods 0421 0422 /// 0423 /// Accepts an incoming connection request on the current instance, leaving the current instance still listening. 0424 /// 0425 /// - returns: New BlueSocket instance representing the newly accepted socket. 0426 /// 0427 public func acceptConnectionAndKeepListening() throws -> BlueSocket { 0428 0429 // The socket must've been created, not connected and listening... 0430 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) { 0431 0432 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil) 0433 } 0434 0435 if self.connected { 0436 0437 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ALREADY_CONNECTED, reason: nil) 0438 } 0439 0440 if !self.listening { 0441 0442 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_LISTENING, reason: nil) 0443 } 0444 0445 // Accept the remote connection... 0446 var acceptAddr = sockaddr_in() 0447 var addrSize: socklen_t = socklen_t(sizeof(sockaddr_in)) 0448 let socketfd2 = withUnsafeMutablePointer(&acceptAddr) { 0449 accept(self.socketfd, UnsafeMutablePointer($0), &addrSize) 0450 } 0451 if socketfd2 < 0 { 0452 0453 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ACCEPT_FAILED, reason: self.lastError()) 0454 } 0455 0456 // Create and return the new socket... 0457 // Note: The current socket continues to listen. 0458 return try BlueSocket(fd: socketfd2, remoteAddress: acceptAddr) 0459 } 0460 0461 /// 0462 /// Accepts an incoming connection request replacing the existing socket with the newly accepted one. 0463 /// 0464 public func acceptConnection() throws { 0465 0466 // The socket must've been created, not connected and listening... 0467 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) { 0468 0469 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil) 0470 } 0471 0472 if self.connected { 0473 0474 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ALREADY_CONNECTED, reason: nil) 0475 } 0476 0477 if !self.listening { 0478 0479 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_LISTENING, reason: nil) 0480 } 0481 0482 // Accept the remote connection... 0483 var acceptAddr = sockaddr_in() 0484 var addrSize: socklen_t = socklen_t(sizeof(sockaddr_in)) 0485 let socketfd2 = withUnsafeMutablePointer(&acceptAddr) { 0486 accept(self.socketfd, UnsafeMutablePointer($0), &addrSize) 0487 } 0488 if socketfd2 < 0 { 0489 0490 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ACCEPT_FAILED, reason: self.lastError()) 0491 } 0492 0493 // Close the old socket... 0494 self.close() 0495 0496 // Replace the existing socketfd with the new one... 0497 self.socketfd = socketfd2 0498 self.remotePort = Int(acceptAddr.sin_port) 0499 if let hostname = BlueSocket.dottedIP(acceptAddr.sin_addr) { 0500 self.remoteHostName = hostname 0501 } 0502 0503 // We're connected... 0504 self.connected = true 0505 self.listening = false 0506 } 0507 0508 /// 0509 /// Closes the current socket. 0510 /// 0511 public func close
BlueSocket.swift:458 return try BlueSocket(fd: socketfd2, remoteAddress: acceptAddr)() { 0512 0513 if self.socketfd != Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) { 0514 0515 // Note: if the socket is listening, we need to shut it down prior to closing 0516 // or the socket will be left hanging until it times out. 0517 #if os(Linux) 0518 if self.listening { 0519 Glibc.shutdown(self.socketfd, Int32(SHUT_RDWR)) 0520 } 0521 Glibc.close(self.socketfd) 0522 #else 0523 if self.listening { 0524 Darwin.shutdown(self.socketfd, Int32(SHUT_RDWR)) 0525 } 0526 Darwin.close(self.socketfd) 0527 #endif 0528 0529 self.socketfd = Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) 0530 } 0531 0532 self.remoteHostName = BlueSocket.NO_HOSTNAME 0533 self.connected = false 0534 self.listening = false 0535 } 0536 0537 /// 0538 /// Connects to the named host on the specified port. 0539 /// 0540 /// - Parameter host: The host name to connect to. 0541 /// - Parameter port: The port to be used. 0542 /// 0543 public func connectTo(host: String, port: Int32) throws { 0544 0545 // The socket must've been created and must not be connected... 0546 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) { 0547 0548 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil) 0549 } 0550 0551 if self.connected { 0552 0553 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ALREADY_CONNECTED, reason: nil) 0554 } 0555 0556 if host.utf8.count == 0 { 0557 0558 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INVALID_HOSTNAME, reason: nil) 0559 } 0560 0561 // Look up the host... 0562 self.remoteHostName = host 0563 let remoteHost: UnsafeMutablePointer<hostent> = gethostbyname(self.remoteHostName) 0564 if remoteHost == nil { 0565 0566 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_GETHOSTBYNAME_FAILED, reason: self.lastError()) 0567 } 0568 0569 // Copy the info into the socket address structure... 0570 var remoteAddr = sockaddr_in() 0571 remoteAddr.sin_family = sa_family_t(AF_INET) 0572 bcopy(remoteHost.memory.h_addr_list[0], &remoteAddr.sin_addr.s_addr, Int(remoteHost.memory.h_length)) 0573 remoteAddr.sin_port = UInt16(port).bigEndian 0574 0575 // Now, do the connection... 0576 let rc = withUnsafeMutablePointer(&remoteAddr) { 0577 connect(self.socketfd, UnsafeMutablePointer($0), socklen_t(sizeof(sockaddr_in))) 0578 } 0579 if rc < 0 { 0580 0581 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_CONNECT_FAILED, reason: self.lastError()) 0582 } 0583 0584 self.remoteHostName = host 0585 self.remotePort = Int(port) 0586 self.connected = true 0587 } 0588 0589 /// 0590 /// Determines if this socket can be read from or written to. 0591 /// 0592 /// - Returns: Tuple containing two boolean values, one for readable and one for writable. 0593 /// 0594 public func isReadableOrWritable
BlueSocket.swift:412 self.close()BlueSocket.swift:494 self.close()() throws -> (readable: Bool, writable: Bool) { 0595 0596 // The socket must've been created and must be connected... 0597 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) { 0598 0599 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil) 0600 } 0601 0602 if !self.connected { 0603 0604 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil) 0605 } 0606 0607 // Create a read and write file descriptor set for this socket... 0608 var readfds = fd_set() 0609 fdZero(&readfds) 0610 fdSet(self.socketfd, set: &readfds) 0611 0612 var writefds = fd_set() 0613 fdZero(&writefds) 0614 fdSet(self.socketfd, set: &writefds) 0615 0616 // Create a timeout of zero (i.e. don't wait)... 0617 var timeout = timeval() 0618 0619 // See if there's data on the socket... 0620 let count = select(self.socketfd + 1, &readfds, &writefds, nil, &timeout) 0621 0622 // A count of less than zero indicates select failed... 0623 if count < 0 { 0624 0625 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_SELECT_FAILED, reason: self.lastError()) 0626 } 0627 0628 // Return a tuple containing whether or not this socket is readable and/or writable... 0629 return (fdIsSet(self.socketfd, set: &readfds), fdIsSet(self.socketfd, set: &writefds)) 0630 } 0631 0632 /// 0633 /// Listen on a port using the default for max pending connections. 0634 /// 0635 /// - Parameter port: The port to listen on. 0636 /// 0637 public func listenOn(port: Int) throws { 0638 0639 return try self.listenOn(port, maxPendingConnections: self.maxPendingConnections) 0640 } 0641 0642 /// 0643 /// Listen on a port, limiting the maximum number of pending connections. 0644 /// 0645 /// - Parameter port: The port to listen on. 0646 /// - Parameter maxPendingConnections: The maximum number of pending connections to allow. 0647 /// 0648 public func listenOn
BlueSocket.swift:343 let result = try socket.isReadableOrWritable()(port: Int, maxPendingConnections: Int) throws { 0649 0650 // Set a flag so that this address can be re-used immediately after the connection 0651 // closes. (TCP normally imposes a delay before an address can be re-used.) 0652 var on: Int32 = 1 0653 if setsockopt(self.socketfd, SOL_SOCKET, SO_REUSEADDR, &on, socklen_t(sizeof(Int32))) < 0 { 0654 0655 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_SETSOCKOPT_FAILED, reason: self.lastError()) 0656 } 0657 0658 // Bind the address to the socket.... 0659 var localAddr = sockaddr_in() 0660 localAddr.sin_family = sa_family_t(AF_INET) 0661 localAddr.sin_addr.s_addr = BlueSocket.INADDR_ANY 0662 localAddr.sin_port = in_port_t(UInt16(bigEndian: UInt16(port))) 0663 0664 var bindAddr = sockaddr() 0665 memcpy(&bindAddr, &localAddr, Int(sizeof(sockaddr_in))) 0666 0667 let addrSize: socklen_t = socklen_t(sizeof(sockaddr_in)) 0668 0669 if bind(self.socketfd, &bindAddr, addrSize) < 0 { 0670 0671 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BIND_FAILED, reason: self.lastError()) 0672 } 0673 0674 // Now listen for connections... 0675 if listen(self.socketfd, Int32(maxPendingConnections)) < 0 { 0676 0677 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_LISTEN_FAILED, reason: self.lastError()) 0678 } 0679 0680 self.listening = true 0681 } 0682 0683 /// 0684 /// Read data from the socket. 0685 /// 0686 /// - Parameter buffer: The buffer to return the data in. 0687 /// - Parameter bufSize: The size of the buffer. 0688 /// 0689 /// - Throws: `BlueSocket.SOCKET_ERR_RECV_BUFFER_TOO_SMALL` if the buffer provided is too small. 0690 /// Call again with proper buffer size (see `BlueSocketError.bufferSizeNeeded`) or 0691 /// use `readData(data: NSMutableData)`. 0692 /// 0693 /// - Returns: The number of bytes returned in the buffer. 0694 /// 0695 public func readData(buffer: UnsafeMutablePointer<CChar>, bufSize: Int) throws -> Int { 0696 0697 // Make sure the buffer is valid... 0698 if buffer == nil || bufSize == 0 { 0699 0700 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INVALID_BUFFER, reason: nil) 0701 } 0702 0703 // The socket must've been created and must be connected... 0704 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) { 0705 0706 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil) 0707 } 0708 0709 if !self.connected { 0710 0711 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil) 0712 } 0713 0714 // See if we have cached data to send back... 0715 if self.readStorage.length > 0 { 0716 0717 if bufSize < self.readStorage.length { 0718 0719 throw BlueSocketError(bufferSize: self.readStorage.length) 0720 } 0721 0722 let returnCount = self.readStorage.length 0723 0724 // - We've got data we've already read, copy to the caller's buffer... 0725 memcpy(buffer, self.readStorage.bytes, self.readStorage.length) 0726 0727 // - Reset the storage buffer... 0728 self.readStorage.length = 0 0729 0730 return returnCount 0731 } 0732 0733 // Read all available bytes... 0734 let count = try self.readDataIntoStorage() 0735 0736 // Check for disconnect... 0737 if count == 0 { 0738 0739 return count 0740 } 0741 0742 // Did we get data? 0743 var returnCount: Int = 0 0744 if self.readStorage.length > 0 { 0745 0746 // Is the caller's buffer big enough? 0747 if bufSize < self.readStorage.length { 0748 0749 // Nope, throw an exception telling the caller how big the buffer must be... 0750 throw BlueSocketError(bufferSize: self.readStorage.length) 0751 } 0752 0753 // - We've read data, copy to the callers buffer... 0754 memcpy(buffer, self.readStorage.bytes, self.readStorage.length) 0755 0756 returnCount = self.readStorage.length 0757 0758 // - Reset the storage buffer... 0759 self.readStorage.length = 0 0760 } 0761 0762 return returnCount 0763 } 0764 0765 /// 0766 /// Read a string from the socket 0767 /// 0768 /// - Returns: String containing the data read from the socket. 0769 /// 0770 public func readString() throws -> String? { 0771 0772 guard let data = NSMutableData(capacity: 2000) else { 0773 0774 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INTERNAL, reason: "Unable to create temporary NSData...") 0775 } 0776 0777 try self.readData(data) 0778 0779 guard let str = NSString(data: data, encoding: NSUTF8StringEncoding) else { 0780 0781 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INTERNAL, reason: "Unable to convert data to NSString.") 0782 } 0783 0784 #if os(Linux) 0785 return str.bridge() 0786 #else 0787 return str as String 0788 #endif 0789 0790 } 0791 0792 0793 /// 0794 /// Read data from the socket. 0795 /// 0796 /// - Parameter data: The buffer to return the data in. 0797 /// 0798 /// - Returns: The number of bytes returned in the buffer. 0799 /// 0800 public func readData
BlueSocket.swift:639 return try self.listenOn(port, maxPendingConnections: self.maxPendingConnections)(data: NSMutableData) throws -> Int { 0801 0802 // The socket must've been created and must be connected... 0803 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) { 0804 0805 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil) 0806 } 0807 0808 if !self.connected { 0809 0810 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil) 0811 } 0812 0813 // Read all available bytes... 0814 let count = try self.readDataIntoStorage() 0815 0816 // Check for disconnect... 0817 if count == 0 { 0818 0819 return count 0820 } 0821 0822 // Did we get data? 0823 var returnCount: Int = 0 0824 if count > 0 { 0825 0826 // - Yes, move to caller's buffer... 0827 data.appendData(self.readStorage) 0828 0829 returnCount = self.readStorage.length 0830 0831 // - Reset the storage buffer... 0832 self.readStorage.length = 0 0833 } 0834 0835 return returnCount 0836 } 0837 0838 /// 0839 /// Write data to the socket. 0840 /// 0841 /// - Parameter buffer: The buffer containing the data to write. 0842 /// - Parameter bufSize: The size of the buffer. 0843 /// 0844 public func writeData
BlueSocket.swift:777 try self.readData(data)(buffer: UnsafePointer<Void>, bufSize: Int) throws { 0845 0846 // Make sure the buffer is valid... 0847 if buffer == nil || bufSize == 0 { 0848 0849 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_INVALID_BUFFER, reason: nil) 0850 } 0851 0852 // The socket must've been created and must be connected... 0853 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) { 0854 0855 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil) 0856 } 0857 0858 if !self.connected { 0859 0860 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil) 0861 } 0862 0863 var sent = 0 0864 while sent < bufSize { 0865 0866 let s = write(self.socketfd, buffer + sent, Int(bufSize - sent)) 0867 if s <= 0 { 0868 0869 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_WRITE_FAILED, reason: self.lastError()) 0870 } 0871 sent += s 0872 } 0873 } 0874 0875 /// 0876 /// Write data to the socket. 0877 /// 0878 /// - Parameter data: The NSData object containing the data to write. 0879 /// 0880 public func writeData(data: NSData) throws { 0881 0882 // The socket must've been created and must be connected... 0883 if self.socketfd == Int32(BlueSocket.SOCKET_INVALID_DESCRIPTOR) { 0884 0885 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil) 0886 } 0887 0888 if !self.connected { 0889 0890 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_NOT_CONNECTED, reason: nil) 0891 } 0892 0893 // If there's no data in the NSData object, why bother? Fail silently... 0894 if data.length == 0 { 0895 return 0896 } 0897 0898 var sent = 0 0899 let buffer = data.bytes 0900 while sent < data.length { 0901 0902 let s = write(self.socketfd, buffer + sent, Int(data.length - sent)) 0903 if s <= 0 { 0904 0905 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_WRITE_FAILED, reason: self.lastError()) 0906 } 0907 sent += s 0908 } 0909 } 0910 0911 /// 0912 /// Write a string to the socket. 0913 /// 0914 /// - Parameter string: The string to write. 0915 /// 0916 public func writeString(string: String) throws { 0917 0918 try string.nulTerminatedUTF8.withUnsafeBufferPointer() { 0919 0920 // The count returned by nullTerminatedUTF8 includes the null terminator... 0921 try self.writeData($0.baseAddress, bufSize: $0.count-1) 0922 } 0923 } 0924 0925 /// 0926 /// Set blocking mode for socket. 0927 /// 0928 /// - Parameter shouldBlock: True to block, false to not. 0929 /// 0930 public func setBlocking(shouldBlock: Bool) throws { 0931 0932 let flags = fcntl(self.socketfd, F_GETFL) 0933 if flags < 0 { 0934 0935 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_GET_FCNTL_FAILED, reason: self.lastError()) 0936 } 0937 0938 var result: Int32 = 0 0939 if shouldBlock { 0940 0941 result = fcntl(self.socketfd, F_SETFL, flags & ~O_NONBLOCK) 0942 0943 } else { 0944 0945 result = fcntl(self.socketfd, F_SETFL, flags | O_NONBLOCK) 0946 } 0947 0948 if result < 0 { 0949 0950 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_SET_FCNTL_FAILED, reason: self.lastError()) 0951 } 0952 0953 self.isBlocking = shouldBlock 0954 } 0955 0956 // MARK: Private Methods 0957 0958 /// 0959 /// Private method that reads all available data on an open socket into storage. 0960 /// 0961 /// - Returns: number of bytes read. 0962 /// 0963 private func readDataIntoStorage
BlueSocket.swift:921 try self.writeData($0.baseAddress, bufSize: $0.count-1)() throws -> Int { 0964 0965 // Clear the buffer... 0966 if self.readBuffer != nil { 0967 0968 self.readBuffer.destroy() 0969 self.readBuffer.initialize(0x0) 0970 memset(self.readBuffer, 0x0, self.readBufferSize) 0971 } 0972 0973 // Read all the available data... 0974 var count: Int = 0 0975 repeat { 0976 0977 count = recv(self.socketfd, self.readBuffer, self.readBufferSize, 0) 0978 0979 // Check for error... 0980 if count < 0 { 0981 0982 // - Could be an error, but if errno is EAGAIN or EWOULDBLOCK (if a non-blocking socket), 0983 // it means there was NO data to read... 0984 if errno == EAGAIN || errno == EWOULDBLOCK { 0985 0986 return 0 0987 } 0988 0989 // - Something went wrong... 0990 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_RECV_FAILED, reason: self.lastError()) 0991 } 0992 0993 if count > 0 { 0994 0995 self.readStorage.appendBytes(self.readBuffer, length: count) 0996 } 0997 0998 // Didn't fill the buffer so we've got everything available... 0999 if count < self.readBufferSize { 1000 1001 break 1002 } 1003 1004 } while count > 0 1005 1006 return self.readStorage.length 1007 } 1008 1009 /// 1010 /// Private method to return the last error based on the value of errno. 1011 /// 1012 /// - Returns: String containing relevant text about the error. 1013 /// 1014 private func lastError
BlueSocket.swift:734 let count = try self.readDataIntoStorage()BlueSocket.swift:814 let count = try self.readDataIntoStorage()() -> String { 1015 1016 return String.fromCString(strerror(errno)) ?? "Error: \(errno)" 1017 } 1018 1019 } 1020
BlueSocket.swift:380 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_UNABLE_TO_CREATE_SOCKET, reason: self.lastError())BlueSocket.swift:453 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ACCEPT_FAILED, reason: self.lastError())BlueSocket.swift:490 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_ACCEPT_FAILED, reason: self.lastError())BlueSocket.swift:566 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_GETHOSTBYNAME_FAILED, reason: self.lastError())BlueSocket.swift:581 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_CONNECT_FAILED, reason: self.lastError())BlueSocket.swift:625 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_SELECT_FAILED, reason: self.lastError())BlueSocket.swift:655 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_SETSOCKOPT_FAILED, reason: self.lastError())BlueSocket.swift:671 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_BIND_FAILED, reason: self.lastError())BlueSocket.swift:677 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_LISTEN_FAILED, reason: self.lastError())BlueSocket.swift:869 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_WRITE_FAILED, reason: self.lastError())BlueSocket.swift:905 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_WRITE_FAILED, reason: self.lastError())BlueSocket.swift:935 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_GET_FCNTL_FAILED, reason: self.lastError())BlueSocket.swift:950 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_SET_FCNTL_FAILED, reason: self.lastError())BlueSocket.swift:990 throw BlueSocketError(code: BlueSocket.SOCKET_ERR_RECV_FAILED, reason: self.lastError())