Просмотр исходного кода

Merge branch 'stable' of github.com:glock45/swifter into stable

Damian Kołakowski 10 лет назад
Родитель
Сommit
a8e2a529d4
4 измененных файлов с 56 добавлено и 11 удалено
  1. 45 3
      Sources/HttpServerIO.swift
  2. 6 6
      Sources/Process.swift
  3. 1 1
      Sources/Socket.swift
  4. 4 1
      XCode/SwifterSampleOSX/main.swift

+ 45 - 3
Sources/HttpServerIO.swift

@@ -11,17 +11,58 @@
     import Foundation
 #endif
 
-
 public class HttpServerIO {
     
+    public enum ServerStatus {
+        case Stopped
+        case Running
+    }
+    
     private var listenSocket: Socket = Socket(socketFileDescriptor: -1)
+    private var listenPort: in_port_t = 8080
+    private var ipv4 = false
+    private var listenPriority: Int = DISPATCH_QUEUE_PRIORITY_BACKGROUND
+    private var serverStatus: ServerStatus = .Stopped
+    
     private var clientSockets: Set<Socket> = []
     private let clientSocketsLock = NSLock()
     
-    public func start(listenPort: in_port_t = 8080, forceIPv4: Bool = false, priority: Int = DISPATCH_QUEUE_PRIORITY_BACKGROUND) throws {
+    // Returns the port used by the server for listening connection.
+    public var port: Int {
+        get {
+            return Int(listenPort)
+        }
+    }
+    
+    // True if the IPv4 has been forced on start.
+    public var forcedIPv4: Bool {
+        get {
+            return ipv4
+        }
+    }
+    
+    // Returns the priority used for dispatch
+    public var priority: Int {
+        get {
+            return listenPriority
+        }
+    }
+    
+    // Returns the server status (Running or not).
+    public var status: ServerStatus {
+        get {
+            return serverStatus
+        }
+    }
+    
+    public func start(port: in_port_t = 8080, forceIPv4: Bool = false, priority: Int = DISPATCH_QUEUE_PRIORITY_BACKGROUND) throws {
         stop()
-        listenSocket = try Socket.tcpSocketForListen(listenPort, forceIPv4: forceIPv4)
+        self.listenSocket = try Socket.tcpSocketForListen(port, forceIPv4: forceIPv4)
+        self.listenPort = try self.listenSocket.port()
+        self.ipv4 = forceIPv4
+        self.listenPriority = priority
         dispatch_async(dispatch_get_global_queue(priority, 0)) {
+            self.serverStatus = .Running
             while let socket = try? self.listenSocket.acceptClientSocket() {
                 self.lock(self.clientSocketsLock) {
                     self.clientSockets.insert(socket)
@@ -34,6 +75,7 @@ public class HttpServerIO {
                 })
             }
             self.stop()
+            self.serverStatus = .Stopped
         }
     }
     

+ 6 - 6
Sources/Process.swift

@@ -18,12 +18,12 @@ public class Process {
     }
     
     public static var TID: UInt64 {
-	#if os(Linux)
-        return UInt64(pthread_self())
-	#else
-        var tid: __uint64_t = 0
-        pthread_threadid_np(nil, &tid);
-        return UInt64(tid)
+        #if os(Linux)
+            return UInt64(pthread_self())
+        #else
+            var tid: __uint64_t = 0
+            pthread_threadid_np(nil, &tid);
+            return UInt64(tid)
         #endif
     }
     

+ 1 - 1
Sources/Socket.swift

@@ -144,7 +144,7 @@ public class Socket: Hashable, Equatable {
             #if os(Linux)
                 return ntohs(addr.sin_port)
             #else
-                return Int(OSHostByteOrder()) == OSLittleEndian ? addr.sin_port.littleEndian : addr.sin_port.bigEndian
+                return Int(OSHostByteOrder()) != OSLittleEndian ? addr.sin_port.littleEndian : addr.sin_port.bigEndian
             #endif
         }
     }

+ 4 - 1
XCode/SwifterSampleOSX/main.swift

@@ -12,9 +12,12 @@ do {
     server["/testAfterBaseRoute"] = { request in
         return .OK(.Html("ok !"))
     }
+    
     try server.start(9080, forceIPv4: true)
-    print("Server has started ( port = 9080 ). Try to connect now...")
+    print("Server has started ( port = \(server.port) ). Try to connect now...")
+    
     NSRunLoop.mainRunLoop().run()
+    
 } catch {
     print("Server start error: \(error)")
 }