Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable decoupling of acceptClientConnection and delegate.onAccept #85

Merged
merged 1 commit into from
Sep 14, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions Sources/Socket/Socket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1339,10 +1339,14 @@ public class Socket: SocketReader, SocketWriter {

///
/// Accepts an incoming client connection request on the current instance, leaving the current instance still listening.
///
/// - Parameters:
/// - invokeDelegate: Whether to invoke the delegate's `onAccept()` function after accepting
/// a new connection. Defaults to `true`
///
/// - Returns: New Socket instance representing the newly accepted socket.
///
public func acceptClientConnection() throws -> Socket {
public func acceptClientConnection(invokeDelegate: Bool = true) throws -> Socket {

// The socket must've been created, not connected and listening...
if self.socketfd == Socket.SOCKET_INVALID_DESCRIPTOR {
Expand Down Expand Up @@ -1457,26 +1461,33 @@ public class Socket: SocketReader, SocketWriter {
// Note: The current socket continues to listen.
let newSocket = try Socket(fd: socketfd2, remoteAddress: address!, path: self.signature?.path)

// Let the delegate do post accept handling and verification...
// Let the delegate do post accept handling and verification...
if invokeDelegate, self.delegate != nil {
try invokeDelegateOnAccept(socket: newSocket)
}

// Return the new socket...
return newSocket
}

/// Invokes the delegate's `onAccept()` function for a client socket. This should be performed
/// only with a Socket obtained by calling `acceptClientConnection(invokeDelegate: false)`.
///
/// - Parameters:
/// - socket: The newly accepted Socket that requires further processing by our delegate
///
public func invokeDelegateOnAccept(socket newSocket: Socket) throws {
do {

if self.delegate != nil {
try self.delegate?.onAccept(socket: newSocket)
newSocket.signature?.isSecure = true
}

} catch let error {

}
} catch let error {
guard let sslError = error as? SSLError else {

throw error
}

throw Error(with: sslError)
}

// Return the new socket...
return newSocket
}

///
Expand Down