Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package messenger.server.packets;
- import java.net.Socket;
- import java.nio.ByteBuffer;
- import messenger.server.ClientConnection;
- import messenger.server.MessengerServer;
- /**
- * This class parses packets.
- * @author Neglected
- */
- public final class PacketManager {
- /**
- * Contains a list of all Packet opcodes.
- * @author Neglected
- *
- */
- enum Opcodes {
- CMSG_PING (0x01), // long ms
- SMSG_PINGRESPONSE (0x02), // long ms
- CMSG_AUTHREQUEST (0x03), // int user_t, String user, int pass_t, String pass
- SMSG_AUTHRESULT (0x04), // int success (0 = false)
- CMSG_MESSAGE (0x05), // int msg_t, String msg, int recv_t, String recv, long time
- SMSG_MESSAGE (0x06), // int sender_t, String sender, int msg_t, String msg, long time
- SMSG_SYSMESSAGE (0x07); // int msg_t, String msg, int mode, long time
- int op;
- String s;
- Opcodes(int i) {op = i;}
- public int getOpcode() { return op; }
- }
- public static Opcodes getOpcodeById(int id) {
- for(Opcodes o : Opcodes.values())
- if(o.getOpcode() == id)
- return o;
- return null;
- }
- /**
- * Parses packet data.
- * This method is invoked when a client reads data.
- * It will decide what to do with the packet.
- * @param conn
- * @param opcode
- * @param packetSize
- * @param data
- */
- public static void parse(ClientConnection conn, int opcode, int packetSize, byte[] data) {
- switch(opcode) {
- case 0x01: CMSG_PING(conn, data);
- case 0x03: CMSG_AUTHREQUEST(conn, data);
- default: MessengerServer.println("PacketManager", "Received unknown packet "+opcode+" from "+conn.toString()+".", true);
- }
- }
- public static void CMSG_PING(ClientConnection conn, byte[] data) {
- // long ms;
- ByteBuffer buff = ByteBuffer.wrap(data);
- long timeSent = buff.getLong();
- conn.send(SMSG_PINGRESPONSE(timeSent));
- }
- public static Packet SMSG_PINGRESPONSE(long sentTime) {
- Packet p = new Packet(0x02, 8);
- p.writeLong(System.currentTimeMillis() - sentTime);
- return p;
- }
- public static void CMSG_AUTHREQUEST(ClientConnection conn, byte[] data) {
- // int user_t;
- // String user;
- // int pass_t;
- // String pass;
- ByteBuffer buff = ByteBuffer.wrap(data);
- int user_t = buff.getInt();
- byte[] _buff = new byte[user_t];
- char[] _char = new char[user_t];
- buff.get(_buff, 0, user_t);
- for(int i = 0; i < _buff.length; i++)
- _char[i] = (char)_buff[i];
- String userName = new String(_char);
- buff.flip();
- int pass_t = buff.getInt();
- buff.get(_buff, 0, pass_t);
- for(int i = 0; i < _buff.length; i++)
- _char[i] = (char)_buff[i];
- String pass = new String(_char);
- // if it meets some criteria..
- Packet p;
- if(userName.toLowerCase().equals("Neglected") && pass.equals("daniel")) {
- p = SMSG_AUTHRESULT(true);
- conn.login(userName, System.currentTimeMillis());
- } else
- p = SMSG_AUTHRESULT(false);
- conn.send(p);
- }
- public static Packet SMSG_AUTHRESULT(boolean result) {
- Packet p = new Packet(0x04, 4);
- p.writeBoolean(result);
- return p;
- }
- public static void CMSG_MESSAGE(ClientConnection conn, byte[] data) {
- //int msg_t,
- //String msg,
- //long time (time message was sent in ms).
- ByteBuffer buff = ByteBuffer.wrap(data);
- int msg_t = buff.getInt();
- buff.flip();
- byte[] buffer_b = new byte[msg_t];
- char[] buffer_c = new char[msg_t];
- buff.get(buffer_b, 0, msg_t);
- for(int i = 0; i < buffer_b.length; i++)
- buffer_c[i] = (char)buffer_b[i];
- String message = new String(buffer_c);
- buff.flip();
- int recv_t = buff.getInt();
- buffer_b = new byte[recv_t];
- buffer_c = new char[recv_t];
- buff.get(buffer_b, 0, msg_t);
- for(int i = 0; i < buffer_b.length; i++)
- buffer_c[i] = (char)buffer_b[i];
- String recipient = new String(buffer_c);
- buff.flip();
- long time = buff.getLong();
- // then we send this message to the intended recipient
- // String sender = conn.getActiveUser();
- // int sender_t = sender.getBytes().length;
- // recv.send(SMSG_MESSAGE(sender_t, sender, msg_t, msg, time));
- // also log this message
- }
- public static Packet SMSG_MESSAGE(int sender_t, String sender, int msg_t, String msg, long time) {
- int size = 4 + sender.getBytes().length + 4 + sender.getBytes().length + 8;
- Packet p = new Packet(0x06, size);
- p.writeInt(sender_t);
- p.writeString(sender);
- p.writeInt(msg_t);
- p.writeString(msg);
- p.writeLong(time);
- return p;
- }
- }
Add Comment
Please, Sign In to add comment