diff options
Diffstat (limited to 'src/io/serversocket.nim')
| -rw-r--r-- | src/io/serversocket.nim | 62 |
1 files changed, 31 insertions, 31 deletions
diff --git a/src/io/serversocket.nim b/src/io/serversocket.nim index abb3491a..a5b0fd67 100644 --- a/src/io/serversocket.nim +++ b/src/io/serversocket.nim @@ -1,24 +1,12 @@ -import std/nativesockets -import std/net import std/os +import std/posix -when defined(posix): - import std/posix +import io/dynstream type ServerSocket* = ref object - sock*: Socket + fd*: cint path*: string - dfd: int - -const SocketPathPrefix = "cha_sock_" -proc getSocketName*(pid: int): string = - SocketPathPrefix & $pid - -func getFd*(ssock: ServerSocket): SocketHandle = - return ssock.sock.getFd() - -proc getSocketPath*(socketDir: string; pid: int): string = - socketDir / getSocketName(pid) + dfd: int #TODO should be cint # The way stdlib does bindUnix is utterly broken at least on FreeBSD. # It seems that just writing it in C is the easiest solution. @@ -33,42 +21,54 @@ when defined(freebsd): proc bindat_unix_from_c(dfd, sock: cint; path: cstring; pathlen: cint): cint {.importc.} -proc initServerSocket*(fd: SocketHandle; sockDir: string; pid, sockDirFd: int): +proc setBlocking*(ssock: ServerSocket; blocking: bool) = + let ofl = fcntl(ssock.fd, F_GETFL, 0) + if blocking: + discard fcntl(ssock.fd, F_SETFL, ofl and not O_NONBLOCK) + else: + discard fcntl(ssock.fd, F_SETFL, ofl and O_NONBLOCK) + +proc newServerSocket*(fd: cint; sockDir: string; pid, sockDirFd: int): ServerSocket = - let sock = newSocket(fd, Domain.AF_UNIX, SockType.SOCK_STREAM, - Protocol.IPPROTO_IP, buffered = false) let path = getSocketPath(sockDir, pid) - return ServerSocket(sock: sock, path: path, dfd: sockDirFd) + return ServerSocket(fd: cint(fd), path: path, dfd: sockDirFd) -proc initServerSocket*(sockDir: string; sockDirFd, pid: int; blocking = true): - ServerSocket = - let sock = newSocket(Domain.AF_UNIX, SockType.SOCK_STREAM, - Protocol.IPPROTO_IP, buffered = false) - if not blocking: - sock.getFd().setBlocking(false) +proc newServerSocket*(sockDir: string; sockDirFd, pid: int): ServerSocket = + let fd = cint(socket(AF_UNIX, SOCK_STREAM, IPPROTO_IP)) let path = getSocketPath(sockDir, pid) + let ssock = ServerSocket(fd: fd, path: path, dfd: sockDirFd) if sockDirFd == -1: discard tryRemoveFile(path) - if bind_unix_from_c(cint(sock.getFd()), cstring(path), cint(path.len)) != 0: + if bind_unix_from_c(fd, cstring(path), cint(path.len)) != 0: raiseOSError(osLastError()) else: when defined(freebsd): let name = getSocketName(pid) discard unlinkat(cint(sockDirFd), cstring(name), 0) - if bindat_unix_from_c(cint(sockDirFd), cint(sock.getFd()), cstring(name), + if bindat_unix_from_c(cint(sockDirFd), fd, cstring(name), cint(name.len)) != 0: raiseOSError(osLastError()) else: # shouldn't have sockDirFd on other architectures doAssert false - listen(sock) - return ServerSocket(sock: sock, path: path, dfd: sockDirFd) + if listen(SocketHandle(fd), 128) != 0: + raiseOSError(osLastError()) + return ssock proc close*(ssock: ServerSocket; unlink = true) = - close(ssock.sock) + discard close(ssock.fd) if unlink: when defined(freebsd): if ssock.dfd != -1: discard unlinkat(cint(ssock.dfd), cstring(ssock.path), 0) return discard tryRemoveFile(ssock.path) + +proc acceptSocketStream*(ssock: ServerSocket; blocking = true): SocketStream = + let fd = cint(accept(SocketHandle(ssock.fd), nil, nil)) + if fd == -1: + return nil + let ss = SocketStream(fd: fd, blocking: false) + if not blocking: + ss.setBlocking(false) + return ss |