Compare commits
1 Commits
master
...
developmen
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
026e2f05fe |
@ -34,9 +34,6 @@ import Foundation
|
||||
@_silgen_name("ytcpsocket_close") private func c_ytcpsocket_close(_ fd:Int32) -> Int32
|
||||
@_silgen_name("ytcpsocket_send") private func c_ytcpsocket_send(_ fd:Int32,buff:UnsafePointer<Byte>,len:Int32) -> Int32
|
||||
@_silgen_name("ytcpsocket_pull") private func c_ytcpsocket_pull(_ fd:Int32,buff:UnsafePointer<Byte>,len:Int32,timeout:Int32) -> Int32
|
||||
@_silgen_name("ytcpsocket_listen") private func c_ytcpsocket_listen(_ address:UnsafePointer<Int8>,port:Int32)->Int32
|
||||
@_silgen_name("ytcpsocket_accept") private func c_ytcpsocket_accept(_ onsocketfd:Int32,ip:UnsafePointer<Int8>,port:UnsafePointer<Int32>) -> Int32
|
||||
@_silgen_name("ytcpsocket_port") private func c_ytcpsocket_port(_ fd:Int32) -> Int32
|
||||
|
||||
open class TCPClient: Socket {
|
||||
|
||||
@ -137,50 +134,3 @@ open class TCPClient: Socket {
|
||||
return data
|
||||
}
|
||||
}
|
||||
|
||||
open class TCPServer: Socket {
|
||||
|
||||
open func listen() -> Result {
|
||||
let fd = c_ytcpsocket_listen(self.address, port: Int32(self.port))
|
||||
if fd > 0 {
|
||||
self.fd = fd
|
||||
|
||||
// If port 0 is used, get the actual port number which the server is listening to
|
||||
if (self.port == 0) {
|
||||
let p = c_ytcpsocket_port(fd)
|
||||
if (p == -1) {
|
||||
return .failure(SocketError.unknownError)
|
||||
} else {
|
||||
self.port = p
|
||||
}
|
||||
}
|
||||
|
||||
return .success
|
||||
} else {
|
||||
return .failure(SocketError.unknownError)
|
||||
}
|
||||
}
|
||||
|
||||
open func accept() -> TCPClient? {
|
||||
guard let serferfd = self.fd else { return nil }
|
||||
|
||||
var buff: [Int8] = [Int8](repeating: 0x0,count: 16)
|
||||
var port: Int32 = 0
|
||||
let clientfd: Int32 = c_ytcpsocket_accept(serferfd, ip: &buff, port: &port)
|
||||
|
||||
guard clientfd >= 0 else { return nil }
|
||||
guard let address = String(cString: buff, encoding: String.Encoding.utf8) else { return nil }
|
||||
|
||||
let client = TCPClient(address: address, port: port)
|
||||
client.fd = clientfd
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
open func close() {
|
||||
guard let fd: Int32=self.fd else { return }
|
||||
|
||||
_ = c_ytcpsocket_close(fd)
|
||||
self.fd = nil
|
||||
}
|
||||
}
|
||||
|
||||
83
Sources/TCPServer.swift
Normal file
83
Sources/TCPServer.swift
Normal file
@ -0,0 +1,83 @@
|
||||
//
|
||||
// Copyright (c) <2014>, skysent
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
// 1. Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
// 3. All advertising materials mentioning features or use of this software
|
||||
// must display the following acknowledgement:
|
||||
// This product includes software developed by skysent.
|
||||
// 4. Neither the name of the skysent nor the
|
||||
// names of its contributors may be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY skysent ''AS IS'' AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
// DISCLAIMED. IN NO EVENT SHALL skysent BE LIABLE FOR ANY
|
||||
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
@_silgen_name("ytcpsocket_listen") private func c_ytcpsocket_listen(_ address:UnsafePointer<Int8>,port:Int32)->Int32
|
||||
@_silgen_name("ytcpsocket_accept") private func c_ytcpsocket_accept(_ onsocketfd:Int32,ip:UnsafePointer<Int8>,port:UnsafePointer<Int32>) -> Int32
|
||||
@_silgen_name("ytcpsocket_port") private func c_ytcpsocket_port(_ fd:Int32) -> Int32
|
||||
@_silgen_name("ytcpsocket_close") private func c_ytcpsocket_close(_ fd:Int32) -> Int32
|
||||
|
||||
open class TCPServer: Socket {
|
||||
|
||||
open func listen() -> Result {
|
||||
let fd = c_ytcpsocket_listen(self.address, port: Int32(self.port))
|
||||
if fd > 0 {
|
||||
self.fd = fd
|
||||
|
||||
// If port 0 is used, get the actual port number which the server is listening to
|
||||
if (self.port == 0) {
|
||||
let p = c_ytcpsocket_port(fd)
|
||||
if (p == -1) {
|
||||
return .failure(SocketError.unknownError)
|
||||
} else {
|
||||
self.port = p
|
||||
}
|
||||
}
|
||||
|
||||
return .success
|
||||
} else {
|
||||
return .failure(SocketError.unknownError)
|
||||
}
|
||||
}
|
||||
|
||||
open func accept() -> TCPClient? {
|
||||
guard let serferfd = self.fd else { return nil }
|
||||
|
||||
var buff: [Int8] = [Int8](repeating: 0x0,count: 16)
|
||||
var port: Int32 = 0
|
||||
let clientfd: Int32 = c_ytcpsocket_accept(serferfd, ip: &buff, port: &port)
|
||||
|
||||
guard clientfd >= 0 else { return nil }
|
||||
guard let address = String(cString: buff, encoding: String.Encoding.utf8) else { return nil }
|
||||
|
||||
let client = TCPClient(address: address, port: port)
|
||||
client.fd = clientfd
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
open func close() {
|
||||
guard let fd: Int32=self.fd else { return }
|
||||
|
||||
_ = c_ytcpsocket_close(fd)
|
||||
self.fd = nil
|
||||
}
|
||||
}
|
||||
@ -30,8 +30,6 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
@_silgen_name("yudpsocket_server") func c_yudpsocket_server(_ host:UnsafePointer<Int8>,port:Int32) -> Int32
|
||||
@_silgen_name("yudpsocket_recive") func c_yudpsocket_recive(_ fd:Int32,buff:UnsafePointer<Byte>,len:Int32,ip:UnsafePointer<Int8>,port:UnsafePointer<Int32>) -> 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<Int8>,ip:UnsafePointer<Int8>) -> Int32
|
||||
@ -119,47 +117,3 @@ open class UDPClient: Socket {
|
||||
}
|
||||
//TODO add multycast and boardcast
|
||||
}
|
||||
|
||||
open class UDPServer: Socket {
|
||||
|
||||
public override init(address: String, port: Int32) {
|
||||
super.init(address: address, port: port)
|
||||
|
||||
let fd = c_yudpsocket_server(address, port: port)
|
||||
if fd > 0 {
|
||||
self.fd = fd
|
||||
}
|
||||
}
|
||||
|
||||
//TODO add multycast and boardcast
|
||||
open func recv(_ expectlen: Int) -> ([Byte]?, String, Int) {
|
||||
if let fd = self.fd {
|
||||
var buff: [Byte] = [Byte](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 address = ""
|
||||
if let ip = String(cString: remoteipbuff, encoding: String.Encoding.utf8) {
|
||||
address = ip
|
||||
}
|
||||
|
||||
if readLen <= 0 {
|
||||
return (nil, address, port)
|
||||
}
|
||||
|
||||
let rs = buff[0...Int(readLen-1)]
|
||||
let data: [Byte] = Array(rs)
|
||||
return (data, address, port)
|
||||
}
|
||||
|
||||
return (nil, "no ip", 0)
|
||||
}
|
||||
|
||||
open func close() {
|
||||
guard let fd = self.fd else { return }
|
||||
|
||||
_ = c_yudpsocket_close(fd)
|
||||
self.fd = nil
|
||||
}
|
||||
}
|
||||
|
||||
78
Sources/UDPServer.swift
Normal file
78
Sources/UDPServer.swift
Normal file
@ -0,0 +1,78 @@
|
||||
//
|
||||
// Copyright (c) <2014>, skysent
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
// 1. Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
// 3. All advertising materials mentioning features or use of this software
|
||||
// must display the following acknowledgement:
|
||||
// This product includes software developed by skysent.
|
||||
// 4. Neither the name of the skysent nor the
|
||||
// names of its contributors may be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY skysent ''AS IS'' AND ANY
|
||||
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
// DISCLAIMED. IN NO EVENT SHALL skysent BE LIABLE FOR ANY
|
||||
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
@_silgen_name("yudpsocket_server") func c_yudpsocket_server(_ host:UnsafePointer<Int8>,port:Int32) -> Int32
|
||||
@_silgen_name("yudpsocket_recive") func c_yudpsocket_recive(_ fd:Int32,buff:UnsafePointer<Byte>,len:Int32,ip:UnsafePointer<Int8>,port:UnsafePointer<Int32>) -> Int32
|
||||
|
||||
open class UDPServer: Socket {
|
||||
|
||||
public override init(address: String, port: Int32) {
|
||||
super.init(address: address, port: port)
|
||||
|
||||
let fd = c_yudpsocket_server(address, port: port)
|
||||
if fd > 0 {
|
||||
self.fd = fd
|
||||
}
|
||||
}
|
||||
|
||||
//TODO add multycast and boardcast
|
||||
open func recv(_ expectlen: Int) -> ([Byte]?, String, Int) {
|
||||
if let fd = self.fd {
|
||||
var buff: [Byte] = [Byte](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 address = ""
|
||||
if let ip = String(cString: remoteipbuff, encoding: String.Encoding.utf8) {
|
||||
address = ip
|
||||
}
|
||||
|
||||
if readLen <= 0 {
|
||||
return (nil, address, port)
|
||||
}
|
||||
|
||||
let rs = buff[0...Int(readLen-1)]
|
||||
let data: [Byte] = Array(rs)
|
||||
return (data, address, port)
|
||||
}
|
||||
|
||||
return (nil, "no ip", 0)
|
||||
}
|
||||
|
||||
open func close() {
|
||||
guard let fd = self.fd else { return }
|
||||
|
||||
_ = c_yudpsocket_close(fd)
|
||||
self.fd = nil
|
||||
}
|
||||
}
|
||||
@ -33,6 +33,12 @@
|
||||
375C48351DDC4C56008C701D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 375C48331DDC4C56008C701D /* Main.storyboard */; };
|
||||
375C48371DDC4C56008C701D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 375C48361DDC4C56008C701D /* Assets.xcassets */; };
|
||||
375C483A1DDC4C56008C701D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 375C48381DDC4C56008C701D /* LaunchScreen.storyboard */; };
|
||||
37DA08A81E1FDD6D00C88E31 /* TCPServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37DA08A71E1FDD6D00C88E31 /* TCPServer.swift */; };
|
||||
37DA08A91E1FDD6D00C88E31 /* TCPServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37DA08A71E1FDD6D00C88E31 /* TCPServer.swift */; };
|
||||
37DA08AA1E1FDD6D00C88E31 /* TCPServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37DA08A71E1FDD6D00C88E31 /* TCPServer.swift */; };
|
||||
37DA08B01E1FDE3500C88E31 /* UDPServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37DA08AF1E1FDE3500C88E31 /* UDPServer.swift */; };
|
||||
37DA08B11E1FDE3500C88E31 /* UDPServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37DA08AF1E1FDE3500C88E31 /* UDPServer.swift */; };
|
||||
37DA08B21E1FDE3500C88E31 /* UDPServer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37DA08AF1E1FDE3500C88E31 /* UDPServer.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
@ -55,6 +61,8 @@
|
||||
375C483B1DDC4C56008C701D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
||||
377DAA691DCDE40200009697 /* SwiftSocket.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftSocket.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
377DAA761DCDE45D00009697 /* SwiftSocket.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftSocket.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
37DA08A71E1FDD6D00C88E31 /* TCPServer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TCPServer.swift; path = Sources/TCPServer.swift; sourceTree = SOURCE_ROOT; };
|
||||
37DA08AF1E1FDE3500C88E31 /* UDPServer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UDPServer.swift; path = Sources/UDPServer.swift; sourceTree = SOURCE_ROOT; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
@ -89,22 +97,24 @@
|
||||
/* End PBXFrameworksBuildPhase section */
|
||||
|
||||
/* Begin PBXGroup section */
|
||||
375BC51D1DCDE86B006AD8F6 /* TCPSocket */ = {
|
||||
375BC51D1DCDE86B006AD8F6 /* TCP */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
372313941DCF84E30042DA87 /* TCPClient.swift */,
|
||||
37DA08A71E1FDD6D00C88E31 /* TCPServer.swift */,
|
||||
372313951DCF84E30042DA87 /* ytcpsocket.c */,
|
||||
);
|
||||
name = TCPSocket;
|
||||
name = TCP;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
375BC51E1DCDE878006AD8F6 /* UDPSocket */ = {
|
||||
375BC51E1DCDE878006AD8F6 /* UDP */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
372313901DCF84D80042DA87 /* UDPClient.swift */,
|
||||
37DA08AF1E1FDE3500C88E31 /* UDPServer.swift */,
|
||||
372313911DCF84D80042DA87 /* yudpsocket.c */,
|
||||
);
|
||||
name = UDPSocket;
|
||||
name = UDP;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
375C482E1DDC4C56008C701D /* iOS Example */ = {
|
||||
@ -156,8 +166,8 @@
|
||||
children = (
|
||||
372313981DCF84EC0042DA87 /* Result.swift */,
|
||||
372313991DCF84EC0042DA87 /* Socket.swift */,
|
||||
375BC51D1DCDE86B006AD8F6 /* TCPSocket */,
|
||||
375BC51E1DCDE878006AD8F6 /* UDPSocket */,
|
||||
375BC51D1DCDE86B006AD8F6 /* TCP */,
|
||||
375BC51E1DCDE878006AD8F6 /* UDP */,
|
||||
377DAA6A1DCDE40200009697 /* Supporting Files */,
|
||||
);
|
||||
name = Source;
|
||||
@ -357,9 +367,11 @@
|
||||
files = (
|
||||
37454EE31E06163D00800AE5 /* UDPClient.swift in Sources */,
|
||||
37454EE01E06163D00800AE5 /* Socket.swift in Sources */,
|
||||
37DA08B21E1FDE3500C88E31 /* UDPServer.swift in Sources */,
|
||||
37454EE41E06163D00800AE5 /* yudpsocket.c in Sources */,
|
||||
37454EDF1E06163D00800AE5 /* Result.swift in Sources */,
|
||||
37454EE21E06163D00800AE5 /* ytcpsocket.c in Sources */,
|
||||
37DA08AA1E1FDD6D00C88E31 /* TCPServer.swift in Sources */,
|
||||
37454EE11E06163D00800AE5 /* TCPClient.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
@ -379,9 +391,11 @@
|
||||
files = (
|
||||
372313931DCF84D80042DA87 /* yudpsocket.c in Sources */,
|
||||
3723139A1DCF84EC0042DA87 /* Result.swift in Sources */,
|
||||
37DA08B01E1FDE3500C88E31 /* UDPServer.swift in Sources */,
|
||||
3723139B1DCF84EC0042DA87 /* Socket.swift in Sources */,
|
||||
372313921DCF84D80042DA87 /* UDPClient.swift in Sources */,
|
||||
372313971DCF84E30042DA87 /* ytcpsocket.c in Sources */,
|
||||
37DA08A81E1FDD6D00C88E31 /* TCPServer.swift in Sources */,
|
||||
372313961DCF84E30042DA87 /* TCPClient.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
@ -392,9 +406,11 @@
|
||||
files = (
|
||||
37454EDC1E06163C00800AE5 /* UDPClient.swift in Sources */,
|
||||
37454ED91E06163C00800AE5 /* Socket.swift in Sources */,
|
||||
37DA08B11E1FDE3500C88E31 /* UDPServer.swift in Sources */,
|
||||
37454EDD1E06163C00800AE5 /* yudpsocket.c in Sources */,
|
||||
37454ED81E06163C00800AE5 /* Result.swift in Sources */,
|
||||
37454EDB1E06163C00800AE5 /* ytcpsocket.c in Sources */,
|
||||
37DA08A91E1FDD6D00C88E31 /* TCPServer.swift in Sources */,
|
||||
37454EDA1E06163C00800AE5 /* TCPClient.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user