在套接字协议的现有事件集之上定义新事件的设计查询

发布于 2024-11-14 21:55:17 字数 631 浏览 0 评论 0原文

我使用JBoss Netty编写了一个java服务器和客户端程序。为了将一些数据发送到远程客户端并从它们接收返回的数据,我为每个事件定义了事件和处理程序。在线路上,每个事件只是一个字节(操作码)标头,后跟消息字节。最初我只支持 TCP,并在程序中定义了 LOG_IN、LOG_OUT、DATA_IN、DATA_OUT 等事件。 例如

public static final int LOG_IN = 0x08;
public static final int LOG_OUT = 0x0a;

,然后我决定也支持 UDP,并最终产生了 LOGIN_UDP、LOGIN_TCP、DATA_OUT_TCP 或 DATA_OUT_UDP 等事件,以便根据生成的事件,正确的事件处理程序将获取该事件并将其写入适当的套接字和远程端口。

正如您所看到的,我面临的第一个问题是,在添加 UDP 时,我几乎将定义的事件和事件处理程序的数量增加了一倍。有没有更好的方法来处理这种情况?

我面临的第二个(次要)问题是,当您从服务器写入客户端时,像 DATA_OUT 这样的事件有意义,但是当在客户端接收相同的事件时,“DATA_OUT”就没有意义了,因为它实际上是传入数据为客户。目前,我有一个解码器可以将 DATA_OUT 转换为 DATA_IN。这是最好的方法吗?

I wrote a java server and client program using JBoss Netty. In order to send some data to the remote client and receive data back from them, I have defined events and handlers for each event. On the wire, each event is just a single byte(opcode) header followed by the message bytes. Initially I had only supported TCP and had defined events like LOG_IN,LOG_OUT,DATA_IN,DATA_OUT etc in my program.
For e.g

public static final int LOG_IN = 0x08;
public static final int LOG_OUT = 0x0a;

Then I decided to support UDP also and ended up having events like LOGIN_UDP, LOGIN_TCP, DATA_OUT_TCP or DATA_OUT_UDP etc so that based on the event generated the correct event handler would get the event and write it to the appropriate socket and remote port.

As you can see the first issue I am facing is that I have almost doubled the number of defined events and event handlers on adding UDP. Is there a better way to approach this scenario?

The second(minor) issue I am facing is that events like DATA_OUT make sense when you are writing from server to client, but when receiving the same event at the client side "DATA_OUT" does not make such sense, since it is actually incoming data for the client. For the moment, I have a decoder which will translate DATA_OUT to DATA_IN. Is this the best approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咽泪装欢 2024-11-21 21:55:17

您可以使用工厂模式根据类型通道(即 TCP 或 UDP)创建连接。在这种情况下,您必须定义一次其他细节,

而不是调用 DATA_OUT,您可以将其称为 SERVER_OUT,同样的方式 SERVER_IN

You can use factory pattern to create connection on the basis of the type channel i.e. TCP or UDP. Other details will you have to define once in this case

Instead Calling DATA_OUT you can call it as SERVER_OUT same way SERVER_IN

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文