Skip to content

Commit 834ad14

Browse files
author
Bill Abt
committed
Changed SOCKET_DEFAULT_MAX_CONNECTIONS to SOCKET_DEFAULT_MAX_BACKLOG and changed the default from 5 to 50. Added SOCKET_MAX_DARWIN_BACKLOG for macOS. Changed the signature of listen(on port: Int, maxPendingConnections: Int) to listen(on port: Int, maxBacklogSize: Int = Socket.SOCKET_DEFAULT_MAX_BACKLOG) and removed the listen(on port: Int) API since the same can be accomplished using defaults. Updated documentation in README.md to reflect changes.
1 parent 9e81d8a commit 834ad14

2 files changed

Lines changed: 23 additions & 31 deletions

File tree

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# BlueSocket
22

33
## Overview
4-
Socket framework for Swift using the Swift Package Manager. Works on OS X and Linux.
4+
Socket framework for Swift using the Swift Package Manager. Works on macOS and Linux.
55

66
## Contents
77

@@ -12,9 +12,9 @@ Socket framework for Swift using the Swift Package Manager. Works on OS X and Li
1212
### Swift
1313
* Swift Open Source `swift-DEVELOPMENT-SNAPSHOT-2016-06-20-a` toolchain (**Minimum REQUIRED for latest release**)
1414

15-
### OS X
15+
### macOS
1616

17-
* OS X 10.11.0 (*El Capitan*) or higher
17+
* macOS 10.11.0 (*El Capitan*) or higher
1818
* Xcode Version 8.0 beta (8S128d) or higher using the above toolchain (*Recommended*)
1919

2020
### Linux
@@ -59,10 +59,9 @@ To close the socket of an open instance, the following function is provided:
5959

6060
### Listen on a socket.
6161

62-
**BlueSocket** supports two ways to listen for an connection on a socket. These are:
63-
- `listen(on port: Int)`
64-
- `listen(on port: Int, maxPendingConnections: Int)`
65-
The first requires that you only specify a port upon which the socket will listen for connections. The second way allow you to limit the maximum number of incoming connection. In both cases, the function will determine the appropriate socket configuration based on the `port` specified.
62+
To use **BlueSocket** to listen for an connection on a socket the following API is provided:
63+
- `listen(on port: Int, maxBacklogSize: Int = Socket.SOCKET_DEFAULT_MAX_BACKLOG)`
64+
The first parameter `port`, is the port to be used to listen on. The second paramete, `maxBacklogSize` allows you to set the size of the queue holding pending connections. The function will determine the appropriate socket configuration based on the `port` specified. For convenience on macOS, the constant `Socket.SOCKET_MAX_DARWIN_BACKLOG` can be set to use the maximum allowed backlog size. The default value for all platforms is `Socket.SOCKET_DEFAULT_MAX_BACKLOG`, currently set to *5*.
6665

6766
#### Example:
6867

Sources/Socket.swift

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020

2121
#if os(OSX) || os(iOS) || os(tvOS) || os(watchOS)
2222
import Darwin
23-
import Foundation
2423
#elseif os(Linux)
25-
import Foundation
2624
import Glibc
2725
#endif
2826

27+
import Foundation
28+
2929
// MARK: Socket
3030

