Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.00 KB | None | 0 0
  1.  
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.net.Socket;
  7.  
  8. public final class ClientSocket implements Runnable {
  9.  
  10.     private final Socket socket;
  11.  
  12.     private String user;
  13.     private String pass;
  14.  
  15.     private static DataInputStream dis;
  16.     private static DataOutputStream dos;
  17.  
  18.     private static PacketHandler packetHandler = new PacketHandler();
  19.  
  20.     public ClientSocket(Socket socket, String user, String pass) {
  21.         this.socket = socket;
  22.         if (socket.isClosed()) {
  23.             return;
  24.         }
  25.         this.user = user;
  26.         this.pass = pass;
  27.         try {
  28.             dis = new DataInputStream(socket.getInputStream());
  29.             dos = new DataOutputStream(socket.getOutputStream());
  30.         } catch (IOException e) {
  31.             return;
  32.         }
  33.     }
  34.  
  35.     /**
  36.      * Initializes the login procedure
  37.      */
  38.     private void sendLoginPacket() {
  39.         if (writeOpcode(45)) {
  40.             writeString(user);
  41.             writeString(pass);
  42.             //int resp = readByte();
  43.             //System.out.println("Resp: " + resp);
  44.         } else {
  45.         }
  46.     }
  47.  
  48.     @Override
  49.     public void run() {
  50.         if (socket == null) {
  51.             System.out.println("NO1");
  52.             return;
  53.         }
  54.         if (socket.isClosed()) {
  55.             System.out.println("NO2");
  56.             return;
  57.         }
  58.         sendLoginPacket();
  59.         try {
  60.             for (;;) {
  61.                 /*if (socket == null)
  62.                     return;
  63.                 if (socket.isClosed())
  64.                     return;*/
  65.                 int opcode = -1;
  66.                 //System.out.println("Waiting for opcode");
  67.                 if ((opcode = dis.readByte()) != -1) {
  68.                     System.out.println("Opcode: " + opcode);
  69.                     switch (opcode) {
  70.                     case 99:
  71.                         //dos.writeByte(254);
  72.                         //dos.writeLong(System.currentTimeMillis());
  73.                         break;
  74.                     case 25:
  75.                         break;
  76.                         /*case 0:
  77.                         dos.writeByte(0);
  78.                         break;
  79.                     case 111:
  80.                         int returnValue = dis.readInt();
  81.                         System.out.println(returnValue + " return value");
  82.                         break;*/
  83.                     default:
  84.                         System.out.println("Packet [" + opcode + "] unhandled");
  85.                         packetHandler.handlePacket(this, opcode);
  86.                         break;
  87.                     }
  88.                 }
  89.             }
  90.         } catch (IOException e) {
  91.             try {
  92.                 socket.close();
  93.             } catch (IOException e1) {
  94.                 e1.printStackTrace();
  95.             }
  96.             return;
  97.         } catch (NullPointerException npe) {
  98.             return;
  99.         }
  100.     }
  101.  
  102.     public void writeKeepAlive() {
  103.         try {
  104.             if (!(dos == null))
  105.                 dos.writeByte(0);
  106.         } catch (IOException e) {
  107.             e.printStackTrace();
  108.         }
  109.     }
  110.    
  111.     public void writeBytes(byte[] bytes) {
  112.         try {
  113.             if (!(dos == null)) {
  114.                 for (int i = 0; i < bytes.length; i++) {
  115.                     dos.writeByte(bytes[i]);
  116.                 }
  117.             }
  118.                 //dos.write(bytes);
  119.         } catch (IOException e) {
  120.             e.printStackTrace();
  121.         }
  122.     }
  123.    
  124.     public boolean write(byte[] write, int offset, int len) {
  125.         try {
  126.             if (!(dos == null))
  127.                 dos.write(write, offset, len);
  128.         } catch (IOException e) {
  129.             return false;
  130.         }
  131.         return true;
  132.     }
  133.    
  134.     public void sendBytes(byte[] myByteArray) throws IOException {
  135.         sendBytes(myByteArray, 0, myByteArray.length);
  136.     }
  137.  
  138.     public void sendBytes(byte[] myByteArray, int start, int len) throws IOException {
  139.         if (len < 0)
  140.             throw new IllegalArgumentException("Negative length not allowed");
  141.         if (start < 0 || start >= myByteArray.length)
  142.             throw new IndexOutOfBoundsException("Out of bounds: " + start);
  143.         // Other checks if needed.
  144.  
  145.         // May be better to save the streams in the support class;
  146.         // just like the socket variable.
  147.         dos.writeInt(len);
  148.         if (len > 0) {
  149.             dos.write(myByteArray, start, len);
  150.         }
  151.     }
  152.    
  153.     public boolean writeInt(int write) {
  154.         try {
  155.             if (!(dos == null))
  156.                 dos.writeInt(write);
  157.         } catch (IOException e) {
  158.             return false;
  159.         }
  160.         return true;
  161.     }
  162.  
  163.     public boolean writeOpcode(int opcode) {
  164.         try {
  165.             if (!(dos == null))
  166.                 dos.writeByte(opcode);
  167.         } catch (IOException e) {
  168.             return false;
  169.         }
  170.         return true;
  171.     }
  172.  
  173.     public boolean writeShort(short s) {
  174.         try {
  175.             if (!(dos == null))
  176.                 dos.writeShort(s);
  177.         } catch (IOException e) {
  178.             return false;
  179.         }
  180.         return true;
  181.     }
  182.  
  183.     public boolean writeBoolean(boolean bool) {
  184.         try {
  185.             if (!(dos == null))
  186.                 dos.writeBoolean(bool);
  187.         } catch (IOException e) {
  188.             return false;
  189.         }
  190.         return true;
  191.     }
  192.  
  193.     public byte readByte() {
  194.         try {
  195.             return dis.readByte();
  196.         } catch (IOException e) {
  197.             //e.printStackTrace();
  198.         }
  199.         return -1;
  200.     }
  201.    
  202.     public byte[] readBytes(int amount) {
  203.         byte[] data = new byte[amount];
  204.         for (int i = 0; i < amount; i++) {
  205.             data[i] = (byte) readByte();
  206.         }
  207.         return data;
  208.     }
  209.  
  210.     public int readInt() {
  211.         try {
  212.             return dis.readInt();
  213.         } catch (IOException e) {
  214.             //e.printStackTrace();
  215.         }
  216.         return -1;
  217.     }
  218.  
  219.     public short readShort() {
  220.         try {
  221.             return dis.readShort();
  222.         } catch (IOException e) {
  223.             //e.printStackTrace();
  224.         }
  225.         return -1;
  226.     }
  227.  
  228.     public long readLong() {
  229.         try {
  230.             return dis.readLong();
  231.         } catch (IOException e) {
  232.             //e.printStackTrace();
  233.         }
  234.         return -1;
  235.     }
  236.  
  237.     public boolean readBoolean() {
  238.         try {
  239.             return dis.readBoolean();
  240.         } catch (IOException e) {
  241.             //e.printStackTrace();
  242.         }
  243.         return false;
  244.     }
  245.  
  246.     public boolean writeString(String s) {
  247.         try {
  248.             if (!(dos == null)) {
  249.                 dos.writeInt(s.length());
  250.                 for (int i = 0; i < s.length(); i++) {
  251.                     dos.writeChar(s.charAt(i));
  252.                 }
  253.             }
  254.             //  dos.writeUTF(s);
  255.         } catch (IOException e) {
  256.             return false;
  257.         }
  258.         return true;
  259.     }
  260.  
  261.     public String readString() {
  262.         StringBuilder sb = new StringBuilder();
  263.         try {
  264.             //return dis.readUTF();
  265.             if (!(dis == null)) {
  266.                 int len = dis.read();
  267.                 for (int i = 0; i < len; i++) {
  268.                     sb.append(dis.readChar());
  269.                 }
  270.             }
  271.         } catch (IOException e) {
  272.             //e.printStackTrace();
  273.         }
  274.         return sb.toString();
  275.     }
  276.  
  277.     public void close() {
  278.         try {
  279.             dis.close();
  280.             if (!(dos == null))
  281.                 dos.flush();
  282.             if (!(dos == null))
  283.                 dos.close();
  284.             socket.close();
  285.         } catch (IOException e) {
  286.             e.printStackTrace();
  287.         }
  288.     }
  289.  
  290.     public boolean writeByte(byte b) {
  291.         try {
  292.             if (!(dos == null)) {
  293.                 dos.writeByte(b);
  294.             }
  295.         } catch (IOException e) {
  296.             return false;
  297.         }
  298.         return true;
  299.     }
  300.  
  301. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement