Advertisement
Guest User

Untitled

a guest
May 20th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.40 KB | None | 0 0
  1. package btserver;
  2. import btUtils.MiscFunc;
  3. import java.nio.*;
  4. import java.io.*;
  5. import java.util.*;
  6. import java.sql.*;
  7. import org.xsocket.stream.*;
  8. import org.apache.log4j.Logger;
  9. public class BTServer implements IConnectHandler, IDataHandler, IDisconnectHandler
  10. {
  11. static Logger logger = Logger.getLogger(BTServer.class.getName());
  12. private UserManager userMgr = null;
  13. public BTServer()
  14. {
  15. logger.debug("[Init start ...]");
  16. userMgr = UserManager.getInstance();
  17. logger.debug("[Init success]");
  18. }
  19. public boolean onData(INonBlockingConnection connection) throws IOException
  20. {
  21. NetUser cur_gmr = userMgr.findByConnection(connection);
  22. if(cur_gmr == null)
  23. {
  24. cur_gmr = new NetUser(connection, false);
  25. logger.debug("[New connection: IP = " + connection.getRemoteAddress().getHostAddress() + "; Address = " +
  26. connection.getRemoteAddress().getHostName() + ";]");
  27. }
  28. ByteBuffer[] bbuf = connection.readAvailable();
  29. int index = 0;
  30. byte[] buf = null;
  31. for(int i=0; i < bbuf.length; i++)
  32. {
  33. buf = new byte[bbuf[i].capacity()];
  34. for(int k=0; k < buf.length; k++)
  35. buf[k] = bbuf[i].get();
  36. if(cur_gmr.last_msg == null)
  37. cur_gmr.last_msg = new NetMessage();
  38. while( (index = cur_gmr.last_msg.createFromBuffer(buf,index)) > 0)
  39. {
  40. if(cur_gmr.last_msg.complete)
  41. ProcessMsg(cur_gmr);
  42. if(cur_gmr.last_msg == null)
  43. cur_gmr.last_msg = new NetMessage();
  44. }
  45. if(cur_gmr.last_msg != null)
  46. {
  47. ProcessMsg(cur_gmr);
  48. }
  49. buf = null;
  50. }
  51. return true;
  52. }
  53. private void ProcessMsg(NetUser usr)
  54. {
  55. NetMessage nm = null;
  56. Random rnd = null;
  57. List<Object> params = null;
  58. boolean status = false;
  59. logger.debug("[" + usr.last_msg.type + " from " + usr.userName + "(" + usr.ID + ")]");
  60. usr.bytes_send += usr.last_msg.body.length;
  61. switch(usr.last_msg.type)
  62. {
  63. case MSGC_LOGIN:
  64. status = userMgr.clientLogin(usr, usr.last_msg.params);
  65. break;
  66. case MSGC_PING:
  67. int userID = (Integer)usr.last_msg.params.get(0);
  68. String key = (String)usr.last_msg.params.get(1);
  69. usr.lastPingTime = MiscFunc.getSeconds();
  70. if(userID != usr.ID && !key.equals(usr.currentKey))
  71. {
  72. logger.info("Client info is wrong!; Removing client from user list");
  73. userMgr.removeClient(usr);
  74. try
  75. {
  76. usr.connection.close();
  77. }
  78. catch (IOException ex)
  79. {
  80. ex.printStackTrace();
  81. }
  82. }
  83. break;
  84. default:
  85. break;
  86. }
  87. usr.last_msg.params.clear();
  88. usr.last_msg = null;
  89. nm = null;
  90. }
  91. public boolean onConnect(INonBlockingConnection connection) throws IOException
  92. {
  93. return true;
  94. }
  95. public boolean onDisconnect(INonBlockingConnection connection) throws IOException
  96. {
  97. NetUser player = userMgr.findByConnection(connection);
  98. if(player != null)
  99. {
  100. logger.info("[Client " + player.userName + "(" + player.ID + ") disconnected]");
  101. userMgr.removeClient(player);
  102. }
  103. else
  104. {
  105. logger.warn("[Unknown client disconnected]");
  106. }
  107. return true;
  108. }
  109. }
  110. Клиент :
  111. package Main;
  112. import javax.microedition.midlet.*;
  113. import javax.microedition.lcdui.*;
  114. public class BTMidlet extends MIDlet implements CommandListener{
  115. Display display = null;
  116. BTApp game = null;
  117. Form form = null;
  118. TextField textField = null;
  119. int fieldType;
  120. public BTMidlet(){
  121. super();
  122. display = Display.getDisplay(this);
  123. game = new BTApp(this);
  124. display.setCurrent(game);
  125. game.start();
  126. }
  127. public void startApp() {
  128. }
  129. public void pauseApp() {
  130. }
  131. public void destroyApp(boolean unconditional) {
  132. notifyDestroyed();
  133. }
  134. public void quit(){
  135. game.stop();
  136. notifyDestroyed();
  137. }
  138. public void commandAction(Command command, Displayable displayable)
  139. {
  140. }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement