Skip to content

Instantly share code, notes, and snippets.

@subsetpark
Created January 5, 2018 20:28
Show Gist options
  • Save subsetpark/bfefbd31ff88e03849de12913001b9a9 to your computer and use it in GitHub Desktop.
Save subsetpark/bfefbd31ff88e03849de12913001b9a9 to your computer and use it in GitHub Desktop.
proc recv*(connection: Connection): int =
var
socket = connection.socket.get()
buffer = newSeq[byte]()
b: byte
while true:
try:
let bytesReceived = socket.recv(addr(b), 1, 1)
if bytesReceived == -1:
error "Socket disconnect on read."
connection.disconnect()
else:
buffer.add(b)
except TimeoutError:
# The socket is empty.
break
if buffer.len > 0:
connection.onDataAvailable(buffer.mapIt(it.char).join())
result = buffer.len
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment