Jump to content

[1.7.2]Sending a packet to the server with the new netty system


Miclebrick

Recommended Posts

How do I send a packet to a server with the netty system?

 

Figured this out just now. Here's the client code I'm using:

 

import static io.netty.buffer.Unpooled.buffer;

import io.netty.buffer.ByteBuf;

 

@Mod(modid = "NetworkExample", name = "NetworkExample", version = "0.1")

public class NetworkExample {

    @EventHandler

    public void serverLoad(FMLServerStartingEvent event) {

        // create our mod's channel.

        NetworkRegistry.INSTANCE.newChannel("NetworkExample", new PacketHandler());

    }

 

    @SubscribeEvent(priority = EventPriority.NORMAL)

    public void onEntityUpdated(LivingEvent.LivingUpdateEvent updateEvent) {

        ByteBuf data = buffer(4);

        data.writeInt(42);

        C17PacketCustomPayload packet = new C17PacketCustomPayload("NetworkExample", data);

        EntityClientPlayerMP player = (EntityClientPlayerMP)updateEvent.entityLiving;

        player.sendQueue.func_147297_a(packet);

    }

}

 

And here's the server-side packet handler:

 

import io.netty.buffer.ByteBuf;

import io.netty.channel.ChannelHandler.Sharable;

import io.netty.channel.ChannelHandlerContext;

import io.netty.channel.SimpleChannelInboundHandler;

import cpw.mods.fml.common.network.internal.FMLProxyPacket;

 

@Sharable

public class PacketHandler extends SimpleChannelInboundHandler<FMLProxyPacket> {

    @Override

    protected void channelRead0(ChannelHandlerContext ctx, FMLProxyPacket packet) throws Exception {

        if (packet.channel().equals("NetworkExample")) {

            ByteBuf payload = packet.payload();

            if (payload.readableBytes() == 4) {

                int number = payload.readInt();

                System.out.println("number = " + number);

            }

        }

    }

}

 

Link to comment
Share on other sites

Taken from build 999s changelog:

Add in a simple(ish) event driven network handling system. Register using newEventDrivenChannel and you'll get a simple network handler that will fire events at the subscriber(s) of your choice, whenever a packet is received. You'll also get some convenience methods for sending to things.

 

It looks like cpw is working on making it easier.

Link to comment
Share on other sites

That would be very usefull, i've tried to send a packet but i need to tell the server which player sent it and with the example above you can just send a packet and i don't know how to execute it in the server .

In 1.6.4 i had done a complete network handling as showed in the forge tutorials and it worked perfectly, but i don't know how to do the same with netty.

Link to comment
Share on other sites

That would be very usefull, i've tried to send a packet but i need to tell the server which player sent it and with the example above you can just send a packet and i don't know how to execute it in the server .

In 1.6.4 i had done a complete network handling as showed in the forge tutorials and it worked perfectly, but i don't know how to do the same with netty.

 

Here's how I identify the player on the client side:

 

    @SubscribeEvent(priority = EventPriority.NORMAL)

    public void onEntityUpdated(LivingEvent.LivingUpdateEvent updateEvent) {

        EntityClientPlayerMP player = (EntityClientPlayerMP)updateEvent.entityLiving;

        ByteBuf data = buffer(4);

        data.writeInt(player.func_145782_y());  // Entity.getEntityID()

        data.writeInt(42);

        C17PacketCustomPayload packet = new C17PacketCustomPayload("NetworkExample", data);

        player.sendQueue.func_147297_a(packet);

    }

 

On the server side I simply use that entity ID to look up something in a hash table to do the player matching.

Link to comment
Share on other sites

Ok, i've done almost the same, i have made a playerhandler server side that extends from the ServerConfigurationManager who receive the packet and handle all the stuff i need server side.

But in the latest 999 i have noticed that there are some event added to simplify the network handling, i will see how to use this stuff.

Link to comment
Share on other sites

Ok, i've done almost the same, i have made a playerhandler server side that extends from the ServerConfigurationManager who receive the packet and handle all the stuff i need server side.

But in the latest 999 i have noticed that there are some event added to simplify the network handling, i will see how to use this stuff.

 

that would be cool, been looking everywhere for info on the new system since 999 released yesterday all i can find is this thread. Looked all over the code too cant find it at all.

Link to comment
Share on other sites

Not that difficult to find

[embed=425,349]

FMLEventChannel event = NetworkRegistry.INSTANCE.newEventDrivenChannel("YourChannelName");

event.register(new YourPacketHandler());

[/embed]

Then in your packet handler you have two functions

 

[embed=425,349]

@SubscribeEvent

public void onServerPacket(ServerCustomPacketEvent event) {

 

}

@SubscribeEvent

public void onClientPacket(ClientCustomPacketEvent event) {

 

}

[/embed]

Link to comment
Share on other sites

Not that difficult to find

[embed=425,349]

FMLEventChannel event = NetworkRegistry.INSTANCE.newEventDrivenChannel("YourChannelName");

event.register(new YourPacketHandler());

[/embed]

Then in your packet handler you have two functions

 

[embed=425,349]

@SubscribeEvent

public void onServerPacket(ServerCustomPacketEvent event) {

 

}

@SubscribeEvent

public void onClientPacket(ClientCustomPacketEvent event) {

 

}

[/embed]

 

How does one send the packet then? In my code above, I'm writing:

 

        ByteBuf data = buffer(4);

        data.writeInt(42);

        C17PacketCustomPayload packet = new C17PacketCustomPayload("NetworkExample", data);

        EntityClientPlayerMP player = (EntityClientPlayerMP)updateEvent.entityLiving;

        player.sendQueue.func_147297_a(packet);

 

Would I just change the packet class to ServerCustomPacketEvent when sending from client to server?

Link to comment
Share on other sites

Not that difficult to find

[embed=425,349]

FMLEventChannel event = NetworkRegistry.INSTANCE.newEventDrivenChannel("YourChannelName");

event.register(new YourPacketHandler());

[/embed]

Then in your packet handler you have two functions

 

[embed=425,349]

@SubscribeEvent

public void onServerPacket(ServerCustomPacketEvent event) {

 

}

@SubscribeEvent

public void onClientPacket(ClientCustomPacketEvent event) {

 

}

[/embed]

 

How does one send the packet then? In my code above, I'm writing:

 

        ByteBuf data = buffer(4);

        data.writeInt(42);

        C17PacketCustomPayload packet = new C17PacketCustomPayload("NetworkExample", data);

        EntityClientPlayerMP player = (EntityClientPlayerMP)updateEvent.entityLiving;

        player.sendQueue.func_147297_a(packet);

 

Would I just change the packet class to ServerCustomPacketEvent when sending from client to server?

It looks like you send them using the FMLEventChannel event and with FMLProxyPacket. Though Im very much unsure if this is the correct way of doing it.

 

Im currently getting an error when I call newEventDrivenChannel: cpw.mods.fml.common.network.NetworkEventFiringHandler is not a @Sharable handler, so can't be added or removed multiple times.

 

Not sure how Im supposed to be calling it

 

edit: I found cpws examples, but those give errors when I try to register my handler https://github.com/MinecraftForge/FML/commit/9cab2ab36e7981c847e3e9ae8c3fbbb36531ba6d

Link to comment
Share on other sites

Not that difficult to find

[embed=425,349]

FMLEventChannel event = NetworkRegistry.INSTANCE.newEventDrivenChannel("YourChannelName");

event.register(new YourPacketHandler());

[/embed]

Then in your packet handler you have two functions

 

[embed=425,349]

@SubscribeEvent

public void onServerPacket(ServerCustomPacketEvent event) {

 

}

@SubscribeEvent

public void onClientPacket(ClientCustomPacketEvent event) {

 

}

[/embed]

 

How does one send the packet then? In my code above, I'm writing:

 

        ByteBuf data = buffer(4);

        data.writeInt(42);

        C17PacketCustomPayload packet = new C17PacketCustomPayload("NetworkExample", data);

        EntityClientPlayerMP player = (EntityClientPlayerMP)updateEvent.entityLiving;

        player.sendQueue.func_147297_a(packet);

 

Would I just change the packet class to ServerCustomPacketEvent when sending from client to server?

How would we send the packet from say a GUI using your code. I tried wrapping that code into a void in my GUI class and refrencing that when I want to send some data, having the void also require some data, but I can get the LivingEvent.LivingUpdateEvent to work.

[shadow=gray,left][glow=red,2,300]KEEGAN[/glow][/shadow]

Link to comment
Share on other sites

Thanks for writing this tutorial. That's a lot of boilerplate to understand. One conceptual question I have is about thread-safety. Is it safe to call

 

    public void sendToAll(AbstractPacket message) {

        this.channels.get(Side.SERVER).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.ALL);

        this.channels.get(Side.SERVER).writeAndFlush(message);

    }

 

from multiple threads? I assume this.channels.get(Side.SERVER).attr() is returning an object, whose value affects the behavior of writeAndFlush().

Link to comment
Share on other sites

I've got one problem: It doesn't work on server. Yea, it works fine on singleplayer but testing on server produces that error:

 

 

[18:24:06] [Netty IO #2/ERROR] [FML]: NetworkDispatcher exception

java.io.IOException: Eine vorhandene Verbindung wurde vom Remotehost geschlossen

at sun.nio.ch.SocketDispatcher.read0(Native Method) ~[?:1.7.0_51]

at sun.nio.ch.SocketDispatcher.read(Unknown Source) ~[?:1.7.0_51]

at sun.nio.ch.IOUtil.readIntoNativeBuffer(Unknown Source) ~[?:1.7.0_51]

at sun.nio.ch.IOUtil.read(Unknown Source) ~[?:1.7.0_51]

at sun.nio.ch.SocketChannelImpl.read(Unknown Source) ~[?:1.7.0_51]

at io.netty.buffer.UnpooledUnsafeDirectByteBuf.setBytes(UnpooledUnsafeDirectByteBuf.java:436) ~[unpooledUnsafeDirectByteBuf.class:?]

at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:870) ~[AbstractByteBuf.class:?]

at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:208) ~[NioSocketChannel.class:?]

at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:87) [AbstractNioByteChannel$NioByteUnsafe.class:?]

at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:480) [NioEventLoop.class:?]

at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:447) [NioEventLoop.class:?]

at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:341) [NioEventLoop.class:?]

at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:101) [singleThreadEventExecutor$2.class:?]

at java.lang.Thread.run(Unknown Source) [?:1.7.0_51]

[18:24:06] [server thread/INFO]: Player949 lost connection: TranslatableComponent{key='disconnect.genericReason', args=[internal Exception: java.io.IOException: Eine vorhandene Verbindung wurde vom Remotehost geschlossen], siblings=[], style=Style{hasParent=false, color=null, bold=null, italic=null, underlined=null, obfuscated=null, clickEvent=null, hoverEvent=null}}

[18:24:06] [server thread/INFO]: Player949 left the game

 

client closes and server reports the same message as eclipse

 

my @Mod class contains:

 

@EventHandler

public void serverLoad(FMLServerStartingEvent event) {

NetworkRegistry.INSTANCE.newChannel("lanceHitEntity", new PacketHandler());

NetworkRegistry.INSTANCE.newChannel("lanceHitValue", new PacketHandler());

NetworkRegistry.INSTANCE.newChannel("lanceIsForward", new PacketHandler());

}

 

my packet handler:

 

@Override

protected void channelRead0(ChannelHandlerContext ctx, FMLProxyPacket packet) throws Exception {

MinecraftServer server = MinecraftServer.getServer();

Item item = ((EntityPlayer) server.getEntityWorld().playerEntities.toArray(

                                                                                            [packet.payload().readInt()]).getCurrentEquippedItem().getItem();

 

if(item instanceof ItemLance) {

ItemLance lance = (ItemLance) item;

 

 

if (packet.channel().equals("lanceHitEntity")) {

lance.entity(packet.payload().readInt(), null, server.getEntityWorld());

 

 

} else if (packet.channel().equals("lanceHitValue")) {

float value = packet.payload().readFloat();

if (lance.hitValue < value || lance.hitTime < server.getSystemTimeMillis()) {

lance.hitValue = value;

lance.hitTime = server.getSystemTimeMillis() + 200;

}

 

 

} else if (packet.channel().equals("lanceIsForward")) {

lance.fwdTime = server.getSystemTimeMillis() + 200;

}

}

 

}

 

could anyone help me please?

thanks a lot!

Link to comment
Share on other sites

knight:

If you were using my code, there was a sided error in the code that is now corrected in the wiki, however, that error seems unrelated. To be honest, if I am reading your code correctly, you have only registered the channel on the server, which just wont work as the client has no channel to receive on...

 

libarclite:

If in doubt you could register multiple channels per thread (instead of auto registering during the init phase, just create another with a different name). However, both attr() and writeAndFlush() are netty methods and netty at its core is both asynchronous and thread safe so I assume it should be fine! According to the JD: http://netty.io/4.0/api/io/netty/util/Attribute.html Attributes can be updated automatically so are thread safe.

 

Link to comment
Share on other sites

Good tutorial sirgingalot !

 

A bit complex to understand but certainly very good to send custom packets in various cases.

 

I had also detected the side error where you called Minecraft (client side only) in the switch method, good to know that you correct it.

 

But i still have a little difficulty.

 

When i need to send a packet, i need to build the packet with two prameter, the Bytebuffer data that is clear to me, but also the Context that i don't know exactly how to use.

With the custompayload packet that was used at the beginning of this post i just had to give the channel name and the data.

Can you give a small practical example how to handle that context parameter.

Thanks for you time!

Link to comment
Share on other sites

kukipett: for most normal circumstances the context is not required. It is just used to pass along the packet data further down the chain. Most of the important things performed using the channel handler context are performed upstream by cpw or by me. However, it is provided in the read packet data for completeness. If you think you will need it you can use the JD: http://docs.jboss.org/netty/3.2/api/org/jboss/netty/channel/ChannelHandlerContext.html to discover what actions are available with it.

Link to comment
Share on other sites

This is a really good tutorial sirgingalot, just one thing that I'm having trouble with(this is a bit over my head, I had just figured out the old packet system) I attempted to send the packet, but I'm 95% sure that I'm doing it incorrectly, and I am probably way off, as well as probably incorrect about how to read/write variables in the packet itself.  So if I could get some help, that would be awesome.

Packet:

 

 

package com.afg.pathofahero.handlers.packets;

import com.afg.pathofahero.handlers.AfgExtendedPlayer;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;

public class PacketSync extends AbstractPacket {
static EntityPlayer player1;
private float maxSS;
public static void getPlayer(EntityPlayer player){
	player1 = player;
}
@Override
public void encodeInto(ChannelHandlerContext ctx, ByteBuf buffer) {
	AfgExtendedPlayer props = AfgExtendedPlayer.get((EntityPlayer) player1);
	buffer.setFloat(2, props.getMaxSS());
}

@Override
public void decodeInto(ChannelHandlerContext ctx, ByteBuf buffer) {
	this.maxSS = buffer.readFloat();
}

@Override
public void handleClientSide(EntityPlayer player) {
	AfgExtendedPlayer props = AfgExtendedPlayer.get((EntityPlayer) player);
	props.setMaxSS(maxSS);
	System.out.println("[PACKET] Speed from packet: " + props.getCurrentSS() + "/" + props.getMaxSS());
}

@Override
public void handleServerSide(EntityPlayer player) {
	// TODO Auto-generated method stub

}

}

 

 

When I send it:

 

 

if (FMLCommonHandler.instance().getEffectiveSide().isServer()) {
	PacketSync.getPlayer(player);
	PacketSync packet = new PacketSync();
	PacketHandler packetHandler = new PacketHandler();
	EntityPlayerMP player1 = (EntityPlayerMP) player;
	packetHandler.sendTo(packet, player1);
[/spoiler]

Since I followed your tutorial to set up everything, it should be pretty much the same, so I won't post my packethandler.

Link to comment
Share on other sites

Hello,

 

I wanted to thank sirgingalot a lot, because I've tried lot of different approachs and finally it the one he provided that allowed me to do networking in 1.7.

 

Previously I was using FMLEventChannel which is fine and very practical but it doesn't work in MP (issue with a client-only class invoked by the FML dispatcher). I also tried the SimpleNetwork but this one throws a Netty TypeMatching exception.

Link to comment
Share on other sites

AFlyingGrayson: It seems you are creating a new packet handler each time. Instead, when you create it you should store it statically somewhere and use that as a reference to send packets.

 

In essence:

1) Create packethandler in your mod constructor, or as one of this first things in preInit. Save it to a public static variable

2) Call the methods (in the correct locations) as per "Registering the Pipeline"

3) Register your packet sync with the packet handler (likely in the init phase)

4) When you need to send packets get the public static variable and call the send methods

5) Hopefully thats it!

 

garvek: I'm glad it worked for you! At least some of my code sees the light of day :D

Link to comment
Share on other sites

AFlyingGrayson: It seems you are creating a new packet handler each time. Instead, when you create it you should store it statically somewhere and use that as a reference to send packets.

Yeah, that fixed the problem that I had been having, I still don't have it right, but I'll get it eventually, thanks for your help.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.