Skip to content

Commit

Permalink
Bumped version to 1.0.1
Browse files Browse the repository at this point in the history
Updated README.md example code to add usage for NetSerializable
Made backends non blocking
  • Loading branch information
adamaq01 committed May 3, 2019
1 parent 18423f5 commit 2016d93
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 48 deletions.
60 changes: 51 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,60 @@ repositories {
```
```
dependencies {
compile 'com.github.Adamaq01:ozao-net:1.0.0'
compile 'com.github.Adamaq01:ozao-net:1.0.1'
}
```

****

## Examples

##### Net Serializable Class:
```Java
public class Bird implements NetSerializable<Bird> {

private String name;
private String message;

public Bird(String name, String message) {
this.name = name;
this.message = message;
}

public String getName() {
return name;
}

public String getMessage() {
return message;
}

@Override
public Packet write(Packet packet) {
return packet.putString(name).putString(message);
}

@Override
public Bird read(Packet packet) {
this.name = packet.getString();
this.message = packet.getString();

return this;
}
}
```

##### UDP Server:
```Java
Server server = Server.create(new UDPServerBackend());
server.addHandler(new ServerHandler() {

@Override
public void onPacketReceive(Connection connection, Packet packet) {
System.out.println("Received a packet ! " + packet.getString());
connection.sendPacket(Packet.create().putInt(0));
Bird bird = new Bird("", "").read(packet);
System.out.println(String.format("%s has arrived, he carries this message: %s", bird.getName(), bird.getMessage()));
connection.sendPacket(new Bird("Pike", "Yes please, I love chocolate !").write(Packet.create()));
connection.sendPacket(Packet.create().putByte((byte) 42));
server.close();
}
});
Expand All @@ -40,16 +78,20 @@ server.bind(2812);
```Java
Client client = Client.create(new UDPClientBackend());
client.addHandler(new ClientHandler() {

@Override
public void onConnect() {
client.sendPacket(Packet.create().putString("Hey"));
client.sendPacket(new Bird("Mike", "Do you want some chocolate ?").write(Packet.create()));
}

@Override
public void onPacketReceive(Packet packet) {
System.out.println("Received a packet ! " + packet.getInt());
client.disconnect();
if (packet.readableBytes() == 1)
client.disconnect();
else {
Bird bird = new Bird("", "").read(packet);
System.out.println(String.format("%s has arrived, he carries this message: %s", bird.getName(), bird.getMessage()));
}
}
});
client.connect("localhost", 2812);
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'fr.adamaq01'
version '1.0.0'
version '1.0.1'

sourceCompatibility = 1.8

Expand All @@ -21,7 +21,7 @@ publishing {
maven(MavenPublication) {
groupId = 'fr.adamaq01'
artifactId = 'ozao-net'
version = '1.0.0'
version = '1.0.1'

from components.java
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import fr.adamaq01.ozao.net.packet.Packet;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.ReferenceCountUtil;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,21 @@ protected void connect(InetSocketAddress address) {
.handler(new TCPChannelInitializer(this))
.option(ChannelOption.AUTO_READ, true);
channelFuture = bootstrap.connect(address).sync();
// Client started
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}

// Client started

new Thread(() -> {
try {
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}).start();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package fr.adamaq01.ozao.net.client.backend.udp;

import fr.adamaq01.ozao.net.packet.Packet;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.ReferenceCountUtil;

class UDPChannelHandler extends SimpleChannelInboundHandler<ByteBuf> {
class UDPChannelHandler extends SimpleChannelInboundHandler<DatagramPacket> {

private UDPClientBackend clientBackend;

Expand All @@ -28,8 +27,8 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
}

@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
Packet packet = Packet.create(ReferenceCountUtil.retain(msg));
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
Packet packet = Packet.create(ReferenceCountUtil.retain(msg.content()));
this.clientBackend.handlers.forEach(handler -> handler.onPacketReceive(packet));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,28 @@ public UDPClientBackend() {
@Override
protected void connect(InetSocketAddress address) {
EventLoopGroup group = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(NioDatagramChannel.class)
.handler(new UDPChannelInitializer(this))
.option(ChannelOption.AUTO_READ, true);
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(NioDatagramChannel.class)
.handler(new UDPChannelInitializer(this))
.option(ChannelOption.AUTO_READ, true);
channelFuture = bootstrap.connect(address).sync();
// Client started
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}

// Client started

new Thread(() -> {
try {
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}).start();
}

@Override
Expand Down
1 change: 0 additions & 1 deletion src/main/java/fr/adamaq01/ozao/net/packet/Packet.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public Packet putPacket(Packet src) {
return this;
}


public Packet putBytes(byte... src) {
this.data.writeBytes(src);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import fr.adamaq01.ozao.net.packet.Packet;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.ReferenceCountUtil;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,22 @@ protected void bind(int port) {
.childHandler(new TCPChannelInitializer(this))
.childOption(ChannelOption.AUTO_READ, true);
channelFuture = serverBootstrap.bind(port).sync();
// Server started
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}

// Server started

new Thread(() -> {
try {
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}).start();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import fr.adamaq01.ozao.net.packet.Packet;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.ReferenceCountUtil;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,29 @@ public UDPServerBackend() {
protected void bind(int port) {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(UDPServerChannel.class)
.childHandler(new UDPChannelInitializer(this))
.childOption(ChannelOption.AUTO_READ, true);
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(UDPServerChannel.class)
.childHandler(new UDPChannelInitializer(this))
.childOption(ChannelOption.AUTO_READ, true);
channelFuture = serverBootstrap.bind(port).sync();
// Server started
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}

// Server started

new Thread(() -> {
try {
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}).start();
}

@Override
Expand Down

0 comments on commit 2016d93

Please sign in to comment.