客户端/服务器-如何将协议与网络逻辑分开? [英] Client/server - How to separate protocol from network logic?

查看:92
本文介绍了客户端/服务器-如何将协议与网络逻辑分开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现和对某个应用程序协议中使用的与TCP服务器通信的客户端应用程序进行单元测试(不一定是TDD).

I want to implement and unit test (not necessarily TDD) a client application which communicates with a TCP server using in a certain application protocol.

我在此处(1)此处(2)最好将协议代码与网络代码分离,以便每个人都可以分别进行单元测试.

I've seen in places such as here (1) and here (2) that protocol code should be preferably decoupled from the network code so I each one can be unit-tested separately.

但是我无法理解我应该如何设计和实现这些部分.

However I'm failing to understand how should I design and implement those parts.

第一个链接讨论使用方法HelloMessage()HowdyMessage()MyProtocolHandler类.这是否意味着协议处理程序应具有生成消息和处理响应的两种方法?我将如何使用它们?还有一件事,每个消息/响应对应该有不同的ProtocolHandler类,还是所有消息/响应对都只有一个?

The first link talks about a MyProtocolHandler class with methods HelloMessage() and HowdyMessage(). Does that mean a protocol handler is expected to have two methods for generating a message and for processing the response? How will I use them? One more thing, there should be different ProtocolHandler classes for each message/response pair or only one for all of them?

第二个链接讨论ReaderWriter.同样,我不能理解应该如何使用它们.

The second link talks about a Reader and a Writer. Again, I can't undersand how should I use them.

这两个只是示例.主要问题是,如何将逻辑与网络分离并对其进行单元测试?我不得不说我还没有尝试过任何东西.我以前只写耦合代码,不知道从哪里开始.

Those two are just examples. The main question is, how can I decouple the logic from the network and unit test them? I have to say I haven't tried anything yet; I'm used to writing coupled code only and don't know where to begin.

推荐答案

这些是处理任务的不同方法.网络堆栈被设计为不同的层,其中每个层为上层提供定义良好"的功能,并通过API提供这些功能.

Those are different ways to approach the task. The network stack is design as different layers in which each layer provides "well defined" functionalities to the upper layer and it provides those functionalities through API's.

因此,如果要实现自己的在TCP或SSL(反过来又可能在TCP上运行)上运行的应用程序层协议,则可以使用套接字接口.设计该零件的方式与设计任何应用程序的方式相同.通常,您要将应用程序逻辑与协议分开,称为A,逻辑.您的协议A将负责使用套接字(写入和读取)向服务器发送消息,从服务器读取消息,处理超时(例如,当期望来自永不到达的服务器的响应时),处理套接字错误,消息格式,消息解析等.协议A将对您的应用程序隐藏所有这些问题.

So if you want to implement your own application layer protocol running on TCP or also on SSL (which in turn will probably run on TCP) you will use the socket interface. The way you design that part is the same way you would design any application. In general you want to separate your application logic from the protocol, call it A, logic. Your protocol A will be in charge of sending a message to the server using sockets (write and read), read message from the server, deal with timeouts (for example when expecting a response from the server that never arrives), deal with socket errors, message format, message parsing, etc. Protocol A will hide all those problems from your application.

您的应用程序将只处理您协议提供的API函数:像HelloMessage一样,它将在内部调用该方法,HelloMessage将处理套接字,消息格式等.

Your application will only deal with the API functions provided by your protocol: like HelloMessage, it will call that method and inside, HelloMessage will deal with sockets, message format, etc.

现在,协议A只能由一个或一组类实现.如果是一组类,我建议您将它们设置为同一软件包的一部分.

Now your protocol A can be implemented by only one class or a set of classes. If it is a set of classes I'd recommend you to make them part of the same package.

详细说明如何实现它,我建议两个选择: 1)您有一个Reader和Writer类,它们是套接字的包装器.然后,您的协议类将使用这些读取器和写入器,并且可以在不与不使用套接字的子读取器和写入器建立网络的情况下对其进行测试. 2)这更加复杂,您可以拥有一个Communication类,该类可能接收要通过消息队列发送的消息,并且还可以使用另一个消息队列发送接收到的消息. Communication类知道如何处理连接或套接字(取决于抽象级别).它知道如何打开连接,处理超时等.这是一个更好的设计,但是太复杂了.

Elaborating more on how to implement it, I would suggest two options: 1) You have a Reader and Writer class that are wrappers of a socket. Then your Protocol classes use these readers and writers and you can test it without having a network with a child Reader and Writer that doesn't use sockets. 2) This is more complex, you can have a Communication class that might receive messages to send through a message queue and it can also send up received messages using another message queue. The Communication class know how to deal with connections or sockets (depending on the level of abstraction). It knows how to open connections, handle timeouts, etc. This is a better design but way too complex.

我希望这会有所帮助.

这篇关于客户端/服务器-如何将协议与网络逻辑分开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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