diff --git a/SwiftSocket.xcodeproj/project.pbxproj b/SwiftSocket.xcodeproj/project.pbxproj index 9b94ce6..ef5b8c7 100644 --- a/SwiftSocket.xcodeproj/project.pbxproj +++ b/SwiftSocket.xcodeproj/project.pbxproj @@ -245,6 +245,7 @@ TargetAttributes = { 5518C82E19A329100049DC22 = { CreatedOnToolsVersion = 6.0; + LastSwiftMigration = 0800; }; D43083861A565F7F004DE4D4 = { CreatedOnToolsVersion = 6.1.1; @@ -370,6 +371,7 @@ ONLY_ACTIVE_ARCH = YES; SDKROOT = macosx; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = ""; }; name = Debug; }; @@ -404,6 +406,7 @@ MACOSX_DEPLOYMENT_TARGET = 10.11; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = macosx; + SWIFT_VERSION = ""; }; name = Release; }; @@ -415,6 +418,7 @@ PRODUCT_NAME = SwiftSocket; SWIFT_OBJC_BRIDGING_HEADER = ""; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -425,6 +429,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks"; PRODUCT_NAME = SwiftSocket; SWIFT_OBJC_BRIDGING_HEADER = ""; + SWIFT_VERSION = 3.0; }; name = Release; }; diff --git a/SwiftSocket/main.swift b/SwiftSocket/main.swift index 4e928f7..1d21fe4 100644 --- a/SwiftSocket/main.swift +++ b/SwiftSocket/main.swift @@ -41,7 +41,7 @@ func testtcpclient(){ //读取数据 let data=client.read(1024*10) if let d=data{ - if let str=String(bytes: d, encoding: NSUTF8StringEncoding){ + if let str=String(bytes: d, encoding: String.Encoding.utf8){ print(str) } } @@ -75,14 +75,14 @@ func testtcpserver(){ } //testclient() func testudpserver(){ - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { () -> Void in + DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.background).async(execute: { () -> Void in let server:UDPServer=UDPServer(addr:"127.0.0.1",port:8080) let run:Bool=true while run{ var (data,remoteip,remoteport)=server.recv(1024) print("recive") if let d=data{ - if let str=String(bytes: d, encoding: NSUTF8StringEncoding){ + if let str=String(bytes: d, encoding: String.Encoding.utf8){ print(str) } } @@ -100,7 +100,7 @@ func testudpclient(){ } //testudpBroadcastclient() func testudpBroadcastserver(){ - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { () -> Void in + DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.background).async(execute: { () -> Void in //turn the server to broadcast mode with the address 255.255.255.255 or empty string let server:UDPServer=UDPServer(addr:"",port:8080) let run:Bool=true @@ -109,7 +109,7 @@ func testudpBroadcastserver(){ let (data,remoteip,remoteport)=server.recv(1024) print("recive\(remoteip);\(remoteport)") if let d=data{ - if let str=String(bytes: d, encoding: NSUTF8StringEncoding){ + if let str=String(bytes: d, encoding: String.Encoding.utf8){ print(str) } } @@ -134,6 +134,6 @@ func testudpBroadcastclient(){ testudpBroadcastserver() testudpBroadcastclient() -var stdinput=NSFileHandle.fileHandleWithStandardInput() +var stdinput=FileHandle.standardInput stdinput.readDataToEndOfFile() diff --git a/SwiftSocket/ysocket/ysocket.swift b/SwiftSocket/ysocket/ysocket.swift index c0d7094..b4b580c 100644 --- a/SwiftSocket/ysocket/ysocket.swift +++ b/SwiftSocket/ysocket/ysocket.swift @@ -29,7 +29,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ import Foundation -public class YSocket{ +open class YSocket{ var addr:String var port:Int var fd:Int32? @@ -41,4 +41,4 @@ public class YSocket{ self.addr=a self.port=p } -} \ No newline at end of file +} diff --git a/SwiftSocket/ysocket/ytcpsocket.swift b/SwiftSocket/ysocket/ytcpsocket.swift index 526b17a..fc2d7e4 100644 --- a/SwiftSocket/ysocket/ytcpsocket.swift +++ b/SwiftSocket/ysocket/ytcpsocket.swift @@ -29,19 +29,19 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ import Foundation -@_silgen_name("ytcpsocket_connect") func c_ytcpsocket_connect(host:UnsafePointer,port:Int32,timeout:Int32) -> Int32 -@_silgen_name("ytcpsocket_close") func c_ytcpsocket_close(fd:Int32) -> Int32 -@_silgen_name("ytcpsocket_send") func c_ytcpsocket_send(fd:Int32,buff:UnsafePointer,len:Int32) -> Int32 -@_silgen_name("ytcpsocket_pull") func c_ytcpsocket_pull(fd:Int32,buff:UnsafePointer,len:Int32,timeout:Int32) -> Int32 -@_silgen_name("ytcpsocket_listen") func c_ytcpsocket_listen(addr:UnsafePointer,port:Int32)->Int32 -@_silgen_name("ytcpsocket_accept") func c_ytcpsocket_accept(onsocketfd:Int32,ip:UnsafePointer,port:UnsafePointer) -> Int32 +@_silgen_name("ytcpsocket_connect") func c_ytcpsocket_connect(_ host:UnsafePointer,port:Int32,timeout:Int32) -> Int32 +@_silgen_name("ytcpsocket_close") func c_ytcpsocket_close(_ fd:Int32) -> Int32 +@_silgen_name("ytcpsocket_send") func c_ytcpsocket_send(_ fd:Int32,buff:UnsafePointer,len:Int32) -> Int32 +@_silgen_name("ytcpsocket_pull") func c_ytcpsocket_pull(_ fd:Int32,buff:UnsafePointer,len:Int32,timeout:Int32) -> Int32 +@_silgen_name("ytcpsocket_listen") func c_ytcpsocket_listen(_ addr:UnsafePointer,port:Int32)->Int32 +@_silgen_name("ytcpsocket_accept") func c_ytcpsocket_accept(_ onsocketfd:Int32,ip:UnsafePointer,port:UnsafePointer) -> Int32 -public class TCPClient:YSocket{ +open class TCPClient:YSocket{ /* * connect to server * return success or fail with message */ - public func connect(timeout t:Int)->(Bool,String){ + open func connect(timeout t:Int)->(Bool,String){ let rs:Int32=c_ytcpsocket_connect(self.addr, port: Int32(self.port), timeout: Int32(t)) if rs>0{ self.fd=rs @@ -63,7 +63,7 @@ public class TCPClient:YSocket{ * close socket * return success or fail with message */ - public func close()->(Bool,String){ + open func close()->(Bool,String){ if let fd:Int32=self.fd{ c_ytcpsocket_close(fd) self.fd=nil @@ -76,7 +76,7 @@ public class TCPClient:YSocket{ * send data * return success or fail with message */ - public func send(data d:[UInt8])->(Bool,String){ + open func send(data d:[UInt8])->(Bool,String){ if let fd:Int32=self.fd{ let sendsize:Int32=c_ytcpsocket_send(fd, buff: d, len: Int32(d.count)) if Int(sendsize)==d.count{ @@ -92,7 +92,7 @@ public class TCPClient:YSocket{ * send string * return success or fail with message */ - public func send(str s:String)->(Bool,String){ + open func send(str s:String)->(Bool,String){ if let fd:Int32=self.fd{ let sendsize:Int32=c_ytcpsocket_send(fd, buff: s, len: Int32(strlen(s))) if sendsize==Int32(strlen(s)){ @@ -108,12 +108,12 @@ public class TCPClient:YSocket{ * * send nsdata */ - public func send(data d:NSData)->(Bool,String){ + open func send(data d:Data)->(Bool,String){ if let fd:Int32=self.fd{ - var buff:[UInt8] = [UInt8](count:d.length,repeatedValue:0x0) - d.getBytes(&buff, length: d.length) - let sendsize:Int32=c_ytcpsocket_send(fd, buff: buff, len: Int32(d.length)) - if sendsize==Int32(d.length){ + var buff:[UInt8] = [UInt8](repeating: 0x0,count: d.count) + (d as NSData).getBytes(&buff, length: d.count) + let sendsize:Int32=c_ytcpsocket_send(fd, buff: buff, len: Int32(d.count)) + if sendsize==Int32(d.count){ return (true,"send success") }else{ return (false,"send error") @@ -126,9 +126,9 @@ public class TCPClient:YSocket{ * read data with expect length * return success or fail with message */ - public func read(expectlen:Int, timeout:Int = -1)->[UInt8]?{ + open func read(_ expectlen:Int, timeout:Int = -1)->[UInt8]?{ if let fd:Int32 = self.fd{ - var buff:[UInt8] = [UInt8](count:expectlen,repeatedValue:0x0) + var buff:[UInt8] = [UInt8](repeating: 0x0,count: expectlen) let readLen:Int32=c_ytcpsocket_pull(fd, buff: &buff, len: Int32(expectlen), timeout: Int32(timeout)) if readLen<=0{ return nil @@ -141,9 +141,9 @@ public class TCPClient:YSocket{ } } -public class TCPServer:YSocket{ +open class TCPServer:YSocket{ - public func listen()->(Bool,String){ + open func listen()->(Bool,String){ let fd:Int32=c_ytcpsocket_listen(self.addr, port: Int32(self.port)) if fd>0{ @@ -153,9 +153,9 @@ public class TCPServer:YSocket{ return (false,"listen fail") } } - public func accept()->TCPClient?{ + open func accept()->TCPClient?{ if let serferfd=self.fd{ - var buff:[Int8] = [Int8](count:16,repeatedValue:0x0) + var buff:[Int8] = [Int8](repeating: 0x0,count: 16) var port:Int32=0 let clientfd:Int32=c_ytcpsocket_accept(serferfd, ip: &buff,port: &port) if clientfd<0{ @@ -164,14 +164,14 @@ public class TCPServer:YSocket{ let tcpClient:TCPClient=TCPClient() tcpClient.fd=clientfd tcpClient.port=Int(port) - if let addr=String(CString: buff, encoding: NSUTF8StringEncoding){ + if let addr=String(cString: buff, encoding: String.Encoding.utf8){ tcpClient.addr=addr } return tcpClient } return nil } - public func close()->(Bool,String){ + open func close()->(Bool,String){ if let fd:Int32=self.fd{ c_ytcpsocket_close(fd) self.fd=nil diff --git a/SwiftSocket/ysocket/yudpsocket.swift b/SwiftSocket/ysocket/yudpsocket.swift index 96be4c2..c5f9bbd 100644 --- a/SwiftSocket/ysocket/yudpsocket.swift +++ b/SwiftSocket/ysocket/yudpsocket.swift @@ -30,21 +30,21 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import Foundation -@_silgen_name("yudpsocket_server") func c_yudpsocket_server(host:UnsafePointer,port:Int32) -> Int32 -@_silgen_name("yudpsocket_recive") func c_yudpsocket_recive(fd:Int32,buff:UnsafePointer,len:Int32,ip:UnsafePointer,port:UnsafePointer) -> Int32 -@_silgen_name("yudpsocket_close") func c_yudpsocket_close(fd:Int32) -> Int32 +@_silgen_name("yudpsocket_server") func c_yudpsocket_server(_ host:UnsafePointer,port:Int32) -> Int32 +@_silgen_name("yudpsocket_recive") func c_yudpsocket_recive(_ fd:Int32,buff:UnsafePointer,len:Int32,ip:UnsafePointer,port:UnsafePointer) -> Int32 +@_silgen_name("yudpsocket_close") func c_yudpsocket_close(_ fd:Int32) -> Int32 @_silgen_name("yudpsocket_client") func c_yudpsocket_client() -> Int32 -@_silgen_name("yudpsocket_get_server_ip") func c_yudpsocket_get_server_ip(host:UnsafePointer,ip:UnsafePointer) -> Int32 -@_silgen_name("yudpsocket_sentto") func c_yudpsocket_sentto(fd:Int32,buff:UnsafePointer,len:Int32,ip:UnsafePointer,port:Int32) -> Int32 -@_silgen_name("enable_broadcast") func c_enable_broadcast(fd:Int32) +@_silgen_name("yudpsocket_get_server_ip") func c_yudpsocket_get_server_ip(_ host:UnsafePointer,ip:UnsafePointer) -> Int32 +@_silgen_name("yudpsocket_sentto") func c_yudpsocket_sentto(_ fd:Int32,buff:UnsafePointer,len:Int32,ip:UnsafePointer,port:Int32) -> Int32 +@_silgen_name("enable_broadcast") func c_enable_broadcast(_ fd:Int32) -public class UDPClient: YSocket { +open class UDPClient: YSocket { public override init(addr a:String,port p:Int){ super.init() - let remoteipbuff:[Int8] = [Int8](count:16,repeatedValue:0x0) + let remoteipbuff:[Int8] = [Int8](repeating: 0x0,count: 16) let ret=c_yudpsocket_get_server_ip(a, ip: remoteipbuff) if ret==0{ - if let ip=String(CString: remoteipbuff, encoding: NSUTF8StringEncoding){ + if let ip=String(cString: remoteipbuff, encoding: String.Encoding.utf8){ self.addr=ip self.port=p let fd:Int32=c_yudpsocket_client() @@ -58,7 +58,7 @@ public class UDPClient: YSocket { * send data * return success or fail with message */ - public func send(data d:[UInt8])->(Bool,String){ + open func send(data d:[UInt8])->(Bool,String){ if let fd:Int32=self.fd{ let sendsize:Int32=c_yudpsocket_sentto(fd, buff: d, len: Int32(d.count), ip: self.addr,port: Int32(self.port)) if Int(sendsize)==d.count{ @@ -74,7 +74,7 @@ public class UDPClient: YSocket { * send string * return success or fail with message */ - public func send(str s:String)->(Bool,String){ + open func send(str s:String)->(Bool,String){ if let fd:Int32=self.fd{ let sendsize:Int32=c_yudpsocket_sentto(fd, buff: s, len: Int32(strlen(s)), ip: self.addr,port: Int32(self.port)) if sendsize==Int32(strlen(s)){ @@ -89,7 +89,7 @@ public class UDPClient: YSocket { /* * enableBroadcast */ - public func enableBroadcast(){ + open func enableBroadcast(){ if let fd:Int32=self.fd{ c_enable_broadcast(fd) @@ -99,12 +99,12 @@ public class UDPClient: YSocket { * * send nsdata */ - public func send(data d:NSData)->(Bool,String){ + open func send(data d:Data)->(Bool,String){ if let fd:Int32=self.fd{ - var buff:[UInt8] = [UInt8](count:d.length,repeatedValue:0x0) - d.getBytes(&buff, length: d.length) - let sendsize:Int32=c_yudpsocket_sentto(fd, buff: buff, len: Int32(d.length), ip: self.addr,port: Int32(self.port)) - if sendsize==Int32(d.length){ + var buff:[UInt8] = [UInt8](repeating: 0x0,count: d.count) + (d as NSData).getBytes(&buff, length: d.count) + let sendsize:Int32=c_yudpsocket_sentto(fd, buff: buff, len: Int32(d.count), ip: self.addr,port: Int32(self.port)) + if sendsize==Int32(d.count){ return (true,"send success") }else{ return (false,"send error") @@ -113,7 +113,7 @@ public class UDPClient: YSocket { return (false,"socket not open") } } - public func close()->(Bool,String){ + open func close()->(Bool,String){ if let fd:Int32=self.fd{ c_yudpsocket_close(fd) self.fd=nil @@ -125,7 +125,7 @@ public class UDPClient: YSocket { //TODO add multycast and boardcast } -public class UDPServer:YSocket{ +open class UDPServer:YSocket{ public override init(addr a:String,port p:Int){ super.init(addr: a, port: p) let fd:Int32 = c_yudpsocket_server(self.addr, port: Int32(self.port)) @@ -134,15 +134,15 @@ public class UDPServer:YSocket{ } } //TODO add multycast and boardcast - public func recv(expectlen:Int)->([UInt8]?,String,Int){ + open func recv(_ expectlen:Int)->([UInt8]?,String,Int){ if let fd:Int32 = self.fd{ - var buff:[UInt8] = [UInt8](count:expectlen,repeatedValue:0x0) - var remoteipbuff:[Int8] = [Int8](count:16,repeatedValue:0x0) + var buff:[UInt8] = [UInt8](repeating: 0x0,count: expectlen) + var remoteipbuff:[Int8] = [Int8](repeating: 0x0,count: 16) var remoteport:Int32=0 let readLen:Int32=c_yudpsocket_recive(fd, buff: buff, len: Int32(expectlen), ip: &remoteipbuff, port: &remoteport) let port:Int=Int(remoteport) var addr:String="" - if let ip=String(CString: remoteipbuff, encoding: NSUTF8StringEncoding){ + if let ip=String(cString: remoteipbuff, encoding: String.Encoding.utf8){ addr=ip } if readLen<=0{ @@ -154,7 +154,7 @@ public class UDPServer:YSocket{ } return (nil,"no ip",0) } - public func close()->(Bool,String){ + open func close()->(Bool,String){ if let fd:Int32=self.fd{ c_yudpsocket_close(fd) self.fd=nil