Skip to content

Commit

Permalink
Merge pull request #15 from shmuelk/asynch_write
Browse files Browse the repository at this point in the history
Add support for non-blocking writes  to TCP sockets
  • Loading branch information
billabt authored Aug 11, 2016
2 parents 7b64a42 + 23516f4 commit dba96ab
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
47 changes: 32 additions & 15 deletions Sources/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1992,9 +1992,12 @@ public class Socket: SocketReader, SocketWriter {
///
/// - Parameters:
/// - buffer: The buffer containing the data to write.
/// - bufSize: The size of the buffer.
/// - bufSize: The size of the buffer.
///
/// - Returns: Integer representing the number of bytes written.
///
public func write(from buffer: UnsafePointer<Void>, bufSize: Int) throws {
@discardableResult
public func write(from buffer: UnsafePointer<Void>, bufSize: Int) throws -> Int {

// Make sure the buffer is valid...
if bufSize == 0 {
Expand Down Expand Up @@ -2047,57 +2050,71 @@ public class Socket: SocketReader, SocketWriter {
#endif
}
if s <= 0 {
if errno == EAGAIN && !isBlocking {
// We have written out as much as we can
return sent
}

throw Error(code: Socket.SOCKET_ERR_WRITE_FAILED, reason: self.lastError())
}
sent += s
}
return sent
}

///
/// Write data to the socket.
///
/// - Parameter data: The NSData object containing the data to write.
/// - Parameter data: The NSData object containing the data to write.
///
/// - Returns: Integer representing the number of bytes written.
///
public func write(from data: NSData) throws {
@discardableResult
public func write(from data: NSData) throws -> Int {

// If there's no data in the NSData object, why bother? Fail silently...
if data.length == 0 {
return
return 0
}

try write(from: data.bytes, bufSize: data.length)
return try write(from: data.bytes, bufSize: data.length)
}

///
/// Write data to the socket.
///
/// - Parameter data: The Data object containing the data to write.
/// - Parameter data: The Data object containing the data to write.
///
/// - Returns: Integer representing the number of bytes written.
///
public func write(from data: Data) throws {
@discardableResult
public func write(from data: Data) throws -> Int {

// If there's no data in the Data object, why bother? Fail silently...
if data.count == 0 {
return
return 0
}

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

try self.write(from: buffer, bufSize: data.count)
return try self.write(from: buffer, bufSize: data.count)
}
}

///
/// Write a string to the socket.
///
/// - Parameter string: The string to write.
/// - Parameter string: The string to write.
///
/// - Returns: Integer representing the number of bytes written.
///
public func write(from string: String) throws {
@discardableResult
public func write(from string: String) throws -> Int {

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

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

Expand Down
14 changes: 10 additions & 4 deletions Sources/SocketProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,22 @@ public protocol SocketWriter {
///
/// Writes data from NSData object.
///
/// - Parameter data: NSData object containing the data to be written.
/// - Parameter data: NSData object containing the data to be written.
///
/// - Returns: Integer representing the number of bytes written.
///
func write(from data: NSData) throws
@discardableResult
func write(from data: NSData) throws -> Int

///
/// Writes a string
///
/// - Parameter string: String data to be written.
/// - Parameter string: String data to be written.
///
/// - Returns: Integer representing the number of bytes written.
///
func write(from string: String) throws
@discardableResult
func write(from string: String) throws -> Int
}

// MARK: SSLServiceDelegate
Expand Down

0 comments on commit dba96ab

Please sign in to comment.