/** * Handle the exception we got. * * @param session The session we got the exception on * @param cause The exception cause * @throws Exception The t */ @Override public void exceptionCaught( IoSession session, Throwable cause ) throws Exception { LOG.warn( cause.getMessage(), cause ); session.setAttribute( EXCEPTION_KEY, cause ); if ( cause instanceof ProtocolEncoderException ) { Throwable realCause = ( ( ProtocolEncoderException ) cause ).getCause(); if ( realCause instanceof MessageEncoderException ) { int messageId = ( ( MessageEncoderException ) realCause ).getMessageId(); ResponseFuture<?> response = futureMap.get( messageId ); response.cancel( true ); response.setCause( realCause ); } } session.closeNow(); }
/** * * @param buffer * @param prefixLength * @throws ProtocolEncoderException */ private void putProtocol(int code, IoBuffer buffer, int prefixLength) throws ProtocolEncoderException { switch (prefixLength) { case 1: buffer.putInt(Protocols.ONE | code | Protocols.MAGIC); break; case 2: buffer.putInt(Protocols.TWO | code | Protocols.MAGIC); break; case 4: buffer.putInt(Protocols.FOUR | code | Protocols.MAGIC); break; default: throw new ProtocolEncoderException("error size"); } }
@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { if (message == null) { return; } if (Req.class.isInstance(message)) { this.encodeReq(session, (Req) message, out); } else if (Response.class.isInstance(message)) { this.encodeResponse(session, (Response) message, out); } else if (String.class.isInstance(message)) { this.encodeString(0, session, (String) message, out); } else { throw new ProtocolEncoderException(message.getClass().getName() + " not support in ProtocolEncoder"); } }
private void encodeString(int code, IoSession session, CharSequence message, ProtocolEncoderOutput out) throws CharacterCodingException, ProtocolEncoderException { code = code | Protocols.FORMAT_JSON; int size = message.length(); int prefixLength = size <= (Protocols.MAX_ONE / 3) ? 1 : size <= (Protocols.MAX_TWO / 3) ? 2 : 4; IoBuffer buffer = IoBuffer.allocate((int) (size * 1.2) + 10).setAutoExpand(true); putProtocol(code, buffer, prefixLength); buffer.putPrefixedString(message, prefixLength, charset.newEncoder()); buffer.flip(); out.write(buffer); }
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception { if (!(message instanceof byte[])) { throw new ProtocolEncoderException("Message is not byte[]."); } // IoBuffer buff = IoBuffer.wrap((byte[]) message); out.write(buff); }
@Override public void filterWrite(NextFilter nextFilter, IoSession session, WriteRequest writeRequest) throws Exception { Object message = writeRequest.getMessage(); ProtocolEncoder encoder = getEncoder(session); ProtocolEncoderOutput encoderOut = new DirectOutput(session, nextFilter, writeRequest); try { encoder.encode(session, message, encoderOut); nextFilter.filterWrite(session, new EndOfMessage(writeRequest)); } catch (ProtocolEncoderException e) { throw e; } catch (Throwable t) { throw new ProtocolEncoderException(t); } }
private void encodeResponse(IoSession session, Response message, ProtocolEncoderOutput out) throws CharacterCodingException, ProtocolEncoderException { this.encodeString(Protocols.RESPONSE_JSON, session, GsonUtil.toJson(message), out); }