mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-30 09:54:49 +00:00
This change was done to avoid confusion with TCP/IP raw sockets. Native sockets module represents handling native system low level socket API in general and is not just limited anyhow to TCP/IP raw sockets. A stub lib/deprecated/pure/rawsockets.nim module has been added as compatibility layer for old code using rawsockets, so this change will not break existing code.
66 lines
1.6 KiB
Nim
66 lines
1.6 KiB
Nim
discard """
|
|
file: "tasyncawait.nim"
|
|
output: "5000"
|
|
"""
|
|
import asyncdispatch, nativesockets, net, strutils, os
|
|
|
|
var msgCount = 0
|
|
|
|
const
|
|
swarmSize = 50
|
|
messagesToSend = 100
|
|
|
|
var clientCount = 0
|
|
|
|
proc sendMessages(client: TAsyncFD) {.async.} =
|
|
for i in 0 .. <messagesToSend:
|
|
await send(client, "Message " & $i & "\c\L")
|
|
|
|
proc launchSwarm(port: TPort) {.async.} =
|
|
for i in 0 .. <swarmSize:
|
|
var sock = newAsyncNativeSocket()
|
|
|
|
await connect(sock, "localhost", port)
|
|
await sendMessages(sock)
|
|
closeSocket(sock)
|
|
|
|
proc readMessages(client: TAsyncFD) {.async.} =
|
|
while true:
|
|
var line = await recvLine(client)
|
|
if line == "":
|
|
closeSocket(client)
|
|
clientCount.inc
|
|
break
|
|
else:
|
|
if line.startswith("Message "):
|
|
msgCount.inc
|
|
else:
|
|
doAssert false
|
|
|
|
proc createServer(port: TPort) {.async.} =
|
|
var server = newAsyncNativeSocket()
|
|
block:
|
|
var name: Sockaddr_in
|
|
when defined(windows):
|
|
name.sin_family = toInt(AF_INET).int16
|
|
else:
|
|
name.sin_family = toInt(AF_INET)
|
|
name.sin_port = htons(int16(port))
|
|
name.sin_addr.s_addr = htonl(INADDR_ANY)
|
|
if bindAddr(server.SocketHandle, cast[ptr SockAddr](addr(name)),
|
|
sizeof(name).Socklen) < 0'i32:
|
|
raiseOSError(osLastError())
|
|
|
|
discard server.SocketHandle.listen()
|
|
while true:
|
|
asyncCheck readMessages(await accept(server))
|
|
|
|
asyncCheck createServer(TPort(10335))
|
|
asyncCheck launchSwarm(TPort(10335))
|
|
while true:
|
|
poll()
|
|
if clientCount == swarmSize: break
|
|
|
|
assert msgCount == swarmSize * messagesToSend
|
|
echo msgCount
|