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;
|
|
|
|
|
2019-06-14 20:36:26 +02:00
|
|
|
import dorkbox.network.connection.RegistrationWrapperClient;
|
2015-07-20 14:18:34 +02:00
|
|
|
import dorkbox.network.connection.registration.MetaChannel;
|
|
|
|
import dorkbox.network.connection.registration.Registration;
|
2014-08-20 23:44:59 +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
|
|
|
|
2015-07-20 14:18:34 +02:00
|
|
|
public
|
2018-01-25 15:48:15 +01:00
|
|
|
class RegistrationRemoteHandlerClientTCP extends RegistrationRemoteHandlerClient {
|
2015-07-20 14:18:34 +02:00
|
|
|
public
|
2018-04-01 14:51:13 +02:00
|
|
|
RegistrationRemoteHandlerClientTCP(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 14:18:34 +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
|
|
|
|
2018-04-05 17:11:49 +02:00
|
|
|
logger.trace("Starting a new TCP Connection. Sending request to server");
|
2014-08-20 23:44:59 +02:00
|
|
|
|
2018-02-16 21:02:05 +01:00
|
|
|
Registration registration = new Registration(0);
|
2014-08-20 23:44:59 +02:00
|
|
|
registration.publicKey = this.registrationWrapper.getPublicKey();
|
|
|
|
|
|
|
|
// client start the handshake with a registration packet
|
2018-02-16 21:02:05 +01:00
|
|
|
context.channel().writeAndFlush(registration);
|
2014-08-20 23:44:59 +02:00
|
|
|
}
|
|
|
|
|
2016-03-09 03:04:10 +01:00
|
|
|
@SuppressWarnings({"AutoUnboxing", "AutoBoxing", "Duplicates"})
|
2014-08-20 23:44:59 +02:00
|
|
|
@Override
|
2015-07-20 14:18:34 +02:00
|
|
|
public
|
2015-07-29 16:47:58 +02:00
|
|
|
void channelRead(final ChannelHandlerContext context, final Object message) throws Exception {
|
2014-08-20 23:44:59 +02:00
|
|
|
Channel channel = context.channel();
|
|
|
|
|
|
|
|
if (message instanceof Registration) {
|
2018-02-16 21:02:05 +01:00
|
|
|
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 TCP channel session ID 0!");
|
|
|
|
shutdown(channel, 0);
|
|
|
|
return;
|
2015-07-20 14:18:34 +02:00
|
|
|
}
|
|
|
|
else {
|
2018-02-16 21:02:05 +01:00
|
|
|
metaChannel = registrationWrapper.getSession(sessionId);
|
|
|
|
|
2018-04-04 15:01:08 +02:00
|
|
|
// TCP channel registration is ALWAYS first, so this is the correct way to do this.
|
2018-02-16 21:02:05 +01:00
|
|
|
if (metaChannel == null) {
|
2019-06-14 20:36:26 +02:00
|
|
|
metaChannel = registrationWrapper.createSession(sessionId);
|
2018-02-16 21:02:05 +01:00
|
|
|
metaChannel.tcpChannel = channel;
|
2018-04-04 15:01:08 +02:00
|
|
|
|
2018-02-16 21:02:05 +01:00
|
|
|
logger.debug("New TCP connection. Saving meta-channel id: {}", metaChannel.sessionId);
|
|
|
|
}
|
2018-04-04 15:01:08 +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.
|
|
|
|
prepChannelForOutOfOrderMessages(channel);
|
2014-08-20 23:44:59 +02:00
|
|
|
}
|
2018-02-16 21:02:05 +01:00
|
|
|
|
2018-04-04 15:01:08 +02:00
|
|
|
logger.trace("TCP read");
|
2019-06-13 22:08:50 +02:00
|
|
|
readClient(context, channel, registration, "TCP client", metaChannel);
|
2014-08-20 23:44:59 +02:00
|
|
|
}
|
|
|
|
else {
|
2018-04-04 15:01:08 +02:00
|
|
|
logger.trace("Out of order TCP message from server!");
|
|
|
|
saveOutOfOrderMessage(channel, message);
|
2014-08-20 23:44:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|