@@ -30,21 +30,21 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
30
31
31
import Foundation
32
32
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
36
36
@_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 )
40
40
41
- public class UDPClient: YSocket {
41
+ open class UDPClient: YSocket {
42
42
public override init( addr a: String, port p: Int) {
43
43
super. init ( )
44
- let remoteipbuff : [ Int8 ] = [ Int8] ( count : 16 , repeatedValue : 0x0 )
44
+ let remoteipbuff : [ Int8 ] = [ Int8] ( repeating : 0x0 , count : 16 )
45
45
let ret = c_yudpsocket_get_server_ip ( a, ip: remoteipbuff)
46
46
if ret== 0 {
47
- if let ip= String ( CString : remoteipbuff, encoding: NSUTF8StringEncoding ) {
47
+ if let ip= String ( cString : remoteipbuff, encoding: String . Encoding . utf8 ) {
48
48
self . addr= ip
49
49
self . port= p
50
50
let fd : Int32 = c_yudpsocket_client ( )
@@ -58,7 +58,7 @@ public class UDPClient: YSocket {
58
58
* send data
59
59
* return success or fail with message
60
60
*/
61
- public func send( data d: [ UInt8 ] ) -> ( Bool , String ) {
61
+ open func send( data d: [ UInt8 ] ) -> ( Bool , String ) {
62
62
if let fd: Int32 = self . fd{
63
63
let sendsize : Int32 = c_yudpsocket_sentto ( fd, buff: d, len: Int32 ( d. count) , ip: self . addr, port: Int32 ( self . port) )
64
64
if Int ( sendsize) == d. count{
@@ -74,7 +74,7 @@ public class UDPClient: YSocket {
74
74
* send string
75
75
* return success or fail with message
76
76
*/
77
- public func send( str s: String ) -> ( Bool , String ) {
77
+ open func send( str s: String ) -> ( Bool , String ) {
78
78
if let fd: Int32 = self . fd{
79
79
let sendsize : Int32 = c_yudpsocket_sentto ( fd, buff: s, len: Int32 ( strlen ( s) ) , ip: self . addr, port: Int32 ( self . port) )
80
80
if sendsize== Int32 ( strlen ( s) ) {
@@ -89,7 +89,7 @@ public class UDPClient: YSocket {
89
89
/*
90
90
* enableBroadcast
91
91
*/
92
- public func enableBroadcast( ) {
92
+ open func enableBroadcast( ) {
93
93
if let fd: Int32 = self . fd{
94
94
c_enable_broadcast ( fd)
95
95
@@ -99,12 +99,12 @@ public class UDPClient: YSocket {
99
99
*
100
100
* send nsdata
101
101
*/
102
- public func send( data d: NSData ) -> ( Bool , String ) {
102
+ open func send( data d: Data ) -> ( Bool , String ) {
103
103
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 ) {
108
108
return ( true , " send success " )
109
109
} else {
110
110
return ( false , " send error " )
@@ -113,7 +113,7 @@ public class UDPClient: YSocket {
113
113
return ( false , " socket not open " )
114
114
}
115
115
}
116
- public func close( ) -> ( Bool , String ) {
116
+ open func close( ) -> ( Bool , String ) {
117
117
if let fd: Int32 = self . fd{
118
118
c_yudpsocket_close ( fd)
119
119
self . fd= nil
@@ -125,7 +125,7 @@ public class UDPClient: YSocket {
125
125
//TODO add multycast and boardcast
126
126
}
127
127
128
- public class UDPServer : YSocket {
128
+ open class UDPServer : YSocket {
129
129
public override init ( addr a: String , port p: Int ) {
130
130
super. init ( addr: a, port: p)
131
131
let fd : Int32 = c_yudpsocket_server ( self . addr, port: Int32 ( self . port) )
@@ -134,15 +134,15 @@ public class UDPServer:YSocket{
134
134
}
135
135
}
136
136
//TODO add multycast and boardcast
137
- public func recv( expectlen: Int ) -> ( [ UInt8 ] ? , String , Int ) {
137
+ open func recv( _ expectlen: Int ) -> ( [ UInt8 ] ? , String , Int ) {
138
138
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 )
141
141
var remoteport : Int32 = 0
142
142
let readLen : Int32 = c_yudpsocket_recive ( fd, buff: buff, len: Int32 ( expectlen) , ip: & remoteipbuff, port: & remoteport)
143
143
let port : Int = Int ( remoteport)
144
144
var addr : String = " "
145
- if let ip= String ( CString : remoteipbuff, encoding: NSUTF8StringEncoding ) {
145
+ if let ip= String ( cString : remoteipbuff, encoding: String . Encoding . utf8 ) {
146
146
addr= ip
147
147
}
148
148
if readLen<= 0 {
@@ -154,7 +154,7 @@ public class UDPServer:YSocket{
154
154
}
155
155
return ( nil , " no ip " , 0 )
156
156
}
157
- public func close( ) -> ( Bool , String ) {
157
+ open func close( ) -> ( Bool , String ) {
158
158
if let fd: Int32 = self . fd{
159
159
c_yudpsocket_close ( fd)
160
160
self . fd= nil
0 commit comments