Skip to content

Commit 8036d03

Browse files
committed
Fixed a problem with the session, that would make the session inaccessible. There is going to be a new release due to this issue. The rest were just small improvements.
Next is the implementation of custom class serialization.
1 parent 19617c3 commit 8036d03

File tree

3 files changed

+31
-26
lines changed

3 files changed

+31
-26
lines changed

client/src/main/java/de/datasec/hydra/client/HydraClient.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,21 @@ public Session build() {
5454
private Session setUpClient() {
5555
HydraChannelInitializer initializer = new HydraChannelInitializer(protocol, new NioEventLoopGroup[]{workerGroup = new NioEventLoopGroup(workerThreads)});
5656

57-
try {
58-
Bootstrap bootstrap = new Bootstrap()
59-
.group(workerGroup)
60-
.channel(NioSocketChannel.class)
61-
.remoteAddress(host, port)
62-
.handler(initializer);
57+
Bootstrap bootstrap = new Bootstrap()
58+
.group(workerGroup)
59+
.channel(NioSocketChannel.class)
60+
.remoteAddress(host, port)
61+
.handler(initializer);
6362

64-
// TODO: THINK OF DIFFERENT SOLUTION FOR CHANNELOPTION
65-
options.forEach((option, value) -> bootstrap.option(ChannelOption.valueOf(option.name()), value));
63+
options.forEach((option, value) -> bootstrap.option(ChannelOption.valueOf(option.name()), value));
6664

65+
try {
6766
bootstrap.connect().sync();
68-
} catch (Exception e) {
67+
} catch (InterruptedException e) {
6968
e.printStackTrace();
7069
}
7170

7271
return initializer.getSession();
7372
}
7473
}
75-
}
74+
}

server/src/main/java/de/datasec/hydra/server/HydraServer.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package de.datasec.hydra.server;
22

3+
import de.datasec.hydra.shared.handler.HydraSession;
34
import de.datasec.hydra.shared.handler.Session;
45
import de.datasec.hydra.shared.initializer.HydraChannelInitializer;
56
import de.datasec.hydra.shared.protocol.HydraProtocol;
67
import io.netty.bootstrap.ServerBootstrap;
8+
import io.netty.channel.Channel;
79
import io.netty.channel.ChannelOption;
810
import io.netty.channel.nio.NioEventLoopGroup;
911
import io.netty.channel.socket.nio.NioServerSocketChannel;
@@ -68,23 +70,26 @@ public Session build() {
6870
}
6971

7072
private Session setUpClient() {
71-
HydraChannelInitializer initializer = new HydraChannelInitializer(protocol, new NioEventLoopGroup[]{bossGroup = new NioEventLoopGroup(bossThreads), workerGroup = new NioEventLoopGroup(workerThreads)});
73+
NioEventLoopGroup[] loopGroups = new NioEventLoopGroup[]{bossGroup = new NioEventLoopGroup(bossThreads), workerGroup = new NioEventLoopGroup(workerThreads)};
74+
Channel channel = null;
7275

73-
try {
74-
ServerBootstrap serverBootstrap = new ServerBootstrap()
75-
.group(bossGroup, workerGroup)
76-
.channel(NioServerSocketChannel.class)
77-
.childHandler(initializer);
76+
ServerBootstrap serverBootstrap = new ServerBootstrap()
77+
.group(bossGroup, workerGroup)
78+
.channel(NioServerSocketChannel.class)
79+
.childHandler(new HydraChannelInitializer(protocol, loopGroups));
7880

79-
options.forEach((option, value) -> serverBootstrap.option(ChannelOption.valueOf(option.name()), value));
80-
childOptions.forEach((option, value) -> serverBootstrap.childOption(ChannelOption.valueOf(option.name()), value));
81+
options.forEach((option, value) -> serverBootstrap.option(ChannelOption.valueOf(option.name()), value));
82+
childOptions.forEach((option, value) -> serverBootstrap.childOption(ChannelOption.valueOf(option.name()), value));
8183

82-
serverBootstrap.bind(host, port).sync().channel().closeFuture().sync();
83-
} catch (Exception e) {
84+
try {
85+
channel = serverBootstrap.bind(host, port).sync().channel();
86+
} catch (InterruptedException e) {
8487
e.printStackTrace();
8588
}
8689

87-
return initializer.getSession();
90+
// Needs to be created here already and overridden in the channel initializer,
91+
// as the channel is not immediately initialized
92+
return new HydraSession(channel, protocol, loopGroups);
8893
}
8994
}
9095
}

shared/src/main/java/de/datasec/hydra/shared/handler/HydraSession.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import io.netty.channel.ChannelHandlerContext;
77
import io.netty.channel.SimpleChannelInboundHandler;
88
import io.netty.channel.nio.NioEventLoopGroup;
9+
import io.netty.util.concurrent.AbstractEventExecutorGroup;
910

1011
import java.net.SocketAddress;
12+
import java.util.Arrays;
1113

1214
/**
1315
* Created by DataSec on 29.09.2017.
@@ -39,16 +41,14 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
3941

4042
@Override
4143
public void send(Packet packet) {
44+
// TODO: Implement custom class serialization
4245
channel.writeAndFlush(packet);
4346
}
4447

4548
@Override
4649
public void close() {
4750
channel.disconnect();
48-
49-
for (NioEventLoopGroup group : loopGroups) {
50-
group.shutdownGracefully();
51-
}
51+
Arrays.stream(loopGroups).forEach(AbstractEventExecutorGroup::shutdownGracefully);
5252
}
5353

5454
@Override
@@ -63,6 +63,7 @@ public HydraProtocol getProtocol() {
6363

6464
@Override
6565
public SocketAddress getRemoteAddress() {
66-
return channel.remoteAddress();
66+
SocketAddress address = channel.remoteAddress();
67+
return address == null ? channel.localAddress() : address;
6768
}
6869
}

0 commit comments

Comments
 (0)