Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[New G2O][Shared] packetListener
#4
(20.06.2018, 17:26)Patrix Wrote: Well, your code is pretty simple and readable, but this:
Squirrel Script
  1. function receivedPacket(pid, packet)
  2. {
  3. if(packet.readUInt8() == packetList.id)
  4. {
  5. switch(packet.readUInt8())
  6. {
  7. case packetList.option_lineChat: changeLine(pid, packet.readInt8()); break;
  8. case packetList.option_hud: changeHud(pid, packet.readInt8()); break;
  9. case packetList.option_password: changePassword(pid, packet.readString(), packet.readString()); break;
  10. case packetList.option_notifications: changeNotifications(pid, packet.readBool()); break;
  11. }
  12. }
  13. }



Can cause problems because in this method, you are reading first packet value, I belive that you connect this method to onPacket event.
So, what if you have more similar functions/methods, and trying to read packet id in this way:

Squirrel Script
  1. if(packet.readUInt8() == packetList.id)



Even if this condition won't pass, you still read first value from packet, which was id.

Take a look at this code:
https://www97.zippyshare.com/v/LxAq6iiv/file.html

To simply avoid this problem, you must have ONLY ONE function connected to onPacket event on each side, then ONLY ONCE read packet Id, and check which function should be called based on it's value.

Even if you try to call two diffrent functions and pass them a packet object, it won't work, because this function will be working with an object reference, not it's copy.

In my opinion, this simple script makes it a little bit easier to add some new function(s) to call when packet with specific id arrive to it's location, but it's only my opinion, maybe i'm wrong Big Grin.
If you write a structured script, it can be useful, but when it comes to oop, g2o packages are enough in this state and you really have to try to make a mistake(I think so). In any case, it can be useful for someone Smile

Squirrel Script
  1. enum packetList
  2. {
  3.    option_lineChat,
  4.    option_hud,
  5.    option_password,
  6.    option_notifications,
  7.  
  8.    id = 255
  9. };


This is a piece of code from my project. Enum is global (client / server) I have cut out a fragment for an example.
Reply


Messages In This Thread
[New G2O][Shared] packetListener - by Patrix - 04.06.2018, 20:22
RE: [New G2O][Shared] packetListener - by Tommy - 20.06.2018, 18:43

Possibly Related Threads…
Thread Author Replies Views Last Post
  [New G2O][Client-Side] bindKey Patrix 6 8,972 14.11.2018, 02:32
Last Post: Patrix
  [New G2O][Shared] Command Handler Patrix 3 6,177 05.09.2018, 15:42
Last Post: Patrix
  [G2O v.0.1.2][Server-Side] NPC Manager Quarchodron 16 17,493 21.01.2018, 19:59
Last Post: Quarchodron
  [New G2O][Client-Side] Key Combination Patrix 1 3,785 23.12.2017, 13:16
Last Post: Patrix
  [G2O v.0.1.0][Client-Side] Notice Board Quarchodron 0 3,140 27.06.2017, 14:28
Last Post: Quarchodron

Forum Jump:


Users browsing this thread: 1 Guest(s)