if (!(msg instanceof ChannelBuffer))
return msg;
final ChannelBuffer buffer = (ChannelBuffer) msg;
if (!handshaked) {
final HandshakeMessage message = new HandshakeMessage();
message.read(buffer);
return message;
} else {
if(buffer.readableBytes() == 0)
return new KeepAliveMessage();
final byte opcode = buffer.readByte();
final PeerWireMessage message;
switch (opcode) {
case CancelMessage.MESSAGE_ID:
message = new CancelMessage();
break;
case BitFieldMessage.MESSAGE_ID:
message = new BitFieldMessage();
break;
case ChokeMessage.MESSAGE_ID:
message = new ChokeMessage();
break;
case HaveMessage.MESSAGE_ID:
message = new HaveMessage();
break;
case InterestedMessage.MESSAGE_ID:
message = new InterestedMessage();
break;
case NotInterestedMessage.MESSAGE_ID:
message = new NotInterestedMessage();
break;
case BlockMessage.MESSAGE_ID:
message = new BlockMessage();
break;
case PortMessage.MESSAGE_ID:
message = new PortMessage();
break;
case RequestMessage.MESSAGE_ID:
message = new RequestMessage();
break;
case UnchokeMessage.MESSAGE_ID:
message = new UnchokeMessage();
break;
default:
return null;
}
message.read(buffer);
return message;
}
}