2015-07-20 16:16:58 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2010 dorkbox, llc
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-08-20 23:44:59 +02:00
|
|
|
package dorkbox.network.connection.registration.remote;
|
|
|
|
|
2017-10-03 00:02:17 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
|
2019-06-14 20:36:26 +02:00
|
|
|
import dorkbox.network.connection.RegistrationWrapperClient;
|
2014-08-20 23:44:59 +02:00
|
|
|
import dorkbox.network.connection.registration.MetaChannel;
|
|
|
|
import dorkbox.network.connection.registration.Registration;
|
2015-07-20 16:16:58 +02:00
|
|
|
import io.netty.channel.Channel;
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
2018-04-01 14:51:13 +02:00
|
|
|
import io.netty.channel.EventLoopGroup;
|
2014-08-20 23:44:59 +02:00
|
|
|
|
2018-02-16 21:02:05 +01:00
|
|
|
@SuppressWarnings("Duplicates")
|
2015-07-20 16:16:58 +02:00
|
|
|
public
|
2018-01-25 15:48:15 +01:00
|
|
|
class RegistrationRemoteHandlerClientUDP extends RegistrationRemoteHandlerClient {
|
2015-07-20 16:16:58 +02:00
|
|
|
public
|
2018-04-01 14:51:13 +02:00
|
|
|
RegistrationRemoteHandlerClientUDP(final String name,
|
2019-06-14 20:36:26 +02:00
|
|
|
final RegistrationWrapperClient registrationWrapper,
|
2018-04-01 14:51:13 +02:00
|
|
|
final EventLoopGroup workerEventLoop) {
|
|
|
|
super(name, registrationWrapper, workerEventLoop);
|
2014-08-20 23:44:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* STEP 2: Channel is now active. Start the registration process
|
|
|
|
*/
|
|
|
|
@Override
|
2015-07-20 16:16:58 +02:00
|
|
|
public
|
2015-07-29 16:47:58 +02:00
|
|
|
void channelActive(final ChannelHandlerContext context) throws Exception {
|
2015-07-30 01:48:46 +02:00
|
|
|
super.channelActive(context);
|
2014-08-20 23:44:59 +02:00
|
|
|
|
|
|
|
Channel channel = context.channel();
|
|
|
|
|
2019-06-14 12:40:38 +02:00
|
|
|
// have to add a way for us to store messages in case the remote end calls "onConnect()" and sends messages before we are ready.
|
|
|
|
// note: UDP channels are also unique (just like TCP channels) because of the SessionManager we added
|
|
|
|
prepChannelForOutOfOrderMessages(channel);
|
|
|
|
|
2014-08-20 23:44:59 +02:00
|
|
|
InetSocketAddress udpRemoteAddress = (InetSocketAddress) channel.remoteAddress();
|
|
|
|
if (udpRemoteAddress != null) {
|
2018-02-16 21:02:05 +01:00
|
|
|
Registration outboundRegister = new Registration(0);
|
|
|
|
outboundRegister.publicKey = this.registrationWrapper.getPublicKey();
|
2014-08-20 23:44:59 +02:00
|
|
|
|
2018-02-16 21:02:05 +01:00
|
|
|
// check to see if we have an already existing TCP connection to the server, so we can reuse the MetaChannel.
|
|
|
|
// UDP will always be registered after TCP
|
|
|
|
MetaChannel firstSession = this.registrationWrapper.getFirstSession();
|
|
|
|
if (firstSession != null) {
|
|
|
|
outboundRegister.sessionID = firstSession.sessionId;
|
|
|
|
outboundRegister.hasMore = registrationWrapper.hasMoreRegistrations();
|
2014-08-22 14:04:38 +02:00
|
|
|
}
|
2014-08-20 23:44:59 +02:00
|
|
|
|
2018-02-16 21:02:05 +01:00
|
|
|
// no size info, since this is UDP, it is not segmented
|
|
|
|
channel.writeAndFlush(outboundRegister);
|
2015-07-20 16:16:58 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-07-28 02:23:01 +02:00
|
|
|
throw new IOException("UDP cannot connect to remote server! No remote address specified!");
|
2014-08-20 23:44:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-07-20 16:16:58 +02:00
|
|
|
public
|
2015-07-29 16:47:58 +02:00
|
|
|
void channelRead(final ChannelHandlerContext context, Object message) throws Exception {
|
2018-02-16 21:02:05 +01:00
|
|
|
// REGISTRATION is the ONLY thing NOT encrypted. ALSO, this handler is REMOVED once registration is complete
|
2014-08-20 23:44:59 +02:00
|
|
|
|
2018-02-16 21:02:05 +01:00
|
|
|
Channel channel = context.channel();
|
2014-08-20 23:44:59 +02:00
|
|
|
|
2018-02-16 21:02:05 +01:00
|
|
|
if (message instanceof Registration) {
|
|
|
|
Registration registration = (Registration) message;
|
2014-08-20 23:44:59 +02:00
|
|
|
|
2018-02-16 21:02:05 +01:00
|
|
|
MetaChannel metaChannel;
|
|
|
|
int sessionId = registration.sessionID;
|
2014-08-20 23:44:59 +02:00
|
|
|
|
2018-02-16 21:02:05 +01:00
|
|
|
if (sessionId == 0) {
|
|
|
|
logger.error("Invalid UDP channel session ID 0!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
metaChannel = registrationWrapper.getSession(sessionId);
|
2014-08-20 23:44:59 +02:00
|
|
|
|
2018-02-16 21:02:05 +01:00
|
|
|
if (metaChannel == null) {
|
2019-06-14 20:36:26 +02:00
|
|
|
metaChannel = registrationWrapper.createSession(sessionId);
|
2018-04-04 15:01:08 +02:00
|
|
|
|
2018-02-16 21:02:05 +01:00
|
|
|
logger.debug("New UDP connection. Saving meta-channel id: {}", metaChannel.sessionId);
|
2014-08-20 23:44:59 +02:00
|
|
|
}
|
2018-04-04 15:01:08 +02:00
|
|
|
else if (metaChannel.udpChannel == null) {
|
|
|
|
logger.debug("Using TCP connection meta-channel for UDP connection");
|
|
|
|
}
|
2014-08-20 23:44:59 +02:00
|
|
|
|
2018-02-16 21:02:05 +01:00
|
|
|
// in the event that we start with a TCP channel first, we still have to set the UDP channel
|
|
|
|
metaChannel.udpChannel = channel;
|
2014-08-20 23:44:59 +02:00
|
|
|
}
|
|
|
|
|
2019-06-13 22:08:50 +02:00
|
|
|
readClient(context, channel, registration, "UDP client", metaChannel);
|
2018-02-16 21:02:05 +01:00
|
|
|
}
|
|
|
|
else {
|
2018-04-04 15:01:08 +02:00
|
|
|
logger.trace("Out of order UDP message from server!");
|
|
|
|
saveOutOfOrderMessage(channel, message);
|
2018-02-16 21:02:05 +01:00
|
|
|
}
|
2014-08-20 23:44:59 +02:00
|
|
|
}
|
|
|
|
}
|