wire layer:

+ estimation of message size in dynamic way by header content
This commit is contained in:
romanman 2014-07-04 14:44:27 +01:00
parent 88301fe1c9
commit 87f1c7a1b8
3 changed files with 50 additions and 1 deletions

View File

@ -0,0 +1,47 @@
package org.ethereum.net;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import io.netty.buffer.UnpooledUnsafeDirectByteBuf;
import io.netty.channel.FileRegion;
import io.netty.channel.MessageSizeEstimator;
import org.spongycastle.util.encoders.Hex;
/**
* www.ethereumJ.com
*
* @author: Roman Mandeleil
* Created on: 04/07/2014 13:16
*/
public class EthereumMessageSizeEstimator implements MessageSizeEstimator {
private final Handle handle = new HandleImpl();
private static final class HandleImpl implements Handle {
private HandleImpl() {
}
@Override
public int size(Object msg) {
ByteBuf buffer = ((ByteBuf)msg);
if (buffer.readableBytes() < 8) throw new RuntimeException("Not Ethereum packet");
int msgSize = ((buffer.getByte(4) & 0xFF) << 24) +
((buffer.getByte(5) & 0xFF) << 16) +
((buffer.getByte(6) & 0xFF) << 8) +
((buffer.getByte(7) & 0xFF));
return msgSize;
}
}
@Override
public Handle newHandle() {
return handle;
}
}

View File

@ -9,6 +9,7 @@ import io.netty.handler.timeout.ReadTimeoutHandler;
import org.ethereum.core.Transaction;
import org.ethereum.manager.MainData;
import org.ethereum.net.EthereumMessageSizeEstimator;
import org.ethereum.net.PeerListener;
import org.ethereum.net.message.StaticMessages;
import org.ethereum.net.message.TransactionsMessage;
@ -51,6 +52,8 @@ public class ClientPeer {
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, new EthereumMessageSizeEstimator());
final EthereumProtocolHandler handler;
if (peerListener != null) {

View File

@ -27,7 +27,6 @@ public class EthereumFrameDecoder extends ByteToMessageDecoder {
(magicBytes >> 8 & 0xFF) == 0x08 &&
(magicBytes & 0xFF) == 0x91 )) {
System.out.println("Not ethereum packet");
ctx.close();
}