处理异步响应 [英] Handling asynchronous responses

查看:208
本文介绍了处理异步响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从头开始建立一个FTP客户端,我已经注意到的响应codeS 不是直接(这并不奇怪)。什么是用于获取相应的code到命令一个好方法?

下面是Filezilla的服务器的输出的一个例子。响应code是邻近每一行的末尾的三位数字。

 (000057)23/05/2010十九时43分10秒 - (未登录)(127.0.0.1)>连接,发送欢迎信息...
(000057)23/05/2010 19点43分10秒 - (未登录)(127.0.0.1)> 220的FileZilla Server版本0.9.12 Beta版
(000057)23/05/2010 19点43分10秒 - (未登录)(127.0.0.1)> 220由Tim Kosse(Tim.Kosse@gmx.de)
(000057)23/05/2010 19点43分10秒 - (未登录)(127.0.0.1)> 220请访问http://sourceforge.net/projects/filezilla/
(000057)23/05/2010 19点43分10秒 - (未登录)(127.0.0.1)>匿名用户
(000057)23/05/2010 19点43分10秒 - (未登录)(127.0.0.1)>要求匿名的密码331


解决方案

在这种特殊情况下我可能不看异步实现这一点。除非发送命令和接收响应code之间的延迟很大(它有可能不是FTP),您可以放心地执行其他命令,不知道最后的结果(这你可能无法) ,它不是真的值得尝试以异步方式实现这一点。

我发送命令字符串,并以伪$ C $收到完整的响应返回,即块之间执行c您可能有一个像execute方法:


  1. 发送命令字符串在网络上

  2. 等待一个字符回来(或超时,如果你想这样做)使用阻塞网络阅读的方法或使用非阻塞方法+视频下载(..)

  3. 检查字符是一个令牌末日的回响(新行?)

  4. 如果没有,回去2,如果是,返回完整的响应字符串

如果你真的下定决心走下来异步路由,你应该有一个看的回调格局

希望这有助于。

I'm building an FTP client from scratch and I've noticed that the response codes aren't immediate (which is no surprise). What would be a good approach for getting the corresponding code to a command?

Below is an example of the output of Filezilla server. The response code is the three digits near the end of each line.

(000057) 23/05/2010 19:43:10 - (not logged in) (127.0.0.1)> Connected, sending welcome message...
(000057) 23/05/2010 19:43:10 - (not logged in) (127.0.0.1)> 220-FileZilla Server version 0.9.12 beta
(000057) 23/05/2010 19:43:10 - (not logged in) (127.0.0.1)> 220-written by Tim Kosse (Tim.Kosse@gmx.de)
(000057) 23/05/2010 19:43:10 - (not logged in) (127.0.0.1)> 220 Please visit http://sourceforge.net/projects/filezilla/
(000057) 23/05/2010 19:43:10 - (not logged in) (127.0.0.1)> user anonymous
(000057) 23/05/2010 19:43:10 - (not logged in) (127.0.0.1)> 331 Password required for anonymous

解决方案

In this particular case I'd probably not look to implement this asynchronously. Unless the delay between sending the command and receiving the respond code is large (which it probably isn't for FTP), and you can safely execute another command not knowing the outcome of the last (which you probably can't), it's not really worth trying to implement this asynchronously.

I'd block execution between sending the command string and receiving the full response back, i.e. in pseudocode you might have an execute method like:

  1. Send command string over the network
  2. Wait for a character to come back (or for a timeout, if you want to do this) using a blocking network read method or using a non-blocking method + Thread.sleep(..)
  3. Check if the character is an 'end of response' token (newline?)
  4. If not, go back to 2, if it is, return full the response string

If you're really determined to go down the asynchronous route, you should have a look at the Callback pattern.

Hope this helps.

这篇关于处理异步响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