Implements nativesockets.accept.
This commit is contained in:
parent
66d7091b79
commit
e5a27c96fb
3 changed files with 20 additions and 9 deletions
|
|
@ -21,6 +21,8 @@
|
||||||
field to help distinguish between ``Event.Error`` events.
|
field to help distinguish between ``Event.Error`` events.
|
||||||
- The `AsyncFD` type now reflects the fact that the underlying FD is registered
|
- The `AsyncFD` type now reflects the fact that the underlying FD is registered
|
||||||
in the async dispatcher.
|
in the async dispatcher.
|
||||||
|
- Implemented an `accept` proc that works on a `SocketHandle` in
|
||||||
|
``nativesockets``.
|
||||||
- The overloading rules changed slightly so that constrained generics are
|
- The overloading rules changed slightly so that constrained generics are
|
||||||
preferred over unconstrained generics. (Bug #6526)
|
preferred over unconstrained generics. (Bug #6526)
|
||||||
- It is now possible to forward declare object types so that mutually
|
- It is now possible to forward declare object types so that mutually
|
||||||
|
|
|
||||||
|
|
@ -187,12 +187,12 @@ proc toSockType*(protocol: Protocol): SockType =
|
||||||
proc newNativeSocket*(domain: Domain = AF_INET,
|
proc newNativeSocket*(domain: Domain = AF_INET,
|
||||||
sockType: SockType = SOCK_STREAM,
|
sockType: SockType = SOCK_STREAM,
|
||||||
protocol: Protocol = IPPROTO_TCP): SocketHandle =
|
protocol: Protocol = IPPROTO_TCP): SocketHandle =
|
||||||
## Creates a new socket; returns `InvalidSocket` if an error occurs.
|
## Creates a new socket; returns `osInvalidSocket` if an error occurs.
|
||||||
socket(toInt(domain), toInt(sockType), toInt(protocol))
|
socket(toInt(domain), toInt(sockType), toInt(protocol))
|
||||||
|
|
||||||
proc newNativeSocket*(domain: cint, sockType: cint,
|
proc newNativeSocket*(domain: cint, sockType: cint,
|
||||||
protocol: cint): SocketHandle =
|
protocol: cint): SocketHandle =
|
||||||
## Creates a new socket; returns `InvalidSocket` if an error occurs.
|
## Creates a new socket; returns `osInvalidSocket` if an error occurs.
|
||||||
##
|
##
|
||||||
## Use this overload if one of the enums specified above does
|
## Use this overload if one of the enums specified above does
|
||||||
## not contain what you need.
|
## not contain what you need.
|
||||||
|
|
@ -666,6 +666,19 @@ proc selectWrite*(writefds: var seq[SocketHandle],
|
||||||
|
|
||||||
pruneSocketSet(writefds, (wr))
|
pruneSocketSet(writefds, (wr))
|
||||||
|
|
||||||
|
proc accept*(fd: SocketHandle): (SocketHandle, string) =
|
||||||
|
## Accepts a new client connection.
|
||||||
|
##
|
||||||
|
## Returns (osInvalidSocket, "") if an error occurred.
|
||||||
|
var sockAddress: Sockaddr_in
|
||||||
|
var addrLen = sizeof(sockAddress).SockLen
|
||||||
|
var sock = accept(fd, cast[ptr SockAddr](addr(sockAddress)),
|
||||||
|
addr(addrLen))
|
||||||
|
if sock == osInvalidSocket:
|
||||||
|
return (osInvalidSocket, "")
|
||||||
|
else:
|
||||||
|
return (sock, $inet_ntoa(sockAddress.sin_addr))
|
||||||
|
|
||||||
when defined(Windows):
|
when defined(Windows):
|
||||||
var wsa: WSAData
|
var wsa: WSAData
|
||||||
if wsaStartup(0x0101'i16, addr wsa) != 0: raiseOSError(osLastError())
|
if wsaStartup(0x0101'i16, addr wsa) != 0: raiseOSError(osLastError())
|
||||||
|
|
|
||||||
|
|
@ -753,10 +753,8 @@ proc acceptAddr*(server: Socket, client: var Socket, address: var string,
|
||||||
## flag is specified then this error will not be raised and instead
|
## flag is specified then this error will not be raised and instead
|
||||||
## accept will be called again.
|
## accept will be called again.
|
||||||
assert(client != nil)
|
assert(client != nil)
|
||||||
var sockAddress: Sockaddr_in
|
let ret = accept(server.fd)
|
||||||
var addrLen = sizeof(sockAddress).SockLen
|
let sock = ret[0]
|
||||||
var sock = accept(server.fd, cast[ptr SockAddr](addr(sockAddress)),
|
|
||||||
addr(addrLen))
|
|
||||||
|
|
||||||
if sock == osInvalidSocket:
|
if sock == osInvalidSocket:
|
||||||
let err = osLastError()
|
let err = osLastError()
|
||||||
|
|
@ -764,6 +762,7 @@ proc acceptAddr*(server: Socket, client: var Socket, address: var string,
|
||||||
acceptAddr(server, client, address, flags)
|
acceptAddr(server, client, address, flags)
|
||||||
raiseOSError(err)
|
raiseOSError(err)
|
||||||
else:
|
else:
|
||||||
|
address = ret[1]
|
||||||
client.fd = sock
|
client.fd = sock
|
||||||
client.isBuffered = server.isBuffered
|
client.isBuffered = server.isBuffered
|
||||||
|
|
||||||
|
|
@ -776,9 +775,6 @@ proc acceptAddr*(server: Socket, client: var Socket, address: var string,
|
||||||
let ret = SSLAccept(client.sslHandle)
|
let ret = SSLAccept(client.sslHandle)
|
||||||
socketError(client, ret, false)
|
socketError(client, ret, false)
|
||||||
|
|
||||||
# Client socket is set above.
|
|
||||||
address = $inet_ntoa(sockAddress.sin_addr)
|
|
||||||
|
|
||||||
when false: #defineSsl:
|
when false: #defineSsl:
|
||||||
proc acceptAddrSSL*(server: Socket, client: var Socket,
|
proc acceptAddrSSL*(server: Socket, client: var Socket,
|
||||||
address: var string): SSLAcceptResult {.
|
address: var string): SSLAcceptResult {.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue