
com.digitalpetri.opcua.stack.server.handlers.UaTcpServerSymmetricHandler Maven / Gradle / Ivy
/*
* Copyright 2015 Kevin Herron
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.digitalpetri.opcua.stack.server.handlers;
import java.io.IOException;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
import com.digitalpetri.opcua.stack.core.StatusCodes;
import com.digitalpetri.opcua.stack.core.UaException;
import com.digitalpetri.opcua.stack.core.application.services.ServiceRequest;
import com.digitalpetri.opcua.stack.core.application.services.ServiceResponse;
import com.digitalpetri.opcua.stack.core.channel.ChannelSecurity;
import com.digitalpetri.opcua.stack.core.channel.ExceptionHandler;
import com.digitalpetri.opcua.stack.core.channel.SerializationQueue;
import com.digitalpetri.opcua.stack.core.channel.ServerSecureChannel;
import com.digitalpetri.opcua.stack.core.channel.headers.HeaderDecoder;
import com.digitalpetri.opcua.stack.core.channel.headers.SymmetricSecurityHeader;
import com.digitalpetri.opcua.stack.core.channel.messages.ErrorMessage;
import com.digitalpetri.opcua.stack.core.channel.messages.MessageType;
import com.digitalpetri.opcua.stack.core.serialization.UaRequestMessage;
import com.digitalpetri.opcua.stack.core.serialization.UaResponseMessage;
import com.digitalpetri.opcua.stack.core.util.BufferUtil;
import com.digitalpetri.opcua.stack.server.tcp.UaTcpStackServer;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageCodec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UaTcpServerSymmetricHandler extends ByteToMessageCodec implements HeaderDecoder {
private final Logger logger = LoggerFactory.getLogger(getClass());
private List chunkBuffers;
private final int maxChunkCount;
private final int maxChunkSize;
private final UaTcpStackServer server;
private final SerializationQueue serializationQueue;
private final ServerSecureChannel secureChannel;
public UaTcpServerSymmetricHandler(UaTcpStackServer server,
SerializationQueue serializationQueue,
ServerSecureChannel secureChannel) {
this.server = server;
this.serializationQueue = serializationQueue;
this.secureChannel = secureChannel;
maxChunkCount = serializationQueue.getParameters().getLocalMaxChunkCount();
maxChunkSize = serializationQueue.getParameters().getLocalReceiveBufferSize();
chunkBuffers = new ArrayList<>(maxChunkCount);
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
if (secureChannel != null) {
secureChannel.attr(UaTcpStackServer.BoundChannelKey).set(ctx.channel());
}
super.channelActive(ctx);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
if (secureChannel != null) {
secureChannel.attr(UaTcpStackServer.BoundChannelKey).remove();
}
super.channelInactive(ctx);
}
@Override
protected void encode(ChannelHandlerContext ctx, ServiceResponse message, ByteBuf out) throws Exception {
serializationQueue.encode((binaryEncoder, chunkEncoder) -> {
ByteBuf messageBuffer = BufferUtil.buffer();
try {
binaryEncoder.setBuffer(messageBuffer);
binaryEncoder.encodeMessage(null, message.getResponse());
final List chunks = chunkEncoder.encodeSymmetric(
secureChannel,
MessageType.SecureMessage,
messageBuffer,
message.getRequestId()
);
ctx.executor().execute(() -> {
chunks.forEach(c -> ctx.write(c, ctx.voidPromise()));
ctx.flush();
});
} catch (UaException e) {
logger.error("Error encoding {}: {}", message.getResponse().getClass(), e.getMessage(), e);
ctx.close();
} finally {
messageBuffer.release();
}
});
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy