mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-10 05:38:10 +00:00
Create IPPROTO_NONE alias & Add test for Unix socket (#24139)
closes #24116
This commit is contained in:
@@ -97,6 +97,8 @@ type
|
||||
length*: int
|
||||
addrList*: seq[string]
|
||||
|
||||
const IPPROTO_NONE* = IPPROTO_IP ## Use this if your socket type requires a protocol value of zero (e.g. Unix sockets).
|
||||
|
||||
when useWinVersion:
|
||||
let
|
||||
osInvalidSocket* = winlean.INVALID_SOCKET
|
||||
|
||||
@@ -97,7 +97,7 @@ import std/nativesockets
|
||||
import std/[os, strutils, times, sets, options, monotimes]
|
||||
import std/ssl_config
|
||||
export nativesockets.Port, nativesockets.`$`, nativesockets.`==`
|
||||
export Domain, SockType, Protocol
|
||||
export Domain, SockType, Protocol, IPPROTO_NONE
|
||||
|
||||
const useWinVersion = defined(windows) or defined(nimdoc)
|
||||
const useNimNetLite = defined(nimNetLite) or defined(freertos) or defined(zephyr) or
|
||||
|
||||
35
tests/stdlib/tunixsocket.nim
Normal file
35
tests/stdlib/tunixsocket.nim
Normal file
@@ -0,0 +1,35 @@
|
||||
import std/[assertions, net, os, osproc]
|
||||
|
||||
# XXX: Make this test run on Windows too when we add support for Unix sockets on Windows
|
||||
when defined(posix) and not defined(nimNetLite):
|
||||
const nim = getCurrentCompilerExe()
|
||||
let
|
||||
dir = currentSourcePath().parentDir()
|
||||
serverPath = dir / "unixsockettest"
|
||||
|
||||
let (_, err) = execCmdEx(nim & " c " & quoteShell(dir / "unixsockettest.nim"))
|
||||
doAssert err == 0
|
||||
|
||||
let svproc = startProcess(serverPath, workingDir = dir)
|
||||
doAssert svproc.running()
|
||||
# Wait for the server to open the socket and listen from it
|
||||
sleep(400)
|
||||
|
||||
block unixSocketSendRecv:
|
||||
let
|
||||
unixSocketPath = dir / "usox"
|
||||
socket = newSocket(AF_UNIX, SOCK_STREAM, IPPROTO_NONE)
|
||||
|
||||
socket.connectUnix(unixSocketPath)
|
||||
# for a blocking Unix socket this should never fail
|
||||
socket.send("data sent through the socket\c\l", maxRetries = 0)
|
||||
var resp: string
|
||||
socket.readLine(resp)
|
||||
doAssert resp == "Hello from server"
|
||||
|
||||
socket.send("bye\c\l")
|
||||
socket.readLine(resp)
|
||||
doAssert resp == "bye"
|
||||
socket.close()
|
||||
|
||||
svproc.close()
|
||||
26
tests/stdlib/unixsockettest.nim
Normal file
26
tests/stdlib/unixsockettest.nim
Normal file
@@ -0,0 +1,26 @@
|
||||
import std/[assertions, net, os]
|
||||
|
||||
let unixSocketPath = getCurrentDir() / "usox"
|
||||
|
||||
removeFile(unixSocketPath)
|
||||
|
||||
let socket = newSocket(AF_UNIX, SOCK_STREAM, IPPROTO_NONE)
|
||||
socket.bindUnix(unixSocketPath)
|
||||
socket.listen()
|
||||
|
||||
var
|
||||
clientSocket: Socket
|
||||
data: string
|
||||
|
||||
socket.accept(clientSocket)
|
||||
clientSocket.readLine(data)
|
||||
doAssert data == "data sent through the socket"
|
||||
clientSocket.send("Hello from server\c\l")
|
||||
|
||||
clientSocket.readLine(data)
|
||||
doAssert data == "bye"
|
||||
clientSocket.send("bye\c\l")
|
||||
|
||||
clientSocket.close()
|
||||
socket.close()
|
||||
removeFile(unixSocketPath)
|
||||
Reference in New Issue
Block a user