private ReturnMessage reactToMessages(
HtmlUnitSessionHandler htmlUnitSessionHandler, boolean expectReturn)
throws IOException, BrowserChannelException {
while (true) {
ExceptionOrReturnValue returnValue;
MessageType type = Message.readMessageType(getStreamFromOtherSide());
logger.log(TreeLogger.INFO, "client: received " + type + ", thread: "
+ Thread.currentThread().getName());
try {
switch (type) {
case INVOKE:
InvokeOnClientMessage invokeMessage = InvokeOnClientMessage.receive(this);
returnValue = htmlUnitSessionHandler.invoke(this,
invokeMessage.getThis(), invokeMessage.getMethodName(),
invokeMessage.getArgs());
htmlUnitSessionHandler.sendFreeValues(this);
new ReturnMessage(this, returnValue.isException(),
returnValue.getReturnValue()).send();
break;
case INVOKE_SPECIAL:
InvokeSpecialMessage invokeSpecialMessage = InvokeSpecialMessage.receive(this);
logger.log(TreeLogger.DEBUG, type + " message " + ", thisRef: "
+ invokeSpecialMessage.getArgs());
returnValue = htmlUnitSessionHandler.invokeSpecial(this,
invokeSpecialMessage.getDispatchId(),
invokeSpecialMessage.getArgs());
htmlUnitSessionHandler.sendFreeValues(this);
new ReturnMessage(this, returnValue.isException(),
returnValue.getReturnValue()).send();
break;
case FREE_VALUE:
FreeMessage freeMessage = FreeMessage.receive(this);
logger.log(TreeLogger.DEBUG, type + " message "
+ freeMessage.getIds());