Guest User

Untitled

a guest
Jan 18th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. private void initializeServerCommunication() {
  2. if (clientSocket == null) {
  3. SocketHints socketHints = new SocketHints();
  4. socketHints.connectTimeout = 4000;
  5. socketHints.keepAlive = true;
  6. clientSocket = Gdx.net.newClientSocket(Net.Protocol.TCP, HOST, 6078, socketHints);
  7. outputStream = clientSocket.getOutputStream();
  8. inputStream = clientSocket.getInputStream();
  9.  
  10. timeoutService = Executors.newSingleThreadScheduledExecutor();
  11. timeoutFuture = timeoutService.scheduleAtFixedRate(keepAliveRunnable, KEEP_ALIVE_TIMEOUT, KEEP_ALIVE_TIMEOUT, TimeUnit.SECONDS);
  12. }
  13.  
  14. if (listenerSocket == null) {
  15. // create second thread to listen for incoming messages from server
  16. new Thread(new Runnable() {
  17. @Override
  18. public void run() {
  19. try {
  20. SocketHints socketHints = new SocketHints();
  21. socketHints.connectTimeout = 4000;
  22. socketHints.keepAlive = true;
  23. listenerSocket = Gdx.net.newClientSocket(Net.Protocol.TCP, HOST, 6079, socketHints);
  24. serverClientOutputStream = listenerSocket.getOutputStream();
  25. serverClientInputStream = listenerSocket.getInputStream();
  26.  
  27. int result = 0;
  28. while (result != -1) {
  29. try {
  30. result = serverClientInputStream.read();
  31. } catch (Exception e) {
  32. Gdx.app.log(TAG, "Server - Client Connection lost. Try to reconnect");
  33. reconnect(); // start reconnection
  34. Thread.currentThread().interrupt();
  35. }
  36. if (result != -1) {
  37. switch (result) {
  38. ...
  39. }
  40. }
  41. }
  42. }
  43. }
  44. }).start();
  45. }
  46. }
  47.  
  48. private void reconnect() {
  49. cleanUp();
  50.  
  51. ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
  52. reConnectFuture = scheduledExecutorService.scheduleAtFixedRate(reConnectorRunnable, RECONNECT_INTERVAL, RECONNECT_INTERVAL, TimeUnit.SECONDS);
  53. }
  54.  
  55. private Runnable reConnectorRunnable = new Runnable() {
  56. @Override
  57. public void run() {
  58. try {
  59. initializeServerCommunication();
  60. stopReconnectionService();
  61. if (errorHandlingListener != null) {
  62. errorHandlingListener.reconnected();
  63. } else {
  64. Gdx.app.error(TAG, "Reconnected but could not inform the app about it.");
  65. }
  66. } catch (GdxRuntimeException r) {
  67. reconnectAttemptsLeft--;
  68. cleanUp();
  69. if (reconnectAttemptsLeft <= 0) {
  70. stopReconnectionService();
  71. }
  72. } catch (Exception e) {
  73. reconnectAttemptsLeft--;
  74. cleanUp();
  75. if (reconnectAttemptsLeft <= 0) {
  76. stopReconnectionService();
  77.  
  78. }
  79. }
  80. }
  81. };
  82.  
  83. private void stopReconnectionService() {
  84. reconnectAttemptsLeft = MAX_RECONNECT_ATTEMPTS;
  85. reConnectFuture.cancel(true);
  86. }
  87.  
  88. private void cleanUp() {
  89. if (inputStream != null) {
  90. try {
  91. inputStream.close();
  92. } catch (IOException i) {
  93. Gdx.app.log(TAG, "Could not close client - server input stream");
  94. }
  95. inputStream = null;
  96. }
  97.  
  98. if (outputStream != null) {
  99. try {
  100. outputStream.close();
  101. } catch (IOException e) {
  102. Gdx.app.log(TAG, "Could not close client - server output stream");
  103. }
  104. outputStream = null;
  105. }
  106.  
  107. if (serverClientInputStream != null) {
  108. try {
  109. serverClientInputStream.close();
  110. } catch (IOException e) {
  111. Gdx.app.log(TAG, "Could not close server - client input stream");
  112. }
  113. serverClientInputStream = null;
  114. }
  115.  
  116. if (serverClientOutputStream != null) {
  117. try {
  118. serverClientOutputStream.close();
  119. } catch (IOException e) {
  120. Gdx.app.log(TAG, "Could not close server - client output stream");
  121. }
  122. serverClientOutputStream = null;
  123. }
  124. if (clientSocket != null) {
  125. clientSocket.dispose();
  126. }
  127. clientSocket = null;
  128.  
  129. if (listenerSocket != null) {
  130. listenerSocket.dispose();
  131. }
  132. listenerSocket = null;
  133. }
Add Comment
Please, Sign In to add comment