Welcome, guest! Please login or register.

    * Shoutbox

    RefreshHistory
    • Salayor: o.O
      December 13, 2018, 01:02:33 AM
    • ZHer0kool: This is by far one of my oldest existing forum accounts
      December 12, 2018, 08:40:43 PM
    • DeathsChaos9::|
      December 12, 2018, 06:41:54 PM
    • TheMane221: Dont know if this is allowed but I have an OSRS account I need to sell asap. 95 att, 92 str, 93 def, 94 hp, 77 range and 73 magic. Dm me please.
      December 09, 2018, 04:27:28 PM
    • runescape3dude: I haven't been on here in about 10 years
      December 08, 2018, 11:14:56 PM
    • runescape3dude: holy crap
      December 08, 2018, 11:14:47 PM
    • howi: Gosh i never been on this site since... ever
      December 08, 2018, 01:36:16 AM
    • TinyScape: ur gay
      December 07, 2018, 09:03:26 PM
    • Travas: drub is gay
      December 02, 2018, 11:48:32 PM
    • drubrkletern: asdf
      November 27, 2018, 02:44:00 PM
    • Black Paw: TokHaar aims to give you the best OSRS features in Real High Definition with a mixture of 550 content! (Quality Driven RSPS) -- Come join us for → PvP, PvMing, Skilling, Prosperous Economy, Minigames/Bosses & Much more.. [link]
      November 21, 2018, 06:55:31 PM
    • i5hy: AllstarLegends - Oldschool Nostalgia @ Allstarlegends.eu Long Lasting server been around since 2013-2014
      November 21, 2018, 01:49:59 PM
    • lordvirius: Is there a mobile private server?
      November 21, 2018, 09:34:33 AM
    • Coldmedicine: FREE ADVERTISEMENT ON DISCORD
      November 20, 2018, 03:30:50 AM
    • Coldmedicine:[link]
      November 20, 2018, 03:30:42 AM
    • Coldmedicine: ...
      November 20, 2018, 03:24:02 AM
    • dylanwill: message me bros
      November 19, 2018, 04:49:29 PM
    • dylanwill: I got banned from runeserver
      November 19, 2018, 04:49:08 PM
    • Wayne.RSPS: TokHaar aims to give you the best OSRS features in Real High Definition with a mixture of 550 content! (Quality Driven RSPS) -- Come join us for → PvP, PvMing, Skilling, Prosperous Economy, Minigames/Bosses & Much more.. [link]
      November 16, 2018, 11:24:39 AM
    • Fridder: a q  p
      November 14, 2018, 02:16:21 PM

    Author Topic: Packets not arriving/decoding as they should  (Read 315 times)

    0 Members and 1 Guest are viewing this topic.

    OfflineCres

    • Member
    • ****
    • Posts: 1,392
    • Thanks: +0/-0
      • View Profile
    Packets not arriving/decoding as they should
    « on: March 09, 2018, 09:33:33 AM »
    Hi

    I'm writing my own 317 from scratch, based on RuneSource.

    I'm using a 459 lookalike client (it's actually a 317)

    This client connects and functions correctly when connecting to the original RuneSource (as a reference).

    My server finishes up the login protocol but somehow screws up when it has to decode incoming packets. When doing nothing after logging in, I'm expecting the 0 packet (or idle packet). Instead, I just get random packets.

    I think I'm missing something in regards to ByteBuffers but I am clueless.

    Here's the relevant code:


    PacketDecoder -> This is where the packets are actually being decoded
    Code: Java
    1. packagecom.rs2.net.codecs;
    2.  
    3. importcom.rs2.Server;
    4. importcom.rs2.net.Client;
    5. importcom.rs2.net.packets.PacketHandler;
    6.  
    7. importjava.nio.ByteBuffer;
    8.  
    9. publicclass PacketDecoder implements Decoder {
    10.  
    11.         private Client client;
    12.         privateint opcode =-1;
    13.         privateint length =-1;
    14.  
    15.         public PacketDecoder(Client client){
    16.                 this.client= client;
    17.         }
    18.  
    19.         @Override
    20.         publicvoid decode(){
    21.                 reset();
    22.                 ByteBuffer buffer = client.getInBuffer();
    23.                 while(buffer.hasRemaining()){
    24.                         //decode the opcode
    25.                         if(opcode ==-1){
    26.                                 opcode = buffer.get()& 0xff;
    27.                                 opcode =(opcode - client.getDecryptor().getNextValue())& 0xff;
    28.                         }
    29.                         //decode the length
    30.                         if(length ==-1){
    31.                                 length = Server.getPackets().getExpectedSize(opcode);
    32.                                 if(length ==-1){
    33.                                         if(!buffer.hasRemaining()){
    34.                                                 break;
    35.                                         }
    36.                                         length = buffer.get()& 0xff;
    37.                                 }
    38.                         }
    39.                         //process the packet
    40.                         if(buffer.remaining()>= length){
    41.                                 PacketHandler handler = Server.getPackets().getPacketHandler(opcode);
    42.                                 int positionBefore = buffer.position();
    43.                                 if(handler !=null){
    44.                                         handler.execute(client);
    45.                                 }
    46.                                 System.out.println("Received packet: "+opcode+" with length: "+length);
    47.                                 //ensure all bytes for this packet have been read
    48.                                 int read = buffer.position()- positionBefore;
    49.                                 for(int i = read; i < length; i++){
    50.                                         buffer.get();
    51.                                 }
    52.                                 reset();
    53.                         }else{
    54.                                 break;
    55.                         }
    56.                 }
    57.         }
    58.  
    59.         privatevoid reset(){
    60.                 opcode =-1;
    61.                 length =-1;
    62.         }
    63.  
    64. }

    LoginDecoder -> Deals with the login protocol. Functions as it should, but just incase anything is wrong here I'm posting it
    Code: Java
    1. packagecom.rs2.net.codecs;
    2.  
    3. importcom.rs2.Server;
    4. importcom.rs2.models.players.Player;
    5. importcom.rs2.net.Client;
    6. importcom.rs2.net.ISAACCipher;
    7. importcom.rs2.net.ServerResponse;
    8. importcom.rs2.net.StreamBuffer;
    9. importcom.rs2.util.Utility;
    10.  
    11.  
    12. importjava.nio.ByteBuffer;
    13.  
    14. publicclass LoginDecoder implements Decoder {
    15.  
    16.         private Client client;
    17.         private LoginStage loginStage;
    18.         privatelong serverSessionKey;
    19.  
    20.         privateenum LoginStage {
    21.                 STAGE_1,
    22.                 STAGE_2
    23.         }
    24.  
    25.         public LoginDecoder(Client client){
    26.                 this.client= client;
    27.                 this.loginStage= LoginStage.STAGE_1;
    28.         }
    29.  
    30.         @Override
    31.         publicvoid decode(){
    32.                 ByteBuffer buffer = client.getInBuffer();
    33.                 switch(loginStage){
    34.                         case STAGE_1:
    35.                                 performStage1(buffer);
    36.                                 break;
    37.                         case STAGE_2:
    38.                                 performStage2(buffer);
    39.                                 break;
    40.                 }
    41.         }
    42.  
    43.         privatevoid performStage1(ByteBuffer buffer){
    44.                 StreamBuffer.OutBuffer out = StreamBuffer.newOutBuffer(17);
    45.                 out.writeLong(0l);
    46.                 out.writeByte(ServerResponse.LOGIN_ACCEPTED);
    47.                 serverSessionKey = Utility.getSecureLong();
    48.                 out.writeLong(serverSessionKey);
    49.                 client.send(out.getBuffer());
    50.                 this.loginStage= LoginStage.STAGE_2;
    51.         }
    52.  
    53.         @SuppressWarnings("unused")
    54.         privatevoid performStage2(ByteBuffer buffer){
    55.                 int connectType = buffer.get();
    56.                 if(connectType !=16&& connectType !=18){
    57.                         sendError(ServerResponse.ERROR_REJECTED_SESSION);
    58.                         return;
    59.                 }
    60.                 boolean reconnect = connectType ==18;
    61.                 int length = buffer.get()& 0xff;
    62.                 if(buffer.remaining()< length){
    63.                         sendError(ServerResponse.ERROR_INCOMPLETE_LOGIN);
    64.                         return;
    65.                 }
    66.                 StreamBuffer.InBuffer in = StreamBuffer.newInBuffer(buffer);
    67.                 if((in.readByte()& 0xff)!=255){
    68.                         sendError(ServerResponse.ERROR_INCOMPLETE_LOGIN);
    69.                         return;
    70.                 }
    71.                 int version = in.readShort();
    72.                 int memVersion = in.readByte();
    73.                 for(int i =0; i <9; i++){
    74.                         in.readInt();
    75.                 }
    76.                 in.readByte();//rsa
    77.                 if((in.readByte()& 0xff)!=10){
    78.                         sendError(ServerResponse.ERROR_INCOMPLETE_LOGIN);
    79.                         return;
    80.                 }
    81.                 long csk = in.readLong();
    82.                 long ssk = in.readLong();
    83.                 if(ssk != serverSessionKey){
    84.                         sendError(ServerResponse.ERROR_INCOMPLETE_LOGIN);
    85.                         return;
    86.                 }
    87.                 int[] seed =newint[4];
    88.                 seed[0]=(int)csk>>32;
    89.                 seed[1]=(int)csk;
    90.                 seed[2]=(int)ssk>>32;
    91.                 seed[3]=(int)ssk;
    92.                 client.setDecryptor(new ISAACCipher(seed));
    93.                 seed[0]+=50;
    94.                 seed[1]+=50;
    95.                 seed[2]+=50;
    96.                 seed[3]+=50;
    97.                 client.setEncryptor(new ISAACCipher(seed));
    98.                 int uid = in.readInt();
    99.                 String name = Utility.normalizeUsername(in.readString());
    100.                 String pass = in.readString();
    101.                 /*
    102.                 * TODO
    103.                 * -> Player loading
    104.                 * -> Validate login credentials
    105.                 * -> Login attempts
    106.                 * -> Throttlefilter (probably deal with this at the Gateway)
    107.                 * */
    108.                 int slot = Server.getClients().register(client);
    109.                 if(slot ==-1){
    110.                         sendError(ServerResponse.ERROR_SERVER_IS_FULL);
    111.                         return;
    112.                 }
    113.                 client.setID(slot);
    114.  
    115.                 //dummy
    116.                 client.setPlayer(new Player());
    117.                 client.getPlayer().setName(name);
    118.                 client.getPlayer().setPassword(pass);
    119.  
    120.                 ByteBuffer out = ByteBuffer.allocate(3);
    121.                 out.put((byte) ServerResponse.LOGIN_COMPLETE);
    122.                 out.put((byte) client.getPlayer().getRank());
    123.                 out.put((byte)0);
    124.                 client.setDecoder(new PacketDecoder(client));
    125.                 client.send(out);
    126.                 client.getInBuffer().compact();
    127.         }
    128.  
    129.         privatevoid sendError(int message){
    130.                 ByteBuffer packet = ByteBuffer.allocate(1);
    131.                 packet.put((byte) message);
    132.                 client.send(packet);
    133.         }
    134.  
    135. }

    Gateway -> Deals with the networking and reading bytes from the connections. This is where the above code is being called from.
    Code: Java
    1. packagecom.rs2.net;
    2.  
    3. importjava.io.IOException;
    4. importjava.nio.channels.SocketChannel;
    5. importjava.net.InetSocketAddress;
    6. importjava.nio.channels.Selector;
    7. importjava.nio.channels.SelectionKey;
    8. importjava.nio.channels.ServerSocketChannel;
    9.  
    10. importcom.rs2.Server;
    11. importcom.rs2.engine.Task;
    12.  
    13. publicclass Gateway {
    14.  
    15.         private ServerSocketChannel serverSocketChannel;
    16.         private Selector selector;
    17.         privateint port;
    18.  
    19.         public Gateway(int port){
    20.                 this.port= port;
    21.                 try{
    22.                         serverSocketChannel = ServerSocketChannel.open();
    23.                         serverSocketChannel.socket().bind(new InetSocketAddress("127.0.0.1", port));
    24.                         serverSocketChannel.configureBlocking(false);
    25.                         selector = Selector.open();
    26.                         serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
    27.                 }catch(Exception e){
    28.                         e.printStackTrace();
    29.                 }
    30.         }
    31.  
    32.         publicvoid processIncomingPackets(){
    33.                 try{
    34.                         selector.selectNow();
    35.                         for(SelectionKey key : selector.selectedKeys()){
    36.                                 if(key.isAcceptable()){
    37.                                         acceptConnection();
    38.                                 }
    39.                                 if(key.isReadable()){
    40.                                         readFromConnection(key);
    41.                                 }
    42.                         }
    43.                 }catch(Exception e){
    44.                         e.printStackTrace();
    45.                 }
    46.                 Server.getEngine().processQueuedTasks();
    47.         }
    48.  
    49.         privatevoid acceptConnection(){
    50.                 Server.getEngine().queueTask(new Task(){
    51.                         publicvoid execute(){
    52.                                 try{
    53.                                         SocketChannel channel = serverSocketChannel.accept();
    54.                                         if(channel ==null){
    55.                                                 return;
    56.                                         }
    57.                                         channel.configureBlocking(false);
    58.                                         Client client =new Client(channel);
    59.                                         channel.register(selector, SelectionKey.OP_READ, client);
    60.                                 }catch(Exception e){
    61.                                         e.printStackTrace();
    62.                                 }
    63.                         }
    64.                 });
    65.         }
    66.  
    67.         privatevoid readFromConnection(SelectionKey key){
    68.                 Server.getEngine().queueTask(new Task(){
    69.                         publicvoid execute(){
    70.                                 try{
    71.                                         SocketChannel channel =(SocketChannel) key.channel();
    72.                                         Client client =(Client) key.attachment();
    73.                                         client.getInBuffer().clear();
    74.                                         channel.read(client.getInBuffer());
    75.                                         client.getInBuffer().flip();
    76.                                         client.getDecoder().decode();
    77.                                 }catch(Exception e){
    78.                                         e.printStackTrace();
    79.                                 }
    80.                         }
    81.                 });
    82.         }
    83.  
    84. }
    And I can find myself alone with just my thoughts
    As time crumbles away

    Avenged Sevenfold - Darkness Sourrounding
    Runescape Gambling

     

    Copyright © 2017 MoparScape. All rights reserved.
    Powered by SMFPacks SEO Pro Mod |
    SimplePortal 2.3.5 © 2008-2012, SimplePortal