Thread: Just to understand something with packet size and string

Results 1 to 10 of 10
  1. #1 Just to understand something with packet size and string 
    Registered Member
    BenBeri's Avatar
    Join Date
    Jan 2013
    Posts
    484
    Thanks given
    57
    Thanks received
    130
    Discord
    View profile
    Rep Power
    248
    Hello.

    A packet is formed with an opcode & data, the server counts the size of the packet and makes sure that the size is legal & vice-verse.
    But what if I want to send a string? A string does not have a fixed size?

    For example a login packet (dummy one), max username size is 18, max password size is 12, the packet size should be 31, 18+12+opcode.
    What if the entered password's size is 6 and username is 5? means the sent packet size will be 12, 12 != 31.

    How does RS determine a packet size that contains string(s)?
    Reply With Quote  
     

  2. #2  
    Номер 1


    Leanbow's Avatar
    Join Date
    Feb 2008
    Posts
    5,910
    Thanks given
    1,558
    Thanks received
    2,617
    Rep Power
    5000
    Code:
    int available = Class3_Sub15.worldConnection.available();
    	   if (available == 0) {
    		   return false;
    	   }
    	   if(jString.packetType == -1) {
    		   Class3_Sub15.worldConnection.read(RS2Client.inputStream.buf, 0, 1);
    		   RS2Client.inputStream.pos = 0;
    		   jString.packetType = RS2Client.inputStream.getUByteIsaac();
    		   RS2Client.packetSize = Constants.PACKET_SIZES[jString.packetType];
    		   available--;
    	   }
    	   if(RS2Client.packetSize == -1) {
    		   if(available <= 0) {
    			   return false;
    		   }
    		   Class3_Sub15.worldConnection.read(RS2Client.inputStream.buf, 0, 1);
    		   RS2Client.packetSize = RS2Client.inputStream.buf[0] & 0xff;
    		   available--;
    	   }
    They send packet size if the packet isn't fixed size(contains string etc).
    Reply With Quote  
     

  3. Thankful user:


  4. #3  
    Registered Member
    BenBeri's Avatar
    Join Date
    Jan 2013
    Posts
    484
    Thanks given
    57
    Thanks received
    130
    Discord
    View profile
    Rep Power
    248
    Quote Originally Posted by Leanbow View Post
    Code:
    int available = Class3_Sub15.worldConnection.available();
    	   if (available == 0) {
    		   return false;
    	   }
    	   if(jString.packetType == -1) {
    		   Class3_Sub15.worldConnection.read(RS2Client.inputStream.buf, 0, 1);
    		   RS2Client.inputStream.pos = 0;
    		   jString.packetType = RS2Client.inputStream.getUByteIsaac();
    		   RS2Client.packetSize = Constants.PACKET_SIZES[jString.packetType];
    		   available--;
    	   }
    	   if(RS2Client.packetSize == -1) {
    		   if(available <= 0) {
    			   return false;
    		   }
    		   Class3_Sub15.worldConnection.read(RS2Client.inputStream.buf, 0, 1);
    		   RS2Client.packetSize = RS2Client.inputStream.buf[0] & 0xff;
    		   available--;
    	   }
    They send packet size if the packet isn't fixed size(contains string etc).
    So the opcode is -1?
    Reply With Quote  
     

  5. #4  
    Registered Member
    BenBeri's Avatar
    Join Date
    Jan 2013
    Posts
    484
    Thanks given
    57
    Thanks received
    130
    Discord
    View profile
    Rep Power
    248
    Quote Originally Posted by Belz View Post
    The opcode wouldn't change. If the size of the packet is -1, that means an extra byte is sent which contains the length of the packet.
    But how can the packet size be -1? Wait, packet size is always being sent in the packet?
    Reply With Quote  
     

  6. #5  
    Номер 1


    Leanbow's Avatar
    Join Date
    Feb 2008
    Posts
    5,910
    Thanks given
    1,558
    Thanks received
    2,617
    Rep Power
    5000
    Quote Originally Posted by JonyFX View Post
    But how can the packet size be -1? Wait, packet size is always being sent in the packet?
    Client has an array with all packet sizes, if packet size is set to -1 then it asks you to define it.
    Reply With Quote  
     

  7. #6  
    Registered Member
    BenBeri's Avatar
    Join Date
    Jan 2013
    Posts
    484
    Thanks given
    57
    Thanks received
    130
    Discord
    View profile
    Rep Power
    248
    Quote Originally Posted by Leanbow View Post
    Client has an array with all packet sizes, if packet size is set to -1 then it asks you to define it.
    Ah okay, i see.. But in general, what is the point of matching packet sizes ? For what purpose is this done?
    Reply With Quote  
     

  8. #7  
    Registered Member
    Whis's Avatar
    Join Date
    Dec 2015
    Age
    26
    Posts
    164
    Thanks given
    56
    Thanks received
    82
    Discord
    View profile
    Rep Power
    340
    I think the answer I've given here should answer yours as well: [Only registered and activated users can see links. ]

    The same concept is applied to game packets where strings are included. The size of the payload is set after the opcode in the packet header. This of course varies per packet.

    Quote Originally Posted by JonyFX View Post
    But how can the packet size be -1? Wait, packet size is always being sent in the packet?
    As Leanbow has mentioned in his posts; when the client receives the packet, it decodes the header (opcode) and looks up the size of the payload of the packet in an array using the opcode. The client has a similar array to this where each element represents the size of the packet:

    Code:
    public static final PacketMetaDataGroup METADATA_GROUP = PacketMetaDataGroup.createFromArray(new int[] {
    			-3, -3, 2, -3, -3, -3, -3, -3, 8, -3, // 0
    			-3, -3, -3, 2, -3, -3, -3, -3, -3, -3, // 10
    			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 20
    			1, -3, 2, -3, -3, -3, -3, -3, -3, 6, // 30
    			-3, -3, 8, -3, -3, -3, -3, 8, -3, -3, // 40
    			-3, -3, -3, 8, -3, -3, -3, -3, -3, -3, // 50
    			-3, 4, -3, -3, -3, -3, -1, -3, -3, -3, // 60
    			-1, -3, -3, -3, 2, -3, -3, -3, -3, 0, // 70
    			-3, -3, -3, -3, -3, 2, -3, 2, -3, -3, // 80
    			4, 0, -3, -3, -3, -1, -3, -3, -3, -3, // 90
    			-3, 8, -3, -3, 14, -3, 8, -1, -3, -3, // 100
    			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 110
    			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 120
    			-3, -3, -3, -3, 8, -3, -3, -3, -3, -3, // 130
    			8, -3, -3, -3, -3, -3, 0, -3, -3, -3, // 140
    			-3, -3, -3, -3, 6, -3, -3, -3, -3, 2, // 150
    			-3, 16, -3, -3, -3, -3, -3, 2, 12, -3, // 160
    			6, -3, -3, -3, -3, -3, 2, -3, -3, -3, // 170
    			-3, -3, -3, 4, -3, 8, -3, -3, -3, -3, // 180
    			-3, 8, -3, 10, -3, -3, -3, -3, -1, -3, // 190
    			-3, -3, 8, -3, -3, -3, -3, -3, -3, -3, // 200
    			-3, -3, -3, 4, -3, -3, -3, -3, -3, 0, // 210
    			-3, 6, -3, 2, -3, -3, -3, -3, -3, 9, // 220
    			-3, 2, 14, -3, -3, -3, 8, -3, -3, -3, // 230
    			-3, 8, -3, -3, -3, -3, -3, -3, -3, -1, // 240
    			-3, -3, -3, -3, -3, -3,				    // 250
    	});
    In the code above, you can see a lot of -3. This would indicate that the packet's size has not been identified yet (The code is from the server). "-2" indicates that the size of the packet is variable and should be read as a short while "-1" indicates that the packet size must be read as a byte. Any positive value means that the size of the packet is fixed to that specific value.

    Quote Originally Posted by JonyFX View Post
    Ah okay, i see.. But in general, what is the point of matching packet sizes ? For what purpose is this done?
    I'm no networking expert but I'm going to assume it is to ensure clients do not try to send more or less than what is necessary. Think of bandwidth.
    "It's all a matter of perspective. There is no single path in life that is right and fair and does no harm."
    Reply With Quote  
     

  9. #8  
    Registered Member
    BenBeri's Avatar
    Join Date
    Jan 2013
    Posts
    484
    Thanks given
    57
    Thanks received
    130
    Discord
    View profile
    Rep Power
    248
    Quote Originally Posted by Whis View Post
    I think the answer I've given here should answer yours as well: [Only registered and activated users can see links. ]

    The same concept is applied to game packets where strings are included. The size of the payload is set after the opcode in the packet header. This of course varies per packet.



    As Leanbow has mentioned in his posts; when the client receives the packet, it decodes the header (opcode) and looks up the size of the payload of the packet in an array using the opcode. The client has a similar array to this where each element represents the size of the packet:

    Code:
    public static final PacketMetaDataGroup METADATA_GROUP = PacketMetaDataGroup.createFromArray(new int[] {
    			-3, -3, 2, -3, -3, -3, -3, -3, 8, -3, // 0
    			-3, -3, -3, 2, -3, -3, -3, -3, -3, -3, // 10
    			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 20
    			1, -3, 2, -3, -3, -3, -3, -3, -3, 6, // 30
    			-3, -3, 8, -3, -3, -3, -3, 8, -3, -3, // 40
    			-3, -3, -3, 8, -3, -3, -3, -3, -3, -3, // 50
    			-3, 4, -3, -3, -3, -3, -1, -3, -3, -3, // 60
    			-1, -3, -3, -3, 2, -3, -3, -3, -3, 0, // 70
    			-3, -3, -3, -3, -3, 2, -3, 2, -3, -3, // 80
    			4, 0, -3, -3, -3, -1, -3, -3, -3, -3, // 90
    			-3, 8, -3, -3, 14, -3, 8, -1, -3, -3, // 100
    			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 110
    			-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, // 120
    			-3, -3, -3, -3, 8, -3, -3, -3, -3, -3, // 130
    			8, -3, -3, -3, -3, -3, 0, -3, -3, -3, // 140
    			-3, -3, -3, -3, 6, -3, -3, -3, -3, 2, // 150
    			-3, 16, -3, -3, -3, -3, -3, 2, 12, -3, // 160
    			6, -3, -3, -3, -3, -3, 2, -3, -3, -3, // 170
    			-3, -3, -3, 4, -3, 8, -3, -3, -3, -3, // 180
    			-3, 8, -3, 10, -3, -3, -3, -3, -1, -3, // 190
    			-3, -3, 8, -3, -3, -3, -3, -3, -3, -3, // 200
    			-3, -3, -3, 4, -3, -3, -3, -3, -3, 0, // 210
    			-3, 6, -3, 2, -3, -3, -3, -3, -3, 9, // 220
    			-3, 2, 14, -3, -3, -3, 8, -3, -3, -3, // 230
    			-3, 8, -3, -3, -3, -3, -3, -3, -3, -1, // 240
    			-3, -3, -3, -3, -3, -3,				    // 250
    	});
    In the code above, you can see a lot of -3. This would indicate that the packet's size has not been identified yet (The code is from the server). "-2" indicates that the size of the packet is variable and should be read as a short while "-1" indicates that the packet size must be read as a byte. Any positive value means that the size of the packet is fixed to that specific value.
    After you read the size from the variable, what do you do? You just start using the packet like you do after size validation, right?
    Reply With Quote  
     

  10. #9  
    Registered Member
    Whis's Avatar
    Join Date
    Dec 2015
    Age
    26
    Posts
    164
    Thanks given
    56
    Thanks received
    82
    Discord
    View profile
    Rep Power
    340
    Quote Originally Posted by JonyFX View Post
    After you read the size from the variable, what do you do?
    You allocate a buffer that is limited to that specific size which you can then pass on to the decoder that takes care of the packet.

    Code:
    private void decodePayload(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    		if (buffer.readableBytes() >= length) {
    			ByteBuf payload = buffer.readBytes(length);
    			checkpoint(GameDecoderState.GAME_OPCODE);
    			
    			out.add(new GamePacket(opcode, type, payload));
    		}
    	}
    "It's all a matter of perspective. There is no single path in life that is right and fair and does no harm."
    Reply With Quote  
     

  11. #10  
    Registered Member
    Ynneh's Avatar
    Join Date
    Mar 2010
    Posts
    2,204
    Thanks given
    146
    Thanks received
    254
    Discord
    View profile
    Rep Power
    241
    Quote Originally Posted by JonyFX View Post
    So the opcode is -1?
    varbyte, varshort is -2.
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 1
    Last Post: 07-13-2014, 06:48 PM
  2. Replies: 4
    Last Post: 07-01-2013, 01:10 AM
  3. Looking to host something with my vps
    By Brezzo in forum Hosting
    Replies: 6
    Last Post: 12-05-2012, 12:26 AM
  4. Replies: 18
    Last Post: 08-22-2009, 07:01 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •