Oliver преди 9 години
родител
ревизия
d01907c2aa
променени са 3 файла, в които са добавени 24 реда и са изтрити 11 реда
  1. 1 0
      Sources/HttpResponse.swift
  2. 4 0
      Sources/HttpServerIO.swift
  3. 19 11
      Sources/Socket.swift

+ 1 - 0
Sources/HttpResponse.swift

@@ -20,6 +20,7 @@ public protocol HttpResponseBodyWriter {
     func write(file: File) throws
     func write(file: File) throws
     func write(data: [UInt8]) throws
     func write(data: [UInt8]) throws
     func write(data: ArraySlice<UInt8>) throws
     func write(data: ArraySlice<UInt8>) throws
+    func write(data: NSData) throws
 }
 }
 
 
 public enum HttpResponseBody {
 public enum HttpResponseBody {

+ 4 - 0
Sources/HttpServerIO.swift

@@ -109,6 +109,10 @@ public class HttpServerIO {
         func write(data: ArraySlice<UInt8>) throws {
         func write(data: ArraySlice<UInt8>) throws {
             try socket.writeUInt8(data)
             try socket.writeUInt8(data)
         }
         }
+
+        func write(data: NSData) throws {
+            try socket.writeData(data)
+        }
     }
     }
     
     
     private func respond(socket: Socket, response: HttpResponse, keepAlive: Bool) throws -> Bool {
     private func respond(socket: Socket, response: HttpResponse, keepAlive: Bool) throws -> Bool {

+ 19 - 11
Sources/Socket.swift

@@ -96,18 +96,26 @@ public class Socket: Hashable, Equatable {
     
     
     public func writeUInt8(data: ArraySlice<UInt8>) throws {
     public func writeUInt8(data: ArraySlice<UInt8>) throws {
         try data.withUnsafeBufferPointer {
         try data.withUnsafeBufferPointer {
-            var sent = 0
-            while sent < data.count {
-                #if os(Linux)
-                    let s = send(self.socketFileDescriptor, $0.baseAddress + sent, Int(data.count - sent), Int32(MSG_NOSIGNAL))
-                #else
-                    let s = write(self.socketFileDescriptor, $0.baseAddress + sent, Int(data.count - sent))
-                #endif
-                if s <= 0 {
-                    throw SocketError.WriteFailed(Errno.description())
-                }
-                sent += s
+            try writeBuffer($0.baseAddress, length: data.count)
+        }
+    }
+
+    public func writeData(data: NSData) throws {
+        try writeBuffer(data.bytes, length: data.length)
+    }
+
+    private func writeBuffer(pointer: UnsafePointer<Void>, length: Int) throws {
+        var sent = 0
+        while sent < length {
+            #if os(Linux)
+                let s = send(self.socketFileDescriptor, pointer + sent, Int(length - sent), Int32(MSG_NOSIGNAL))
+            #else
+                let s = write(self.socketFileDescriptor, pointer + sent, Int(length - sent))
+            #endif
+            if s <= 0 {
+                throw SocketError.WriteFailed(Errno.description())
             }
             }
+            sent += s
         }
         }
     }
     }