实现协议的最佳方法是什么?

时间:2022-09-02 11:38:20

Say you are writing an application that must implement the HTTP protocol. Protocols are quite complex and may allow several commands depending on which stage of a transaction they are in.

假设您正在编写必须实现HTTP协议的应用程序。协议非常复杂,可能允许多个命令,具体取决于它们所处的事务的哪个阶段。

As an example, look at SMTP. An SMTP server must throw an error if the "data" command is sent before receiving "rcpt" and "mail".

例如,看看SMTP。如果在接收“rcpt”和“mail”之前发送“data”命令,则SMTP服务器必须抛出错误。

My question is: what is the best way to handle protocols such as this in code? Are there any design patterns related to this?

我的问题是:在代码中处理诸如此类协议的最佳方法是什么?有没有与此相关的设计模式?

Edit: This question relates to the theory behind implementing protocols. I'm aware that using a library is the best approach in practise.

编辑:这个问题涉及实施协议背后的理论。我知道使用库是实践中最好的方法。

4 个解决方案

#1


12  

State Machines

To my mind, a state machine is the easiest way to model and handle protocols. A state would be reached by several transitions relating to valid commands received. Each state would then allow only a certain subset of commands.

在我看来,状态机是建模和处理协议的最简单方法。通过与接收的有效命令相关的若干转换将到达状态。然后,每个状态将仅允许某个命令子集。

State machines are used in compiler construction for lexical analysis of a program. I see the problem of protocol implementation as a special case of this.

状态机用于编译器构造以用于程序的词法分析。我认为协议实现的问题是一个特例。

#2


1  

The best ways to handle protocols like this is to use a library. Almost every computer language used on earth has preexisting, well tested libraries to handle http and smtp.

处理这样的协议的最佳方法是使用库。几乎所有在地球上使用的计算机语言都有预先存在的,经过良好测试的库来处理http和smtp。

#3


0  

@fluffels@

Zed Shaw (author of Mongrel) agrees with you; he uses Ragel.

Zed Shaw(Mongrel的作者)同意你的意见;他使用Ragel。

#4


-1  

I agree with a28, the best way is to either:

我同意a28,最好的方法是:

  • Use a library which implements the protocol server
  • 使用实现协议服务器的库

  • Write your application as an extension to an existing server (e.g. web server extension via IIS, Apache APIs etc, Sendmail Milter etc) OR
  • 将您的应用程序编写为现有服务器的扩展(例如,通过IIS的Web服务器扩展,Apache API等,Sendmail Milter等)或

  • Modify an existing server to make RPC calls to your application as it receives requests.
  • 修改现有服务器,以便在接收请求时对应用程序进行RPC调用。

Writing your own implementation of the protocol is likely to result in a buggy implementation with interoperability problems.

编写自己的协议实现可能会导致实现互操作性问题的错误。

An interesting tool to do this is twisted which is python-specific but rather clever and includes implementations of numerous existing protocols (HTTP, SMTP, IRC etc).

一个有趣的工具是扭曲的,它是特定于python但非常聪明,包括许多现有协议(HTTP,SMTP,IRC等)的实现。

#1


12  

State Machines

To my mind, a state machine is the easiest way to model and handle protocols. A state would be reached by several transitions relating to valid commands received. Each state would then allow only a certain subset of commands.

在我看来,状态机是建模和处理协议的最简单方法。通过与接收的有效命令相关的若干转换将到达状态。然后,每个状态将仅允许某个命令子集。

State machines are used in compiler construction for lexical analysis of a program. I see the problem of protocol implementation as a special case of this.

状态机用于编译器构造以用于程序的词法分析。我认为协议实现的问题是一个特例。

#2


1  

The best ways to handle protocols like this is to use a library. Almost every computer language used on earth has preexisting, well tested libraries to handle http and smtp.

处理这样的协议的最佳方法是使用库。几乎所有在地球上使用的计算机语言都有预先存在的,经过良好测试的库来处理http和smtp。

#3


0  

@fluffels@

Zed Shaw (author of Mongrel) agrees with you; he uses Ragel.

Zed Shaw(Mongrel的作者)同意你的意见;他使用Ragel。

#4


-1  

I agree with a28, the best way is to either:

我同意a28,最好的方法是:

  • Use a library which implements the protocol server
  • 使用实现协议服务器的库

  • Write your application as an extension to an existing server (e.g. web server extension via IIS, Apache APIs etc, Sendmail Milter etc) OR
  • 将您的应用程序编写为现有服务器的扩展(例如,通过IIS的Web服务器扩展,Apache API等,Sendmail Milter等)或

  • Modify an existing server to make RPC calls to your application as it receives requests.
  • 修改现有服务器,以便在接收请求时对应用程序进行RPC调用。

Writing your own implementation of the protocol is likely to result in a buggy implementation with interoperability problems.

编写自己的协议实现可能会导致实现互操作性问题的错误。

An interesting tool to do this is twisted which is python-specific but rather clever and includes implementations of numerous existing protocols (HTTP, SMTP, IRC etc).

一个有趣的工具是扭曲的,它是特定于python但非常聪明,包括许多现有协议(HTTP,SMTP,IRC等)的实现。