简体   繁体   English

Apache Mina-多次向客户端小写

[英]Apache Mina - Multiple small write to client

I built a tcp server based on apache mina 2.0.4, and have some problems writing back to the client. 我基于apache mina 2.0.4构建了一个TCP服务器,并且在写回客户端时遇到了一些问题。

We have some tcp clients that can handle only one message at a time and with a buffer size of 256 bytes max. 我们有一些tcp客户端,一次只能处理一条消息,最大缓冲区大小为256字节。 When I send 2+ messages (< 256 bytes) to the client, they arrive in one or two big blocks that the client can't handle, instead of 2+ separated messages. 当我向客户端发送2+条消息(<256字节)时,它们到达客户端无法处理的一两个大块,而不是2+条分开的消息。 I tried to set sessionConfig.setTcpNoDelay(true/false); 我试图设置sessionConfig.setTcpNoDelay(true/false); with no success, as well as sessionConfig.setSendBufferSize( 256 ); 没有成功,还有sessionConfig.setSendBufferSize( 256 ); .

In the message response encoder I also tried to flush the output: 在消息响应编码器中,我还尝试刷新输出:

int capacity = 256;
IoBuffer buffer = IoBuffer.allocate(capacity, false);
buffer.setAutoExpand(false);
buffer.setAutoShrink(true);
buffer.putShort(type);
buffer.putShort(length);
buffer.put(gmtpMsg.getMessage().getBytes());
buffer.flip();
out.write(buffer);
out.flush();

And in the thread responsible to send the messages, I tried to wait for the message to be written 在负责发送消息的线程中,我试图等待消息被写入

for (Entry<Long, OutgoingMessage> outgoingMsg : outgoingMsgs.entrySet()) {
      WriteFuture future = session.write(outgoingMsg.getValue());
      future.awaitUninterruptibly();
}

All this fails miserably, and the only solution working is a ridiculous 500 msec sleep between the session write, which is hardly acceptable. 所有这些都不幸地失败了,唯一可行的解​​决方案是会话写入之间的500 ms荒谬的睡眠,这几乎是不能接受的。 Anyone see what I am doing wrong? 有人看到我在做什么错吗?

After reading a bit more on the tcp protocol and specially https://stackoverflow.com/a/6614586/1280034 , it is clear that the problem is on the client side, not handling the packets correctly. 在阅读了更多关于tcp协议的信息,特别是https://stackoverflow.com/a/6614586/1280034之后 ,很明显问题出在客户端,而不是正确处理数据包。

Since we can't rebuilt the clients, my only solution is to delay each outgoing messages by approx 500ms. 由于我们无法重建客户端,因此我唯一的解决方案是将每个传出消息延迟大约500毫秒。 To do so I created an extra queue in charge of the writting to clients in order to let the server continue its normal job. 为此,我创建了一个额外的队列,负责写给客户端,以使服务器继续其正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM