/** * Writes an the {@link Events#DISCONNECT} to the client, flushes all the pending writes and * closes the channel. */ @Override public void close() { LOG.debug("Going to close tcp connection in class: {}", this.getClass().getName()); Event event = Events.event(null, Events.DISCONNECT); if (channel.isActive()) { channel.write(event).addListener(ChannelFutureListener.CLOSE); } else { channel.close(); LOG.trace("Unable to write the Event {} with type {} to socket", event, event.getType()); } }
@Override protected void encode(ChannelHandlerContext ctx, Event event, List<Object> out) throws Exception { ByteBuf msg = null; if (null != event.getSource()) { LOG.trace("Event class: {}", event.getClass()); ByteBuf buf = ctx.alloc().buffer(1); buf.writeByte(event.getType()); msg = Unpooled.wrappedBuffer(buf, Unpooled.wrappedBuffer(msgPack.write(event.getSource()))); } else { msg = ctx.alloc().buffer(1); msg.writeByte(event.getType()); } out.add(msg); }