Jump to content

[SOLVED] How to fix Netty Packet Handling with SimpleChannelInboundHandler?


Recommended Posts

Posted

Hi,

the current tutorial for the Netty Packet Handling on the Minecraft Forge Wiki is outdated and causes memory leak, as stated by cpw.

http://www.minecraftforge.net/wiki/Netty_Packet_Handling

 

I already used this tutorial and i would like to fix this.

 

Is there any new tutorial on how to do it right? And do you know a good tutorial generally explaining the 1.7 Netty Packet System?

If not, can someone please give (at least) an example code on how to do it, or do a tutorial on it?

It also would be awesome if you could include how to send the packets.

I heared something about extending SimpleChannelInboundHandler?

 

EDIT:

This is where I got a working example from. So you don´t have to read all posts in case you are just searching for the solution itself:

This link shows working example of the SimpleNetworkWrapper system:  https://github.com/pahimar/Equivalent-Exchange-3/tree/master/src/main/java/com/pahimar/ee3/network

Posted

I've been interested as well in trying the SimpleNetworkWrapper method.  If you find that class there is a lot of comments that explain how to use it.  I think that was meant to be primary way for us lesser modders to quickly get packets working, but apparently it was buggy until very recently so people made all these other tutorials.

 

I'll probably be trying to get this working over next couple weeks, but it is probably worth an attempt if you're up for it.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

I'm using it as well and haven't noticed any issues.  My server reboots only once a day and my kids play for way to long during the summer.  Unless it is an extremely small memory leak, I think it would have caused issues by now.

Long time Bukkit & Forge Programmer

Happy to try and help

Posted

I've also been using the network code from that Netty tutorial and haven't noticed any problems. Looking at cpw's SimpleNetworkHandler, it seems easy enough to use, but honestly, I prefer the tutorial code, since I can create a packet that I can send to either side, whereas cpw's you have to provide the Side when you register the packet, as well as manually enter the discriminator bytes. I also prefer how each packet is its own handler, vs. cpw's system of registering a handler with a packet. There doesn't seem to be an easy way to get the player after receiving a packet on the server, either, but I probably just missed that.

 

Anyway, my overall impression of cpw's code is that it is more complicated and cumbersome than it needs to be, at least in comparison to what I have been using. Surely whatever memory leak there is in the tutorial code wouldn't be hard to fix?

 

EDIT: After seeing EE3's setup, I guess cpw's system is more straightforward than I had gathered from the documentation. Though Pahimar didn't use one, it's easy enough to use a self-iterating index during registration, but still, it would have been nice to have the discriminator self-indexing built in, like with the outdated tutorial code. I also still dislike how the packet needs to be registered with a Side; sure most packets are only sent one way, but sometimes I need one that is sent in either direction.

Posted

I've been fretting over this topic for a while too. 

 

This link shows working example of the SimpleNetworkWrapper system:  https://github.com/pahimar/Equivalent-Exchange-3/tree/master/src/main/java/com/pahimar/ee3/network

 

It is meant to be "simple" so perhaps loses some power and flexibility that some modders would like.  But if cpw and pahimar both recommend it I've got to believe it is worth checking out.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

The example code from EE3 seems pretty simple. Thanks for the link jabelar.

I will try to use this as soon as I have the time.

I haven´t looked into netty yet, so I needed an easy tutorial.

About the memory leak:

There is just something I experienced in Minecraft 1.7.2 (not sure if this is not a problem in the Forge version).

I have a modpack with about 20 mods. After some time the game uses much more memory and freezes when i switch to another window.

When I read about the memory leak in the tutorial Packet Handling, which I guess tons of modders are using, I was wondering if this could be the problem. Any ideas on that? 

Posted

In terms of Netty (or packet handling generally) the key thing is to understand the concepts they all have in common, namely:

 

The Steps Needed For A Packet Handling System

 

Basically, you’ll need a packet system that:

 

a. Registers a networking channel specific for your mod (this allows you to ignore packets that may come from other mods).  You could theoretically use an existing channel but then there would be chance that your payload format conflicts with other mods or vanilla packets.

 

b. Has packet handler classes on each side that is called on packet events.  Depending on the packet system, you may need to subscribe to an event or simply use an onMessage() method or similar.

 

c. Has a class on each side to create packets.  The creation mostly involves creating a payload which is usually a ByteBuf.  Most systems make the first int as an identifier of the packet type, often called the "discriminator".  You can make up your own types.  The rest of the creation of the payload is often called "encoding" but really just means you put the values you care about into the buffer in an order of your choosing. 

 

d. Has a class on each side to process received packets, distinguish packet type and what side it is on and process the packet accordingly. You could actually put this code in the handler, but as your mod grows the number of packet types and complexity of their payloads will grow so most systems put these in separate message classes.  Again most of the work is related to the payload, with the first int usually being a discriminator that represents the packet type, then remaining payload which needs to be "decoded" into retrieved values in the order you expect them for that packet type.

 

d. Sends the packets as needed (ideally just when there are changes that require syncing of a value between client and server or vice versa, which is often in a setter method).

 

I have a modpack with about 20 mods. After some time the game uses much more memory and freezes when i switch to another window.
When I read about the memory leak in the tutorial Packet Handling, which I guess tons of modders are using, I was wondering if this could be the problem. Any ideas on that? 

 

A memory leak would do something like that -- increasing memory use until system has trouble. 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Posted

Thanks for the nice explanation jabelar. Haven´t found anything that informative (and easy to understand) yet.

 

There is just one thing I need to know. I have a packet (message) on Side.Client and would need to get an instance of EntityPlayer in onMessage method.

I used the version from pahimar´s EE3 and was able to use ctx.getServerHandler().playerEntity for my Side.SERVER messages. But this unfortinatly does not work for clients and ctx.getClientHandler() does not have a method to get the EntityPlayer.

 

@Override
public IMessage onMessage(SyncPlayerPropsPacket message, MessageContext ctx) {
	EntityPlayer player = FMLClientHandler.instance().getClientPlayerEntity();

                // The class below implements IExtendeEntityProperties
	MinetweaksExtendedPlayer.get(player).loadNBTData(data);
	return null;
}

Posted

Thanks. I was using this before, but I derped something else.

I forgot to do message.data and used this.data instead -> So it was always null.

In other words I was reading the field of my class instead of reading the send data.

 

Thank you for all the help guys. I marked this topic as solved.

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.