|
@@ -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
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|