@Override
    public boolean receive(final int channelId, final ByteBuffer receivedData, final int valid, final Object attachment) {
        final ChannelAttachment channelState = (ChannelAttachment) attachment;
        final ByteArrayBuffer buffer = channelState.buffer;

        byte[] data;
        if (receivedData.hasArray()) {
            data = receivedData.array();
        } else {
            data = new byte[valid];
            receivedData.get(data, 0, valid);
        }

        buffer.write(data, 0, valid);

        boolean ret = true;

        while (true) {
            if (channelState.state == ChannelState.LENGTH && buffer.readableSize() >= 4) {
                channelState.dataSize = buffer.getInt();
                channelState.state = ChannelState.DATA;
            } else if (channelState.state == ChannelState.DATA && buffer.readableSize() >= channelState.dataSize) {
                Object obj = buffer.getObject(channelState.dataSize);
                ret = channelState.callBack.receive(channelId, obj, channelState.clientAttachment);
                channelState.state = ChannelState.LENGTH;
            } else {
                break;
            }
        }
        buffer.compact();
        return ret;
    }