Skip to content

Commit cdee414

Browse files
committed
modified to support swift error handling
1 parent 11c2b5d commit cdee414

File tree

1 file changed

+50
-34
lines changed

1 file changed

+50
-34
lines changed

SwiftSocket/ysocket/ytcpsocket.swift

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,21 @@ public class TCPClient:YSocket{
4141
* connect to server
4242
* return success or fail with message
4343
*/
44-
public func connect(timeout t:Int)->(Bool,String){
44+
public func connect(timeout t:Int) throws {
4545
let rs:Int32=c_ytcpsocket_connect(self.addr, port: Int32(self.port), timeout: Int32(t))
4646
if rs>0{
4747
self.fd=rs
48-
return (true,"connect success")
48+
return
4949
}else{
5050
switch rs{
5151
case -1:
52-
return (false,"qeury server fail")
52+
throw Error.ConnectFail
5353
case -2:
54-
return (false,"connection closed")
54+
throw Error.ConnectionClosed
5555
case -3:
56-
return (false,"connect timeout")
56+
throw Error.Timeout
5757
default:
58-
return (false,"unknow err.")
58+
throw Error.Unknown
5959
}
6060
}
6161
}
@@ -76,57 +76,52 @@ public class TCPClient:YSocket{
7676
* send data
7777
* return success or fail with message
7878
*/
79-
public func send(data d:[UInt8])->(Bool,String){
80-
if let fd:Int32=self.fd{
81-
let sendsize:Int32=c_ytcpsocket_send(fd, buff: d, len: Int32(d.count))
82-
if Int(sendsize)==d.count{
83-
return (true,"send success")
84-
}else{
85-
return (false,"send error")
79+
80+
public func send(bytes bytes: [UInt8], length: Int32? = nil) throws {
81+
if let fd:Int32 = self.fd{
82+
let len = length ?? Int32(bytes.count)
83+
let sendsize: Int32 = c_ytcpsocket_send(fd, buff: bytes, len: len)
84+
if Int(sendsize) == bytes.count{
85+
return
86+
} else {
87+
throw Error.Send
8688
}
87-
}else{
88-
return (false,"socket not open")
89+
} else {
90+
throw Error.SocketNotOpen
8991
}
9092
}
93+
94+
9195
/*
9296
* send string
9397
* return success or fail with message
9498
*/
95-
public func send(str s:String)->(Bool,String){
99+
public func send(str s: String) throws {
96100
if let fd:Int32=self.fd{
97101
let sendsize:Int32=c_ytcpsocket_send(fd, buff: s, len: Int32(strlen(s)))
98102
if sendsize==Int32(strlen(s)){
99-
return (true,"send success")
103+
return
100104
}else{
101-
return (false,"send error")
105+
throw Error.Send
102106
}
103-
}else{
104-
return (false,"socket not open")
107+
} else{
108+
throw Error.SocketNotOpen
105109
}
106110
}
107111
/*
108112
*
109113
* send nsdata
110114
*/
111-
public func send(data d:NSData)->(Bool,String){
112-
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){
117-
return (true,"send success")
118-
}else{
119-
return (false,"send error")
120-
}
121-
}else{
122-
return (false,"socket not open")
123-
}
115+
public func send(data d:NSData, length: Int32? = nil) throws {
116+
var buff:[UInt8] = [UInt8](count:d.length,repeatedValue:0x0)
117+
d.getBytes(&buff, length: d.length)
118+
return try self.send(bytes: buff, length: length)
124119
}
125120
/*
126121
* read data with expect length
127122
* return success or fail with message
128123
*/
129-
public func read(expectlen:Int, timeout:Int = -1)->[UInt8]?{
124+
public func read(expectlen:Int, timeout:Int = -1) -> [UInt8]? {
130125
if let fd:Int32 = self.fd{
131126
var buff:[UInt8] = [UInt8](count:expectlen,repeatedValue:0x0)
132127
let readLen:Int32=c_ytcpsocket_pull(fd, buff: &buff, len: Int32(expectlen), timeout: Int32(timeout))
@@ -139,8 +134,29 @@ public class TCPClient:YSocket{
139134
}
140135
return nil
141136
}
137+
138+
public func read(inout buffer buffer:[UInt8], expectlen:Int? = nil, timeout:Int = -1) throws -> Int {
139+
if let fd:Int32 = self.fd{
140+
let len = expectlen ?? buffer.count
141+
let readLen:Int32=c_ytcpsocket_pull(fd, buff: &buffer, len: Int32(len), timeout: Int32(timeout))
142+
return Int(readLen)
143+
} else {
144+
throw Error.SocketNotOpen
145+
}
146+
}
147+
148+
public enum Error : ErrorType {
149+
case SocketNotOpen
150+
case SocketFunction
151+
case Send
152+
case Timeout
153+
case ConnectFail
154+
case ConnectionClosed
155+
case Unknown
156+
}
142157
}
143158

159+
144160
public class TCPServer:YSocket{
145161

146162
public func listen()->(Bool,String){

0 commit comments

Comments
 (0)