Add support for SOCK_DGRAM
This commit is contained in:
parent
3f357a3c79
commit
9b0b0f9b51
|
@ -167,9 +167,9 @@ class BaseSocket : Selectable {
|
|||
})
|
||||
}
|
||||
}
|
||||
static func newSocket(protocolFamily: Int32) throws -> Int32 {
|
||||
static func newSocket(protocolFamily: Int32, type: CInt) throws -> Int32 {
|
||||
let sock = try Posix.socket(domain: protocolFamily,
|
||||
type: Posix.SOCK_STREAM,
|
||||
type: type,
|
||||
protocol: 0)
|
||||
|
||||
if protocolFamily == AF_INET6 {
|
||||
|
|
|
@ -21,7 +21,7 @@ final class ServerSocket: BaseSocket {
|
|||
}
|
||||
|
||||
init(protocolFamily: Int32) throws {
|
||||
let sock = try BaseSocket.newSocket(protocolFamily: protocolFamily)
|
||||
let sock = try BaseSocket.newSocket(protocolFamily: protocolFamily, type: Posix.SOCK_STREAM)
|
||||
super.init(descriptor: sock)
|
||||
}
|
||||
|
||||
|
|
|
@ -29,8 +29,8 @@ final class Socket : BaseSocket {
|
|||
}
|
||||
static let writevLimitIOVectors: Int = Posix.UIO_MAXIOV
|
||||
|
||||
init(protocolFamily: Int32) throws {
|
||||
let sock = try BaseSocket.newSocket(protocolFamily: protocolFamily)
|
||||
init(protocolFamily: CInt, type: CInt) throws {
|
||||
let sock = try BaseSocket.newSocket(protocolFamily: protocolFamily, type: type)
|
||||
super.init(descriptor: sock)
|
||||
}
|
||||
|
||||
|
@ -82,6 +82,14 @@ final class Socket : BaseSocket {
|
|||
|
||||
return try Posix.writev(descriptor: self.descriptor, iovecs: iovecs)
|
||||
}
|
||||
|
||||
func sendto(pointer: UnsafePointer<UInt8>, size: Int, destinationPtr: UnsafePointer<sockaddr>, destinationSize: socklen_t) throws -> IOResult<Int> {
|
||||
guard self.open else {
|
||||
throw IOError(errnoCode: EBADF, reason: "can't sendto to socket as it's not open anymore.")
|
||||
}
|
||||
|
||||
return try Posix.sendto(descriptor: self.descriptor, pointer: UnsafeMutablePointer(mutating: pointer), size: size, destinationPtr: destinationPtr, destinationSize: destinationSize)
|
||||
}
|
||||
|
||||
func read(pointer: UnsafeMutablePointer<UInt8>, size: Int) throws -> IOResult<Int> {
|
||||
guard self.open else {
|
||||
|
@ -90,6 +98,16 @@ final class Socket : BaseSocket {
|
|||
|
||||
return try Posix.read(descriptor: self.descriptor, pointer: pointer, size: size)
|
||||
}
|
||||
|
||||
func recvfrom(pointer: UnsafeMutablePointer<UInt8>, size: Int, storage: inout sockaddr_storage, storageLen: inout socklen_t) throws -> IOResult<(Int)> {
|
||||
guard self.open else {
|
||||
throw IOError(errnoCode: EBADF, reason: "can't recvfrom socket as it's not open anymore")
|
||||
}
|
||||
|
||||
return try storage.withMutableSockAddr { (storagePtr, _) in
|
||||
try Posix.recvfrom(descriptor: self.descriptor, pointer: pointer, len: size, addr: storagePtr, addrlen: &storageLen)
|
||||
}
|
||||
}
|
||||
|
||||
func sendFile(fd: Int32, offset: Int, count: Int) throws -> IOResult<Int> {
|
||||
guard self.open else {
|
||||
|
|
|
@ -635,7 +635,7 @@ final class SocketChannel: BaseSocketChannel<Socket> {
|
|||
}
|
||||
|
||||
init(eventLoop: SelectableEventLoop, protocolFamily: Int32) throws {
|
||||
let socket = try Socket(protocolFamily: protocolFamily)
|
||||
let socket = try Socket(protocolFamily: protocolFamily, type: Posix.SOCK_STREAM)
|
||||
do {
|
||||
try socket.setNonBlocking()
|
||||
} catch let err {
|
||||
|
|
|
@ -43,6 +43,8 @@ private let sysWrite = write
|
|||
private let sysWritev = writev
|
||||
private let sysRead = read
|
||||
private let sysLseek = lseek
|
||||
private let sysRecvFrom = recvfrom
|
||||
private let sysSendTo = sendto
|
||||
|
||||
private func isBlacklistedErrno(_ code: Int32) -> Bool {
|
||||
switch code {
|
||||
|
@ -114,14 +116,19 @@ enum Shutdown {
|
|||
internal enum Posix {
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
static let SOCK_STREAM: CInt = CInt(Darwin.SOCK_STREAM)
|
||||
static let SOCK_DGRAM: CInt = CInt(Darwin.SOCK_DGRAM)
|
||||
static let UIO_MAXIOV: Int = 1024
|
||||
#elseif os(Linux) || os(FreeBSD) || os(Android)
|
||||
static let SOCK_STREAM: CInt = CInt(Glibc.SOCK_STREAM.rawValue)
|
||||
static let SOCK_DGRAM : CInt = CInt(Glibc.SOCK_DGRAM.rawValue)
|
||||
static let UIO_MAXIOV: Int = Int(Glibc.UIO_MAXIOV)
|
||||
#else
|
||||
static var SOCK_STREAM: CInt {
|
||||
fatalError("unsupported OS")
|
||||
}
|
||||
static var SOCK_DGRAM: CInt {
|
||||
fatalError("unsupported OS")
|
||||
}
|
||||
static var UIO_MAXIOV: Int {
|
||||
fatalError("unsupported OS")
|
||||
}
|
||||
|
@ -261,6 +268,14 @@ internal enum Posix {
|
|||
sysWritev(descriptor, iovecs.baseAddress!, Int32(iovecs.count))
|
||||
}
|
||||
}
|
||||
|
||||
@inline(never)
|
||||
public static func sendto(descriptor: CInt, pointer: UnsafePointer<UInt8>, size: Int,
|
||||
destinationPtr: UnsafePointer<sockaddr>, destinationSize: socklen_t) throws -> IOResult<Int> {
|
||||
return try wrapSyscallMayBlock {
|
||||
sysSendTo(descriptor, pointer, size, 0, destinationPtr, destinationSize)
|
||||
}
|
||||
}
|
||||
|
||||
@inline(never)
|
||||
public static func read(descriptor: Int32, pointer: UnsafeMutablePointer<UInt8>, size: Int) throws -> IOResult<Int> {
|
||||
|
@ -268,6 +283,13 @@ internal enum Posix {
|
|||
Int(sysRead(descriptor, pointer, size))
|
||||
}
|
||||
}
|
||||
|
||||
@inline(never)
|
||||
public static func recvfrom(descriptor: CInt, pointer: UnsafeMutablePointer<UInt8>, len: Int, addr: UnsafeMutablePointer<sockaddr>, addrlen: UnsafeMutablePointer<socklen_t>) throws -> IOResult<Int> {
|
||||
return try wrapSyscallMayBlock {
|
||||
sysRecvFrom(descriptor, pointer, len, 0, addr, addrlen)
|
||||
}
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
@inline(never)
|
||||
|
|
Loading…
Reference in New Issue