Welcome, guest! Please login or register.

    * Shoutbox

    RefreshHistory
    • `Discardedx2`: ?
      September 21, 2018, 12:24:09 PM
    • Pookey71: ARE YOU HERE TO RECEIVE MY LIMP PENIS!?
      September 20, 2018, 11:55:24 PM
    • Pookey71: THIS IS DEMOCRACY MANIFESTO
      September 20, 2018, 11:55:07 PM
    • Vasi28: Гранд
      September 03, 2018, 07:06:22 PM
    • Rash: test
      August 30, 2018, 09:09:18 PM
    • Guruu:[link]
      August 30, 2018, 12:04:45 AM
    • Guruu: am i allowed to post links shoutbox?
      August 30, 2018, 12:03:46 AM
    • drubrkletern:[link] feedback wanted
      August 28, 2018, 03:55:17 PM
    • bugz000: Anyone got a link for the old ass silab client and cache.zip? from mopar 3.2 era :) 317
      August 25, 2018, 12:48:15 AM
    • cholland2015: Does anyone know where I can download the old moparscape client/server?
      August 24, 2018, 02:30:51 PM
    • hexedss1: Anyone would like to help on client side got a variable declartion Error
      August 15, 2018, 09:06:30 PM
    • SOUPSOUP: just get a vpn
      August 13, 2018, 06:44:28 PM
    • jme: _^_ will answer all of your javascript questions
      August 12, 2018, 07:11:00 PM
    • doomsale: and where is boxman?
      August 10, 2018, 03:16:59 AM
    • doomsale: Where can i play the Moparscape MMORPG with the Metallica instrumentals?
      August 10, 2018, 03:16:47 AM
    • doomsale: no longer clicking on doors and dummies at east bank
      August 10, 2018, 03:16:07 AM
    • doomsale: were all grown up now huh?
      August 10, 2018, 03:15:58 AM
    • _^_: hey everyone look at my graphs [link]
      August 06, 2018, 01:52:16 PM
    • braedyn: Someone pl0x help me out with PI Lottery!!
      August 03, 2018, 02:12:25 AM
    • Travas: p3n1s
      July 26, 2018, 09:46:33 PM

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