public void onMessage(T listener, Message message) {
try {
byte[] bytes = message.asBytes();
int[] holder = new int[1];
int position = readInt(holder, bytes, 0, bytes.length);
int nameLength = holder[0];
String commandName;
if (nameLength != -1) {
commandName = new String(bytes, position, nameLength);
} else {
position = 0;
nameLength = indexOf((byte) '{', bytes, position, bytes.length);
commandName = new String(bytes, position, nameLength);
}
int offset = position + nameLength;
Map.Entry<Class<? extends CommandPojo>, CommandHandler<? extends T, ? extends CommandPojo>> commandHandlerPair = handlers.get(commandName);
if (commandHandlerPair == null)
throw new IllegalArgumentException("Unknown command: " + commandName);
CommandHandler<T, CommandPojo> handler = (CommandHandler<T, CommandPojo>) commandHandlerPair.getValue();
Class<? extends CommandPojo> commandClass = commandHandlerPair.getKey();
CommandPojo command = (CommandPojo) reader.read(commandClass, bytes, offset, bytes.length - offset);
handler.handle(listener, command);
} catch (Exception e) {
onError(e);
}
}