Skip to content

Commit 4616896

Browse files
committed
Merge branch 'stable'
2 parents 7201026 + 243f0a3 commit 4616896

8 files changed

+301
-38
lines changed

Trevi.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@
217217
23D2F3991C1DA4A000F0C784 /* views */,
218218
239411E41C0C2E19001B9B07 /* AppDelegate.swift */,
219219
239411E61C0C2E19001B9B07 /* ViewController.swift */,
220+
239412131C0DECC2001B9B07 /* Lime.swift */,
220221
239411E81C0C2E19001B9B07 /* Assets.xcassets */,
221222
239411EA1C0C2E19001B9B07 /* Main.storyboard */,
222223
239411ED1C0C2E19001B9B07 /* Info.plist */,
223-
239412131C0DECC2001B9B07 /* Lime.swift */,
224224
);
225225
path = Trevi_ver_lime;
226226
sourceTree = "<group>";

Trevi/Http/HttpSocket.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public class TreviSocket {
1919
func sendData ( data: NSData ) {
2020

2121
socket.write (data, queue: dispatch_get_main_queue())
22-
2322
socket.close ()
2423
}
2524
}
@@ -32,7 +31,7 @@ class TreviSocketServer {
3231

3332
func startOnPort ( p: Int ) throws {
3433

35-
guard let socket = ListenSocket<IPv4> ( address: IPv4 ( port: p )) else {
34+
guard let socket = ListenSocket<IPv4> ( address: IPv4 (port: p)) else {
3635
// Should handle Listener error
3736
return
3837
}

Trevi/Socket/ConnectedSocket.swift

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ import Darwin
1010

1111
let ClientBufferSize = 4096
1212

13-
// Should add connect function
13+
/**
14+
* ConnectedSocket class
15+
*
16+
* Manage a tcp client socket, and provide read, write functions.
17+
*
18+
* Should add connect function.
19+
*
20+
*/
1421
public class ConnectedSocket<T: InetAddress> : Socket<T> {
1522

1623
var bufferPtr = UnsafeMutablePointer<CChar>.alloc(ClientBufferSize + 2)
@@ -19,7 +26,19 @@ public class ConnectedSocket<T: InetAddress> : Socket<T> {
1926
var isConnected : Bool = false
2027
var isClosing : Bool = false
2128

22-
// Accept client socket
29+
/**
30+
* init?
31+
* After accept, create a client socket,
32+
*
33+
* @param
34+
* First : Client socket's file descriptor.
35+
* Second : Client socket's address family.
36+
* Third : A dispatch queue for this socket's read event.
37+
*
38+
* @return
39+
* If bind function succeeds, create a client socket.
40+
* However, if it fails, returns nil
41+
*/
2342
public init?(fd : Int32, address : T, queue : dispatch_queue_t = defaultQueue) {
2443

2544
super.init(fd: fd, address: address)
@@ -49,25 +68,25 @@ public class ConnectedSocket<T: InetAddress> : Socket<T> {
4968

5069
super.close()
5170
}
52-
53-
func accept() -> (Int32, T) {
54-
var clientAddr = T()
55-
var clientAddrLen = socklen_t(T.length)
56-
57-
let clientFd = withUnsafeMutablePointer(&clientAddr) {
58-
ptr -> Int32 in
59-
let addrPtr = UnsafeMutablePointer<sockaddr>(ptr)
60-
return Darwin.accept(self.fd, addrPtr, &clientAddrLen);
61-
}
62-
63-
return (clientFd, clientAddr)
64-
}
6571

6672
// Should add connect()
6773

74+
/**
75+
* read
76+
* Read recived data from this socket.
77+
*
78+
* @param
79+
*
80+
* @return
81+
* First : Read length.
82+
* Second : Read data buffer pointer.
83+
*/
6884
public func read() -> (length: Int, buffer: UnsafePointer<CChar>) {
6985
let readBufferPtr = UnsafePointer<CChar>(bufferPtr)
7086
let readBufferLen = Darwin.read(fd, bufferPtr, bufferLen)
87+
88+
// print(length)
89+
// print(blockToString(buffer, length: length))
7190

7291
guard readBufferLen >= 0 else {
7392
bufferPtr[0] = 0
@@ -79,6 +98,20 @@ public class ConnectedSocket<T: InetAddress> : Socket<T> {
7998
return ( readBufferLen, readBufferPtr )
8099
}
81100

101+
/**
102+
* write
103+
* Write response data to this socket.
104+
*
105+
* @param
106+
* First : Data buffer pointer.
107+
* Second : Data length.
108+
* Third : A dispatch queue for write event.
109+
* If you use dispatch_get_main_queue(), all write event in this program
110+
* will be processed by main thread.
111+
*
112+
* @return
113+
* Success or failure.
114+
*/
82115
public func write<M>(buffer: UnsafePointer<M>, length : Int,
83116
queue : dispatch_queue_t = defaultQueue) -> Bool {
84117

Trevi/Socket/EventHandler.swift

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,32 @@
88

99
import Dispatch
1010

11-
// For single thread server model
12-
//public let defaultQueue = dispatch_get_main_queue()
1311

14-
// For multi thread server model (It does not mean blocking, just for parallelizing)
12+
/**
13+
* Set the 'defaultQueue' to 'dispatch_get_main_queue()' for single thread server model.
14+
* Then all event will dispatch to main queue in GCD, and all tasks will be processed by main thread.
15+
* Examples:
16+
* public let defaultQueue = dispatch_get_main_queue()
17+
*
18+
* On the other hand if you want multi thread server model and more parallelizing,
19+
* set the defaultQueue to dispatch_get_global_queue(_ , 0)
20+
*
21+
*/
1522
public let defaultQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
1623

1724
// Below codes are prototype for injecting read event according with Socket's states.
1825
// I think it's not good way, should find better way
1926
public typealias readCallbackType = (() -> Int)
2027

28+
29+
/**
30+
* ReadEvent
31+
* Socket's read event protocol
32+
*
33+
* Set read event type in EventHandler according with block and non-block.
34+
* These classes will be injected when socket's isNonblocking sets.
35+
*
36+
*/
2137
public protocol ReadEvent {
2238
func excute(callback : readCallbackType) -> Bool
2339
}
@@ -36,6 +52,15 @@ public class NonBlockingRead : ReadEvent {
3652
}
3753
}
3854

55+
56+
/**
57+
* EventHandler class
58+
*
59+
* Manage all socket' read and write event based on GCD.
60+
*
61+
* Should change this module to be working in Linux.
62+
*
63+
*/
3964
public class EventHandler {
4065

4166
let fd : Int32
@@ -74,6 +99,30 @@ public class EventHandler {
7499
return false
75100
}
76101

102+
103+
/**
104+
* dispatchReadEvent
105+
* Dispatch socket's read event.
106+
*
107+
* Examples:
108+
* eventHandle.dispatchReadEvent(){
109+
*
110+
* let (count, buffer) = clientSocket.read()
111+
*
112+
* clientSocket.write(buffer, length: count, queue: dispatch_get_main_queue())
113+
*
114+
* return count
115+
* }
116+
*
117+
* @param
118+
* First : Should be readCallbackType and return read length. If return 0 this EventHandler's
119+
* read event will stop, and parent's socket will deinit. Don't be worry about strong
120+
* reference. All parent's properties will be destroyed.
121+
*
122+
*
123+
* @return
124+
* Success or failure.
125+
*/
77126
public func dispatchReadEvent(callback : readCallbackType) -> Bool {
78127
source = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ,
79128
UInt(fd), 0, queue)
@@ -96,6 +145,23 @@ public class EventHandler {
96145
}
97146
}
98147

148+
/**
149+
* dispatchWriteEvent
150+
* Write response data to this socket.
151+
*
152+
* Examples:
153+
* eventHandle.dispatchWriteEvent(buffer, length : length) {
154+
* if self.isClosing { self.close() }
155+
* }
156+
*
157+
* @param
158+
* First : Data buffer pointer<data type>.
159+
* Second : Daga length.
160+
* Third : Close event to prevent socket closing before writting.
161+
*
162+
* @return
163+
* Success or failure.
164+
*/
99165
public func dispatchWriteEvent<M>(buffer : UnsafePointer<M>,
100166
length : Int, closeSocket : ()->() ) -> Bool {
101167

Trevi/Socket/InetAddress.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,16 @@ extension sockaddr_in : InetAddress {
3131
public static let length = __uint8_t(sizeof(IPv4))
3232
public func port() -> in_port_t { return in_port_t(ntohs(self.sin_port)) }
3333

34-
public init(address : in_addr = INADDR_ANY, port : Int = 0) {
34+
35+
public init(address : String, port : Int){
36+
sin_len = __uint8_t(sizeof(sockaddr_in))
37+
sin_family = sa_family_t(AF_INET)
38+
sin_port = in_port_t(htons(CUnsignedShort(port)))
39+
sin_addr = in_addr(s_addr: inet_addr(address))
40+
sin_zero = (0,0,0,0,0,0,0,0)
41+
}
42+
43+
public init(address : in_addr = INADDR_ANY, port : Int) {
3544
sin_len = __uint8_t(sizeof(sockaddr_in))
3645
sin_family = sa_family_t(AF_INET)
3746
sin_port = in_port_t(htons(CUnsignedShort(port)))

Trevi/Socket/ListenSocket.swift

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,28 @@
99
import Darwin
1010
import Dispatch
1111

12+
/**
13+
* ListenSocket class
14+
*
15+
* Manage a tcp listen socket, and accept client socket.
16+
*
17+
*/
1218
public class ListenSocket<T: InetAddress> : Socket<T> {
1319

1420
var isListening : Bool = false
1521

16-
// Create socket and bind address
22+
/**
23+
* init?
24+
* Create a listen socket,
25+
*
26+
* @param
27+
* First : Listen socket's address family.
28+
* Second : A dispatch queue for this socket's read event.
29+
*
30+
* @return
31+
* If bind function succeeds, calls super.init().
32+
* However, if it fails, returns nil
33+
*/
1734
public init?(address : T, queue : dispatch_queue_t = defaultQueue) {
1835

1936
let fd = socket(T.domain, SOCK_STREAM, 0)
@@ -34,6 +51,17 @@ public class ListenSocket<T: InetAddress> : Socket<T> {
3451
self.close()
3552
}
3653

54+
/**
55+
* listen
56+
* Listen client sockets
57+
*
58+
* @param
59+
* First : Socket's nonBlock mode.
60+
* Second : Backlog queue setting. Handle client's concurrent connect requests.
61+
*
62+
* @return
63+
* Success or failure
64+
*/
3765
// Should extract nonBlock input, and move to Server Model Module
3866
public func listen(nonBlock : Bool, backlog : Int32 = 50) -> Bool {
3967
guard !isListening else { return false }
@@ -49,6 +77,16 @@ public class ListenSocket<T: InetAddress> : Socket<T> {
4977
return self.isListening
5078
}
5179

80+
/**
81+
* accept
82+
* Accept client request.
83+
*
84+
* @param
85+
*
86+
* @return
87+
* First : Client's file descriptor.
88+
* Second : Client's address family.
89+
*/
5290
public func accept() -> (Int32, T) {
5391
var clientAddr = T()
5492
var clientAddrLen = socklen_t(T.length)
@@ -62,6 +100,34 @@ public class ListenSocket<T: InetAddress> : Socket<T> {
62100
return (clientFd, clientAddr)
63101
}
64102

103+
/**
104+
* listenClientEvent
105+
* Listen client sockets, and dispatch client event.
106+
*
107+
* Examples:
108+
* let server: ListenSocket? = ListenSocket(address: IPv4(port: 8080))
109+
*
110+
* server!.listenClientEvent(false) {
111+
* clientSocket in
112+
*
113+
* clientSocket.eventHandle.dispatchReadEvent(){
114+
*
115+
* let (count, buffer) = clientSocket.read()
116+
*
117+
* clientSocket.write(buffer, length: count, queue: dispatch_get_main_queue())
118+
*
119+
* return count
120+
* }
121+
* }
122+
*
123+
* @param
124+
* First : Socket's nonBlock mode.
125+
* Second : Backlog queue setting. Handle client's concurrent connect requests.
126+
* Third : Client socket's callback after it is created.
127+
*
128+
* @return
129+
* Success or failure
130+
*/
65131
public func listenClientEvent(nonBlock : Bool, backlog : Int32 = 50,
66132
clientCallback: (ConnectedSocket<T>) -> Void) -> Bool {
67133

@@ -90,6 +156,33 @@ public class ListenSocket<T: InetAddress> : Socket<T> {
90156
return true
91157
}
92158

159+
160+
/**
161+
* listenClientReadEvent
162+
* Listen client sockets, and dispatch client event.
163+
*
164+
* Examples:
165+
* let server: ListenSocket? = ListenSocket(address: IPv4(port: 8080))
166+
*
167+
* server!.listenClientReadEvent(false) {
168+
* clientSocket in
169+
*
170+
* let (length, buffer) = clientSocket.read()
171+
*
172+
* clientSocket.write(buffer, length: length, queue: dispatch_get_main_queue())
173+
*
174+
* return count
175+
* }
176+
*
177+
* @param
178+
* First : Socket's nonBlock mode.
179+
* Second : Backlog queue setting. Handle client's concurrent connect requests.
180+
* Third : Client socket's read callback when a client socket get a request.
181+
* In this closure, you should return read length, so if 0 value return socket will be closed.
182+
*
183+
* @return
184+
* Success or failure.
185+
*/
93186
public func listenClientReadEvent(nonBlock : Bool, backlog : Int32 = 50,
94187
clientReadCallback: (ConnectedSocket<T>) -> Int) -> Bool {
95188

0 commit comments

Comments
 (0)