Make recvLineInto a proc instead of template

This commit is contained in:
def 2015-03-17 18:15:47 +01:00
commit e127ed77b1
2 changed files with 50 additions and 52 deletions

View file

@ -163,7 +163,7 @@ proc processClient(client: AsyncSocket, address: string,
# First line - GET /path HTTP/1.1 # First line - GET /path HTTP/1.1
line.setLen(0) line.setLen(0)
client.recvLineInto(line) # TODO: Timeouts. await client.recvLineInto(addr line) # TODO: Timeouts.
if line == "": if line == "":
client.close() client.close()
return return
@ -189,7 +189,7 @@ proc processClient(client: AsyncSocket, address: string,
while true: while true:
i = 0 i = 0
line.setLen(0) line.setLen(0)
client.recvLineInto(line) await client.recvLineInto(addr line)
if line == "": if line == "":
client.close(); return client.close(); return

View file

@ -307,8 +307,8 @@ proc accept*(socket: AsyncSocket,
retFut.complete(future.read.client) retFut.complete(future.read.client)
return retFut return retFut
template recvLineInto*(socket: AsyncSocket, resString: var string, proc recvLineInto*(socket: AsyncSocket, resString: ptr string,
flags = {SocketFlag.SafeDisconn}) = flags = {SocketFlag.SafeDisconn}): Future[void] {.async.} =
## Reads a line of data from ``socket`` into ``resString``. ## Reads a line of data from ``socket`` into ``resString``.
## ##
## If a full line is read ``\r\L`` is not ## If a full line is read ``\r\L`` is not
@ -326,25 +326,25 @@ template recvLineInto*(socket: AsyncSocket, resString: var string,
## **Warning**: ``recvLineInto`` on unbuffered sockets assumes that the ## **Warning**: ``recvLineInto`` on unbuffered sockets assumes that the
## protocol uses ``\r\L`` to delimit a new line. ## protocol uses ``\r\L`` to delimit a new line.
assert SocketFlag.Peek notin flags ## TODO: assert SocketFlag.Peek notin flags ## TODO:
result = newFuture[void]("asyncnet.recvLineInto")
template addNLIfEmpty(): stmt = template addNLIfEmpty(): stmt =
if resString.len == 0: if resString[].len == 0:
resString.add("\c\L") resString[].add("\c\L")
block recvLineInto:
if socket.isBuffered: if socket.isBuffered:
if socket.bufLen == 0: if socket.bufLen == 0:
let res = socket.readIntoBuf(flags) let res = socket.readIntoBuf(flags)
if res == 0: if res == 0:
break recvLineInto return
var lastR = false var lastR = false
while true: while true:
if socket.currPos >= socket.bufLen: if socket.currPos >= socket.bufLen:
let res = socket.readIntoBuf(flags) let res = socket.readIntoBuf(flags)
if res == 0: if res == 0:
resString.setLen(0) resString[].setLen(0)
break recvLineInto return
case socket.buffer[socket.currPos] case socket.buffer[socket.currPos]
of '\r': of '\r':
@ -353,34 +353,32 @@ template recvLineInto*(socket: AsyncSocket, resString: var string,
of '\L': of '\L':
addNLIfEmpty() addNLIfEmpty()
socket.currPos.inc() socket.currPos.inc()
break recvLineInto return
else: else:
if lastR: if lastR:
socket.currPos.inc() socket.currPos.inc()
break recvLineInto return
else: else:
resString.add socket.buffer[socket.currPos] resString[].add socket.buffer[socket.currPos]
socket.currPos.inc() socket.currPos.inc()
else: else:
var c = "" var c = ""
while true: while true:
let recvFut = recv(socket, 1, flags) let recvFut = recv(socket, 1, flags)
yield recvFut
c = recvFut.read() c = recvFut.read()
if c.len == 0: if c.len == 0:
resString.setLen(0) resString[].setLen(0)
break recvLineInto return
if c == "\r": if c == "\r":
let recvFut = recv(socket, 1, flags) # Skip \L let recvFut = recv(socket, 1, flags) # Skip \L
yield recvFut
c = recvFut.read() c = recvFut.read()
assert c == "\L" assert c == "\L"
addNLIfEmpty() addNLIfEmpty()
break recvLineInto return
elif c == "\L": elif c == "\L":
addNLIfEmpty() addNLIfEmpty()
break recvLineInto return
add(resString, c) add(resString[], c)
proc recvLine*(socket: AsyncSocket, proc recvLine*(socket: AsyncSocket,
flags = {SocketFlag.SafeDisconn}): Future[string] {.async.} = flags = {SocketFlag.SafeDisconn}): Future[string] {.async.} =
@ -407,7 +405,7 @@ proc recvLine*(socket: AsyncSocket,
assert SocketFlag.Peek notin flags ## TODO: assert SocketFlag.Peek notin flags ## TODO:
result = "" result = ""
socket.recvLineInto(result, flags) await socket.recvLineInto(addr result, flags)
proc listen*(socket: AsyncSocket, backlog = SOMAXCONN) {.tags: [ReadIOEffect].} = proc listen*(socket: AsyncSocket, backlog = SOMAXCONN) {.tags: [ReadIOEffect].} =
## Marks ``socket`` as accepting connections. ## Marks ``socket`` as accepting connections.