Welcome, guest! Please login or register.

    * Shoutbox

    RefreshHistory
    • Dell: A wild Dell appears
      June 23, 2018, 05:39:57 AM
    • FightMexoxo: jump onto mopar
      June 23, 2018, 12:43:14 AM
    • The Soul: of course @_^_
      June 22, 2018, 10:53:32 PM
    • Travas: ya
      June 22, 2018, 08:20:46 PM
    • _^_: hey folks do you think God wanted president Trump to win the election ?
      June 22, 2018, 08:20:24 PM
    • _^_: Vain_ you are so cruel
      June 22, 2018, 08:00:18 PM
    • pkerproooo: unip ban me from mopar please
      June 22, 2018, 03:14:19 PM
    • The Soul: <3
      June 22, 2018, 02:45:55 PM
    • Bitsybop: Stand at the dock as long as you want, but this ship has sailed. We didn't have long to watch the sails go down, shit be sinkin' yo. Y'dig?
      June 21, 2018, 11:42:23 PM
    • Andrew421: I refuse to leave!
      June 21, 2018, 11:27:53 PM
    • Vain_: Go away _^_
      June 21, 2018, 12:42:55 PM
    • _^_: hey folks, ya boy _^_ here
      June 21, 2018, 10:20:34 AM
    • Picter: Join us at Loyalty-scape.com! [link]
      June 21, 2018, 12:47:35 AM
    • sedveka: bet pi
      June 20, 2018, 11:01:28 PM
    • sedveka: all join my server
      June 20, 2018, 11:01:19 PM
    • Ozank: im not gay but my boyfriend might be
      June 20, 2018, 12:28:57 PM
    • Bitsybop: and single, dtf hmu
      June 19, 2018, 02:12:12 PM
    • Travas: ur gay
      June 19, 2018, 12:44:57 AM
    • dennis7744: just so you know im master owner
      June 18, 2018, 08:49:41 AM
    • dennis7744: mopar is done
      June 18, 2018, 08:49:17 AM

    Author Topic: Packets not arriving/decoding as they should  (Read 179 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
    RS2Ad banner

     

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