Skip to content

Commit 888cf18

Browse files
authored
Merge pull request #40 from danshevluk/master
Breaking improvements
2 parents 484cf9f + 752c9d8 commit 888cf18

32 files changed

+1486
-1507
lines changed

.gitignore

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
*.xcuserdatad
2-
*.DS_Store
3-
*.pbxproj
4-
*.xcuserstate
5-
*.xccheckout
61
# Xcode
7-
.DS_Store
82
build/
93
*.pbxuser
104
!default.pbxuser
@@ -14,12 +8,36 @@ build/
148
!default.mode2v3
159
*.perspectivev3
1610
!default.perspectivev3
17-
*.xcworkspace
18-
!default.xcworkspace
1911
xcuserdata
20-
profile
12+
*.xccheckout
2113
*.moved-aside
2214
DerivedData
23-
.idea/
24-
# Pods - for those of you who use CocoaPods
25-
Pods
15+
*.hmap
16+
*.ipa
17+
*.xcuserstate
18+
19+
#OS X Stuff
20+
.localized
21+
.DS_Store
22+
*.zip
23+
24+
# Pods
25+
Pods/
26+
27+
# Editors
28+
.idea
29+
*.swp
30+
31+
# Fastlane
32+
fastlane/report.xml
33+
*.mobileprovision
34+
*.certSigningRequest
35+
*.cer
36+
37+
# Keys
38+
*.pem
39+
*.pkey
40+
*.p12
41+
42+
# Rbenv
43+
.ruby-version

.swift-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.0

README.md

Lines changed: 54 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,91 @@
1-
# a simple socket library for apple swift lang
2-
# usage
3-
> drag ysocket.c and ysocket.swift to your project
4-
> just use apis in YSocket class
1+
# SwiftSocket
2+
SwiftSocket library provides as easy to use interface for socket based connections on server or client side. Supports both TCP and UDP sockets.
53

6-
# api usage
7-
## create client socket
8-
``` swift
9-
//create a socket connect to www.apple.com and port at 80
10-
var client:TCPClient = TCPClient(addr: "www.apple.com", port: 80)
11-
```
12-
## connect with timeout
13-
``` swift
14-
var (success, errmsg) = client.connect(timeout: 10)
4+
# Installation
5+
## Cocoapods
6+
Add this to your `Podfile`:
7+
```ruby
8+
pod 'SwiftSocket'
159
```
10+
And run then `pod install`
1611

17-
## send data
18-
``` swift
19-
var (success, errmsg) = client.send(str:"GET / HTTP/1.0\n\n")
20-
//or you can send binnary data
21-
//socket.send(data:[Int8])
22-
```
12+
# Code examples
2313

24-
## read data
14+
## Create client socket
2515
``` swift
26-
var data = client.read(1024*10) //return optional [Int8]
16+
// Create a socket connect to www.apple.com and port at 80
17+
let client = TCPClient(address: "www.apple.com", port: 80)
2718
```
28-
29-
## close socket
19+
## Connect with timeout
20+
You can also set timeout to `-1` or leave parameters empty (`client.connect()`) to turn off connection timeout.
3021
``` swift
31-
var (success, errormsg) = client.close()
22+
switch client.connect(timeout: 10) {
23+
case .success:
24+
// Connection successful 🎉
25+
case .failure(let error):
26+
// 💩
27+
}
3228
```
3329

34-
## create servert socket
35-
30+
## Send data
3631
``` swift
37-
var server:TCPServer = TCPServer(addr: "127.0.0.1", port: 8080)
32+
let data: Data = // ... Bytes you want to send
33+
let result = client.send(data: data)
3834
```
3935

40-
## listen
41-
36+
## Read data
4237
``` swift
43-
var (success, msg) = server.listen()
38+
var data = client.read(1024*10) //return optional [Int8]
4439
```
45-
### accept
40+
41+
## Close socket
4642
``` swift
47-
var client = server.accept() //now you can use client socket api to read and write
43+
client.close()
4844
```
4945

50-
# client socket example
46+
## Client socket example
5147
``` swift
52-
//创建socket
53-
var client:TCPClient = TCPClient(addr: "www.apple.com", port: 80)
54-
//连接
55-
var (success, errmsg) = client.connect(timeout: 1)
56-
if success {
57-
//发送数据
58-
var (success, errmsg) = client.send(str:"GET / HTTP/1.0\n\n" )
59-
if success {
60-
//读取数据
61-
var data = client.read(1024*10)
62-
if let d = data {
63-
if let str = String.stringWithBytes(d, length: d.count, encoding: NSUTF8StringEncoding){
64-
println(str)
65-
}
48+
let client = TCPClient(address: "www.apple.com", port: 80)
49+
switch client.connect(timeout: 1) {
50+
case .success:
51+
switch client.send(string: "GET / HTTP/1.0\n\n" ) {
52+
case .success:
53+
guard let data = client.read(1024*10) else { return }
54+
55+
if let response = String(bytes: data, encoding: .utf8) {
56+
print(response)
6657
}
67-
}else {
68-
println(errmsg)
58+
case .failure(let error):
59+
print(error)
6960
}
70-
} else {
71-
println(errmsg)
61+
case .failure(let error):
62+
print(error)
7263
}
64+
7365
```
7466

75-
# server socket example (echo server)
67+
## Server socket example (echo server)
7668
``` swift
77-
func echoService(client c:TCPClient) {
78-
println("newclient from:\(c.addr)[\(c.port)]")
69+
func echoService(client: TCPClient) {
70+
print("Newclient from:\(c.address)[\(c.port)]")
7971
var d = c.read(1024*10)
8072
c.send(data: d!)
8173
c.close()
8274
}
83-
func testserver(){
84-
var server:TCPServer = TCPServer(addr: "127.0.0.1", port: 8080)
85-
var (success, msg) = server.listen()
86-
if success {
75+
76+
func testServer() {
77+
let server = TCPServer(address: "127.0.0.1", port: 8080)
78+
switch server.listen() {
79+
case .success:
8780
while true {
8881
if var client = server.accept() {
8982
echoService(client: client)
9083
} else {
91-
println("accept error")
84+
print("accept error")
9285
}
9386
}
94-
} else {
95-
println(msg)
87+
case .failure(let error):
88+
print(error)
9689
}
9790
}
9891
```
99-
100-
# Copyright and License
101-
Code released under the BSD license.
102-
103-
# QQ group
104-
275935304

ysocket-ios/Info.plist renamed to Sources/Info.plist

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.0</string>
19-
<key>CFBundleSignature</key>
20-
<string>????</string>
18+
<string>1.1</string>
2119
<key>CFBundleVersion</key>
2220
<string>$(CURRENT_PROJECT_VERSION)</string>
2321
<key>NSPrincipalClass</key>

Sources/Result.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import Foundation
2+
3+
public enum Result {
4+
case success
5+
case failure(Error)
6+
7+
public var isSuccess: Bool {
8+
switch self {
9+
case .success:
10+
return true
11+
case .failure:
12+
return false
13+
}
14+
}
15+
16+
public var isFailure: Bool {
17+
return !isSuccess
18+
}
19+
20+
public var error: Error? {
21+
switch self {
22+
case .success:
23+
return nil
24+
case .failure(let error):
25+
return error
26+
}
27+
}
28+
}

Sources/Socket.swift

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// Copyright (c) <2014>, skysent
3+
// All rights reserved.
4+
//
5+
// Redistribution and use in source and binary forms, with or without
6+
// modification, are permitted provided that the following conditions are met:
7+
// 1. Redistributions of source code must retain the above copyright
8+
// notice, this list of conditions and the following disclaimer.
9+
// 2. Redistributions in binary form must reproduce the above copyright
10+
// notice, this list of conditions and the following disclaimer in the
11+
// documentation and/or other materials provided with the distribution.
12+
// 3. All advertising materials mentioning features or use of this software
13+
// must display the following acknowledgement:
14+
// This product includes software developed by skysent.
15+
// 4. Neither the name of the skysent nor the
16+
// names of its contributors may be used to endorse or promote products
17+
// derived from this software without specific prior written permission.
18+
//
19+
// THIS SOFTWARE IS PROVIDED BY skysent ''AS IS'' AND ANY
20+
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
// DISCLAIMED. IN NO EVENT SHALL skysent BE LIABLE FOR ANY
23+
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25+
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26+
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
//
30+
31+
import Foundation
32+
33+
public typealias Byte = UInt8
34+
35+
open class Socket {
36+
37+
public let address: String
38+
public let port: Int32
39+
public var fd: Int32?
40+
41+
public init(address: String, port: Int32) {
42+
self.address = address
43+
self.port = port
44+
}
45+
46+
}
47+
48+
public enum SocketError: Error {
49+
case queryFailed
50+
case connectionClosed
51+
case connectionTimeout
52+
case unknownError
53+
}

Sources/SwiftSocket.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#import <UIKit/UIKit.h>
2+
3+
//! Project version number for SwiftSocket
4+
FOUNDATION_EXPORT double SwiftSocketVersionNumber;
5+
6+
//! Project version string for SwiftSocket
7+
FOUNDATION_EXPORT const unsigned char SwiftSocketVersionString[];
8+
9+
// In this header, you should import all the public headers of your framework using statements like #import <SwiftSocket_iOS/PublicHeader.h>

0 commit comments

Comments
 (0)