Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. package isotopestudio.backdoor.network.tcp.message;
  2.  
  3. import isotopestudio.backdoor.network.tcp.client.Client;
  4. import isotopestudio.backdoor.network.tcp.server.client.ServerClient;
  5. import isotopestudio.backdoor.network.utils.URLTools;
  6.  
  7. public abstract class Message {
  8.  
  9. public static final int MESSAGE_ID = 1;
  10. public static final int REPLY_ID = 2;
  11.  
  12. public static final char SEPARATOR = '/';
  13.  
  14. public static Message[] messages = new Message[] {null};
  15.  
  16. public static Message parseMessage(String data) {
  17. String[] datas = data.split(SEPARATOR+"");
  18. int Message_id = Integer.parseInt(datas[0]);
  19. if(Message_id == 0)
  20. return null;
  21. int Message_type = Integer.parseInt(datas[1]);
  22. if(Message_type == 0) {
  23. return null;
  24. }
  25.  
  26. if(Message_id > messages.length) {
  27. return null;
  28. }
  29.  
  30. Message Message = messages[Message_id].clone();
  31. Message.id = Message_id;
  32. Message.Message_type = Message_type;
  33. Message.datas = datas;
  34. return Message;
  35. }
  36.  
  37. private int id;
  38. private int Message_type;
  39. public String[] datas;
  40.  
  41. public Message(int id, int Message_type, Object... datas) {
  42. super();
  43. this.id = id;
  44. this.Message_type = Message_type;
  45. this.datas = new String[datas.length + 2];
  46. this.datas[0] = id + "";
  47. this.datas[1] = Message_type+"";
  48. for (int i = 2; i < datas.length + 2; i++) {
  49. String data = datas[i-2] != null ? datas[i-2]+"" : "null";
  50. this.datas[i] = URLTools.encode(data);
  51. }
  52. }
  53.  
  54. public String toData() {
  55. String data = this.id+"";
  56. if(this.datas.length > 2) {
  57. for (int i = 1; i < this.datas.length; i++) {
  58. data += SEPARATOR+this.datas[i];
  59. }
  60. }
  61. return data;
  62. }
  63.  
  64. public int getId() {
  65. return id;
  66. }
  67.  
  68. public int getMessageType() {
  69. return Message_type;
  70. }
  71.  
  72. public abstract Message clone();
  73.  
  74. public void read() {
  75. if(getMessageType() == MESSAGE_ID) {
  76. readMessage();
  77. } else if (Message_type == REPLY_ID) {
  78. readReply();
  79. }
  80. }
  81.  
  82. public abstract void readReply();
  83. public abstract void readMessage();
  84.  
  85. public void handle(Client client) {
  86. if(getMessageType() == MESSAGE_ID) {
  87. message(client);
  88. } else if (Message_type == REPLY_ID) {
  89. reply(client);
  90. }
  91. }
  92.  
  93. public void handle(ServerClient client) {
  94. if(getMessageType() == MESSAGE_ID) {
  95. message(client);
  96. } else if (Message_type == REPLY_ID) {
  97. reply(client);
  98. }
  99. }
  100.  
  101. public abstract void message(Client client);
  102. public abstract void message(ServerClient client);
  103.  
  104. public abstract void reply(Client client);
  105. public abstract void reply(ServerClient client);
  106.  
  107. int read_index = 2;
  108.  
  109. public String readString() {
  110. String data = datas[read_index];
  111. read_index++;
  112. return URLTools.decode(data);
  113. }
  114.  
  115. public Boolean readBoolean() {
  116. return Boolean.parseBoolean(readString());
  117. }
  118.  
  119. public Integer readInt() {
  120. return Integer.parseInt(readString());
  121. }
  122.  
  123. public Long readLong() {
  124. return Long.parseLong(readString());
  125. }
  126.  
  127. public Float readFloat() {
  128. return Float.parseFloat(readString());
  129. }
  130.  
  131. public Double readDouble() {
  132. return Double.parseDouble(readString());
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement