@@ -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
0 commit comments