updated asyncio and ftpclient modules

This commit is contained in:
Araq 2014-01-25 14:21:27 +01:00
commit c4cb795081
2 changed files with 19 additions and 19 deletions

View file

@ -139,27 +139,27 @@ type
proc newDelegate*(): PDelegate = proc newDelegate*(): PDelegate =
## Creates a new delegate. ## Creates a new delegate.
new(result) new(result)
result.handleRead = (proc (h: PObject) = nil) result.handleRead = (proc (h: PObject) = discard)
result.handleWrite = (proc (h: PObject) = nil) result.handleWrite = (proc (h: PObject) = discard)
result.handleError = (proc (h: PObject) = nil) result.handleError = (proc (h: PObject) = discard)
result.hasDataBuffered = (proc (h: PObject): bool = return false) result.hasDataBuffered = (proc (h: PObject): bool = return false)
result.task = (proc (h: PObject) = nil) result.task = (proc (h: PObject) = discard)
result.mode = fmRead result.mode = fmRead
proc newAsyncSocket(): PAsyncSocket = proc newAsyncSocket(): PAsyncSocket =
new(result) new(result)
result.info = SockIdle result.info = SockIdle
result.handleRead = (proc (s: PAsyncSocket) = nil) result.handleRead = (proc (s: PAsyncSocket) = discard)
result.handleWrite = nil result.handleWrite = nil
result.handleConnect = (proc (s: PAsyncSocket) = nil) result.handleConnect = (proc (s: PAsyncSocket) = discard)
result.handleAccept = (proc (s: PAsyncSocket) = nil) result.handleAccept = (proc (s: PAsyncSocket) = discard)
result.handleTask = (proc (s: PAsyncSocket) = nil) result.handleTask = (proc (s: PAsyncSocket) = discard)
result.lineBuffer = "".TaintedString result.lineBuffer = "".TaintedString
result.sendBuffer = "" result.sendBuffer = ""
proc AsyncSocket*(domain: TDomain = AF_INET, typ: TType = SOCK_STREAM, proc asyncSocket*(domain: TDomain = AF_INET, typ: TType = SOCK_STREAM,
protocol: TProtocol = IPPROTO_TCP, protocol: TProtocol = IPPROTO_TCP,
buffered = true): PAsyncSocket = buffered = true): PAsyncSocket =
## Initialises an AsyncSocket object. If a socket cannot be initialised ## Initialises an AsyncSocket object. If a socket cannot be initialised

View file

@ -95,7 +95,7 @@ type
EInvalidReply* = object of ESynch EInvalidReply* = object of ESynch
EFTP* = object of ESynch EFTP* = object of ESynch
proc FTPClient*(address: string, port = TPort(21), proc ftpClient*(address: string, port = TPort(21),
user, pass = ""): PFTPClient = user, pass = ""): PFTPClient =
## Create a ``PFTPClient`` object. ## Create a ``PFTPClient`` object.
new(result) new(result)
@ -315,7 +315,7 @@ proc listDirs*(ftp: PFTPClient, dir: string = "",
assertReply ftp.send("NLST " & dir.normalizePathSep), ["125", "150"] assertReply ftp.send("NLST " & dir.normalizePathSep), ["125", "150"]
if not async: if not async:
while not ftp.job.prc(ftp, false): nil while not ftp.job.prc(ftp, false): discard
result = splitLines(ftp.job.lines) result = splitLines(ftp.job.lines)
ftp.deleteJob() ftp.deleteJob()
else: return @[] else: return @[]
@ -390,7 +390,7 @@ proc list*(ftp: PFTPClient, dir: string = "", async = false): string =
assertReply(ftp.send("LIST" & " " & dir.normalizePathSep), ["125", "150"]) assertReply(ftp.send("LIST" & " " & dir.normalizePathSep), ["125", "150"])
if not async: if not async:
while not ftp.job.prc(ftp, false): nil while not ftp.job.prc(ftp, false): discard
result = ftp.job.lines result = ftp.job.lines
ftp.deleteJob() ftp.deleteJob()
else: else:
@ -405,7 +405,7 @@ proc retrText*(ftp: PFTPClient, file: string, async = false): string =
assertReply ftp.send("RETR " & file.normalizePathSep), ["125", "150"] assertReply ftp.send("RETR " & file.normalizePathSep), ["125", "150"]
if not async: if not async:
while not ftp.job.prc(ftp, false): nil while not ftp.job.prc(ftp, false): discard
result = ftp.job.lines result = ftp.job.lines
ftp.deleteJob() ftp.deleteJob()
else: else:
@ -460,7 +460,7 @@ proc retrFile*(ftp: PFTPClient, file, dest: string, async = false) =
ftp.job.filename = file.normalizePathSep ftp.job.filename = file.normalizePathSep
if not async: if not async:
while not ftp.job.prc(ftp, false): nil while not ftp.job.prc(ftp, false): discard
ftp.deleteJob() ftp.deleteJob()
proc doUpload(ftp: PFTPClient, async = false): bool = proc doUpload(ftp: PFTPClient, async = false): bool =
@ -518,7 +518,7 @@ proc store*(ftp: PFTPClient, file, dest: string, async = false) =
assertReply ftp.send("STOR " & dest.normalizePathSep), ["125", "150"] assertReply ftp.send("STOR " & dest.normalizePathSep), ["125", "150"]
if not async: if not async:
while not ftp.job.prc(ftp, false): nil while not ftp.job.prc(ftp, false): discard
ftp.deleteJob() ftp.deleteJob()
proc close*(ftp: PFTPClient) = proc close*(ftp: PFTPClient) =
@ -554,10 +554,10 @@ proc csockHandleRead(s: PAsyncSocket, ftp: PAsyncFTPClient) =
ftp.handleEvent(ftp, r) ftp.handleEvent(ftp, r)
proc AsyncFTPClient*(address: string, port = TPort(21), proc asyncFTPClient*(address: string, port = TPort(21),
user, pass = "", user, pass = "",
handleEvent: proc (ftp: PAsyncFTPClient, ev: TFTPEvent) {.closure.} = handleEvent: proc (ftp: PAsyncFTPClient, ev: TFTPEvent) {.closure.} =
(proc (ftp: PAsyncFTPClient, ev: TFTPEvent) = nil)): PAsyncFTPClient = (proc (ftp: PAsyncFTPClient, ev: TFTPEvent) = discard)): 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.
@ -604,7 +604,7 @@ when isMainModule:
ftp.close() ftp.close()
echo d.len echo d.len
else: assert(false) else: assert(false)
var ftp = AsyncFTPClient("picheta.me", user = "test", pass = "asf", handleEvent = hev) var ftp = asyncFTPClient("picheta.me", user = "test", pass = "asf", handleEvent = hev)
d.register(ftp) d.register(ftp)
d.len.echo() d.len.echo()
@ -618,7 +618,7 @@ when isMainModule:
when isMainModule and false: when isMainModule and false:
var ftp = FTPClient("picheta.me", user = "asdasd", pass = "asfwq") var ftp = ftpClient("picheta.me", user = "asdasd", pass = "asfwq")
ftp.connect() ftp.connect()
echo ftp.pwd() echo ftp.pwd()
echo ftp.list() echo ftp.list()