Improved asyncnet docs (SSL procs now shown). Deprecated handshake.

This commit is contained in:
Dominik Picheta 2016-04-04 21:00:43 +01:00
commit 03be03c6e4
2 changed files with 15 additions and 10 deletions

View file

@ -11,7 +11,7 @@
## asynchronous dispatcher defined in the ``asyncdispatch`` module. ## asynchronous dispatcher defined in the ``asyncdispatch`` module.
## ##
## SSL ## SSL
## --- ## ----
## ##
## SSL can be enabled by compiling with the ``-d:ssl`` flag. ## SSL can be enabled by compiling with the ``-d:ssl`` flag.
## ##
@ -62,7 +62,9 @@ import os
export SOBool export SOBool
when defined(ssl): const defineSsl = defined(ssl) or defined(nimdoc)
when defineSsl:
import openssl import openssl
type type
@ -79,7 +81,7 @@ type
of false: nil of false: nil
case isSsl: bool case isSsl: bool
of true: of true:
when defined(ssl): when defineSsl:
sslHandle: SslPtr sslHandle: SslPtr
sslContext: SslContext sslContext: SslContext
bioIn: BIO bioIn: BIO
@ -125,7 +127,7 @@ proc newAsyncSocket*(domain, sockType, protocol: cint,
Domain(domain), SockType(sockType), Domain(domain), SockType(sockType),
Protocol(protocol), buffered) Protocol(protocol), buffered)
when defined(ssl): when defineSsl:
proc getSslError(handle: SslPtr, err: cint): cint = proc getSslError(handle: SslPtr, err: cint): cint =
assert err < 0 assert err < 0
var ret = SSLGetError(handle, err.cint) var ret = SSLGetError(handle, err.cint)
@ -186,7 +188,7 @@ proc connect*(socket: AsyncSocket, address: string, port: Port) {.async.} =
## or an error occurs. ## or an error occurs.
await connect(socket.fd.AsyncFD, address, port, socket.domain) await connect(socket.fd.AsyncFD, address, port, socket.domain)
if socket.isSsl: if socket.isSsl:
when defined(ssl): when defineSsl:
let flags = {SocketFlag.SafeDisconn} let flags = {SocketFlag.SafeDisconn}
sslSetConnectState(socket.sslHandle) sslSetConnectState(socket.sslHandle)
sslLoop(socket, flags, sslDoHandshake(socket.sslHandle)) sslLoop(socket, flags, sslDoHandshake(socket.sslHandle))
@ -197,7 +199,7 @@ template readInto(buf: cstring, size: int, socket: AsyncSocket,
## this is a template and not a proc. ## this is a template and not a proc.
var res = 0 var res = 0
if socket.isSsl: if socket.isSsl:
when defined(ssl): when defineSsl:
# SSL mode. # SSL mode.
sslLoop(socket, flags, sslLoop(socket, flags,
sslRead(socket.sslHandle, buf, size.cint)) sslRead(socket.sslHandle, buf, size.cint))
@ -274,7 +276,7 @@ proc send*(socket: AsyncSocket, data: string,
## data has been sent. ## data has been sent.
assert socket != nil assert socket != nil
if socket.isSsl: if socket.isSsl:
when defined(ssl): when defineSsl:
var copy = data var copy = data
sslLoop(socket, flags, sslLoop(socket, flags,
sslWrite(socket.sslHandle, addr copy[0], copy.len.cint)) sslWrite(socket.sslHandle, addr copy[0], copy.len.cint))
@ -468,7 +470,7 @@ proc close*(socket: AsyncSocket) =
## Closes the socket. ## Closes the socket.
defer: defer:
socket.fd.AsyncFD.closeSocket() socket.fd.AsyncFD.closeSocket()
when defined(ssl): when defineSsl:
if socket.isSSL: if socket.isSSL:
let res = SslShutdown(socket.sslHandle) let res = SslShutdown(socket.sslHandle)
SSLFree(socket.sslHandle) SSLFree(socket.sslHandle)
@ -478,7 +480,7 @@ proc close*(socket: AsyncSocket) =
raiseSslError() raiseSslError()
socket.closed = true # TODO: Add extra debugging checks for this. socket.closed = true # TODO: Add extra debugging checks for this.
when defined(ssl): when defineSsl:
proc wrapSocket*(ctx: SslContext, socket: AsyncSocket) = proc wrapSocket*(ctx: SslContext, socket: AsyncSocket) =
## Wraps a socket in an SSL context. This function effectively turns ## Wraps a socket in an SSL context. This function effectively turns
## ``socket`` into an SSL socket. ## ``socket`` into an SSL socket.

View file

@ -605,7 +605,8 @@ proc setSockOpt*(socket: Socket, opt: SOBool, value: bool, level = SOL_SOCKET) {
setSockOptInt(socket.fd, cint(level), toCInt(opt), valuei) setSockOptInt(socket.fd, cint(level), toCInt(opt), valuei)
when defineSsl: when defineSsl:
proc handshake*(socket: Socket): bool {.tags: [ReadIOEffect, WriteIOEffect].} = proc handshake*(socket: Socket): bool
{.tags: [ReadIOEffect, WriteIOEffect, deprecated].} =
## This proc needs to be called on a socket after it connects. This is ## This proc needs to be called on a socket after it connects. This is
## only applicable when using ``connectAsync``. ## only applicable when using ``connectAsync``.
## This proc performs the SSL handshake. ## This proc performs the SSL handshake.
@ -614,6 +615,8 @@ when defineSsl:
## ``True`` whenever handshake completed successfully. ## ``True`` whenever handshake completed successfully.
## ##
## A ESSL error is raised on any other errors. ## A ESSL error is raised on any other errors.
##
## **Note:** This procedure is deprecated since version 0.14.0.
result = true result = true
if socket.isSSL: if socket.isSSL:
var ret = SSLConnect(socket.sslHandle) var ret = SSLConnect(socket.sslHandle)