Skip to content

Commit dba96ab

Browse files
authored
Merge pull request #15 from shmuelk/asynch_write
Add support for non-blocking writes to TCP sockets
2 parents 7b64a42 + 23516f4 commit dba96ab

2 files changed

Lines changed: 42 additions & 19 deletions

File tree

Sources/Socket.swift

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,9 +1992,12 @@ public class Socket: SocketReader, SocketWriter {
19921992
///
19931993
/// - Parameters:
19941994
/// - buffer: The buffer containing the data to write.
1995-
/// - bufSize: The size of the buffer.
1995+
/// - bufSize: The size of the buffer.
1996+
///
1997+
/// - Returns: Integer representing the number of bytes written.
19961998
///
1997-
public func write(from buffer: UnsafePointer<Void>, bufSize: Int) throws {
1999+
@discardableResult
2000+
public func write(from buffer: UnsafePointer<Void>, bufSize: Int) throws -> Int {
19982001

19992002
// Make sure the buffer is valid...
20002003
if bufSize == 0 {
@@ -2047,57 +2050,71 @@ public class Socket: SocketReader, SocketWriter {
20472050
#endif
20482051
}
20492052
if s <= 0 {
2053+
if errno == EAGAIN && !isBlocking {
2054+
// We have written out as much as we can
2055+
return sent
2056+
}
20502057

20512058
throw Error(code: Socket.SOCKET_ERR_WRITE_FAILED, reason: self.lastError())
20522059
}
20532060
sent += s
20542061
}
2062+
return sent
20552063
}
20562064

20572065
///
20582066
/// Write data to the socket.
20592067
///
2060-
/// - Parameter data: The NSData object containing the data to write.
2068+
/// - Parameter data: The NSData object containing the data to write.
2069+
///
2070+
/// - Returns: Integer representing the number of bytes written.
20612071
///
2062-
public func write(from data: NSData) throws {
2072+
@discardableResult
2073+
public func write(from data: NSData) throws -> Int {
20632074

20642075
// If there's no data in the NSData object, why bother? Fail silently...
20652076
if data.length == 0 {
2066-
return
2077+
return 0
20672078
}
20682079

2069-
try write(from: data.bytes, bufSize: data.length)
2080+
return try write(from: data.bytes, bufSize: data.length)
20702081
}
20712082

20722083
///
20732084
/// Write data to the socket.
20742085
///
2075-
/// - Parameter data: The Data object containing the data to write.
2086+
/// - Parameter data: The Data object containing the data to write.
2087+
///
2088+
/// - Returns: Integer representing the number of bytes written.
20762089
///
2077-
public func write(from data: Data) throws {
2090+
@discardableResult
2091+
public func write(from data: Data) throws -> Int {
20782092

20792093
// If there's no data in the Data object, why bother? Fail silently...
20802094
if data.count == 0 {
2081-
return
2095+
return 0
20822096
}
20832097

2084-
try data.withUnsafeBytes() { [unowned self] (buffer: UnsafePointer<UInt8>) throws in
2098+
return try data.withUnsafeBytes() { [unowned self] (buffer: UnsafePointer<UInt8>) throws -> Int in
20852099

2086-
try self.write(from: buffer, bufSize: data.count)
2100+
return try self.write(from: buffer, bufSize: data.count)
20872101
}
20882102
}
20892103

20902104
///
20912105
/// Write a string to the socket.
20922106
///
2093-
/// - Parameter string: The string to write.
2107+
/// - Parameter string: The string to write.
2108+
///
2109+
/// - Returns: Integer representing the number of bytes written.
20942110
///
2095-
public func write(from string: String) throws {
2111+
@discardableResult
2112+
public func write(from string: String) throws -> Int {
20962113

2097-
try string.nulTerminatedUTF8.withUnsafeBufferPointer() {
2114+
return try string.nulTerminatedUTF8.withUnsafeBufferPointer() { (buffer: UnsafeBufferPointer) -> Int in
20982115

20992116
// The count returned by nullTerminatedUTF8 includes the null terminator...
2100-
try self.write(from: $0.baseAddress!, bufSize: $0.count-1)
2117+
return try self.write(from: buffer.baseAddress!, bufSize: buffer.count-1)
21012118
}
21022119
}
21032120

Sources/SocketProtocols.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,22 @@ public protocol SocketWriter {
5454
///
5555
/// Writes data from NSData object.
5656
///
57-
/// - Parameter data: NSData object containing the data to be written.
57+
/// - Parameter data: NSData object containing the data to be written.
58+
///
59+
/// - Returns: Integer representing the number of bytes written.
5860
///
59-
func write(from data: NSData) throws
61+
@discardableResult
62+
func write(from data: NSData) throws -> Int
6063

6164
///
6265
/// Writes a string
6366
///
64-
/// - Parameter string: String data to be written.
67+
/// - Parameter string: String data to be written.
68+
///
69+
/// - Returns: Integer representing the number of bytes written.
6570
///
66-
func write(from string: String) throws
71+
@discardableResult
72+
func write(from string: String) throws -> Int
6773
}
6874

6975
// MARK: SSLServiceDelegate

0 commit comments

Comments
 (0)