Skip to content

Commit

Permalink
started working on networking
Browse files Browse the repository at this point in the history
  • Loading branch information
pollend committed Jul 6, 2021
1 parent 3a18fc6 commit d99830c
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 0 deletions.
21 changes: 21 additions & 0 deletions gestalt-networking/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
plugins {
id 'java'
}

group 'org.terasology.gestalt'
version '8.0.0-SNAPSHOT'

repositories {
mavenCentral()
}

dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
implementation group: 'io.netty', name: 'netty-all', version: '4.1.53.Final'

}

test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
reliable.io
Copyright © 2017 - 2019, The Network Protocol Company, Inc.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package org.terasology.networking;

public class NetworkConfig {
public int fragmentAbove;

public NetworkConfig setFragmentAbove(int size) {
this.fragmentAbove = size;
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.terasology.networking;

import io.netty.buffer.ByteBuf;
import io.netty.channel.socket.DuplexChannel;

public class PacketChannel {
private final DuplexChannel channel;

double time;
float packetLost;
float sendBandwidthKbps;
float receivedBandwidthKbps;
float ackedBandwidthKbps;

int sequence;
int numberAcks;
byte[] acks;

SequenceBuffer<SentPacketData> SentPacketBuffer;
SequenceBuffer<ReceivedPacketData> receivedPacketBuffer;
SequenceBuffer<FragmentReassemblyData> fragmentReassemblyBuffer;

public static class SequenceBuffer<T> {
int sequence;
T[] buffer;
int[] entries;

public int getAcks() {
return sequence - 1;
}


}

public static class SentPacketData {
public double time;
public boolean acked;
public int packetBytes;
}

public static class ReceivedPacketData {
public double time;
public int packetBytes;
}

public static class FragmentReassemblyData {
public short sequence;
public short ack;
public int acks;
public int numberFragmentsReceived;
public int numberFragmentsTotal;
public ByteBuf packedData;
public int packedBytes;
public int packedHeaderBytes;
public ByteBuf fragmentReceived;
}
public int generateAckBits(int ackBits) {
return 0;
}



public PacketChannel(DuplexChannel channel) {
this.channel = channel;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.terasology.networking;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class PacketInboundReceiver extends ChannelInboundHandlerAdapter {

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf packet = (ByteBuf) msg;

super.channelRead(ctx, msg);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.terasology.networking;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioDatagramChannel;
import org.terasology.networking.internal.InboundPacketHandler;
import org.terasology.networking.internal.OutboundPacketHandler;

public class Server {

public void main() {
EventLoopGroup main = new NioEventLoopGroup(); // (1)
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(main)
.channel(NioDatagramChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// PacketChannel channel = new PacketChannel(ch, null);
// ch.pipeline().addLast(new InboundPacketHandler(channel));
//
// ch.pipeline().addLast(new OutboundPacketHandler(channel));
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.terasology.networking;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class ServerConnectionHandler extends ChannelHandlerAdapter {

public ServerConnectionHandler() {

}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.terasology.networking.internal;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToMessageDecoder;
import org.terasology.networking.PacketChannel;

import java.util.List;

public class InboundPacketHandler extends MessageToMessageDecoder<ByteBuf> {
private final PacketChannel channel;

public InboundPacketHandler(PacketChannel channel) {
this.channel = channel;

}

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.terasology.networking.internal;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import org.terasology.networking.NetworkConfig;
import org.terasology.networking.PacketChannel;

import java.util.List;

public class OutboundPacketHandler extends MessageToMessageEncoder<ByteBuf> {
private final PacketChannel channel;
private final NetworkConfig config;
public OutboundPacketHandler(NetworkConfig config, PacketChannel channel) {
super(null);
this.channel = channel;
this.config = config;
}

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
if (msg.readableBytes() <= config.fragmentAbove) {

}
}


// @Override
// protected void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) throws Exception {
// ByteBuf buffer = (ByteBuf) msg;
//
//
// if (buffer.readableBytes() <= config.fragmentAbove) {
// }
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.terasology.networking.piplineFactory;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.DuplexChannel;
import io.netty.handler.codec.DatagramPacketDecoder;
import io.netty.handler.codec.DatagramPacketEncoder;
import org.terasology.networking.NetworkConfig;
import org.terasology.networking.PacketChannel;
import org.terasology.networking.internal.InboundPacketHandler;
import org.terasology.networking.internal.OutboundPacketHandler;

public class PacketPipeline extends ChannelInitializer<DuplexChannel> {
public static String InboundPackets = "INBOUND_PACKETS";
public static String OutboundPackets = "INBOUND_PACKETS";

private final NetworkConfig config;

public PacketPipeline(NetworkConfig config) {
this.config = config;
}

@Override
protected void initChannel(DuplexChannel ch) throws Exception {
PacketChannel channel = new PacketChannel(ch);
ch.pipeline().addLast(InboundPackets, new DatagramPacketDecoder(new InboundPacketHandler(channel)));
ch.pipeline().addLast(OutboundPackets, new DatagramPacketEncoder<>(new OutboundPacketHandler(config, channel)));
}
}
2 changes: 2 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ if (rootProject.projectDir.toPath().resolve("local.properties").toFile().exists(
} else {
println "No local.properties file found, bypassing Android elements"
}
include 'gestalt-networking'

0 comments on commit d99830c

Please sign in to comment.