Guest User

Untitled

a guest
Jan 23rd, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.39 KB | None | 0 0
  1. package messenger.server.packets;
  2. import java.net.Socket;
  3. import java.nio.ByteBuffer;
  4.  
  5. import messenger.server.ClientConnection;
  6. import messenger.server.MessengerServer;
  7.  
  8. /**
  9.  * This class parses packets.
  10.  * @author Neglected
  11.  */
  12. public final class PacketManager {
  13.     /**
  14.      * Contains a list of all Packet opcodes.
  15.      * @author Neglected
  16.      *
  17.      */
  18.     enum Opcodes {
  19.         CMSG_PING           (0x01), // long ms
  20.         SMSG_PINGRESPONSE   (0x02), // long ms
  21.         CMSG_AUTHREQUEST    (0x03), // int user_t, String user, int pass_t, String pass
  22.         SMSG_AUTHRESULT     (0x04), // int success (0 = false)
  23.         CMSG_MESSAGE        (0x05), // int msg_t, String msg, int recv_t, String recv, long time
  24.         SMSG_MESSAGE        (0x06), // int sender_t, String sender, int msg_t, String msg, long time
  25.         SMSG_SYSMESSAGE     (0x07); // int msg_t, String msg, int mode, long time
  26.         int op;
  27.         String s;
  28.         Opcodes(int i) {op = i;}
  29.        
  30.         public int getOpcode() { return op; }
  31.  
  32.     }
  33.    
  34.     public static Opcodes getOpcodeById(int id) {
  35.         for(Opcodes o : Opcodes.values())
  36.             if(o.getOpcode() == id)
  37.                 return o;
  38.         return null;
  39.     }
  40.    
  41.     /**
  42.      * Parses packet data.
  43.      * This method is invoked when a client reads data.
  44.      * It will decide what to do with the packet.
  45.      * @param conn
  46.      * @param opcode
  47.      * @param packetSize
  48.      * @param data
  49.      */
  50.     public static void parse(ClientConnection conn, int opcode, int packetSize, byte[] data) {
  51.         switch(opcode) {
  52.             case 0x01: CMSG_PING(conn, data);
  53.             case 0x03: CMSG_AUTHREQUEST(conn, data);
  54.             default: MessengerServer.println("PacketManager", "Received unknown packet "+opcode+" from "+conn.toString()+".", true);
  55.         }
  56.     }
  57.    
  58.     public static void CMSG_PING(ClientConnection conn, byte[] data) {
  59.         // long ms;
  60.         ByteBuffer buff = ByteBuffer.wrap(data);
  61.         long timeSent = buff.getLong();
  62.        
  63.         conn.send(SMSG_PINGRESPONSE(timeSent));
  64.     }
  65.     public static Packet SMSG_PINGRESPONSE(long sentTime) {
  66.         Packet p = new Packet(0x02, 8);
  67.         p.writeLong(System.currentTimeMillis() - sentTime);
  68.         return p;
  69.     }
  70.     public static void CMSG_AUTHREQUEST(ClientConnection conn, byte[] data) {
  71.         // int user_t;
  72.         // String user;
  73.         // int pass_t;
  74.         // String pass;
  75.         ByteBuffer buff = ByteBuffer.wrap(data);
  76.         int user_t = buff.getInt();
  77.        
  78.         byte[] _buff = new byte[user_t];
  79.         char[] _char = new char[user_t];
  80.         buff.get(_buff, 0, user_t);
  81.         for(int i = 0; i < _buff.length; i++)
  82.             _char[i] = (char)_buff[i];
  83.         String userName = new String(_char);
  84.        
  85.         buff.flip();
  86.         int pass_t = buff.getInt();
  87.         buff.get(_buff, 0, pass_t);
  88.         for(int i = 0; i < _buff.length; i++)
  89.             _char[i] = (char)_buff[i];
  90.         String pass = new String(_char);
  91.        
  92.         // if it meets some criteria..
  93.         Packet p;
  94.         if(userName.toLowerCase().equals("Neglected") && pass.equals("daniel")) {
  95.             p = SMSG_AUTHRESULT(true);
  96.             conn.login(userName, System.currentTimeMillis());
  97.         } else
  98.             p = SMSG_AUTHRESULT(false);
  99.        
  100.         conn.send(p);
  101.     }
  102.     public static Packet SMSG_AUTHRESULT(boolean result) {
  103.         Packet p = new Packet(0x04, 4);
  104.         p.writeBoolean(result);
  105.         return p;
  106.     }
  107.     public static void CMSG_MESSAGE(ClientConnection conn, byte[] data) {
  108.         //int msg_t,
  109.         //String msg,
  110.         //long time (time message was sent in ms).
  111.         ByteBuffer buff = ByteBuffer.wrap(data);
  112.         int msg_t = buff.getInt();
  113.         buff.flip();
  114.        
  115.         byte[] buffer_b = new byte[msg_t];
  116.         char[] buffer_c = new char[msg_t];
  117.         buff.get(buffer_b, 0, msg_t);
  118.         for(int i = 0; i < buffer_b.length; i++)
  119.             buffer_c[i] = (char)buffer_b[i];
  120.         String message = new String(buffer_c);
  121.        
  122.         buff.flip();
  123.        
  124.         int recv_t = buff.getInt();
  125.         buffer_b = new byte[recv_t];
  126.         buffer_c = new char[recv_t];
  127.         buff.get(buffer_b, 0, msg_t);
  128.         for(int i = 0; i < buffer_b.length; i++)
  129.             buffer_c[i] = (char)buffer_b[i];       
  130.         String recipient = new String(buffer_c);
  131.        
  132.         buff.flip();
  133.        
  134.         long time = buff.getLong();
  135.        
  136.         // then we send this message to the intended recipient
  137.         // String sender = conn.getActiveUser();
  138.         // int sender_t = sender.getBytes().length;
  139.         // recv.send(SMSG_MESSAGE(sender_t, sender, msg_t, msg, time));
  140.         // also log this message
  141.     }
  142.     public static Packet SMSG_MESSAGE(int sender_t, String sender, int msg_t, String msg, long time) {
  143.         int size = 4 + sender.getBytes().length + 4 + sender.getBytes().length + 8;
  144.         Packet p = new Packet(0x06, size);
  145.         p.writeInt(sender_t);
  146.         p.writeString(sender);
  147.         p.writeInt(msg_t);
  148.         p.writeString(msg);
  149.         p.writeLong(time);
  150.         return p;
  151.     }
  152. }
Add Comment
Please, Sign In to add comment