Skip to content

Commit d67df5a

Browse files
committed
finish client ssl network
1 parent c4fd895 commit d67df5a

9 files changed

Lines changed: 237 additions & 23 deletions

File tree

rlib-network/src/main/java/com/ss/rlib/network/NetworkFactory.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,25 @@ public final class NetworkFactory {
118118
);
119119
}
120120

121+
/**
122+
* Create string packet based asynchronous secure client network.
123+
*
124+
* @param networkConfig the network config.
125+
* @param bufferAllocator the buffer allocator.
126+
* @param sslContext the ssl context.
127+
* @return the client network.
128+
*/
129+
public static @NotNull ClientNetwork<StringDataSSLConnection> newStringDataSSLClientNetwork(
130+
@NotNull NetworkConfig networkConfig,
131+
@NotNull BufferAllocator bufferAllocator,
132+
@NotNull SSLContext sslContext
133+
) {
134+
return newClientNetwork(
135+
networkConfig,
136+
(network, channel) -> new StringDataSSLConnection(network, channel, bufferAllocator, sslContext, true)
137+
);
138+
}
139+
121140
/**
122141
* Create string packet based asynchronous server network.
123142
*

rlib-network/src/main/java/com/ss/rlib/network/client/impl/DefaultClientNetwork.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.ss.rlib.common.concurrent.util.ThreadUtils;
66
import com.ss.rlib.common.util.AsyncUtils;
77
import com.ss.rlib.common.util.Utils;
8+
import com.ss.rlib.logger.api.Logger;
9+
import com.ss.rlib.logger.api.LoggerManager;
810
import com.ss.rlib.network.Connection;
911
import com.ss.rlib.network.Network;
1012
import com.ss.rlib.network.NetworkConfig;
@@ -31,7 +33,9 @@
3133
*/
3234
public class DefaultClientNetwork<C extends Connection<?, ?>> extends AbstractNetwork<C> implements ClientNetwork<C> {
3335

34-
protected final AtomicBoolean connecting;
36+
protected static final Logger LOGGER = LoggerManager.getLogger(DefaultClientNetwork.class);
37+
38+
protected final @NotNull AtomicBoolean connecting;
3539

3640
protected volatile @Nullable CompletableFuture<C> pendingConnection;
3741
protected volatile @Getter @Nullable C currentConnection;
@@ -42,6 +46,13 @@ public DefaultClientNetwork(
4246
) {
4347
super(config, channelToConnection);
4448
this.connecting = new AtomicBoolean(false);
49+
50+
LOGGER.info(config, conf -> "Client network configuration: {\n" +
51+
" groupName: \"" + conf.getThreadGroupName() + "\",\n" +
52+
" readBufferSize: " + conf.getReadBufferSize() + ",\n" +
53+
" pendingBufferSize: " + conf.getPendingBufferSize() + ",\n" +
54+
" writeBufferSize: " + conf.getWriteBufferSize() + "\n" +
55+
"}");
4556
}
4657

4758
@Override

rlib-network/src/main/java/com/ss/rlib/network/impl/AbstractConnection.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,15 @@ protected void sendImpl(@NotNull WritablePacket packet) {
206206
getPacketWriter().writeNextPacket();
207207
}
208208

209+
protected void queueAtFirst(@NotNull WritablePacket packet) {
210+
long stamp = lock.writeLock();
211+
try {
212+
pendingPackets.addFirst(packet);
213+
} finally {
214+
lock.unlockWrite(stamp);
215+
}
216+
}
217+
209218
@Override
210219
public @NotNull CompletableFuture<Boolean> sendWithFeedback(@NotNull W packet) {
211220

rlib-network/src/main/java/com/ss/rlib/network/impl/DefaultDataSSLConnection.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public DefaultDataSSLConnection(
6969
this::onSentPacket,
7070
sslEngine,
7171
this::sendImpl,
72+
this::queueAtFirst,
7273
packetLengthHeaderSize
7374
);
7475
}

rlib-network/src/main/java/com/ss/rlib/network/packet/impl/AbstractSSLPacketReader.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ protected int doHandshake(@NotNull ByteBuffer receivedBuffer, int receivedBytes)
9292
var handshakeStatus = sslEngine.getHandshakeStatus();
9393

9494
while (handshakeStatus != HandshakeStatus.FINISHED && handshakeStatus != HandshakeStatus.NOT_HANDSHAKING) {
95+
LOGGER.debug(handshakeStatus, status -> "Do handshake with status: " + status);
9596

9697
SSLEngineResult result;
9798

@@ -124,6 +125,7 @@ protected int doHandshake(@NotNull ByteBuffer receivedBuffer, int receivedBytes)
124125
LOGGER.debug(receivedBuffer, buff -> "Try to unwrap data:\n" + hexDump(buff));
125126
result = sslEngine.unwrap(receivedBuffer, EMPTY_BUFFERS);
126127
handshakeStatus = result.getHandshakeStatus();
128+
LOGGER.debug(handshakeStatus, status -> "Handshake status: " + status + " after unwrapping");
127129
} catch (SSLException sslException) {
128130
LOGGER.error("A problem was encountered while processing the data that caused the " +
129131
"SSLEngine to abort. Will try to properly close connection...");
@@ -170,20 +172,29 @@ protected int doHandshake(@NotNull ByteBuffer receivedBuffer, int receivedBytes)
170172

171173
handshakeStatus = sslEngine.getHandshakeStatus();
172174

175+
LOGGER.debug(handshakeStatus, status -> "Handshake status: " + status + " after engine tasks");
176+
173177
if (handshakeStatus == HandshakeStatus.NEED_UNWRAP && !receivedBuffer.hasRemaining()) {
174178
sslNetworkBuffer.clear();
175179
return SKIP_READ_PACKETS;
176180
}
177181

178-
break;
179-
case FINISHED:
180-
case NOT_HANDSHAKING:
181182
break;
182183
default:
183184
throw new IllegalStateException("Invalid SSL status: " + handshakeStatus);
184185
}
185186
}
186187

188+
if (!receivedBuffer.hasRemaining()) {
189+
190+
// if buffer is empty and status is FINISHED then we can notify writer
191+
if (handshakeStatus == HandshakeStatus.FINISHED) {
192+
packetWriter.accept(SSLWritablePacket.getInstance());
193+
}
194+
195+
return SKIP_READ_PACKETS;
196+
}
197+
187198
return decryptAndRead(receivedBuffer);
188199
}
189200

rlib-network/src/main/java/com/ss/rlib/network/packet/impl/AbstractSSLPacketWriter.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.ss.rlib.common.function.NotNullConsumer;
77
import com.ss.rlib.common.function.NullableSupplier;
88
import com.ss.rlib.logger.api.Logger;
9+
import com.ss.rlib.logger.api.LoggerLevel;
910
import com.ss.rlib.logger.api.LoggerManager;
1011
import com.ss.rlib.network.BufferAllocator;
1112
import com.ss.rlib.network.Connection;
@@ -33,6 +34,7 @@ public abstract class AbstractSSLPacketWriter<W extends WritablePacket, C extend
3334

3435
protected final @NotNull SSLEngine sslEngine;
3536
protected final @NotNull NotNullConsumer<WritablePacket> packetWriter;
37+
protected final @NotNull NotNullConsumer<WritablePacket> queueAtFirst;
3638

3739
protected volatile @NotNull ByteBuffer sslNetworkBuffer;
3840

