Merge branch 'unix-sockets' of https://github.com/girvo/Nim into girvo-unix-sockets
This commit is contained in:
commit
a484e3f8c2
5 changed files with 60 additions and 3 deletions
|
|
@ -68,6 +68,7 @@
|
|||
{.deadCodeElim: on.}
|
||||
import nativesockets, os, strutils, parseutils, times
|
||||
export Port, `$`, `==`
|
||||
export Domain, SockType, Protocol
|
||||
|
||||
const useWinVersion = defined(Windows) or defined(nimdoc)
|
||||
const defineSsl = defined(ssl) or defined(nimdoc)
|
||||
|
|
@ -605,9 +606,32 @@ proc setSockOpt*(socket: Socket, opt: SOBool, value: bool, level = SOL_SOCKET) {
|
|||
var valuei = cint(if value: 1 else: 0)
|
||||
setSockOptInt(socket.fd, cint(level), toCInt(opt), valuei)
|
||||
|
||||
when defineSsl:
|
||||
when defined(posix) or defined(nimdoc):
|
||||
proc makeUnixAddr(path: string): Sockaddr_un =
|
||||
result.sun_family = AF_UNIX.toInt
|
||||
if path.len >= Sockaddr_un_path_length:
|
||||
raise newException(ValueError, "socket path too long")
|
||||
copyMem(addr result.sun_path, path.cstring, path.len + 1)
|
||||
|
||||
proc connectUnix*(socket: Socket, path: string) =
|
||||
## Connects to Unix socket on `path`.
|
||||
## This only works on Unix-style systems: Mac OS X, BSD and Linux
|
||||
var socketAddr = makeUnixAddr(path)
|
||||
if socket.fd.connect(cast[ptr SockAddr](addr socketAddr),
|
||||
sizeof(socketAddr).Socklen) != 0'i32:
|
||||
raiseOSError(osLastError())
|
||||
|
||||
proc bindUnix*(socket: Socket, path: string) =
|
||||
## Binds Unix socket to `path`.
|
||||
## This only works on Unix-style systems: Mac OS X, BSD and Linux
|
||||
var socketAddr = makeUnixAddr(path)
|
||||
if socket.fd.bindAddr(cast[ptr SockAddr](addr socketAddr),
|
||||
sizeof(socketAddr).Socklen) != 0'i32:
|
||||
raiseOSError(osLastError())
|
||||
|
||||
when defined(ssl):
|
||||
proc handshake*(socket: Socket): bool
|
||||
{.tags: [ReadIOEffect, WriteIOEffect], deprecated.} =
|
||||
{.tags: [ReadIOEffect, WriteIOEffect], deprecated.} =
|
||||
## This proc needs to be called on a socket after it connects. This is
|
||||
## only applicable when using ``connectAsync``.
|
||||
## This proc performs the SSL handshake.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue