Example usage for io.netty.util ReferenceCountUtil release

List of usage examples for io.netty.util ReferenceCountUtil release

Introduction

In this page you can find the example usage for io.netty.util ReferenceCountUtil release.

Prototype

public static boolean release(Object msg) 

Source Link

Document

Try to call ReferenceCounted#release() if the specified message implements ReferenceCounted .

Usage

From source file:com.dianping.cat.message.io.BufReleaseHelper.java

License:Open Source License

public static void release(ByteBuf buf) {
    try {//from  ww w.  j av  a2  s  .  c  o m
        if (buf != null) {
            ReferenceCountUtil.release(buf);
        }
    } catch (Exception e) {
        Cat.logError(e);
    }
}

From source file:com.digitalpetri.modbus.codec.ModbusRequestEncoder.java

License:Apache License

@Override
public ByteBuf encode(ModbusPdu modbusPdu, ByteBuf buffer) throws EncoderException {
    try {/* w  ww .ja  va  2s .c  o  m*/
        switch (modbusPdu.getFunctionCode()) {
        case ReadCoils:
            return encodeReadCoils((ReadCoilsRequest) modbusPdu, buffer);

        case ReadDiscreteInputs:
            return encodeReadDiscreteInputs((ReadDiscreteInputsRequest) modbusPdu, buffer);

        case ReadHoldingRegisters:
            return encodeReadHoldingRegisters((ReadHoldingRegistersRequest) modbusPdu, buffer);

        case ReadInputRegisters:
            return encodeReadInputRegisters((ReadInputRegistersRequest) modbusPdu, buffer);

        case WriteSingleCoil:
            return encodeWriteSingleCoil((WriteSingleCoilRequest) modbusPdu, buffer);

        case WriteSingleRegister:
            return encodeWriteSingleRegister((WriteSingleRegisterRequest) modbusPdu, buffer);

        case WriteMultipleCoils:
            return encodeWriteMultipleCoils((WriteMultipleCoilsRequest) modbusPdu, buffer);

        case WriteMultipleRegisters:
            return encodeWriteMultipleRegisters((WriteMultipleRegistersRequest) modbusPdu, buffer);

        case MaskWriteRegister:
            return encodeMaskWriteRegister((MaskWriteRegisterRequest) modbusPdu, buffer);

        default:
            throw new EncoderException("FunctionCode not supported: " + modbusPdu.getFunctionCode());
        }
    } finally {
        ReferenceCountUtil.release(modbusPdu);
    }
}

From source file:com.digitalpetri.modbus.codec.ModbusResponseEncoder.java

License:Apache License

@Override
public ByteBuf encode(ModbusPdu modbusPdu, ByteBuf buffer) throws EncoderException {
    try {/*from   ww  w  .j a v  a  2  s. c  o m*/
        if (modbusPdu instanceof ExceptionResponse) {
            return encodeExceptionResponse((ExceptionResponse) modbusPdu, buffer);
        } else {
            switch (modbusPdu.getFunctionCode()) {
            case ReadCoils:
                return encodeReadCoils((ReadCoilsResponse) modbusPdu, buffer);

            case ReadDiscreteInputs:
                return encodeReadDiscreteInputs((ReadDiscreteInputsResponse) modbusPdu, buffer);

            case ReadHoldingRegisters:
                return encodeReadHoldingRegisters((ReadHoldingRegistersResponse) modbusPdu, buffer);

            case ReadInputRegisters:
                return encodeReadInputRegisters((ReadInputRegistersResponse) modbusPdu, buffer);

            case WriteSingleCoil:
                return encodeWriteSingleCoil((WriteSingleCoilResponse) modbusPdu, buffer);

            case WriteSingleRegister:
                return encodeWriteSingleRegister((WriteSingleRegisterResponse) modbusPdu, buffer);

            case WriteMultipleCoils:
                return encodeWriteMultipleCoils((WriteMultipleCoilsResponse) modbusPdu, buffer);

            case WriteMultipleRegisters:
                return encodeWriteMultipleRegisters((WriteMultipleRegistersResponse) modbusPdu, buffer);

            case MaskWriteRegister:
                return encodeMaskWriteRegister((MaskWriteRegisterResponse) modbusPdu, buffer);

            default:
                throw new EncoderException("FunctionCode not supported: " + modbusPdu.getFunctionCode());
            }
        }
    } finally {
        ReferenceCountUtil.release(modbusPdu);
    }
}

From source file:com.digitalpetri.modbus.examples.master.MasterExample.java

License:Apache License

private void sendAndReceive(ModbusTcpMaster master) {
    if (!started)
        return;/*  ww w.  j a  va  2 s. c  o  m*/

    CompletableFuture<ReadHoldingRegistersResponse> future = master
            .sendRequest(new ReadHoldingRegistersRequest(0, 10), 0);

    future.whenCompleteAsync((response, ex) -> {
        if (response != null) {
            ReferenceCountUtil.release(response);
        } else {
            logger.error("Completed exceptionally, message={}", ex.getMessage(), ex);
        }
        scheduler.schedule(() -> sendAndReceive(master), 1, TimeUnit.SECONDS);
    }, Modbus.sharedExecutor());
}

From source file:com.digitalpetri.modbus.examples.slave.SlaveExample.java

License:Apache License

public void start() throws ExecutionException, InterruptedException {
    slave.setRequestHandler(new ServiceRequestHandler() {
        @Override//w  w  w .ja v  a2 s.c  o m
        public void onReadHoldingRegisters(
                ServiceRequest<ReadHoldingRegistersRequest, ReadHoldingRegistersResponse> service) {
            ReadHoldingRegistersRequest request = service.getRequest();

            ByteBuf registers = PooledByteBufAllocator.DEFAULT.buffer(request.getQuantity());

            for (int i = 0; i < request.getQuantity(); i++) {
                registers.writeShort(i);
            }

            service.sendResponse(new ReadHoldingRegistersResponse(registers));

            ReferenceCountUtil.release(request);
        }
    });

    slave.bind("localhost", 50200).get();
}

From source file:com.digitalpetri.modbus.master.ModbusTcpMaster.java

License:Apache License

private void handleResponse(short transactionId, short unitId, ModbusResponse response) {
    PendingRequest<?> pending = pendingRequests.remove(transactionId);

    if (pending != null) {
        responseCounter.inc();// w w  w.ja  v  a  2 s.c  o m

        pending.context.stop();
        pending.timeout.cancel();

        if (response instanceof ExceptionResponse) {
            pending.promise.completeExceptionally(new ModbusResponseException((ExceptionResponse) response));
        } else {
            pending.promise.complete(response);
        }
    } else {
        lateResponseCounter.inc();
        ReferenceCountUtil.release(response);

        logger.debug("Received response for unknown transactionId: {}", transactionId);
    }
}

From source file:com.digitalpetri.modbus.slave.ServiceRequestHandler.java

License:Apache License

default void onReadHoldingRegisters(
        ServiceRequest<ReadHoldingRegistersRequest, ReadHoldingRegistersResponse> service) {
    service.sendException(ExceptionCode.IllegalFunction);
    ReferenceCountUtil.release(service.getRequest());
}

From source file:com.digitalpetri.modbus.slave.ServiceRequestHandler.java

License:Apache License

default void onReadInputRegisters(
        ServiceRequest<ReadInputRegistersRequest, ReadInputRegistersResponse> service) {
    service.sendException(ExceptionCode.IllegalFunction);
    ReferenceCountUtil.release(service.getRequest());
}

From source file:com.digitalpetri.modbus.slave.ServiceRequestHandler.java

License:Apache License

default void onReadCoils(ServiceRequest<ReadCoilsRequest, ReadCoilsResponse> service) {
    service.sendException(ExceptionCode.IllegalFunction);
    ReferenceCountUtil.release(service.getRequest());
}

From source file:com.digitalpetri.modbus.slave.ServiceRequestHandler.java

License:Apache License

default void onReadDiscreteInputs(
        ServiceRequest<ReadDiscreteInputsRequest, ReadDiscreteInputsResponse> service) {
    service.sendException(ExceptionCode.IllegalFunction);
    ReferenceCountUtil.release(service.getRequest());
}