3131
///
@@ -39,7 +39,10 @@ public class Socket: SocketReader, SocketWriter {
3939

4040
public static let SOCKET_MINIMUM_READ_BUFFER_SIZE = 1024
4141
public static let SOCKET_DEFAULT_READ_BUFFER_SIZE = 4096
42-
public static let SOCKET_DEFAULT_MAX_CONNECTIONS = 5
42+
public static let SOCKET_DEFAULT_MAX_BACKLOG = 50
43+
#if os(OSX)
44+
public static let SOCKET_MAX_DARWIN_BACKLOG = 128
45+
#endif
4346

4447
public static let SOCKET_INVALID_PORT = Int32(0)
4548
public static let SOCKET_INVALID_DESCRIPTOR = Int32(-1)
@@ -582,10 +585,10 @@ public class Socket: SocketReader, SocketWriter {
582585
}
583586

584587
///
585-
/// Maximum number of pending connections per listening socket.
586-
/// **Note:** Default value is `Socket.SOCKET_DEFAULT_MAX_CONNECTIONS`
588+
/// Maximum size of the queue containing pending connections.
589+
/// **Note:** Default value is `Socket.SOCKET_DEFAULT_MAX_BACKLOG`
587590
///
588-
public var maxPendingConnections: Int = Socket.SOCKET_DEFAULT_MAX_CONNECTIONS
591+
public var maxBacklogSize: Int = Socket.SOCKET_DEFAULT_MAX_BACKLOG
589592

590593
///
591594
/// True if this socket is connected. False otherwise. (Readonly)
@@ -1541,24 +1544,14 @@ public class Socket: SocketReader, SocketWriter {
15411544

15421545
// MARK: -- Listen
15431546

1544-
///
1545-
/// Listen on a port using the default for max pending connections.
1546-
///
1547-
/// - Parameter port: The port to listen on.
1548-
///
1549-
public func listen(on port: Int) throws {
1550-
1551-
return try self.listen(on: port, maxPendingConnections: self.maxPendingConnections)
1552-
}
1553-
15541547
///
15551548
/// Listen on a port, limiting the maximum number of pending connections.
15561549
///
15571550
/// - Parameters:
1558-
/// - port: The port to listen on.
1559-
/// - maxPendingConnections: The maximum number of pending connections to allow.
1551+
/// - port: The port to listen on.
1552+
/// - maxBacklogSize: The maximum size of the queue containing pending connections. Default is *Socket.SOCKET_DEFAULT_MAX_BACKLOG*.
15601553
///
1561-
public func listen(on port: Int, maxPendingConnections: Int) throws {
1554+
public func listen(on port: Int, maxBacklogSize: Int = Socket.SOCKET_DEFAULT_MAX_BACKLOG) throws {
15621555

15631556
// Set a flag so that this address can be re-used immediately after the connection
15641557
// closes. (TCP normally imposes a delay before an address can be re-used.)
@@ -1568,10 +1561,10 @@ public class Socket: SocketReader, SocketWriter {
15681561
throw Error(code: Socket.SOCKET_ERR_SETSOCKOPT_FAILED, reason: self.lastError())
15691562
}
15701563

1571-
// Set the socket to ignore SIGPIPE to avoid dying on interrupted connections...
1572-
// Note: Linux does not support the SO_NOSIGPIPE option. Instead, we use the
1573-
// MSG_NOSIGNAL flags passed to send. See the writeData() functions below.
15741564
#if !os(Linux)
1565+
// Set the socket to ignore SIGPIPE to avoid dying on interrupted connections...
1566+
// Note: Linux does not support the SO_NOSIGPIPE option. Instead, we use the
1567+
// MSG_NOSIGNAL flags passed to send. See the writeData() functions below.
15751568
if setsockopt(self.socketfd, SOL_SOCKET, SO_NOSIGPIPE, &on, socklen_t(sizeof(Int32.self))) < 0 {
15761569

15771570
throw Error(code: Socket.SOCKET_ERR_SETSOCKOPT_FAILED, reason: self.lastError())
@@ -1705,12 +1698,12 @@ public class Socket: SocketReader, SocketWriter {
17051698

17061699
// Now listen for connections...
17071700
#if os(Linux)
1708-
if Glibc.listen(self.socketfd, Int32(maxPendingConnections)) < 0 {
1701+
if Glibc.listen(self.socketfd, Int32(maxBacklogSize)) < 0 {
17091702

17101703
throw Error(code: Socket.SOCKET_ERR_LISTEN_FAILED, reason: self.lastError())
17111704
}
17121705
#else
1713-
if Darwin.listen(self.socketfd, Int32(maxPendingConnections)) < 0 {
1706+
if Darwin.listen(self.socketfd, Int32(maxBacklogSize)) < 0 {
17141707

17151708
throw Error(code: Socket.SOCKET_ERR_LISTEN_FAILED, reason: self.lastError())
17161709
}

0 commit comments

Comments
 (0)