Skip to content

Commit 484cf9f

Browse files
author
小p
authored
Merge pull request #35 from katsuma/swift3.0
Use Swift 3.0 style
2 parents 7cdf0b8 + 1e56b41 commit 484cf9f

File tree

5 files changed

+61
-56
lines changed

5 files changed

+61
-56
lines changed

SwiftSocket.xcodeproj/project.pbxproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@
245245
TargetAttributes = {
246246
5518C82E19A329100049DC22 = {
247247
CreatedOnToolsVersion = 6.0;
248+
LastSwiftMigration = 0800;
248249
};
249250
D43083861A565F7F004DE4D4 = {
250251
CreatedOnToolsVersion = 6.1.1;
@@ -370,6 +371,7 @@
370371
ONLY_ACTIVE_ARCH = YES;
371372
SDKROOT = macosx;
372373
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
374+
SWIFT_VERSION = "";
373375
};
374376
name = Debug;
375377
};
@@ -404,6 +406,7 @@
404406
MACOSX_DEPLOYMENT_TARGET = 10.11;
405407
MTL_ENABLE_DEBUG_INFO = NO;
406408
SDKROOT = macosx;
409+
SWIFT_VERSION = "";
407410
};
408411
name = Release;
409412
};
@@ -415,6 +418,7 @@
415418
PRODUCT_NAME = SwiftSocket;
416419
SWIFT_OBJC_BRIDGING_HEADER = "";
417420
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
421+
SWIFT_VERSION = 3.0;
418422
};
419423
name = Debug;
420424
};
@@ -425,6 +429,7 @@
425429
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
426430
PRODUCT_NAME = SwiftSocket;
427431
SWIFT_OBJC_BRIDGING_HEADER = "";
432+
SWIFT_VERSION = 3.0;
428433
};
429434
name = Release;
430435
};