@@ -45,7 +47,8 @@ public AbstractSSLPacketWriter(
4547
@NotNull NotNullConsumer<WritablePacket> writtenPacketHandler,
4648
@NotNull NotNullBiConsumer<WritablePacket, Boolean> sentPacketHandler,
4749
@NotNull SSLEngine sslEngine,
48-
@NotNull NotNullConsumer<WritablePacket> packetWriter
50+
@NotNull NotNullConsumer<WritablePacket> packetWriter,
51+
@NotNull NotNullConsumer<WritablePacket> queueAtFirst
4952
) {
5053
super(
5154
connection,
@@ -58,16 +61,34 @@ public AbstractSSLPacketWriter(
5861
);
5962
this.sslEngine = sslEngine;
6063
this.packetWriter = packetWriter;
64+
this.queueAtFirst = queueAtFirst;
6165
this.sslNetworkBuffer = bufferAllocator.takeBuffer(sslEngine.getSession().getPacketBufferSize());
6266
}
6367

68+
@Override
69+
public void writeNextPacket() {
70+
71+
var status = sslEngine.getHandshakeStatus();
72+
73+
switch (status) {
74+
case NEED_UNWRAP:
75+
return;
76+
}
77+
78+
super.writeNextPacket();
79+
}
80+
6481
@Override
6582
protected @NotNull ByteBuffer serialize(@NotNull WritablePacket packet) {
6683

6784
var status = sslEngine.getHandshakeStatus();
6885

6986
if (status == HandshakeStatus.FINISHED || status == HandshakeStatus.NOT_HANDSHAKING) {
7087

88+
if (packet instanceof SSLWritablePacket) {
89+
return EMPTY_BUFFER;
90+
}
91+
7192
SSLEngineResult result;
7293
try {
7394
result = sslEngine.wrap(super.serialize(packet), sslNetworkBuffer.clear());
@@ -106,7 +127,8 @@ public AbstractSSLPacketWriter(
106127
protected @Nullable ByteBuffer doHandshake(@NotNull WritablePacket packet) throws SSLException {
107128

108129
if (!(packet instanceof SSLWritablePacket)) {
109-
throw new IllegalStateException();
130+
LOGGER.debug(packet, pck -> "Return packet " + pck + " to queue as first");
131+
queueAtFirst.accept(packet);
110132
}
111133

112134
var handshakeStatus = sslEngine.getHandshakeStatus();
@@ -136,10 +158,11 @@ public AbstractSSLPacketWriter(
136158

137159
if (handshakeStatus == HandshakeStatus.NEED_WRAP) {
138160
LOGGER.debug("Send command to wrap data again");
139-
packetWriter.accept(SSLWritablePacket.getInstance());
161+
queueAtFirst.accept(SSLWritablePacket.getInstance());
140162
}
141163

142-
LOGGER.debug(sslNetworkBuffer,
164+
LOGGER.debug(
165+
sslNetworkBuffer,
143166
result,
144167
(buf, res) -> "Send wrapped data:\n" + hexDump(buf, res)
145168
);
@@ -170,18 +193,14 @@ public AbstractSSLPacketWriter(
170193
}
171194
handshakeStatus = sslEngine.getHandshakeStatus();
172195
break;
173-
case FINISHED:
174-
break;
175-
case NOT_HANDSHAKING:
176-
break;
177196
case NEED_UNWRAP:
178-
return EMPTY_BUFFER;
197+
break;
179198
default:
180199
throw new IllegalStateException("Invalid SSL status: " + handshakeStatus);
181200
}
182201
}
183202

184-
return null;
203+
return EMPTY_BUFFER;
185204
}
186205

187206
private void increaseNetworkBuffer() {

rlib-network/src/main/java/com/ss/rlib/network/packet/impl/DefaultSSLPacketWriter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public DefaultSSLPacketWriter(
3030
@NotNull NotNullBiConsumer<WritablePacket, Boolean> sentPacketHandler,
3131
@NotNull SSLEngine sslEngine,
3232
@NotNull NotNullConsumer<WritablePacket> packetWriter,
33+
@NotNull NotNullConsumer<WritablePacket> queueAtFirst,
3334
int packetLengthHeaderSize
3435
) {
3536
super(
@@ -41,7 +42,8 @@ public DefaultSSLPacketWriter(
4142
writtenPacketHandler,
4243
sentPacketHandler,
4344
sslEngine,
44-
packetWriter
45+
packetWriter,
46+
queueAtFirst
4547
);
4648
this.packetLengthHeaderSize = packetLengthHeaderSize;
4749
}

rlib-network/src/main/java/com/ss/rlib/network/server/impl/DefaultServerNetwork.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.ss.rlib.common.util.Utils;
77
import com.ss.rlib.common.util.array.Array;
88
import com.ss.rlib.common.util.array.ArrayFactory;
9+
import com.ss.rlib.logger.api.Logger;
10+
import com.ss.rlib.logger.api.LoggerManager;
911
import com.ss.rlib.network.Network;
1012
import com.ss.rlib.network.ServerNetworkConfig;
1113
import com.ss.rlib.network.UnsafeConnection;
@@ -34,6 +36,8 @@
3436
public final class DefaultServerNetwork<C extends UnsafeConnection<?, ?>> extends AbstractNetwork<C> implements
3537
ServerNetwork<C> {
3638

39+
protected static final Logger LOGGER = LoggerManager.getLogger(DefaultServerNetwork.class);
40+
3741
private interface ServerCompletionHandler<C extends UnsafeConnection<?, ?>> extends
3842
CompletionHandler<AsynchronousSocketChannel, DefaultServerNetwork<C>> {}
3943

0 commit comments

Comments
 (0)