Skip to content

Commit

Permalink
Cleanup connect implementation (java-native-access#659)
Browse files Browse the repository at this point in the history
Motivation:

We can cleanup our code a bit by moving the instanceof check into the
codec

Modifications:

- Move QuicheQuicChannelAddress out of QuicheQuicChannel
- Move instanceof check into QuicheQuicClientCodec
- Remove static helper method

Result:

Code cleanup
  • Loading branch information
normanmaurer authored Feb 4, 2024
1 parent 63cd9b3 commit 30a300e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,9 @@ void attachQuicheConnection(QuicheQuicConnection connection) {
}
}

private void connect(Function<QuicChannel, ? extends QuicSslEngine> engineProvider, Executor sslTaskExecutor,
long configAddr, int localConnIdLength,
boolean supportsDatagram, ByteBuffer fromSockaddrMemory, ByteBuffer toSockaddrMemory)
void connectNow(Function<QuicChannel, ? extends QuicSslEngine> engineProvider, Executor sslTaskExecutor,
long configAddr, int localConnIdLength,
boolean supportsDatagram, ByteBuffer fromSockaddrMemory, ByteBuffer toSockaddrMemory)
throws Exception {
assert this.connection == null;
assert this.traceId == null;
Expand Down Expand Up @@ -1854,34 +1854,6 @@ private void notifyEarlyDataReadyIfNeeded() {
}
}

// TODO: Come up with something better.
static QuicheQuicChannel handleConnect(Function<QuicChannel, ? extends QuicSslEngine> sslEngineProvider,
Executor sslTaskExecutor,
SocketAddress address, long config, int localConnIdLength,
boolean supportsDatagram, ByteBuffer fromSockaddrMemory,
ByteBuffer toSockaddrMemory) throws Exception {
if (address instanceof QuicheQuicChannel.QuicheQuicChannelAddress) {
QuicheQuicChannel.QuicheQuicChannelAddress addr = (QuicheQuicChannel.QuicheQuicChannelAddress) address;
QuicheQuicChannel channel = addr.channel;
channel.connect(sslEngineProvider, sslTaskExecutor, config, localConnIdLength, supportsDatagram,
fromSockaddrMemory, toSockaddrMemory);
return channel;
}
return null;
}

/**
* Just a container to pass the {@link QuicheQuicChannel} to {@link QuicheQuicClientCodec}.
*/
private static final class QuicheQuicChannelAddress extends SocketAddress {

final QuicheQuicChannel channel;

QuicheQuicChannelAddress(QuicheQuicChannel channel) {
this.channel = channel;
}
}

private final class TimeoutHandler implements Runnable {
private ScheduledFuture<?> timeoutFuture;
private final Consumer<QuicheQuicChannel> timeoutTask;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2024 The Netty Project
*
* The Netty Project licenses this file to you 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:
*
* https://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 io.netty.incubator.codec.quic;

import java.net.SocketAddress;

/**
* Just a container to pass the {@link QuicheQuicChannel} to {@link QuicheQuicClientCodec}.
*/
final class QuicheQuicChannelAddress extends SocketAddress {

final QuicheQuicChannel channel;

QuicheQuicChannelAddress(QuicheQuicChannel channel) {
this.channel = channel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,25 @@ protected QuicheQuicChannel quicPacketRead(
@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress,
SocketAddress localAddress, ChannelPromise promise) {
final QuicheQuicChannel channel;
try {
channel = QuicheQuicChannel.handleConnect(sslEngineProvider, sslTaskExecutor, remoteAddress, config.nativeAddress(),
localConnIdLength, config.isDatagramSupported(),
senderSockaddrMemory.internalNioBuffer(0, senderSockaddrMemory.capacity()),
recipientSockaddrMemory.internalNioBuffer(0, recipientSockaddrMemory.capacity()));
} catch (Exception e) {
promise.setFailure(e);
return;
}
if (channel != null) {
addChannel(channel);
if (remoteAddress instanceof QuicheQuicChannelAddress) {
QuicheQuicChannelAddress addr = (QuicheQuicChannelAddress) remoteAddress;
QuicheQuicChannel channel = addr.channel;
try {
channel.connectNow(sslEngineProvider, sslTaskExecutor, config.nativeAddress(),
localConnIdLength, config.isDatagramSupported(),
senderSockaddrMemory.internalNioBuffer(0, senderSockaddrMemory.capacity()),
recipientSockaddrMemory.internalNioBuffer(0, recipientSockaddrMemory.capacity()));
} catch (Throwable cause) {
promise.setFailure(cause);
return;
}

addChannel(channel);
channel.finishConnect();
promise.setSuccess();
return;
}

ctx.connect(remoteAddress, localAddress, promise);
}
}

0 comments on commit 30a300e

Please sign in to comment.