From 867dfbfbfa68c61a2b65d46af7c22aa47857fa21 Mon Sep 17 00:00:00 2001 From: Dominik Picheta Date: Thu, 18 Sep 2014 17:01:44 +0100 Subject: Adds socket creation for arbitrary domain, type and protocol. --- lib/pure/asyncdispatch.nim | 11 +++++++++++ lib/pure/asyncnet.nim | 4 ++++ lib/pure/net.nim | 9 +++++++++ lib/pure/rawsockets.nim | 7 +++++++ 4 files changed, 31 insertions(+) (limited to 'lib/pure') 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..cc7f096eb 100644 --- a/lib/pure/asyncnet.nim +++ b/lib/pure/asyncnet.nim @@ -101,6 +101,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: -- cgit 1.4.1-2-gfad0