Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package isotopestudio.backdoor.network.tcp.message;
- import isotopestudio.backdoor.network.tcp.client.Client;
- import isotopestudio.backdoor.network.tcp.server.client.ServerClient;
- import isotopestudio.backdoor.network.utils.URLTools;
- public abstract class Message {
- public static final int MESSAGE_ID = 1;
- public static final int REPLY_ID = 2;
- public static final char SEPARATOR = '/';
- public static Message[] messages = new Message[] {null};
- public static Message parseMessage(String data) {
- String[] datas = data.split(SEPARATOR+"");
- int Message_id = Integer.parseInt(datas[0]);
- if(Message_id == 0)
- return null;
- int Message_type = Integer.parseInt(datas[1]);
- if(Message_type == 0) {
- return null;
- }
- if(Message_id > messages.length) {
- return null;
- }
- Message Message = messages[Message_id].clone();
- Message.id = Message_id;
- Message.Message_type = Message_type;
- Message.datas = datas;
- return Message;
- }
- private int id;
- private int Message_type;
- public String[] datas;
- public Message(int id, int Message_type, Object... datas) {
- super();
- this.id = id;
- this.Message_type = Message_type;
- this.datas = new String[datas.length + 2];
- this.datas[0] = id + "";
- this.datas[1] = Message_type+"";
- for (int i = 2; i < datas.length + 2; i++) {
- String data = datas[i-2] != null ? datas[i-2]+"" : "null";
- this.datas[i] = URLTools.encode(data);
- }
- }
- public String toData() {
- String data = this.id+"";
- if(this.datas.length > 2) {
- for (int i = 1; i < this.datas.length; i++) {
- data += SEPARATOR+this.datas[i];
- }
- }
- return data;
- }
- public int getId() {
- return id;
- }
- public int getMessageType() {
- return Message_type;
- }
- public abstract Message clone();
- public void read() {
- if(getMessageType() == MESSAGE_ID) {
- readMessage();
- } else if (Message_type == REPLY_ID) {
- readReply();
- }
- }
- public abstract void readReply();
- public abstract void readMessage();
- public void handle(Client client) {
- if(getMessageType() == MESSAGE_ID) {
- message(client);
- } else if (Message_type == REPLY_ID) {
- reply(client);
- }
- }
- public void handle(ServerClient client) {
- if(getMessageType() == MESSAGE_ID) {
- message(client);
- } else if (Message_type == REPLY_ID) {
- reply(client);
- }
- }
- public abstract void message(Client client);
- public abstract void message(ServerClient client);
- public abstract void reply(Client client);
- public abstract void reply(ServerClient client);
- int read_index = 2;
- public String readString() {
- String data = datas[read_index];
- read_index++;
- return URLTools.decode(data);
- }
- public Boolean readBoolean() {
- return Boolean.parseBoolean(readString());
- }
- public Integer readInt() {
- return Integer.parseInt(readString());
- }
- public Long readLong() {
- return Long.parseLong(readString());
- }
- public Float readFloat() {
- return Float.parseFloat(readString());
- }
- public Double readDouble() {
- return Double.parseDouble(readString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement