Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[New G2O][Shared] packetListener
#1
*Introduction:

NOTE! This script allows you to bind ONE function to listen packet with specific id.
NOTE! You have to specify packet id(s) by yourself in messageid.nut file
NOTE! Default packet Id type is Uint16, but you can change this easily by editing packetListener.nut script
NOTE! If you decide to test this script, make sure that you don't have any function(s) bind to onPacket event

Info: This script is stable and propably doesn't have any bugs.

Hi, as you know G2O allows you to communicate with client or server using packets, but if you want to split functions which are reading packets,
you still must have ONLY one onPacket event, in which you are reading packet id. If you have more complex scripts, it's not good to read a code
from file to file, so i've made very simple handler, which allows you to bind ONE function to specific packet id. Feel free to use it.

License

Download



*List of functions:



void addPacketListener(mixed packetId, func callback)
void removePacketListener(mixed packetId)



*Function (addPacketListener, removePacketListener) parameters description:

Info: packetId is "mixed" because it's up to you what packet id type will be (default is Uint16).



// Required parameters for addPacketListener

mixed packetId // The packetId, (it's recommended to specify packet Id's in file messageid.nut)
void callback // The function which will be called, when packet arrived to specific site.

// Required parameters for removePacketListener

mixed packetId // The packetId



*Installation:



1.Download zip archive
2.Unpack archive and place network folder in your G2O server directory wherever you want.
3.Add this line to parsed xml file, (for example: config.xml)

PHP
  1. <import src="network/network.xml" />



4.After that, you've loaded both scripts: (packetListener.nut and messageid.nut) to client-side and server-side.



*Examples:



*Sending client-side statistics to server, which can be later used for building an anti-cheat:



*Client-side:

Squirrel Script
  1. // creating infinite timer which will be called with one second delay
  2. setTimer(function()
  3. {
  4. local packet = Packet() // creating packet
  5.  
  6. packet.writeUint16(PACKET.ID) // writing some id from messageid.nut, you have to define id yourself, it's only a dummy example
  7. packet.writeInt32(getPlayerStrength(heroId)) // writing player strength
  8. packet.writeInt32(getPlayerDexterity(heroId)) // writing player dexterity
  9.  
  10. packet.send(RELIABLE_ORDERED) // sending packet to server
  11.  
  12. }, 1000, -1)



*Server-side:

Squirrel Script
  1. addPacketListener(PACKET.ID, function(pid, packet) // binding function to packet with id PACKET.ID
  2. {
  3. local strength = packet.readInt32() // reading player strength
  4. local dexterity = packet.readInt32() // reading player dexterity
  5.  
  6. if (strength != getPlayerStrength(pid)) // comparing client-side strength with server-side strength
  7. {
  8. ban(pid, -1, "Cheating strength") // givining permanent ban for cheating
  9. }
  10. else if (dexterity != getPlayerDexterity(pid)) // comparing client-side dexterity with server-side dexterity
  11. {
  12. ban(pid, -1, "Cheating dexterity") // givining permanent ban for cheating
  13. }
  14. })





*Sending server-side custom player class to client



*Server-side:

Squirrel Script
  1. local heroClass =
  2. {
  3. health = 1000,
  4. mana = 100,
  5.  
  6. strength = 200,
  7. dexterity = 200
  8. }
  9.  
  10. addEventHandler("onCommand", function(pid, cmd, params)
  11. {
  12. if (cmd == "giveclass") // if player type /giveclass command, then..
  13. {
  14. local packet = Packet() // creating packet
  15.  
  16. packet.writeUint16(PACKET.CLASS) // writing some id from messageid.nut, you have to define id yourself, it's only a dummy example
  17. packet.writeInt32(heroClass.health) // writing class health
  18. packet.writeInt32(heroClass.mana) // writing class mana
  19. packet.writeInt32(heroClass.strength) // writing class strength
  20. packet.writeInt32(heroClass.dexterity) // writing class dexterity
  21.  
  22. packet.send(pid, RELIABLE_ORDERED) // sending packet to client
  23. }
  24. })



*Client-side:

Squirrel Script
  1. addPacketListener(PACKET.ID, function(packet) // binding function to packet with id PACKET.ID
  2. {
  3. local health = packet.readInt32() // reading class health
  4. local mana = packet.readInt32() // reading class mana
  5. local strength = packet.readInt32() // reading class strength
  6. local dexterity = packet.readInt32() // reading class dexterity
  7.  
  8. setPlayerHealth(heroId, health)
  9. setPlayerMaxHealth(heroId, health)
  10.  
  11. setPlayerMana(heroId, mana)
  12. setPlayerMaxMana(heroId, mana)
  13.  
  14. setPlayerStrength(heroId, strength)
  15. setPlayerDexterity(heroId, dexterity)
  16. })





*Examples comment



Example nr 2:

This is only a dummy example, to illustrate how addPacketListener function works.
If you really want to create a script which give some class to player, i recommend you to
giving statistics on server-side, because this example is not safe.
Reply


Messages In This Thread
[New G2O][Shared] packetListener - by Patrix - 04.06.2018, 20:22

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

Forum Jump:


Users browsing this thread: 1 Guest(s)