Skip to content

Commit

Permalink
Merge pull request #42 from vapor/write
Browse files Browse the repository at this point in the history
write
  • Loading branch information
tanner0101 authored May 3, 2017
2 parents 62ff9d1 + b6262ef commit 13757d3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
16 changes: 11 additions & 5 deletions Sources/TLS/InternetSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ public final class InternetSocket: Socket {
public let socket: TCPInternetSocket
public let context: Context
public var cSSL: CSSL?

public var client: TCPInternetSocket?

public init(_ socket: TCPInternetSocket, _ context: Context) {
self.socket = socket
self.context = context
Expand All @@ -16,7 +16,7 @@ public final class InternetSocket: Socket {
try socket.close()
try client?.close()
}

deinit {
SSL_free(cSSL)
}
Expand All @@ -29,11 +29,11 @@ extension InternetSocket: InternetStream {
public var scheme: String {
return socket.scheme
}

public var hostname: String {
return socket.hostname
}

public var port: Port {
return socket.port
}
Expand All @@ -46,3 +46,9 @@ extension InternetSocket: WriteableSocket { }

extension InternetSocket: ClientSocket { }
extension InternetSocket: ServerSocket { }

extension InternetSocket: DescriptorRepresentable {
public func makeDescriptor() -> Descriptor {
return socket.descriptor
}
}
34 changes: 11 additions & 23 deletions Sources/TLS/WriteableSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,20 @@ extension WriteableSocket {
/// Sends bytes to the secure socket.
///
/// - parameter bytes: An array of bytes to send.
public func write(_ bytes: Bytes) throws {
var totalBytesSent = 0
let buffer = UnsafeBufferPointer<Byte>(start: bytes, count: bytes.count)
guard let bufferBaseAddress = buffer.baseAddress else {
throw TLSError(
functionName: "baseAddress",
returnCode: nil,
reason: "Could not fetch buffer base address"
public func write(max: Int, from buffer: Bytes) throws -> Int {
let bytesSent = SSL_write(
cSSL,
buffer,
Int32(max)
)
if bytesSent <= 0 {
throw makeError(
functionName: "SSL_write",
returnCode: bytesSent
)
}

while totalBytesSent < bytes.count {
let bytesSent = SSL_write(
cSSL,
bufferBaseAddress.advanced(by: totalBytesSent),
Int32(bytes.count - totalBytesSent)
)
if bytesSent <= 0 {
throw makeError(
functionName: "SSL_write",
returnCode: bytesSent
)
}

totalBytesSent += Int(bytesSent)
}
return Int(bytesSent)
}

public func flush() throws {
Expand Down

0 comments on commit 13757d3

Please sign in to comment.