mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
Motivation: A small number of socket options have values that do not fit into a C int type. Our current ChannelOption based approach for setting these simply does not work, and cannot be extended to support the truly arbitrary types that the setsockopt/getsockopt functions allow here. This makes it impossible to use some socket options, which is hardly a good place to be. There were a number of ways we could have addressed this: we could have special-cased all socket options with non-integer types in ChannelOption, but I believe that would be too manual, and risk limiting users that need to set other socket options. We could also have added a ChannelOption that simply allows users to pass a buffer to write into or read from, but that is a very un-Swift-like API that feels pretty gross to hold. Ultimately, the nicest seemed to be a new protocol users could check for, and that would provide APIs that let users hold the correct concrete type. As with setsockopt/getsockopt, while this API is typed it is not type-safe: ultimately, the struct we have here is treated just as a buffer by setsockopt/getsockopt. We do not attempt to prevent users from shooting themselves in the foot here. This PR does not include an example use case in any server, as I will provide such an example in a subsequent multicast PR. Modifications: - Added a SocketOptionChannel protocol. - Conformed BaseSocketChannel to SocketOptionChannel. - Wrote some tests for this. Result: Users can set and get sockopts with data that is larger than a C int.