SwiftSocket/main.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func testtcpclient(){
4141
//读取数据
4242
let data=client.read(1024*10)
4343
if let d=data{
44-
if let str=String(bytes: d, encoding: NSUTF8StringEncoding){
44+
if let str=String(bytes: d, encoding: String.Encoding.utf8){
4545
print(str)
4646
}
4747
}
@@ -75,14 +75,14 @@ func testtcpserver(){
7575
}
7676
//testclient()
7777
func testudpserver(){
78-
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { () -> Void in
78+
DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.background).async(execute: { () -> Void in
7979
let server:UDPServer=UDPServer(addr:"127.0.0.1",port:8080)
8080
let run:Bool=true
8181
while run{
8282
var (data,remoteip,remoteport)=server.recv(1024)
8383
print("recive")
8484
if let d=data{
85-
if let str=String(bytes: d, encoding: NSUTF8StringEncoding){
85+
if let str=String(bytes: d, encoding: String.Encoding.utf8){
8686
print(str)
8787
}
8888
}
@@ -100,7 +100,7 @@ func testudpclient(){
100100
}
101101
//testudpBroadcastclient()
102102
func testudpBroadcastserver(){
103-
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { () -> Void in
103+
DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.background).async(execute: { () -> Void in
104104
//turn the server to broadcast mode with the address 255.255.255.255 or empty string
105105
let server:UDPServer=UDPServer(addr:"",port:8080)
106106
let run:Bool=true
@@ -109,7 +109,7 @@ func testudpBroadcastserver(){
109109
let (data,remoteip,remoteport)=server.recv(1024)
110110
print("recive\(remoteip);\(remoteport)")
111111
if let d=data{
112-
if let str=String(bytes: d, encoding: NSUTF8StringEncoding){
112+
if let str=String(bytes: d, encoding: String.Encoding.utf8){
113113
print(str)
114114
}
115115
}
@@ -134,6 +134,6 @@ func testudpBroadcastclient(){
134134
testudpBroadcastserver()
135135
testudpBroadcastclient()
136136

137-
var stdinput=NSFileHandle.fileHandleWithStandardInput()
137+
var stdinput=FileHandle.standardInput
138138
stdinput.readDataToEndOfFile()
139139

SwiftSocket/ysocket/ysocket.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929
*/
3030

3131
import Foundation
32-
public class YSocket{
32+
open class YSocket{
3333
var addr:String
3434
var port:Int
3535
var fd:Int32?
@@ -41,4 +41,4 @@ public class YSocket{
4141
self.addr=a
4242
self.port=p
4343
}
44-
}
44+
}

SwiftSocket/ysocket/ytcpsocket.swift

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2929
*/
3030
import Foundation
3131

32-
@_silgen_name("ytcpsocket_connect") func c_ytcpsocket_connect(host:UnsafePointer<Int8>,port:Int32,timeout:Int32) -> Int32
33-
@_silgen_name("ytcpsocket_close") func c_ytcpsocket_close(fd:Int32) -> Int32
34-
@_silgen_name("ytcpsocket_send") func c_ytcpsocket_send(fd:Int32,buff:UnsafePointer<UInt8>,len:Int32) -> Int32
35-
@_silgen_name("ytcpsocket_pull") func c_ytcpsocket_pull(fd:Int32,buff:UnsafePointer<UInt8>,len:Int32,timeout:Int32) -> Int32
36-
@_silgen_name("ytcpsocket_listen") func c_ytcpsocket_listen(addr:UnsafePointer<Int8>,port:Int32)->Int32
37-
@_silgen_name("ytcpsocket_accept") func c_ytcpsocket_accept(onsocketfd:Int32,ip:UnsafePointer<Int8>,port:UnsafePointer<Int32>) -> Int32
32+
@_silgen_name("ytcpsocket_connect") func c_ytcpsocket_connect(_ host:UnsafePointer<Int8>,port:Int32,timeout:Int32) -> Int32
33+
@_silgen_name("ytcpsocket_close") func c_ytcpsocket_close(_ fd:Int32) -> Int32
34+
@_silgen_name("ytcpsocket_send") func c_ytcpsocket_send(_ fd:Int32,buff:UnsafePointer<UInt8>,len:Int32) -> Int32
35+
@_silgen_name("ytcpsocket_pull") func c_ytcpsocket_pull(_ fd:Int32,buff:UnsafePointer<UInt8>,len:Int32,timeout:Int32) -> Int32
36+
@_silgen_name("ytcpsocket_listen") func c_ytcpsocket_listen(_ addr:UnsafePointer<Int8>,port:Int32)->Int32
37+
@_silgen_name("ytcpsocket_accept") func c_ytcpsocket_accept(_ onsocketfd:Int32,ip:UnsafePointer<Int8>,port:UnsafePointer<Int32>) -> Int32
3838

39-
public class TCPClient:YSocket{
39+
open class TCPClient:YSocket{
4040
/*
4141
* connect to server
4242
* return success or fail with message
4343
*/
44-
public func connect(timeout t:Int)->(Bool,String){
44+
open func connect(timeout t:Int)->(Bool,String){
4545
let rs:Int32=c_ytcpsocket_connect(self.addr, port: Int32(self.port), timeout: Int32(t))
4646
if rs>0{
4747
self.fd=rs
@@ -63,7 +63,7 @@ public class TCPClient:YSocket{
6363
* close socket
6464
* return success or fail with message
6565
*/
66-
public func close()->(Bool,String){
66+
open func close()->(Bool,String){
6767
if let fd:Int32=self.fd{
6868
c_ytcpsocket_close(fd)
6969
self.fd=nil
@@ -76,7 +76,7 @@ public class TCPClient:YSocket{
7676
* send data
7777
* return success or fail with message
7878
*/
79-
public func send(data d:[UInt8])->(Bool,String){
79+
open func send(data d:[UInt8])->(Bool,String){
8080
if let fd:Int32=self.fd{
8181
let sendsize:Int32=c_ytcpsocket_send(fd, buff: d, len: Int32(d.count))
8282
if Int(sendsize)==d.count{
@@ -92,7 +92,7 @@ public class TCPClient:YSocket{
9292
* send string
9393
* return success or fail with message
9494
*/
95-
public func send(str s:String)->(Bool,String){
95+
open func send(str s:String)->(Bool,String){
9696
if let fd:Int32=self.fd{
9797
let sendsize:Int32=c_ytcpsocket_send(fd, buff: s, len: Int32(strlen(s)))
9898
if sendsize==Int32(strlen(s)){
@@ -108,12 +108,12 @@ public class TCPClient:YSocket{
108108
*
109109
* send nsdata
110110
*/
111-
public func send(data d:NSData)->(Bool,String){
111+
open func send(data d:Data)->(Bool,String){
112112
if let fd:Int32=self.fd{
113-
var buff:[UInt8] = [UInt8](count:d.length,repeatedValue:0x0)
114-
d.getBytes(&buff, length: d.length)
115-
let sendsize:Int32=c_ytcpsocket_send(fd, buff: buff, len: Int32(d.length))
116-
if sendsize==Int32(d.length){
113+
var buff:[UInt8] = [UInt8](repeating: 0x0,count: d.count)
114+
(d as NSData).getBytes(&buff, length: d.count)
115+
let sendsize:Int32=c_ytcpsocket_send(fd, buff: buff, len: Int32(d.count))
116+
if sendsize==Int32(d.count){
117117
return (true,"send success")
118118
}else{
119119
return (false,"send error")
@@ -126,9 +126,9 @@ public class TCPClient:YSocket{
126126
* read data with expect length
127127
* return success or fail with message
128128
*/
129-
public func read(expectlen:Int, timeout:Int = -1)->[UInt8]?{
129+
open func read(_ expectlen:Int, timeout:Int = -1)->[UInt8]?{
130130
if let fd:Int32 = self.fd{
131-
var buff:[UInt8] = [UInt8](count:expectlen,repeatedValue:0x0)
131+
var buff:[UInt8] = [UInt8](repeating: 0x0,count: expectlen)
132132
let readLen:Int32=c_ytcpsocket_pull(fd, buff: &buff, len: Int32(expectlen), timeout: Int32(timeout))
133133
if readLen<=0{
134134
return nil
@@ -141,9 +141,9 @@ public class TCPClient:YSocket{
141141
}
142142
}
143143

144-
public class TCPServer:YSocket{
144+
open class TCPServer:YSocket{
145145

146-
public func listen()->(Bool,String){
146+
open func listen()->(Bool,String){
147147

148148
let fd:Int32=c_ytcpsocket_listen(self.addr, port: Int32(self.port))
149149
if fd>0{
@@ -153,9 +153,9 @@ public class TCPServer:YSocket{
153153
return (false,"listen fail")
154154
}
155155
}
156-
public func accept()->TCPClient?{
156+
open func accept()->TCPClient?{
157157
if let serferfd=self.fd{
158-
var buff:[Int8] = [Int8](count:16,repeatedValue:0x0)
158+
var buff:[Int8] = [Int8](repeating: 0x0,count: 16)
159159
var port:Int32=0
160160
let clientfd:Int32=c_ytcpsocket_accept(serferfd, ip: &buff,port: &port)
161161
if clientfd<0{
@@ -164,14 +164,14 @@ public class TCPServer:YSocket{
164164
let tcpClient:TCPClient=TCPClient()
165165
tcpClient.fd=clientfd
166166
tcpClient.port=Int(port)
167-
if let addr=String(CString: buff, encoding: NSUTF8StringEncoding){
167+
if let addr=String(cString: buff, encoding: String.Encoding.utf8){
168168
tcpClient.addr=addr
169169
}
170170
return tcpClient
171171
}
172172
return nil
173173
}
174-
public func close()->(Bool,String){
174+
open func close()->(Bool,String){
175175
if let fd:Int32=self.fd{
176176
c_ytcpsocket_close(fd)
177177
self.fd=nil

SwiftSocket/ysocket/yudpsocket.swift

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030

3131
import Foundation
3232

33-
@_silgen_name("yudpsocket_server") func c_yudpsocket_server(host:UnsafePointer<Int8>,port:Int32) -> Int32
34-
@_silgen_name("yudpsocket_recive") func c_yudpsocket_recive(fd:Int32,buff:UnsafePointer<UInt8>,len:Int32,ip:UnsafePointer<Int8>,port:UnsafePointer<Int32>) -> Int32
35-
@_silgen_name("yudpsocket_close") func c_yudpsocket_close(fd:Int32) -> Int32
33+
@_silgen_name("yudpsocket_server") func c_yudpsocket_server(_ host:UnsafePointer<Int8>,port:Int32) -> Int32
34+
@_silgen_name("yudpsocket_recive") func c_yudpsocket_recive(_ fd:Int32,buff:UnsafePointer<UInt8>,len:Int32,ip:UnsafePointer<Int8>,port:UnsafePointer<Int32>) -> Int32
35+
@_silgen_name("yudpsocket_close") func c_yudpsocket_close(_ fd:Int32) -> Int32
3636
@_silgen_name("yudpsocket_client") func c_yudpsocket_client() -> Int32
37-
@_silgen_name("yudpsocket_get_server_ip") func c_yudpsocket_get_server_ip(host:UnsafePointer<Int8>,ip:UnsafePointer<Int8>) -> Int32
38-
@_silgen_name("yudpsocket_sentto") func c_yudpsocket_sentto(fd:Int32,buff:UnsafePointer<UInt8>,len:Int32,ip:UnsafePointer<Int8>,port:Int32) -> Int32
39-
@_silgen_name("enable_broadcast") func c_enable_broadcast(fd:Int32)
37+
@_silgen_name("yudpsocket_get_server_ip") func c_yudpsocket_get_server_ip(_ host:UnsafePointer<Int8>,ip:UnsafePointer<Int8>) -> Int32
38+
@_silgen_name("yudpsocket_sentto") func c_yudpsocket_sentto(_ fd:Int32,buff:UnsafePointer<UInt8>,len:Int32,ip:UnsafePointer<Int8>,port:Int32) -> Int32
39+
@_silgen_name("enable_broadcast") func c_enable_broadcast(_ fd:Int32)
4040

41-
public class UDPClient: YSocket {
41+
open class UDPClient: YSocket {
4242
public override init(addr a:String,port p:Int){
4343
super.init()
44-
let remoteipbuff:[Int8] = [Int8](count:16,repeatedValue:0x0)
44+
let remoteipbuff:[Int8] = [Int8](repeating: 0x0,count: 16)
4545
let ret=c_yudpsocket_get_server_ip(a, ip: remoteipbuff)
4646
if ret==0{
47-
if let ip=String(CString: remoteipbuff, encoding: NSUTF8StringEncoding){
47+
if let ip=String(cString: remoteipbuff, encoding: String.Encoding.utf8){
4848
self.addr=ip
4949
self.port=p
5050
let fd:Int32=c_yudpsocket_client()
@@ -58,7 +58,7 @@ public class UDPClient: YSocket {
5858
* send data
5959
* return success or fail with message
6060
*/
61-
public func send(data d:[UInt8])->(Bool,String){
61+
open func send(data d:[UInt8])->(Bool,String){
6262
if let fd:Int32=self.fd{
6363
let sendsize:Int32=c_yudpsocket_sentto(fd, buff: d, len: Int32(d.count), ip: self.addr,port: Int32(self.port))
6464
if Int(sendsize)==d.count{
@@ -74,7 +74,7 @@ public class UDPClient: YSocket {
7474
* send string
7575
* return success or fail with message
7676
*/
77-
public func send(str s:String)->(Bool,String){
77+
open func send(str s:String)->(Bool,String){
7878
if let fd:Int32=self.fd{
7979
let sendsize:Int32=c_yudpsocket_sentto(fd, buff: s, len: Int32(strlen(s)), ip: self.addr,port: Int32(self.port))
8080
if sendsize==Int32(strlen(s)){
@@ -89,7 +89,7 @@ public class UDPClient: YSocket {
8989
/*
9090
* enableBroadcast
9191
*/
92-
public func enableBroadcast(){
92+
open func enableBroadcast(){
9393
if let fd:Int32=self.fd{
9494
c_enable_broadcast(fd)
9595

@@ -99,12 +99,12 @@ public class UDPClient: YSocket {
9999
*
100100
* send nsdata
101101
*/
102-
public func send(data d:NSData)->(Bool,String){
102+
open func send(data d:Data)->(Bool,String){
103103
if let fd:Int32=self.fd{
104-
var buff:[UInt8] = [UInt8](count:d.length,repeatedValue:0x0)
105-
d.getBytes(&buff, length: d.length)
106-
let sendsize:Int32=c_yudpsocket_sentto(fd, buff: buff, len: Int32(d.length), ip: self.addr,port: Int32(self.port))
107-
if sendsize==Int32(d.length){
104+
var buff:[UInt8] = [UInt8](repeating: 0x0,count: d.count)
105+
(d as NSData).getBytes(&buff, length: d.count)
106+
let sendsize:Int32=c_yudpsocket_sentto(fd, buff: buff, len: Int32(d.count), ip: self.addr,port: Int32(self.port))
107+
if sendsize==Int32(d.count){
108108
return (true,"send success")
109109
}else{
110110
return (false,"send error")
@@ -113,7 +113,7 @@ public class UDPClient: YSocket {
113113
return (false,"socket not open")
114114
}
115115
}
116-
public func close()->(Bool,String){
116+
open func close()->(Bool,String){
117117
if let fd:Int32=self.fd{
118118
c_yudpsocket_close(fd)
119119
self.fd=nil
@@ -125,7 +125,7 @@ public class UDPClient: YSocket {
125125
//TODO add multycast and boardcast
126126
}
127127

128-
public class UDPServer:YSocket{
128+
open class UDPServer:YSocket{
129129
public override init(addr a:String,port p:Int){
130130
super.init(addr: a, port: p)
131131
let fd:Int32 = c_yudpsocket_server(self.addr, port: Int32(self.port))
@@ -134,15 +134,15 @@ public class UDPServer:YSocket{
134134
}
135135
}
136136
//TODO add multycast and boardcast
137-
public func recv(expectlen:Int)->([UInt8]?,String,Int){
137+
open func recv(_ expectlen:Int)->([UInt8]?,String,Int){
138138
if let fd:Int32 = self.fd{
139-
var buff:[UInt8] = [UInt8](count:expectlen,repeatedValue:0x0)
140-
var remoteipbuff:[Int8] = [Int8](count:16,repeatedValue:0x0)
139+
var buff:[UInt8] = [UInt8](repeating: 0x0,count: expectlen)
140+
var remoteipbuff:[Int8] = [Int8](repeating: 0x0,count: 16)
141141
var remoteport:Int32=0
142142
let readLen:Int32=c_yudpsocket_recive(fd, buff: buff, len: Int32(expectlen), ip: &remoteipbuff, port: &remoteport)
143143
let port:Int=Int(remoteport)
144144
var addr:String=""
145-
if let ip=String(CString: remoteipbuff, encoding: NSUTF8StringEncoding){
145+
if let ip=String(cString: remoteipbuff, encoding: String.Encoding.utf8){
146146
addr=ip
147147
}
148148
if readLen<=0{
@@ -154,7 +154,7 @@ public class UDPServer:YSocket{
154154
}
155155
return (nil,"no ip",0)
156156
}
157-
public func close()->(Bool,String){
157+
open func close()->(Bool,String){
158158
if let fd:Int32=self.fd{
159159
c_yudpsocket_close(fd)
160160
self.fd=nil

0 commit comments

Comments
 (0)