All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.yawk.dbus.protocol.codec.Local Maven / Gradle / Ivy

/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

package at.yawk.dbus.protocol.codec;

import at.yawk.dbus.protocol.MessageHeader;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
import java.nio.ByteOrder;

/**
 * Channel properties used by the decoders
 *
 * @author yawkat
 */
interface Local {
    String PREFIX = Local.class.getName() + '.';

    /**
     * Byte order to use for outbound messages. Using {@link ByteOrder#BIG_ENDIAN} because netty buffers follow this
     * order by default.
     */
    ByteOrder OUTBOUND_ORDER = ByteOrder.BIG_ENDIAN;

    /**
     * The last message serial.
     */
    AttributeKey LAST_SERIAL = AttributeKey.newInstance(PREFIX + "serial");
    /**
     * The current decoding header. If this is unset or null, the current data should not be decoded.
     */
    AttributeKey CURRENT_HEADER = AttributeKey.newInstance(PREFIX + "header");

    /**
     * Generate a serial for the given channel.
     */
    static int generateSerial(ChannelHandlerContext ctx) {
        int serial;Attribute attr = ctx.attr(LAST_SERIAL);
        Integer lastSerial;
        do {
            lastSerial = attr.get();
            serial = (lastSerial == null ? 0 : lastSerial) + 1;
            if (serial == 0) { serial = 1; }
        } while (!attr.compareAndSet(lastSerial, serial));
        return serial;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy