about summary refs log tree commit diff stats
path: root/src/io/serversocket.nim
blob: e8bbf549f04e8f86ab2a17d8bddbd52279257adc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import std/nativesockets
import std/net
import std/os

when defined(posix):
  import std/posix

type ServerSocket* = object
  sock*: Socket
  path*: string

var SocketDirectory* = "/tmp/cha"
const SocketPathPrefix = "cha_sock_"
proc getSocketPath*(pid: Pid): string =
  SocketDirectory / SocketPathPrefix & $pid

# The way stdlib does bindUnix is utterly broken at least on FreeBSD.
# It seems that just writing it in C is the easiest solution.
{.compile: "bind_unix.c".}
proc bind_unix_from_c(fd: cint, path: cstring, pathlen: cint): cint {.importc.}

proc initServerSocket*(buffered = true, blocking = true): ServerSocket =
  createDir(SocketDirectory)
  let sock = newSocket(Domain.AF_UNIX, SockType.SOCK_STREAM, Protocol.IPPROTO_IP, buffered)
  if not blocking:
    sock.getFd().setBlocking(false)
  let path = getSocketPath(getpid())
  discard unlink(cstring(path))
  if bind_unix_from_c(cint(sock.getFd()), cstring(path), cint(path.len)) != 0:
    raiseOSError(osLastError())
  listen(sock)
  return ServerSocket(sock: sock, path: path)

proc close*(ssock: ServerSocket) =
  close(ssock.sock)
  discard unlink(cstring(ssock.path))