我正在经历新的Netty代码组织,似乎处理程序包(请参阅https://github.com/netty/netty/tree/master/handler/src/main/java/io/netty/handler )节食。 ipfilter软件包放在哪里? 我在新闻( http:/ ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
我正在运行用于在线游戏的Netty服务器,但是遇到一些奇怪的现象:
随机地,该通道只是停止能够从服务器进行写操作,即用户仍然处于连接状态,并且接收到传入数据,但是传出数据没有到达客户端。
我花了一些时间来调试它,然后发现channel.isWriteable()返回false来连接客户端与问题。
谁能阐明为什么会这样吗?
通道不再可写的原因是什么?
顺便说一句,这也发生在本地主机连接上。
public ChannelFuture write(Packet message) {
ioWrite++;
if (!channel.isConnected()) {
System.err.println("Trying to write to bad channel");
return null;
}
if (!channel.isWritable()) {
System.err.println("Channel buffer is full?");
}
if ((channel != null) && channel.isConnected()) {
return channel.write(message);
}
return null;
}
编码器:
public class GameProtocolEncoder extends OneToOneEncoder {
private ChannelBuffer response;
protected Object encode(ChannelHandlerContext channelHandlerContext, Channel channel, Object o) throws Exception {
Packet p = (Packet) o;
try {
byte[] data = p.getData();
int dataLength = p.getLength();
if (p.getLength() > 5000) {
System.err.println("unable to write data chunk to large");
return null;
}
if (!p.isBare()) {
int siz = 0;
siz += (p.getSize() == Packet.Size.VariableShort)
? 2
: 1;
siz += (p.getId() > 128)
? 2
: 1;
response = ChannelBuffers.buffer(siz + p.getLength());
int id = p.getId();
if (id< 128) {
response.writeByte(id);
} else {
response.writeByte((byte) ((id >> 8) + 128));
response.writeByte((byte) (id & 0xFF));
}
if (p.getSize() != Packet.Size.Fixed) { // variable length
if (p.getSize() == Packet.Size.VariableByte) {
if (dataLength > 255) { // trying to send more data then we can represent with 8 bits!
throw new IllegalArgumentException("Tried to send packet length " + dataLength
+ " in 8 bits [pid=" + p.getId() + "]");
}
response.writeByte((byte) dataLength);
} else if (p.getSize() == Packet.Size.VariableShort) {
if (dataLength > 5000) {
throw new IllegalArgumentException("Tried to send packet length to big: " + id);
}
response.writeShort((short) dataLength);
}
}
} else {
response = ChannelBuffers.buffer(dataLength);
}
response.writeBytes(p.getData());
return response;
} catch (Exception e) {
Logger.err("Error handling message: " + p);
Logger.err(e);
}
return null;
}
我认为问题出在这里,您没有刷新Channel。 尝试将channel.write(...)替换为channel.writeAndFlush(...)。
当通道的套接字发送缓冲区已满时,通道将停止可写,而当发送方超出接收方时,通道将发生可写状态。
如果经常发生这种情况,您可能要考虑加快接收代码的速度,但是通常它可以随时发生,并且您所能做的就是将写入推迟到该通道再次变为可写入状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.