private NioChildDatagramChannel getChildChannel(Channel channel, SocketAddress remoteAddress) throws Exception {
return childChannels.computeIfAbsent(remoteAddress, x -> {
ChannelPipelineFactory childPipelineFactory = getPipelineFactory();
ChannelPipeline childPipeline;
try {
childPipeline = childPipelineFactory.getPipeline();
} catch (Exception e) {
throw new RuntimeException(e);
}
ChannelFactory channelFactory = channel.getFactory();
NioChildDatagramChannel childChannel = ((NioServerDatagramChannelFactory)channelFactory).newChildChannel(channel, childPipeline);
childChannel.setLocalAddress((InetSocketAddress) channel.getLocalAddress());
childChannel.setRemoteAddress((InetSocketAddress) remoteAddress);
channel.getPipeline().sendUpstream(new DefaultChildChannelStateEvent(channel, childChannel));
AbstractNioWorker childWorker = childChannel.getWorker();
ChannelStateEvent connected = new UpstreamChannelStateEvent(childChannel, ChannelState.CONNECTED, remoteAddress);
childWorker.messageReceived(childChannel, connected);
ChannelStateEvent open = new UpstreamChannelStateEvent(childChannel, ChannelState.OPEN, Boolean.TRUE);
childWorker.messageReceived(childChannel, open);
return childChannel;
});
}
}