Added a TSocketHandle type to winlean.nim and posix.nim
Edited sockets.nim and asyncio.nim to use the new TSocketHandle type.
This commit is contained in:
parent
b753edec1d
commit
229d83f6c6
4 changed files with 30 additions and 27 deletions
|
|
@ -81,6 +81,9 @@ else:
|
||||||
## A type representing a directory stream.
|
## A type representing a directory stream.
|
||||||
|
|
||||||
type
|
type
|
||||||
|
TSocketHandle* = cint # The type used to represent socket descripters
|
||||||
|
# Should this be distinct? Is this the right place?
|
||||||
|
|
||||||
Tdirent* {.importc: "struct dirent",
|
Tdirent* {.importc: "struct dirent",
|
||||||
header: "<dirent.h>", final, pure.} = object ## dirent_t struct
|
header: "<dirent.h>", final, pure.} = object ## dirent_t struct
|
||||||
d_ino*: TIno ## File serial number.
|
d_ino*: TIno ## File serial number.
|
||||||
|
|
|
||||||
|
|
@ -89,13 +89,13 @@ import sockets, os
|
||||||
## getSocket(s).accept(client)
|
## getSocket(s).accept(client)
|
||||||
|
|
||||||
when defined(windows):
|
when defined(windows):
|
||||||
from winlean import TTimeVal, TFdSet, FD_ZERO, FD_SET, FD_ISSET, select
|
from winlean import TTimeVal, TSocketHandle, TFdSet, FD_ZERO, FD_SET, FD_ISSET, select
|
||||||
else:
|
else:
|
||||||
from posix import TTimeVal, TFdSet, FD_ZERO, FD_SET, FD_ISSET, select
|
from posix import TTimeVal, TSocketHandle, TFdSet, FD_ZERO, FD_SET, FD_ISSET, select
|
||||||
|
|
||||||
type
|
type
|
||||||
TDelegate* = object
|
TDelegate* = object
|
||||||
fd*: cint
|
fd*: TSocketHandle
|
||||||
deleVal*: PObject
|
deleVal*: PObject
|
||||||
|
|
||||||
handleRead*: proc (h: PObject) {.nimcall.}
|
handleRead*: proc (h: PObject) {.nimcall.}
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,7 @@ const
|
||||||
|
|
||||||
type
|
type
|
||||||
TSocketImpl = object ## socket type
|
TSocketImpl = object ## socket type
|
||||||
fd: cint
|
fd: TSocketHandle
|
||||||
case isBuffered: bool # determines whether this socket is buffered.
|
case isBuffered: bool # determines whether this socket is buffered.
|
||||||
of true:
|
of true:
|
||||||
buffer: array[0..BufferSize, char]
|
buffer: array[0..BufferSize, char]
|
||||||
|
|
@ -126,7 +126,7 @@ type
|
||||||
|
|
||||||
ETimeout* = object of ESynch
|
ETimeout* = object of ESynch
|
||||||
|
|
||||||
proc newTSocket(fd: int32, isBuff: bool): TSocket =
|
proc newTSocket(fd: TSocketHandle, isBuff: bool): TSocket =
|
||||||
new(result)
|
new(result)
|
||||||
result.fd = fd
|
result.fd = fd
|
||||||
result.isBuffered = isBuff
|
result.isBuffered = isBuff
|
||||||
|
|
@ -1608,14 +1608,14 @@ when defined(Windows):
|
||||||
FIONBIO = IOC_IN.int32 or ((sizeof(int32) and IOCPARM_MASK) shl 16) or
|
FIONBIO = IOC_IN.int32 or ((sizeof(int32) and IOCPARM_MASK) shl 16) or
|
||||||
(102 shl 8) or 126
|
(102 shl 8) or 126
|
||||||
|
|
||||||
proc ioctlsocket(s: TWinSocket, cmd: clong,
|
proc ioctlsocket(s: TSocketHandle, cmd: clong,
|
||||||
argptr: ptr clong): cint {.
|
argptr: ptr clong): cint {.
|
||||||
stdcall, importc:"ioctlsocket", dynlib: "ws2_32.dll".}
|
stdcall, importc:"ioctlsocket", dynlib: "ws2_32.dll".}
|
||||||
|
|
||||||
proc setBlocking(s: TSocket, blocking: bool) =
|
proc setBlocking(s: TSocket, blocking: bool) =
|
||||||
when defined(Windows):
|
when defined(Windows):
|
||||||
var mode = clong(ord(not blocking)) # 1 for non-blocking, 0 for blocking
|
var mode = clong(ord(not blocking)) # 1 for non-blocking, 0 for blocking
|
||||||
if ioctlsocket(TWinSocket(s.fd), FIONBIO, addr(mode)) == -1:
|
if ioctlsocket(TSocketHandle(s.fd), FIONBIO, addr(mode)) == -1:
|
||||||
OSError(OSLastError())
|
OSError(OSLastError())
|
||||||
else: # BSD sockets
|
else: # BSD sockets
|
||||||
var x: int = fcntl(s.fd, F_GETFL, 0)
|
var x: int = fcntl(s.fd, F_GETFL, 0)
|
||||||
|
|
@ -1656,7 +1656,7 @@ proc connect*(socket: TSocket, address: string, port = TPort(0), timeout: int,
|
||||||
proc isSSL*(socket: TSocket): bool = return socket.isSSL
|
proc isSSL*(socket: TSocket): bool = return socket.isSSL
|
||||||
## Determines whether ``socket`` is a SSL socket.
|
## Determines whether ``socket`` is a SSL socket.
|
||||||
|
|
||||||
proc getFD*(socket: TSocket): cint = return socket.fd
|
proc getFD*(socket: TSocket): TSocketHandle = return socket.fd
|
||||||
## Returns the socket's file descriptor
|
## Returns the socket's file descriptor
|
||||||
|
|
||||||
when defined(Windows):
|
when defined(Windows):
|
||||||
|
|
|
||||||
|
|
@ -390,11 +390,11 @@ type
|
||||||
h_length*: int16
|
h_length*: int16
|
||||||
h_addr_list*: cstringArray
|
h_addr_list*: cstringArray
|
||||||
|
|
||||||
TWinSocket* = cint
|
TSocketHandle* = int # Make distinct? Is this the right place for this?
|
||||||
|
|
||||||
TFdSet* {.pure, final.} = object
|
TFdSet* {.pure, final.} = object
|
||||||
fd_count*: cint # unsigned
|
fd_count*: cint # unsigned
|
||||||
fd_array*: array[0..FD_SETSIZE-1, TWinSocket]
|
fd_array*: array[0..FD_SETSIZE-1, TSocketHandle]
|
||||||
|
|
||||||
TTimeval* {.pure, final.} = object
|
TTimeval* {.pure, final.} = object
|
||||||
tv_sec*, tv_usec*: int32
|
tv_sec*, tv_usec*: int32
|
||||||
|
|
@ -426,45 +426,45 @@ proc gethostbyaddr*(ip: ptr TInAddr, len: cuint, theType: cint): ptr THostEnt {.
|
||||||
proc gethostbyname*(name: cstring): ptr THostEnt {.
|
proc gethostbyname*(name: cstring): ptr THostEnt {.
|
||||||
stdcall, importc: "gethostbyname", dynlib: ws2dll.}
|
stdcall, importc: "gethostbyname", dynlib: ws2dll.}
|
||||||
|
|
||||||
proc socket*(af, typ, protocol: cint): TWinSocket {.
|
proc socket*(af, typ, protocol: cint): TSocketHandle {.
|
||||||
stdcall, importc: "socket", dynlib: ws2dll.}
|
stdcall, importc: "socket", dynlib: ws2dll.}
|
||||||
|
|
||||||
proc closesocket*(s: TWinSocket): cint {.
|
proc closesocket*(s: TSocketHandle): cint {.
|
||||||
stdcall, importc: "closesocket", dynlib: ws2dll.}
|
stdcall, importc: "closesocket", dynlib: ws2dll.}
|
||||||
|
|
||||||
proc accept*(s: TWinSocket, a: ptr TSockAddr, addrlen: ptr TSockLen): TWinSocket {.
|
proc accept*(s: TSocketHandle, a: ptr TSockAddr, addrlen: ptr TSockLen): TSocketHandle {.
|
||||||
stdcall, importc: "accept", dynlib: ws2dll.}
|
stdcall, importc: "accept", dynlib: ws2dll.}
|
||||||
proc bindSocket*(s: TWinSocket, name: ptr TSockAddr, namelen: TSockLen): cint {.
|
proc bindSocket*(s: TSocketHandle, name: ptr TSockAddr, namelen: TSockLen): cint {.
|
||||||
stdcall, importc: "bind", dynlib: ws2dll.}
|
stdcall, importc: "bind", dynlib: ws2dll.}
|
||||||
proc connect*(s: TWinSocket, name: ptr TSockAddr, namelen: TSockLen): cint {.
|
proc connect*(s: TSocketHandle, name: ptr TSockAddr, namelen: TSockLen): cint {.
|
||||||
stdcall, importc: "connect", dynlib: ws2dll.}
|
stdcall, importc: "connect", dynlib: ws2dll.}
|
||||||
proc getsockname*(s: TWinSocket, name: ptr TSockAddr,
|
proc getsockname*(s: TSocketHandle, name: ptr TSockAddr,
|
||||||
namelen: ptr TSockLen): cint {.
|
namelen: ptr TSockLen): cint {.
|
||||||
stdcall, importc: "getsockname", dynlib: ws2dll.}
|
stdcall, importc: "getsockname", dynlib: ws2dll.}
|
||||||
proc getsockopt*(s: TWinSocket, level, optname: cint, optval: pointer,
|
proc getsockopt*(s: TSocketHandle, level, optname: cint, optval: pointer,
|
||||||
optlen: ptr TSockLen): cint {.
|
optlen: ptr TSockLen): cint {.
|
||||||
stdcall, importc: "getsockopt", dynlib: ws2dll.}
|
stdcall, importc: "getsockopt", dynlib: ws2dll.}
|
||||||
proc setsockopt*(s: TWinSocket, level, optname: cint, optval: pointer,
|
proc setsockopt*(s: TSocketHandle, level, optname: cint, optval: pointer,
|
||||||
optlen: TSockLen): cint {.
|
optlen: TSockLen): cint {.
|
||||||
stdcall, importc: "setsockopt", dynlib: ws2dll.}
|
stdcall, importc: "setsockopt", dynlib: ws2dll.}
|
||||||
|
|
||||||
proc listen*(s: TWinSocket, backlog: cint): cint {.
|
proc listen*(s: TSocketHandle, backlog: cint): cint {.
|
||||||
stdcall, importc: "listen", dynlib: ws2dll.}
|
stdcall, importc: "listen", dynlib: ws2dll.}
|
||||||
proc recv*(s: TWinSocket, buf: pointer, len, flags: cint): cint {.
|
proc recv*(s: TSocketHandle, buf: pointer, len, flags: cint): cint {.
|
||||||
stdcall, importc: "recv", dynlib: ws2dll.}
|
stdcall, importc: "recv", dynlib: ws2dll.}
|
||||||
proc recvfrom*(s: TWinSocket, buf: cstring, len, flags: cint,
|
proc recvfrom*(s: TSocketHandle, buf: cstring, len, flags: cint,
|
||||||
fromm: ptr TSockAddr, fromlen: ptr Tsocklen): cint {.
|
fromm: ptr TSockAddr, fromlen: ptr Tsocklen): cint {.
|
||||||
stdcall, importc: "recvfrom", dynlib: ws2dll.}
|
stdcall, importc: "recvfrom", dynlib: ws2dll.}
|
||||||
proc select*(nfds: cint, readfds, writefds, exceptfds: ptr TFdSet,
|
proc select*(nfds: cint, readfds, writefds, exceptfds: ptr TFdSet,
|
||||||
timeout: ptr TTimeval): cint {.
|
timeout: ptr TTimeval): cint {.
|
||||||
stdcall, importc: "select", dynlib: ws2dll.}
|
stdcall, importc: "select", dynlib: ws2dll.}
|
||||||
proc send*(s: TWinSocket, buf: pointer, len, flags: cint): cint {.
|
proc send*(s: TSocketHandle, buf: pointer, len, flags: cint): cint {.
|
||||||
stdcall, importc: "send", dynlib: ws2dll.}
|
stdcall, importc: "send", dynlib: ws2dll.}
|
||||||
proc sendto*(s: TWinSocket, buf: pointer, len, flags: cint,
|
proc sendto*(s: TSocketHandle, buf: pointer, len, flags: cint,
|
||||||
to: ptr TSockAddr, tolen: Tsocklen): cint {.
|
to: ptr TSockAddr, tolen: Tsocklen): cint {.
|
||||||
stdcall, importc: "sendto", dynlib: ws2dll.}
|
stdcall, importc: "sendto", dynlib: ws2dll.}
|
||||||
|
|
||||||
proc shutdown*(s: TWinSocket, how: cint): cint {.
|
proc shutdown*(s: TSocketHandle, how: cint): cint {.
|
||||||
stdcall, importc: "shutdown", dynlib: ws2dll.}
|
stdcall, importc: "shutdown", dynlib: ws2dll.}
|
||||||
|
|
||||||
proc getnameinfo*(a1: ptr Tsockaddr, a2: Tsocklen,
|
proc getnameinfo*(a1: ptr Tsockaddr, a2: Tsocklen,
|
||||||
|
|
@ -475,13 +475,13 @@ proc getnameinfo*(a1: ptr Tsockaddr, a2: Tsocklen,
|
||||||
proc inet_addr*(cp: cstring): int32 {.
|
proc inet_addr*(cp: cstring): int32 {.
|
||||||
stdcall, importc: "inet_addr", dynlib: ws2dll.}
|
stdcall, importc: "inet_addr", dynlib: ws2dll.}
|
||||||
|
|
||||||
proc WSAFDIsSet(s: TWinSocket, FDSet: var TFDSet): bool {.
|
proc WSAFDIsSet(s: TSocketHandle, FDSet: var TFDSet): bool {.
|
||||||
stdcall, importc: "__WSAFDIsSet", dynlib: ws2dll.}
|
stdcall, importc: "__WSAFDIsSet", dynlib: ws2dll.}
|
||||||
|
|
||||||
proc FD_ISSET*(Socket: TWinSocket, FDSet: var TFDSet): cint =
|
proc FD_ISSET*(Socket: TSocketHandle, FDSet: var TFDSet): cint =
|
||||||
result = if WSAFDIsSet(Socket, FDSet): 1'i32 else: 0'i32
|
result = if WSAFDIsSet(Socket, FDSet): 1'i32 else: 0'i32
|
||||||
|
|
||||||
proc FD_SET*(Socket: TWinSocket, FDSet: var TFDSet) =
|
proc FD_SET*(Socket: TSocketHandle, FDSet: var TFDSet) =
|
||||||
if FDSet.fd_count < FD_SETSIZE:
|
if FDSet.fd_count < FD_SETSIZE:
|
||||||
FDSet.fd_array[int(FDSet.fd_count)] = Socket
|
FDSet.fd_array[int(FDSet.fd_count)] = Socket
|
||||||
inc(FDSet.fd_count)
|
inc(FDSet.fd_count)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue