Skip to content

Commit d99830c

Browse files
committed
started working on networking
1 parent 3a18fc6 commit d99830c

File tree

10 files changed

+270
-0
lines changed

10 files changed

+270
-0
lines changed

gestalt-networking/build.gradle

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
group 'org.terasology.gestalt'
6+
version '8.0.0-SNAPSHOT'
7+
8+
repositories {
9+
mavenCentral()
10+
}
11+
12+
dependencies {
13+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
14+
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
15+
implementation group: 'io.netty', name: 'netty-all', version: '4.1.53.Final'
16+
17+
}
18+
19+
test {
20+
useJUnitPlatform()
21+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
reliable.io
3+
4+
Copyright © 2017 - 2019, The Network Protocol Company, Inc.
5+
6+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7+
8+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9+
10+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
11+
in the documentation and/or other materials provided with the distribution.
12+
13+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
14+
from this software without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
17+
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21+
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
22+
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23+
*/
24+
25+
package org.terasology.networking;
26+
27+
public class NetworkConfig {
28+
public int fragmentAbove;
29+
30+
public NetworkConfig setFragmentAbove(int size) {
31+
this.fragmentAbove = size;
32+
return this;
33+
}
34+
35+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.terasology.networking;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import io.netty.channel.socket.DuplexChannel;
5+
6+
public class PacketChannel {
7+
private final DuplexChannel channel;
8+
9+
double time;
10+
float packetLost;
11+
float sendBandwidthKbps;
12+
float receivedBandwidthKbps;
13+
float ackedBandwidthKbps;
14+
15+
int sequence;
16+
int numberAcks;
17+
byte[] acks;
18+
19+
SequenceBuffer<SentPacketData> SentPacketBuffer;
20+
SequenceBuffer<ReceivedPacketData> receivedPacketBuffer;
21+
SequenceBuffer<FragmentReassemblyData> fragmentReassemblyBuffer;
22+
23+
public static class SequenceBuffer<T> {
24+
int sequence;
25+
T[] buffer;
26+
int[] entries;
27+
28+
public int getAcks() {
29+
return sequence - 1;
30+
}
31+
32+
33+
}
34+
35+
public static class SentPacketData {
36+
public double time;
37+
public boolean acked;
38+
public int packetBytes;
39+
}
40+
41+
public static class ReceivedPacketData {
42+
public double time;
43+
public int packetBytes;
44+
}
45+
46+
public static class FragmentReassemblyData {
47+
public short sequence;
48+
public short ack;
49+
public int acks;
50+
public int numberFragmentsReceived;
51+
public int numberFragmentsTotal;
52+
public ByteBuf packedData;
53+
public int packedBytes;
54+
public int packedHeaderBytes;
55+
public ByteBuf fragmentReceived;
56+
}
57+
public int generateAckBits(int ackBits) {
58+
return 0;
59+
}
60+
61+
62+
63+
public PacketChannel(DuplexChannel channel) {
64+
this.channel = channel;
65+
}
66+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.terasology.networking;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import io.netty.channel.ChannelHandlerContext;
5+
import io.netty.channel.ChannelInboundHandlerAdapter;
6+
7+
public class PacketInboundReceiver extends ChannelInboundHandlerAdapter {
8+
9+
@Override
10+
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
11+
ByteBuf packet = (ByteBuf) msg;
12+
13+
super.channelRead(ctx, msg);
14+
}
15+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.terasology.networking;
2+
3+
import io.netty.bootstrap.Bootstrap;
4+
import io.netty.channel.ChannelInitializer;
5+
import io.netty.channel.EventLoopGroup;
6+
import io.netty.channel.nio.NioEventLoopGroup;
7+
import io.netty.channel.socket.SocketChannel;
8+
import io.netty.channel.socket.nio.NioDatagramChannel;
9+
import org.terasology.networking.internal.InboundPacketHandler;
10+
import org.terasology.networking.internal.OutboundPacketHandler;
11+
12+
public class Server {
13+
14+
public void main() {
15+
EventLoopGroup main = new NioEventLoopGroup(); // (1)
16+
Bootstrap bootstrap = new Bootstrap();
17+
bootstrap.group(main)
18+
.channel(NioDatagramChannel.class)
19+
.handler(new ChannelInitializer<SocketChannel>() {
20+
@Override
21+
protected void initChannel(SocketChannel ch) throws Exception {
22+
// PacketChannel channel = new PacketChannel(ch, null);
23+
// ch.pipeline().addLast(new InboundPacketHandler(channel));
24+
//
25+
// ch.pipeline().addLast(new OutboundPacketHandler(channel));
26+
}
27+
});
28+
}
29+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.terasology.networking;
2+
3+
import io.netty.channel.ChannelHandlerAdapter;
4+
import io.netty.channel.ChannelHandlerContext;
5+
import io.netty.channel.ChannelInboundHandlerAdapter;
6+
7+
public class ServerConnectionHandler extends ChannelHandlerAdapter {
8+
9+
public ServerConnectionHandler() {
10+
11+
}
12+
13+
14+
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.terasology.networking.internal;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import io.netty.channel.ChannelHandlerContext;
5+
import io.netty.handler.codec.ByteToMessageDecoder;
6+
import io.netty.handler.codec.MessageToMessageDecoder;
7+
import org.terasology.networking.PacketChannel;
8+
9+
import java.util.List;
10+
11+
public class InboundPacketHandler extends MessageToMessageDecoder<ByteBuf> {
12+
private final PacketChannel channel;
13+
14+
public InboundPacketHandler(PacketChannel channel) {
15+
this.channel = channel;
16+
17+
}
18+
19+
@Override
20+
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
21+
22+
}
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.terasology.networking.internal;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import io.netty.channel.ChannelHandlerContext;
5+
import io.netty.handler.codec.MessageToMessageEncoder;
6+
import org.terasology.networking.NetworkConfig;
7+
import org.terasology.networking.PacketChannel;
8+
9+
import java.util.List;
10+
11+
public class OutboundPacketHandler extends MessageToMessageEncoder<ByteBuf> {
12+
private final PacketChannel channel;
13+
private final NetworkConfig config;
14+
public OutboundPacketHandler(NetworkConfig config, PacketChannel channel) {
15+
super(null);
16+
this.channel = channel;
17+
this.config = config;
18+
}
19+
20+
@Override
21+
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
22+
if (msg.readableBytes() <= config.fragmentAbove) {
23+
24+
}
25+
}
26+
27+
28+
// @Override
29+
// protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
30+
// ByteBuf buffer = (ByteBuf) msg;
31+
//
32+
//
33+
// if (buffer.readableBytes() <= config.fragmentAbove) {
34+
// }
35+
// }
36+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.terasology.networking.piplineFactory;
2+
3+
import io.netty.channel.ChannelInitializer;
4+
import io.netty.channel.socket.DuplexChannel;
5+
import io.netty.handler.codec.DatagramPacketDecoder;
6+
import io.netty.handler.codec.DatagramPacketEncoder;
7+
import org.terasology.networking.NetworkConfig;
8+
import org.terasology.networking.PacketChannel;
9+
import org.terasology.networking.internal.InboundPacketHandler;
10+
import org.terasology.networking.internal.OutboundPacketHandler;
11+
12+
public class PacketPipeline extends ChannelInitializer<DuplexChannel> {
13+
public static String InboundPackets = "INBOUND_PACKETS";
14+
public static String OutboundPackets = "INBOUND_PACKETS";
15+
16+
private final NetworkConfig config;
17+
18+
public PacketPipeline(NetworkConfig config) {
19+
this.config = config;
20+
}
21+
22+
@Override
23+
protected void initChannel(DuplexChannel ch) throws Exception {
24+
PacketChannel channel = new PacketChannel(ch);
25+
ch.pipeline().addLast(InboundPackets, new DatagramPacketDecoder(new InboundPacketHandler(channel)));
26+
ch.pipeline().addLast(OutboundPackets, new DatagramPacketEncoder<>(new OutboundPacketHandler(config, channel)));
27+
}
28+
}

settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ if (rootProject.projectDir.toPath().resolve("local.properties").toFile().exists(
55
} else {
66
println "No local.properties file found, bypassing Android elements"
77
}
8+
include 'gestalt-networking'
9+

0 commit comments

Comments
 (0)