Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.rs.entities;
- import java.io.*;
- import java.net.*;
- import com.rs2.jindex.*;
- public class World extends Thread
- {
- private final static int buffersize = 5000;
- private java.net.Socket socket;
- private InputStream in;
- private OutputStream out;
- public byte buffer[];
- private int playerid;
- private int readptr;
- private int writeptr;
- private stream instream;
- private stream outstream;
- private Cryption instreamdecryption;
- private Cryption outstreamdecryption;
- public int packetsize = 0, packettype = -1;
- private int returncode = 2;
- private int rights;
- private boolean isrunning = true;
- private String username;
- private String password;
- private Player player;
- public World()
- {
- new World(socket, playerid);
- username = player.username;
- password = player.password;
- }
- public World(Player player)
- {
- this.player = player;
- }
- public World(java.net.Socket socket, int playerid)
- {
- this.playerid = playerid;
- this.socket = socket;
- try
- {
- in = socket.getInputStream();
- out = socket.getOutputStream();
- } catch(IOException ioe)
- {
- System.err.println(ioe.getMessage());
- }
- outstream = new stream(new byte[5000]);
- outstream.currentOffset = 0;
- instream = new stream(new byte[5000]);
- instream.currentOffset = 0;
- readptr = writeptr = 0;
- buffer = buffer = new byte[5000];
- }
- public void run()
- {
- long serversessionkey = 0;
- long clientsessionkey = 0;
- serversessionkey = ((long)(Math.random() * 99999999D) << 32) + (long)(Math.random() * 99999999D);
- try
- {
- fillinstream(2);
- if(instream.readUnsignedByte() != 14)
- {
- System.err.println("Needed 14 login id, received " + instream.readUnsignedByte());
- return;
- }
- int namepart = instream.readUnsignedByte();
- for(int i = 0; i < 8; i++)
- {
- out.write(0);
- }
- out.write(0);
- outstream.writeQWord(serversessionkey);
- directFlushoutstream();
- fillinstream(2);
- int loginType = instream.readUnsignedByte();
- if(loginType != 16 && loginType != 18)
- {
- System.out.println("Needed 16 or 18 login response, received " + loginType);
- return;
- }
- int loginpacketsize = instream.readUnsignedByte();
- int loginencryptpacketsize = loginpacketsize-(36+1+1+2);
- fillinstream(loginpacketsize);
- if(instream.readUnsignedByte() != 255 || instream.readUnsignedWord() != 317)
- {
- System.out.println("Wrong login packet id, needed 255, 317");
- return;
- }
- int lowmemoryversion = instream.readUnsignedByte();
- loginencryptpacketsize--;
- int temp = instream.readUnsignedByte();
- if(loginencryptpacketsize != temp)
- {
- System.out.println("Encrypted packet data length " + loginencryptpacketsize + " different from length byte of " + temp);
- return;
- }
- temp = instream.readUnsignedByte();
- if(temp != 10)
- {
- System.out.println("Tempt is not equal to 10");
- return;
- }
- clientsessionkey = instream.readQWord();
- serversessionkey = instream.readQWord();
- username = instream.readString();
- password = instream.readString();
- int sessionkey[] = new int[4];
- sessionkey[0] = (int)(clientsessionkey >> 32);
- sessionkey[1] = (int)clientsessionkey;
- sessionkey[2] = (int)(serversessionkey >> 32);
- sessionkey[3] = (int)serversessionkey;
- instreamdecryption = new Cryption(sessionkey);
- outstreamdecryption = new Cryption(sessionkey);
- outstream.packetEncryption = outstreamdecryption;
- returncode = 2;
- rights = 2;
- PlayerHandler handler = new PlayerHandler();
- boolean bool = true;
- if(bool)
- {
- player.x = 3200;
- player.y = 3200;
- player.h = 0;
- }
- out.write(returncode);
- out.write(rights);
- out.write(0);
- } catch(Exception exp)
- {
- System.out.println(exp.getMessage());
- return;
- }
- if(playerid == -1 || returncode != 2)
- {
- return;
- }
- packetsize = 0;
- packettype = -1;
- readptr = 0;
- writeptr = 0;
- int numbytesinbuffer;
- int offset;
- while(isrunning)
- {
- synchronized(this)
- {
- if(writeptr == readptr)
- {
- try
- {
- wait();
- } catch(Exception ex)
- {
- System.out.println(ex.getMessage());
- }
- }
- offset = readptr;
- if(writeptr >= readptr)
- {
- numbytesinbuffer = writeptr - readptr;
- } else
- {
- numbytesinbuffer = 5000 - readptr;
- }
- }
- if(numbytesinbuffer > 0)
- {
- try
- {
- out.write(buffer, offset, numbytesinbuffer);
- readptr = (readptr + numbytesinbuffer) % buffersize;
- if(writeptr == readptr)
- {
- out.flush();
- }
- } catch(Exception ex)
- {
- System.out.println(ex.getMessage());
- }
- }
- }
- return;
- }
- private void fillinstream(int forceRead) throws IOException
- {
- instream.currentOffset = 0;
- in.read(instream.buffer, 0, forceRead);
- }
- private void directFlushoutstream() throws java.io.IOException
- {
- out.write(outstream.buffer, 0, outstream.currentOffset);
- outstream.currentOffset = 0; // reset
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement