summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2014-09-19 01:46:33 +0200
committerAraq <rumpf_a@web.de>2014-09-19 01:46:33 +0200
commitd615a2930924274187b4ab55e4b22be8489a788e (patch)
tree1002d44f45b4a5a1b0b5f322eee40c14edd17a8d /lib/pure
parentbf2e8ee95bf714191f379d4f5f8ebcd990659033 (diff)
parent83e26cc4fa68f5af26301a75be9781bf007663c1 (diff)
downloadNim-d615a2930924274187b4ab55e4b22be8489a788e.tar.gz
Merge branch 'bigbreak' of https://github.com/Araq/Nimrod into bigbreak
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/asyncdispatch.nim11
-rw-r--r--lib/pure/asyncnet.nim7
-rw-r--r--lib/pure/net.nim9
-rw-r--r--lib/pure/rawsockets.nim7
4 files changed, 33 insertions, 1 deletions
diff --git a/lib/pure/asyncdispatch.nim b/lib/pure/asyncdispatch.nim
index cac98e160..fb0023292 100644
--- a/lib/pure/asyncdispatch.nim
+++ b/lib/pure/asyncdispatch.nim
@@ -766,6 +766,12 @@ when defined(windows) or defined(nimdoc):
 
     return retFuture
 
+  proc newAsyncRawSocket*(domain, typ, protocol: cint): TAsyncFD =
+    ## Creates a new socket and registers it with the dispatcher implicitly.
+    result = newRawSocket(domain, typ, protocol).TAsyncFD
+    result.SocketHandle.setBlocking(false)
+    register(result)
+
   proc newAsyncRawSocket*(domain: Domain = AF_INET,
                typ: SockType = SOCK_STREAM,
                protocol: Protocol = IPPROTO_TCP): TAsyncFD =
@@ -832,6 +838,11 @@ else:
     var data = PData(sock: sock, readCBs: @[], writeCBs: @[])
     p.selector.register(sock.SocketHandle, {}, data.PObject)
 
+  proc newAsyncRawSocket*(domain: cint, typ: cint, protocol: cint): TAsyncFD =
+    result = newRawSocket(domain, typ, protocol).TAsyncFD
+    result.SocketHandle.setBlocking(false)
+    register(result)
+
   proc newAsyncRawSocket*(domain: TDomain = AF_INET,
                typ: TType = SOCK_STREAM,
                protocol: TProtocol = IPPROTO_TCP): TAsyncFD =
diff --git a/lib/pure/asyncnet.nim b/lib/pure/asyncnet.nim
index c6918517e..72fe51a7e 100644
--- a/lib/pure/asyncnet.nim
+++ b/lib/pure/asyncnet.nim
@@ -31,7 +31,7 @@
 ##
 ##   import asyncnet, asyncdispatch
 ##
-##   var clients: seq[AsyncSocket] = @[]
+##   var clients {.threadvar.}: seq[AsyncSocket]
 ##
 ##   proc processClient(client: AsyncSocket) {.async.} =
 ##     while true:
@@ -40,6 +40,7 @@
 ##         await c.send(line & "\c\L")
 ##
 ##   proc serve() {.async.} =
+##     clients = @[]
 ##     var server = newAsyncSocket()
 ##     server.bindAddr(Port(12345))
 ##     server.listen()
@@ -101,6 +102,10 @@ proc newAsyncSocket*(domain: TDomain = AF_INET, typ: TType = SOCK_STREAM,
   ## Creates a new asynchronous socket.
   result = newSocket(newAsyncRawSocket(domain, typ, protocol), buffered)
 
+proc newAsyncSocket*(domain, typ, protocol: cint, buffered = true): PAsyncSocket =
+  ## Creates a new asynchronous socket.
+  result = newSocket(newAsyncRawSocket(domain, typ, protocol), buffered)
+
 when defined(ssl):
   proc getSslError(handle: SslPtr, err: cint): cint =
     assert err < 0
diff --git a/lib/pure/net.nim b/lib/pure/net.nim
index 886ed792f..60298ec88 100644
--- a/lib/pure/net.nim
+++ b/lib/pure/net.nim
@@ -108,6 +108,15 @@ proc createSocket(fd: SocketHandle, isBuff: bool): Socket =
   if isBuff:
     result.currPos = 0
 
+proc newSocket*(domain, typ, protocol: cint, buffered = true): Socket =
+  ## Creates a new socket.
+  ##
+  ## If an error occurs EOS will be raised.
+  let fd = newRawSocket(domain, typ, protocol)
+  if fd == osInvalidSocket:
+    raiseOSError(osLastError())
+  result = createSocket(fd, buffered)
+
 proc newSocket*(domain: Domain = AF_INET, typ: SockType = SOCK_STREAM,
              protocol: Protocol = IPPROTO_TCP, buffered = true): Socket =
   ## Creates a new socket.
diff --git a/lib/pure/rawsockets.nim b/lib/pure/rawsockets.nim
index c76e99fcb..5756ca23d 100644
--- a/lib/pure/rawsockets.nim
+++ b/lib/pure/rawsockets.nim
@@ -153,6 +153,13 @@ proc newRawSocket*(domain: Domain = AF_INET, typ: SockType = SOCK_STREAM,
   ## Creates a new socket; returns `InvalidSocket` if an error occurs.
   socket(toInt(domain), toInt(typ), toInt(protocol))
 
+proc newRawSocket*(domain: cint, typ: cint, protocol: cint): SocketHandle =
+  ## Creates a new socket; returns `InvalidSocket` if an error occurs.
+  ##
+  ## Use this overload if one of the enums specified above does
+  ## not contain what you need.
+  socket(domain, typ, protocol)
+
 proc close*(socket: SocketHandle) =
   ## closes a socket.
   when useWinVersion: