Skip to content

Commit 60e591c

Browse files
committed
Finish implement_ssl_network
2 parents 11d399b + 9a0b8ff commit 60e591c

43 files changed

Lines changed: 2084 additions & 329 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.ss.rlib.common.function;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
5+
@FunctionalInterface
6+
public interface NotNullSafeTriFunction<F, S, T, R> extends SafeTriFunction<F, S, T, R> {
7+
8+
@Override
9+
@NotNull R apply(@NotNull F first, @NotNull S second, @NotNull T third) throws Exception;
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.ss.rlib.common.function;
2+
3+
import org.jetbrains.annotations.Nullable;
4+
5+
import java.util.function.Supplier;
6+
7+
@FunctionalInterface
8+
public interface NullableSupplier<T> extends Supplier<T> {
9+
10+
@Override
11+
@Nullable T get();
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.ss.rlib.common.function;
2+
3+
import org.jetbrains.annotations.Nullable;
4+
5+
@FunctionalInterface
6+
public interface SafeTriFunction<F, S, T, R> {
7+
8+
@Nullable R apply(@Nullable F first, @Nullable S second, @Nullable T third) throws Exception;
9+
}

rlib-common/src/main/java/com/ss/rlib/common/util/Utils.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,34 @@ public static <F, S> void unchecked(
264264
}
265265
}
266266

267+
/**
268+
* Execute the function with auto-converting a checked exception to an unchecked.
269+
*
270+
* @param first the first argument.
271+
* @param second the second argument.
272+
* @param function the function.
273+
* @param <F> the first's type.
274+
* @param <S> the second's type.
275+
* @param <T> the third's type.
276+
* @param <R> the result's type.
277+
* @return the result.
278+
* @since 9.9.0
279+
*/
280+
public static <F, S, T, R> @NotNull R uncheckedGet(
281+
@NotNull F first,
282+
@NotNull S second,
283+
@NotNull T third,
284+
@NotNull NotNullSafeTriFunction<F, S, T, R> function
285+
) {
286+
try {
287+
return function.apply(first, second, third);
288+
} catch (IOException e) {
289+
throw new UncheckedIOException(e);
290+
} catch (Exception e) {
291+
throw new RuntimeException(e);
292+
}
293+
}
294+
267295
/**
268296
* Try to execute a function with some result.
269297
*

rlib-logger-api/src/main/java/com/ss/rlib/logger/api/Logger.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.ss.rlib.logger.api;
22

33
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.Nullable;
45

56
/**
67
* The interface to implement a logger.
@@ -33,6 +34,12 @@ interface BiFactory<F, S> {
3334
@NotNull String make(@NotNull F first, @NotNull S second);
3435
}
3536

37+
@FunctionalInterface
38+
interface NullableBiFactory<F, S> {
39+
40+
@NotNull String make(@Nullable F first, @Nullable S second);
41+
}
42+
3643
@FunctionalInterface
3744
interface ObjIntFactory<F> {
3845

@@ -98,6 +105,23 @@ default <F, S> void debug(
98105
print(LoggerLevel.DEBUG, first, second, messageFactory);
99106
}
100107

108+
/**
109+
* Print a build debug message.
110+
*
111+
* @param first the first arg for the message factory.
112+
* @param second the second arg for the message factory.
113+
* @param messageFactory the message factory.
114+
* @param <F> the first argument's type.
115+
* @param <S> the second argument's type.
116+
*/
117+
default <F, S> void debugNullable(
118+
@Nullable F first,
119+
@Nullable S second,
120+
@NotNull Logger.NullableBiFactory<F, S> messageFactory
121+
) {
122+
print(LoggerLevel.DEBUG, first, second, messageFactory);
123+
}
124+
101125
/**
102126
* Print a build debug message.
103127
*
@@ -366,6 +390,27 @@ default <F, S> void print(
366390
}
367391
}
368392

393+
/**
394+
* Print a build message.
395+
*
396+
* @param level the level of the message.
397+
* @param first the first arg for the message factory.
398+
* @param second the second arg for the message factory.
399+
* @param messageFactory the message factory.
400+
* @param <F> the first argument's type.
401+
* @param <S> the second argument's type.
402+
*/
403+
default <F, S> void print(
404+
@NotNull LoggerLevel level,
405+
@Nullable F first,
406+
@Nullable S second,
407+
@NotNull Logger.NullableBiFactory<F, S> messageFactory
408+
) {
409+
if (isEnabled(level)) {
410+
print(level, messageFactory.make(first, second));
411+
}
412+
}
413+
369414
/**
370415
* Print a build message.
371416
*

rlib-logger-api/src/main/java/com/ss/rlib/logger/api/LoggerManager.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,24 @@ public static void removeListener(@NotNull LoggerListener listener) {
116116
public static void removeWriter(@NotNull Writer writer) {
117117
LOGGER_FACTORY.removeWriter(writer);
118118
}
119+
120+
/**
121+
* Enable passed logger level for some logger.
122+
*
123+
* @param cs the class which use its own logger.
124+
* @param level the logger level to enable.
125+
*/
126+
public static void enable(@NotNull Class<?> cs, @NotNull LoggerLevel level) {
127+
getLogger(cs).setEnabled(level, true);
128+
}
129+
130+
/**
131+
* Disable passed logger level for some logger.
132+
*
133+
* @param cs the class which use its own logger.
134+
* @param level the logger level to disable.
135+
*/
136+
public static void disable(@NotNull Class<?> cs, @NotNull LoggerLevel level) {
137+
getLogger(cs).setEnabled(level, false);
138+
}
119139
}

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.ss.rlib.network;
22

3+
import com.ss.rlib.common.function.NotNullBiConsumer;
34
import com.ss.rlib.network.packet.ReadablePacket;
45
import com.ss.rlib.network.packet.WritablePacket;
56
import lombok.AllArgsConstructor;
67
import org.jetbrains.annotations.NotNull;
78
import reactor.core.publisher.Flux;
89

910
import java.util.concurrent.CompletableFuture;
10-
import java.util.function.BiConsumer;
1111

1212
/**
1313
* The interface to implement an async connection.
@@ -69,7 +69,7 @@ class ReceivedPacketEvent<C extends Connection<?, ?>, R extends ReadablePacket>
6969
*
7070
* @param consumer the consumer.
7171
*/
72-
void onReceive(@NotNull BiConsumer<? super Connection<R, W>, ? super R> consumer);
72+
void onReceive(@NotNull NotNullBiConsumer<? super Connection<R, W>, ? super R> consumer);
7373

7474
/**
7575
* Get a stream of received packet events.
@@ -84,11 +84,4 @@ class ReceivedPacketEvent<C extends Connection<?, ?>, R extends ReadablePacket>
8484
* @return the stream of received packets.
8585
*/
8686
@NotNull Flux<? extends R> receivedPackets();
87-
88-
/**
89-
* Get a used implementation of a data crypt.
90-
*
91-
* @return the used implementation of a data crypt.
92-
*/
93-
@NotNull NetworkCryptor getCrypt();
9487
}

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

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
import com.ss.rlib.network.impl.DefaultBufferAllocator;
66
import com.ss.rlib.network.impl.DefaultConnection;
77
import com.ss.rlib.network.impl.StringDataConnection;
8+
import com.ss.rlib.network.impl.StringDataSSLConnection;
89
import com.ss.rlib.network.packet.impl.DefaultReadablePacket;
910
import com.ss.rlib.network.packet.registry.ReadablePacketRegistry;
1011
import com.ss.rlib.network.server.ServerNetwork;
1112
import com.ss.rlib.network.server.impl.DefaultServerNetwork;
1213
import org.jetbrains.annotations.NotNull;
1314

15+
import javax.net.ssl.SSLContext;
1416
import java.nio.channels.AsynchronousSocketChannel;
1517
import java.util.function.BiFunction;
1618

@@ -21,14 +23,14 @@
2123
*/
2224
public final class NetworkFactory {
2325

24-
public static <C extends Connection<?, ?>> @NotNull ClientNetwork<C> newClientNetwork(
26+
public static <C extends UnsafeConnection<?, ?>> @NotNull ClientNetwork<C> newClientNetwork(
2527
@NotNull NetworkConfig networkConfig,
2628
@NotNull BiFunction<Network<C>, AsynchronousSocketChannel, C> channelToConnection
2729
) {
2830
return new DefaultClientNetwork<>(networkConfig, channelToConnection);
2931
}
3032

31-
public static <C extends Connection<?, ?>> @NotNull ServerNetwork<C> newServerNetwork(
33+
public static <C extends UnsafeConnection<?, ?>> @NotNull ServerNetwork<C> newServerNetwork(
3234
@NotNull ServerNetworkConfig networkConfig,
3335
@NotNull BiFunction<Network<C>, AsynchronousSocketChannel, C> channelToConnection
3436
) {
@@ -107,15 +109,34 @@ public final class NetworkFactory {
107109
) {
108110
return newClientNetwork(
109111
networkConfig,
110-
(network, channel) -> new DefaultConnection(network,
112+
(network, channel) -> new DefaultConnection(
113+
network,
111114
channel,
112-
NetworkCryptor.NULL,
113115
bufferAllocator,
114116
packetRegistry
115117
)
116118
);
117119
}
118120

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+
119140
/**
120141
* Create string packet based asynchronous server network.
121142
*
@@ -157,6 +178,25 @@ public final class NetworkFactory {
157178
);
158179
}
159180

181+
/**
182+
* Create string packet based asynchronous secure server network.
183+
*
184+
* @param networkConfig the network config.
185+
* @param bufferAllocator the buffer allocator.
186+
* @param sslContext the ssl context.
187+
* @return the server network.
188+
*/
189+
public static @NotNull ServerNetwork<StringDataSSLConnection> newStringDataSSLServerNetwork(
190+
@NotNull ServerNetworkConfig networkConfig,
191+
@NotNull BufferAllocator bufferAllocator,
192+
@NotNull SSLContext sslContext
193+
) {
194+
return newServerNetwork(
195+
networkConfig,
196+
(network, channel) -> new StringDataSSLConnection(network, channel, bufferAllocator, sslContext, false)
197+
);
198+
}
199+
160200
/**
161201
* Create id based packet default asynchronous server network.
162202
*
@@ -188,9 +228,9 @@ public final class NetworkFactory {
188228
) {
189229
return newServerNetwork(
190230
networkConfig,
191-
(network, channel) -> new DefaultConnection(network,
231+
(network, channel) -> new DefaultConnection(
232+
network,
192233
channel,
193-
NetworkCryptor.NULL,
194234
bufferAllocator,
195235
packetRegistry
196236
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.ss.rlib.network;
2+
3+
import com.ss.rlib.network.packet.ReadablePacket;
4+
import com.ss.rlib.network.packet.WritablePacket;
5+
6+
public interface UnsafeConnection<R extends ReadablePacket, W extends WritablePacket> extends Connection<R, W> {
7+
8+
void onConnected();
9+
10+
}

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import static com.ss.rlib.common.util.Utils.uncheckedGet;
55
import com.ss.rlib.common.concurrent.util.ThreadUtils;
66
import com.ss.rlib.common.util.AsyncUtils;
7-
import com.ss.rlib.common.util.Utils;
7+
import com.ss.rlib.logger.api.Logger;
8+
import com.ss.rlib.logger.api.LoggerManager;
89
import com.ss.rlib.network.Connection;
910
import com.ss.rlib.network.Network;
1011
import com.ss.rlib.network.NetworkConfig;
@@ -31,7 +32,9 @@
3132
*/
3233
public class DefaultClientNetwork<C extends Connection<?, ?>> extends AbstractNetwork<C> implements ClientNetwork<C> {
3334

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

3639
protected volatile @Nullable CompletableFuture<C> pendingConnection;
3740
protected volatile @Getter @Nullable C currentConnection;
@@ -42,6 +45,13 @@ public DefaultClientNetwork(
4245
) {
4346
super(config, channelToConnection);
4447
this.connecting = new AtomicBoolean(false);
48+
49+
LOGGER.info(config, conf -> "Client network configuration: {\n" +
50+
" groupName: \"" + conf.getThreadGroupName() + "\",\n" +
51+
" readBufferSize: " + conf.getReadBufferSize() + ",\n" +
52+
" pendingBufferSize: " + conf.getPendingBufferSize() + ",\n" +
53+
" writeBufferSize: " + conf.getWriteBufferSize() + "\n" +
54+
"}");
4555
}
4656

4757
@Override
@@ -74,7 +84,7 @@ public DefaultClientNetwork(
7484

7585
@Override
7686
public void completed(@Nullable Void result, @Nullable Void attachment) {
77-
LOGGER.info(channel, ch -> "Connected to server: " + NetworkUtils.getSocketAddress(ch));
87+
LOGGER.info(channel, ch -> "Connected to server: " + NetworkUtils.getRemoteAddress(ch));
7888
asyncResult.complete(channelToConnection.apply(DefaultClientNetwork.this, channel));
7989
}
8090

0 commit comments

Comments
 (0)