Ftpclient now fully works both synchronously and asynchronously.

Fixed some deprecation doc messages. And some issues with asyncio.
This commit is contained in:
Dominik Picheta 2012-09-22 16:39:51 +01:00
commit 6c6f9e6674
3 changed files with 190 additions and 164 deletions

View file

@ -215,8 +215,12 @@ proc asyncSockHandleWrite(h: PObject) =
if PAsyncSocket(h).info == SockConnecting: if PAsyncSocket(h).info == SockConnecting:
PAsyncSocket(h).handleConnect(PAsyncSocket(h)) PAsyncSocket(h).handleConnect(PAsyncSocket(h))
# Stop receiving write events PAsyncSocket(h).info = SockConnected
# Stop receiving write events if there is no handleWrite event.
if PAsyncSocket(h).handleWrite == nil:
PAsyncSocket(h).deleg.mode = fmRead PAsyncSocket(h).deleg.mode = fmRead
else:
PAsyncSocket(h).deleg.mode = fmReadWrite
else: else:
if PAsyncSocket(h).handleWrite != nil: if PAsyncSocket(h).handleWrite != nil:
PAsyncSocket(h).handleWrite(PAsyncSocket(h)) PAsyncSocket(h).handleWrite(PAsyncSocket(h))
@ -345,7 +349,7 @@ proc acceptAddr*(server: PAsyncSocket): tuple[sock: PAsyncSocket,
address: string] {.deprecated.} = address: string] {.deprecated.} =
## Equivalent to ``sockets.acceptAddr``. ## Equivalent to ``sockets.acceptAddr``.
## ##
## **Warning**: This is deprecated in favour of the above. ## **Deprecated since version 0.9.0:** Please use the function above.
var client = newAsyncSocket() var client = newAsyncSocket()
var address: string = "" var address: string = ""
acceptAddr(server, client, address) acceptAddr(server, client, address)
@ -354,7 +358,7 @@ proc acceptAddr*(server: PAsyncSocket): tuple[sock: PAsyncSocket,
proc accept*(server: PAsyncSocket): PAsyncSocket {.deprecated.} = proc accept*(server: PAsyncSocket): PAsyncSocket {.deprecated.} =
## Equivalent to ``sockets.accept``. ## Equivalent to ``sockets.accept``.
## ##
## **Warning**: This is deprecated. ## **Deprecated since version 0.9.0:** Please use the function above.
new(result) new(result)
var address = "" var address = ""
server.acceptAddr(result, address) server.acceptAddr(result, address)
@ -510,6 +514,7 @@ proc poll*(d: PDispatcher, timeout: int = 500): bool =
# File/socket has been closed. Remove it from dispatcher. # File/socket has been closed. Remove it from dispatcher.
d.delegates[dc] = d.delegates[len-1] d.delegates[dc] = d.delegates[len-1]
dec len dec len
d.delegates.setLen(len) d.delegates.setLen(len)
var hasDataBufferedCount = 0 var hasDataBufferedCount = 0
@ -527,6 +532,7 @@ proc poll*(d: PDispatcher, timeout: int = 500): bool =
for i in 0..len(d.delegates)-1: for i in 0..len(d.delegates)-1:
if i > len(d.delegates)-1: break # One delegate might've been removed. if i > len(d.delegates)-1: break # One delegate might've been removed.
let deleg = d.delegates[i] let deleg = d.delegates[i]
if not deleg.open: continue # This delegate might've been closed.
if (deleg.mode != fmWrite or deleg.mode != fmAppend) and if (deleg.mode != fmWrite or deleg.mode != fmAppend) and
deleg notin readDg: deleg notin readDg:
deleg.handleRead(deleg.deleVal) deleg.handleRead(deleg.deleVal)

View file

@ -29,14 +29,17 @@ import sockets, strutils, parseutils, times, os, asyncio
type type
TFTPClient* = object of TObject TFTPClient* = object of TObject
csock: TSocket # Command connection socket
case isAsync: bool case isAsync: bool
of false: of false:
csock: TSocket # Command connection socket
dsock: TSocket # Data connection socket dsock: TSocket # Data connection socket
else: else:
dummyA, dummyB: pointer # workaround a Nimrod API issue dummyA, dummyB: pointer # workaround a Nimrod API issue
asyncCSock: PAsyncSocket # csock belongs to this. asyncCSock: PAsyncSocket
asyncDSock: PAsyncSocket asyncDSock: PAsyncSocket
handleEvent*: proc (ftp: PAsyncFTPClient, ev: TFTPEvent) {.closure.}
disp: PDispatcher
asyncDSockID: PDelegate
user, pass: string user, pass: string
address: string address: string
port: TPort port: TPort
@ -46,11 +49,13 @@ type
dsockConnected: bool dsockConnected: bool
FTPJobType = enum PFTPClient* = ref TFTPClient
FTPJobType* = enum
JRetrText, JRetr, JStore JRetrText, JRetr, JStore
TFTPJob = object TFTPJob = object
prc: proc (ftp: var TFTPClient, async: bool): bool {.nimcall.} prc: proc (ftp: PFTPClient, async: bool): bool {.nimcall.}
case typ*: FTPJobType case typ*: FTPJobType
of JRetrText: of JRetrText:
lines: string lines: string
@ -66,9 +71,6 @@ type
PAsyncFTPClient* = ref TAsyncFTPClient ## Async alternative to TFTPClient. PAsyncFTPClient* = ref TAsyncFTPClient ## Async alternative to TFTPClient.
TAsyncFTPClient* = object of TFTPClient TAsyncFTPClient* = object of TFTPClient
handleEvent*: proc (ftp: var TAsyncFTPClient, ev: TFTPEvent) {.closure.}
disp: PDispatcher
asyncCSockID: PDelegate
FTPEventType* = enum FTPEventType* = enum
EvTransferProgress, EvLines, EvRetr, EvStore EvTransferProgress, EvLines, EvRetr, EvStore
@ -83,6 +85,7 @@ type
bytesTotal*: biggestInt ## Bytes total. bytesTotal*: biggestInt ## Bytes total.
bytesFinished*: biggestInt ## Bytes transferred. bytesFinished*: biggestInt ## Bytes transferred.
speed*: biggestInt ## Speed in bytes/s speed*: biggestInt ## Speed in bytes/s
currentJob*: FTPJobType ## The current job being performed.
EInvalidReply* = object of ESynch EInvalidReply* = object of ESynch
EFTP* = object of ESynch EFTP* = object of ESynch
@ -97,10 +100,14 @@ proc FTPClient*(address: string, port = TPort(21),
result.isAsync = false result.isAsync = false
result.dsockConnected = false result.dsockConnected = false
result.csock = socket()
proc getDSock(ftp: var TFTPClient): TSocket = proc getDSock(ftp: PFTPClient): TSocket =
if ftp.isAsync: return ftp.asyncDSock else: return ftp.dsock if ftp.isAsync: return ftp.asyncDSock else: return ftp.dsock
proc getCSock(ftp: PFTPClient): TSocket =
if ftp.isAsync: return ftp.asyncCSock else: return ftp.csock
template blockingOperation(sock: TSocket, body: stmt) = template blockingOperation(sock: TSocket, body: stmt) =
if ftp.isAsync: if ftp.isAsync:
sock.setBlocking(true) sock.setBlocking(true)
@ -108,15 +115,15 @@ template blockingOperation(sock: TSocket, body: stmt) =
if ftp.isAsync: if ftp.isAsync:
sock.setBlocking(false) sock.setBlocking(false)
proc expectReply(ftp: var TFTPClient): TaintedString = proc expectReply(ftp: PFTPClient): TaintedString =
result = TaintedString"" result = TaintedString""
blockingOperation(ftp.csock): blockingOperation(ftp.getCSock()):
if not ftp.csock.recvLine(result): setLen(result.string, 0) if not ftp.getCSock().recvLine(result): setLen(result.string, 0)
proc send*(ftp: var TFTPClient, m: string): TaintedString = proc send*(ftp: PFTPClient, m: string): TaintedString =
## Send a message to the server, and wait for a primary reply. ## Send a message to the server, and wait for a primary reply.
## ``\c\L`` is added for you. ## ``\c\L`` is added for you.
ftp.csock.send(m & "\c\L") ftp.getCSock().send(m & "\c\L")
return ftp.expectReply() return ftp.expectReply()
proc assertReply(received: TaintedString, expected: string) = proc assertReply(received: TaintedString, expected: string) =
@ -132,8 +139,8 @@ proc assertReply(received: TaintedString, expected: varargs[string]) =
"Expected reply '$1' got: $2" % "Expected reply '$1' got: $2" %
[expected.join("' or '"), received.string]) [expected.join("' or '"), received.string])
proc createJob(ftp: var TFTPClient, proc createJob(ftp: PFTPClient,
prc: proc (ftp: var TFTPClient, async: bool): bool {.nimcall.}, prc: proc (ftp: PFTPClient, async: bool): bool {.nimcall.},
cmd: FTPJobType) = cmd: FTPJobType) =
if ftp.jobInProgress: if ftp.jobInProgress:
raise newException(EFTP, "Unable to do two jobs at once.") raise newException(EFTP, "Unable to do two jobs at once.")
@ -147,7 +154,7 @@ proc createJob(ftp: var TFTPClient,
of JRetr, JStore: of JRetr, JStore:
ftp.job.toStore = "" ftp.job.toStore = ""
proc deleteJob(ftp: var TFTPClient) = proc deleteJob(ftp: PFTPClient) =
assert ftp.jobInProgress assert ftp.jobInProgress
ftp.jobInProgress = false ftp.jobInProgress = false
case ftp.job.typ case ftp.job.typ
@ -155,12 +162,63 @@ proc deleteJob(ftp: var TFTPClient) =
ftp.job.lines = "" ftp.job.lines = ""
of JRetr, JStore: of JRetr, JStore:
ftp.job.file.close() ftp.job.file.close()
if ftp.isAsync:
ftp.asyncDSock.close()
else:
ftp.dsock.close()
proc pasv(ftp: var TFTPClient) = proc handleTask(s: PAsyncSocket, ftp: PFTPClient) =
if ftp.jobInProgress:
if ftp.job.typ in {JRetr, JStore}:
if epochTime() - ftp.job.lastProgressReport >= 1.0:
var r: TFTPEvent
ftp.job.lastProgressReport = epochTime()
r.typ = EvTransferProgress
r.bytesTotal = ftp.job.total
r.bytesFinished = ftp.job.progress
r.speed = ftp.job.oneSecond
r.filename = ftp.job.filename
r.currentJob = ftp.job.typ
ftp.job.oneSecond = 0
ftp.handleEvent(PAsyncFTPClient(ftp), r)
proc handleWrite(s: PAsyncSocket, ftp: PFTPClient) =
if ftp.jobInProgress:
if ftp.job.typ == JStore:
assert (not ftp.job.prc(ftp, true))
proc handleConnect(s: PAsyncSocket, ftp: PFTPClient) =
ftp.dsockConnected = true
assert(ftp.jobInProgress)
if ftp.job.typ == JStore:
s.setHandleWrite(proc (s: PAsyncSocket) = handleWrite(s, ftp))
else:
s.delHandleWrite()
proc handleRead(s: PAsyncSocket, ftp: PFTPClient) =
assert ftp.jobInProgress
assert ftp.job.typ != JStore
# This can never return true, because it shouldn't check for code
# 226 from csock.
assert(not ftp.job.prc(ftp, true))
proc pasv(ftp: PFTPClient) =
## Negotiate a data connection. ## Negotiate a data connection.
if not ftp.isAsync: if not ftp.isAsync:
ftp.dsock = socket() ftp.dsock = socket()
else: ftp.asyncDSock = AsyncSocket() else:
ftp.asyncDSock = AsyncSocket()
ftp.asyncDSock.handleRead =
proc (s: PAsyncSocket) =
handleRead(s, ftp)
ftp.asyncDSock.handleConnect =
proc (s: PAsyncSocket) =
handleConnect(s, ftp)
ftp.asyncDSock.handleTask =
proc (s: PAsyncSocket) =
handleTask(s, ftp)
ftp.disp.register(ftp.asyncDSock)
var pasvMsg = ftp.send("PASV").string.strip.TaintedString var pasvMsg = ftp.send("PASV").string.strip.TaintedString
assertReply(pasvMsg, "227") assertReply(pasvMsg, "227")
var betweenParens = captureBetween(pasvMsg.string, '(', ')') var betweenParens = captureBetween(pasvMsg.string, '(', ')')
@ -178,13 +236,12 @@ proc pasv(ftp: var TFTPClient) =
proc normalizePathSep(path: string): string = proc normalizePathSep(path: string): string =
return replace(path, '\\', '/') return replace(path, '\\', '/')
proc connect*(ftp: var TFTPClient) = proc connect*(ftp: PFTPClient) =
## Connect to the FTP server specified by ``ftp``. ## Connect to the FTP server specified by ``ftp``.
if ftp.isAsync: if ftp.isAsync:
ftp.asyncCSock = AsyncSocket() blockingOperation(ftp.asyncCSock):
ftp.asyncCSock.connect(ftp.address, ftp.port)
else: else:
ftp.csock = socket()
blockingOperation(ftp.csock):
ftp.csock.connect(ftp.address, ftp.port) ftp.csock.connect(ftp.address, ftp.port)
# TODO: Handle 120? or let user handle it. # TODO: Handle 120? or let user handle it.
@ -196,21 +253,21 @@ proc connect*(ftp: var TFTPClient) =
if ftp.pass != "": if ftp.pass != "":
assertReply ftp.send("PASS " & ftp.pass), "230" assertReply ftp.send("PASS " & ftp.pass), "230"
proc pwd*(ftp: var TFTPClient): string = proc pwd*(ftp: PFTPClient): string =
## Returns the current working directory. ## Returns the current working directory.
var wd = ftp.send("PWD") var wd = ftp.send("PWD")
assertReply wd, "257" assertReply wd, "257"
return wd.string.captureBetween('"') # " return wd.string.captureBetween('"') # "
proc cd*(ftp: var TFTPClient, dir: string) = proc cd*(ftp: PFTPClient, dir: string) =
## Changes the current directory on the remote FTP server to ``dir``. ## Changes the current directory on the remote FTP server to ``dir``.
assertReply ftp.send("CWD " & dir.normalizePathSep), "250" assertReply ftp.send("CWD " & dir.normalizePathSep), "250"
proc cdup*(ftp: var TFTPClient) = proc cdup*(ftp: PFTPClient) =
## Changes the current directory to the parent of the current directory. ## Changes the current directory to the parent of the current directory.
assertReply ftp.send("CDUP"), "200" assertReply ftp.send("CDUP"), "200"
proc getLines(ftp: var TFTPClient, async: bool = false): bool = proc getLines(ftp: PFTPClient, async: bool = false): bool =
## Downloads text data in ASCII mode ## Downloads text data in ASCII mode
## Returns true if the download is complete. ## Returns true if the download is complete.
## It doesn't if `async` is true, because it doesn't check for 226 then. ## It doesn't if `async` is true, because it doesn't check for 226 then.
@ -223,14 +280,14 @@ proc getLines(ftp: var TFTPClient, async: bool = false): bool =
ftp.dsockConnected = False ftp.dsockConnected = False
if not async: if not async:
var readSocks: seq[TSocket] = @[ftp.csock] var readSocks: seq[TSocket] = @[ftp.getCSock()]
# This is only needed here. Asyncio gets this socket... # This is only needed here. Asyncio gets this socket...
blockingOperation(ftp.csock): blockingOperation(ftp.getCSock()):
if readSocks.select(1) != 0 and ftp.csock notin readSocks: if readSocks.select(1) != 0 and ftp.getCSock() notin readSocks:
assertReply ftp.expectReply(), "226" assertReply ftp.expectReply(), "226"
return true return true
proc listDirs*(ftp: var TFTPClient, dir: string = "", proc listDirs*(ftp: PFTPClient, dir: string = "",
async = false): seq[string] = async = false): seq[string] =
## Returns a list of filenames in the given directory. If ``dir`` is "", ## Returns a list of filenames in the given directory. If ``dir`` is "",
## the current directory is used. If ``async`` is true, this ## the current directory is used. If ``async`` is true, this
@ -248,7 +305,7 @@ proc listDirs*(ftp: var TFTPClient, dir: string = "",
ftp.deleteJob() ftp.deleteJob()
else: return @[] else: return @[]
proc fileExists*(ftp: var TFTPClient, file: string): bool {.deprecated.} = proc fileExists*(ftp: PFTPClient, file: string): bool {.deprecated.} =
## **Deprecated since version 0.9.0:** Please use ``existsFile``. ## **Deprecated since version 0.9.0:** Please use ``existsFile``.
## ##
## Determines whether ``file`` exists. ## Determines whether ``file`` exists.
@ -259,7 +316,7 @@ proc fileExists*(ftp: var TFTPClient, file: string): bool {.deprecated.} =
for f in items(files): for f in items(files):
if f.normalizePathSep == file.normalizePathSep: return true if f.normalizePathSep == file.normalizePathSep: return true
proc existsFile*(ftp: var TFTPClient, file: string): bool = proc existsFile*(ftp: PFTPClient, file: string): bool =
## Determines whether ``file`` exists. ## Determines whether ``file`` exists.
## ##
## Warning: This function may block. Especially on directories with many ## Warning: This function may block. Especially on directories with many
@ -268,7 +325,7 @@ proc existsFile*(ftp: var TFTPClient, file: string): bool =
for f in items(files): for f in items(files):
if f.normalizePathSep == file.normalizePathSep: return true if f.normalizePathSep == file.normalizePathSep: return true
proc createDir*(ftp: var TFTPClient, dir: string, recursive: bool = false) = proc createDir*(ftp: PFTPClient, dir: string, recursive: bool = false) =
## Creates a directory ``dir``. If ``recursive`` is true, the topmost ## Creates a directory ``dir``. If ``recursive`` is true, the topmost
## subdirectory of ``dir`` will be created first, following the secondmost... ## subdirectory of ``dir`` will be created first, following the secondmost...
## etc. this allows you to give a full path as the ``dir`` without worrying ## etc. this allows you to give a full path as the ``dir`` without worrying
@ -285,7 +342,7 @@ proc createDir*(ftp: var TFTPClient, dir: string, recursive: bool = false) =
previousDirs.add('/') previousDirs.add('/')
assertReply reply, "257" assertReply reply, "257"
proc chmod*(ftp: var TFTPClient, path: string, proc chmod*(ftp: PFTPClient, path: string,
permissions: set[TFilePermission]) = permissions: set[TFilePermission]) =
## Changes permission of ``path`` to ``permissions``. ## Changes permission of ``path`` to ``permissions``.
var userOctal = 0 var userOctal = 0
@ -307,7 +364,7 @@ proc chmod*(ftp: var TFTPClient, path: string,
assertReply ftp.send("SITE CHMOD " & perm & assertReply ftp.send("SITE CHMOD " & perm &
" " & path.normalizePathSep), "200" " " & path.normalizePathSep), "200"
proc list*(ftp: var TFTPClient, dir: string = "", async = false): string = proc list*(ftp: PFTPClient, dir: string = "", async = false): string =
## Lists all files in ``dir``. If ``dir`` is ``""``, uses the current ## Lists all files in ``dir``. If ``dir`` is ``""``, uses the current
## working directory. If ``async`` is true, this function will return ## working directory. If ``async`` is true, this function will return
## immediately and it will be your job to call asyncio's ## immediately and it will be your job to call asyncio's
@ -324,7 +381,7 @@ proc list*(ftp: var TFTPClient, dir: string = "", async = false): string =
else: else:
return "" return ""
proc retrText*(ftp: var TFTPClient, file: string, async = false): string = proc retrText*(ftp: PFTPClient, file: string, async = false): string =
## Retrieves ``file``. File must be ASCII text. ## Retrieves ``file``. File must be ASCII text.
## If ``async`` is true, this function will return immediately and ## If ``async`` is true, this function will return immediately and
## it will be your job to call ``poll`` to progress this operation. ## it will be your job to call ``poll`` to progress this operation.
@ -339,7 +396,7 @@ proc retrText*(ftp: var TFTPClient, file: string, async = false): string =
else: else:
return "" return ""
proc getFile(ftp: var TFTPClient, async = false): bool = proc getFile(ftp: PFTPClient, async = false): bool =
if ftp.dsockConnected: if ftp.dsockConnected:
var r = "".TaintedString var r = "".TaintedString
var returned = false var returned = false
@ -358,13 +415,13 @@ proc getFile(ftp: var TFTPClient, async = false): bool =
ftp.dsockConnected = False ftp.dsockConnected = False
if not async: if not async:
var readSocks: seq[TSocket] = @[ftp.csock] var readSocks: seq[TSocket] = @[ftp.getCSock()]
blockingOperation(ftp.csock): blockingOperation(ftp.getCSock()):
if readSocks.select(1) != 0 and ftp.csock notin readSocks: if readSocks.select(1) != 0 and ftp.getCSock() notin readSocks:
assertReply ftp.expectReply(), "226" assertReply ftp.expectReply(), "226"
return true return true
proc retrFile*(ftp: var TFTPClient, file, dest: string, async = false) = proc retrFile*(ftp: PFTPClient, file, dest: string, async = false) =
## Downloads ``file`` and saves it to ``dest``. Usage of this function ## Downloads ``file`` and saves it to ``dest``. Usage of this function
## asynchronously is recommended to view the progress of the download. ## asynchronously is recommended to view the progress of the download.
## The ``EvRetr`` event is given by ``poll`` when the download is finished, ## The ``EvRetr`` event is given by ``poll`` when the download is finished,
@ -388,7 +445,7 @@ proc retrFile*(ftp: var TFTPClient, file, dest: string, async = false) =
while not ftp.job.prc(ftp, false): nil while not ftp.job.prc(ftp, false): nil
ftp.deleteJob() ftp.deleteJob()
proc doUpload(ftp: var TFTPClient, async = false): bool = proc doUpload(ftp: PFTPClient, async = false): bool =
if ftp.dsockConnected: if ftp.dsockConnected:
if ftp.job.toStore.len() > 0: if ftp.job.toStore.len() > 0:
assert(async) assert(async)
@ -403,7 +460,7 @@ proc doUpload(ftp: var TFTPClient, async = false): bool =
setLen(s, len) setLen(s, len)
if len == 0: if len == 0:
# File finished uploading. # File finished uploading.
getDSock(ftp).close() if ftp.isAsync: ftp.asyncDSock.close() else: ftp.dsock.close()
ftp.dsockConnected = false ftp.dsockConnected = false
if not async: if not async:
@ -420,7 +477,7 @@ proc doUpload(ftp: var TFTPClient, async = false): bool =
ftp.job.progress.inc(len) ftp.job.progress.inc(len)
ftp.job.oneSecond.inc(len) ftp.job.oneSecond.inc(len)
proc store*(ftp: var TFTPClient, file, dest: string, async = false) = proc store*(ftp: PFTPClient, file, dest: string, async = false) =
## Uploads ``file`` to ``dest`` on the remote FTP server. Usage of this ## Uploads ``file`` to ``dest`` on the remote FTP server. Usage of this
## function asynchronously is recommended to view the progress of ## function asynchronously is recommended to view the progress of
## the download. ## the download.
@ -439,26 +496,16 @@ proc store*(ftp: var TFTPClient, file, dest: string, async = false) =
while not ftp.job.prc(ftp, false): nil while not ftp.job.prc(ftp, false): nil
ftp.deleteJob() ftp.deleteJob()
proc close*(ftp: var TFTPClient) = proc close*(ftp: PFTPClient) =
## Terminates the connection to the server. ## Terminates the connection to the server.
assertReply ftp.send("QUIT"), "221" assertReply ftp.send("QUIT"), "221"
if ftp.jobInProgress: ftp.deleteJob() if ftp.jobInProgress: ftp.deleteJob()
if ftp.isAsync:
ftp.asyncCSock.close()
ftp.asyncDSock.close()
else:
ftp.csock.close() ftp.csock.close()
getDSock(ftp).close() ftp.dsock.close()
proc handleTask(s: PAsyncSocket, ftp: PAsyncFTPClient) =
if ftp.jobInProgress:
if ftp.job.typ in {JRetr, JStore}:
if epochTime() - ftp.job.lastProgressReport >= 1.0:
var r: TFTPEvent
ftp.job.lastProgressReport = epochTime()
r.typ = EvTransferProgress
r.bytesTotal = ftp.job.total
r.bytesFinished = ftp.job.progress
r.speed = ftp.job.oneSecond
r.filename = ftp.job.filename
ftp.job.oneSecond = 0
ftp.handleEvent(ftp[], r)
discard """proc getSocket(h: PObject): tuple[info: TInfo, sock: TSocket] = discard """proc getSocket(h: PObject): tuple[info: TInfo, sock: TSocket] =
result = (SockIdle, InvalidSocket) result = (SockIdle, InvalidSocket)
@ -470,14 +517,9 @@ discard """proc getSocket(h: PObject): tuple[info: TInfo, sock: TSocket] =
result = (ftp.dsockStatus, ftp.dsock) result = (ftp.dsockStatus, ftp.dsock)
else: result = (SockIdle, ftp.dsock)""" else: result = (SockIdle, ftp.dsock)"""
proc handleWrite(s: PAsyncSocket, ftp: PAsyncFTPClient) =
if ftp.jobInProgress:
if ftp.job.typ == JStore:
assert (not ftp.job.prc(ftp[], true))
proc csockHandleRead(s: PAsyncSocket, ftp: PAsyncFTPClient) = proc csockHandleRead(s: PAsyncSocket, ftp: PAsyncFTPClient) =
assert(ftp.jobInProgress) if ftp.jobInProgress:
assertReply ftp[].expectReply(), "226" # Make sure the transfer completed. assertReply ftp.expectReply(), "226" # Make sure the transfer completed.
var r: TFTPEvent var r: TFTPEvent
case ftp.job.typ case ftp.job.typ
of JRetrText: of JRetrText:
@ -493,28 +535,9 @@ proc csockHandleRead(s: PAsyncSocket, ftp: PAsyncFTPClient) =
r.filename = ftp.job.filename r.filename = ftp.job.filename
if ftp.job.progress != ftp.job.total: if ftp.job.progress != ftp.job.total:
raise newException(EFTP, "Didn't upload full file.") raise newException(EFTP, "Didn't upload full file.")
ftp[].deleteJob() ftp.deleteJob()
# Unregister asyncCSock
ftp.disp.unregister(ftp.asyncCSockID)
ftp.asyncCSockID = nil
ftp.handleEvent(ftp[], r) ftp.handleEvent(ftp, r)
proc handleConnect(s: PAsyncSocket, ftp: PAsyncFTPClient) =
ftp.dsockConnected = true
assert(ftp.jobInProgress)
if ftp.job.typ == JStore:
s.setHandleWrite(proc (s: PAsyncSocket) = handleWrite(s, ftp))
else:
s.delHandleWrite()
# Wrap c sock in a PAsyncSocket and add it to dispatcher.
assert ftp.disp != nil
assert ftp.asyncCSockID == nil
ftp.asyncCSock = ftp.csock.toAsyncSocket(state = SockConnected)
ftp.asyncCSock.handleRead =
proc (s: PAsyncSocket) =
csockHandleRead(s, ftp)
ftp.asyncCSockID = ftp.disp.register(ftp.asyncCSock)
discard """proc handleConnect(h: PObject) = discard """proc handleConnect(h: PObject) =
var ftp = PAsyncFTPClient(h) var ftp = PAsyncFTPClient(h)
@ -525,13 +548,6 @@ discard """proc handleConnect(h: PObject) =
else: else:
ftp.dele.mode = MReadable""" ftp.dele.mode = MReadable"""
proc handleRead(s: PAsyncSocket, ftp: PAsyncFTPClient) =
assert ftp.jobInProgress
assert ftp.job.typ != JStore
# This can never return true, because it shouldn't check for code
# 226 from csock.
assert(not ftp.job.prc(ftp[], true))
discard """proc handleRead(h: PObject) = discard """proc handleRead(h: PObject) =
var ftp = PAsyncFTPClient(h) var ftp = PAsyncFTPClient(h)
assert(ftp.jobInProgress) assert(ftp.jobInProgress)
@ -551,74 +567,78 @@ discard """proc csockGetSocket(h: PObject): tuple[info: TInfo, sock: TSocket] =
proc AsyncFTPClient*(address: string, port = TPort(21), proc AsyncFTPClient*(address: string, port = TPort(21),
user, pass = "", user, pass = "",
handleEvent: proc (ftp: var TAsyncFTPClient, ev: TFTPEvent) {.closure.} = handleEvent: proc (ftp: PAsyncFTPClient, ev: TFTPEvent) {.closure.} =
(proc (ftp: var TAsyncFTPClient, ev: TFTPEvent) = nil)): PAsyncFTPClient = (proc (ftp: PAsyncFTPClient, ev: TFTPEvent) = nil)): PAsyncFTPClient =
## Create a ``PAsyncFTPClient`` object. ## Create a ``PAsyncFTPClient`` object.
## ##
## Use this if you want to use asyncio's dispatcher. ## Use this if you want to use asyncio's dispatcher.
new(result) var dres: PAsyncFTPClient
result.user = user new(dres)
result.pass = pass dres.user = user
result.address = address dres.pass = pass
result.port = port dres.address = address
result.isAsync = true dres.port = port
result.dsockConnected = false dres.isAsync = true
result.handleEvent = handleEvent dres.dsockConnected = false
dres.handleEvent = handleEvent
dres.asyncCSock = AsyncSocket()
dres.asyncCSock.handleRead =
proc (s: PAsyncSocket) =
csockHandleRead(s, dres)
result = dres
proc register*(d: PDispatcher, ftp: PAsyncFTPClient) = proc register*(d: PDispatcher, ftp: PAsyncFTPClient): PDelegate {.discardable.} =
## Registers ``ftp`` with dispatcher ``d``. ## Registers ``ftp`` with dispatcher ``d``.
assert ftp.isAsync assert ftp.isAsync
ftp.disp = d ftp.disp = d
ftp.asyncDSock.handleRead = return ftp.disp.register(ftp.asyncCSock)
proc (s: PAsyncSocket) =
handleRead(s, ftp)
ftp.asyncDSock.handleConnect =
proc (s: PAsyncSocket) =
handleConnect(s, ftp)
ftp.asyncDSock.handleTask =
proc (s: PAsyncSocket) =
handleTask(s, ftp)
d.register(ftp.asyncDSock)
when isMainModule: when isMainModule:
var ftp = FTPClient("picheta.me", user = "blah", pass = "sd") var d = newDispatcher()
ftp.connect() let hev =
echo ftp.pwd() proc (ftp: PAsyncFTPClient, event: TFTPEvent) =
echo ftp.list()
echo("uploading")
ftp.store("payload.avi", "payload.avi", async = false)
discard """
while True:
var event: TFTPEvent
if ftp.poll(event):
case event.typ case event.typ
of EvStore: of EvStore:
echo("Upload finished!") echo("Upload finished!")
break ftp.retrFile("payload.JPG", "payload2.JPG", async = true)
of EvTransferProgress: of EvTransferProgress:
var time: int64 = -1 var time: int64 = -1
if event.speed != 0: if event.speed != 0:
time = (event.bytesTotal - event.bytesFinished) div event.speed time = (event.bytesTotal - event.bytesFinished) div event.speed
echo(event.currentJob)
echo(event.speed div 1000, " kb/s. - ", echo(event.speed div 1000, " kb/s. - ",
event.bytesFinished, "/", event.bytesTotal, event.bytesFinished, "/", event.bytesTotal,
" - ", time, " seconds") " - ", time, " seconds")
echo(d.len)
else: assert(false)
"""
echo("Upload complete")
ftp.retrFile("payload.avi", "payload2.avi", async = false)
discard """
while True:
var event: TFTPEvent
if ftp.poll(event):
case event.typ
of EvRetr: of EvRetr:
echo("Download finished!") echo("Download finished!")
break ftp.close()
of EvTransferProgress: echo d.len
echo(event.speed div 1000, " kb/s")
else: assert(false) else: assert(false)
""" var ftp = AsyncFTPClient("picheta.me", user = "test", pass = "asf", handleEvent = hev)
d.register(ftp)
d.len.echo()
ftp.connect()
echo "connected"
ftp.store("payload.JPG", "payload.JPG", async = true)
d.len.echo()
echo "uploading..."
while true:
if not d.poll(): break
when isMainModule and false:
var ftp = FTPClient("picheta.me", user = "asdasd", pass = "asfwq")
ftp.connect()
echo ftp.pwd()
echo ftp.list()
echo("uploading")
ftp.store("payload.JPG", "payload.JPG", async = false)
echo("Upload complete")
ftp.retrFile("payload.JPG", "payload2.JPG", async = false)
echo("Download complete") echo("Download complete")
sleep(5000) sleep(5000)
ftp.close() ftp.close()

View file

@ -533,7 +533,7 @@ proc accept*(server: TSocket, client: var TSocket) =
proc acceptAddr*(server: TSocket): tuple[client: TSocket, address: string] {.deprecated.} = proc acceptAddr*(server: TSocket): tuple[client: TSocket, address: string] {.deprecated.} =
## Slightly different version of ``acceptAddr``. ## Slightly different version of ``acceptAddr``.
## ##
## **Warning**: This function is now deprecated, you shouldn't use it! ## **Deprecated since version 0.9.0:** Please use the function above.
var client: TSocket var client: TSocket
new(client) new(client)
var address = "" var address = ""
@ -541,7 +541,7 @@ proc acceptAddr*(server: TSocket): tuple[client: TSocket, address: string] {.dep
return (client, address) return (client, address)
proc accept*(server: TSocket): TSocket {.deprecated.} = proc accept*(server: TSocket): TSocket {.deprecated.} =
## **Warning**: This function is now deprecated, you shouldn't use it! ## **Deprecated since version 0.9.0:** Please use the function above.
new(result) new(result)
var address = "" var address = ""
acceptAddr(server, result, address) acceptAddr(server, result, address)