Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void initializeServerCommunication() {
- if (clientSocket == null) {
- SocketHints socketHints = new SocketHints();
- socketHints.connectTimeout = 4000;
- socketHints.keepAlive = true;
- clientSocket = Gdx.net.newClientSocket(Net.Protocol.TCP, HOST, 6078, socketHints);
- outputStream = clientSocket.getOutputStream();
- inputStream = clientSocket.getInputStream();
- timeoutService = Executors.newSingleThreadScheduledExecutor();
- timeoutFuture = timeoutService.scheduleAtFixedRate(keepAliveRunnable, KEEP_ALIVE_TIMEOUT, KEEP_ALIVE_TIMEOUT, TimeUnit.SECONDS);
- }
- if (listenerSocket == null) {
- // create second thread to listen for incoming messages from server
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- SocketHints socketHints = new SocketHints();
- socketHints.connectTimeout = 4000;
- socketHints.keepAlive = true;
- listenerSocket = Gdx.net.newClientSocket(Net.Protocol.TCP, HOST, 6079, socketHints);
- serverClientOutputStream = listenerSocket.getOutputStream();
- serverClientInputStream = listenerSocket.getInputStream();
- int result = 0;
- while (result != -1) {
- try {
- result = serverClientInputStream.read();
- } catch (Exception e) {
- Gdx.app.log(TAG, "Server - Client Connection lost. Try to reconnect");
- reconnect(); // start reconnection
- Thread.currentThread().interrupt();
- }
- if (result != -1) {
- switch (result) {
- ...
- }
- }
- }
- }
- }
- }).start();
- }
- }
- private void reconnect() {
- cleanUp();
- ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
- reConnectFuture = scheduledExecutorService.scheduleAtFixedRate(reConnectorRunnable, RECONNECT_INTERVAL, RECONNECT_INTERVAL, TimeUnit.SECONDS);
- }
- private Runnable reConnectorRunnable = new Runnable() {
- @Override
- public void run() {
- try {
- initializeServerCommunication();
- stopReconnectionService();
- if (errorHandlingListener != null) {
- errorHandlingListener.reconnected();
- } else {
- Gdx.app.error(TAG, "Reconnected but could not inform the app about it.");
- }
- } catch (GdxRuntimeException r) {
- reconnectAttemptsLeft--;
- cleanUp();
- if (reconnectAttemptsLeft <= 0) {
- stopReconnectionService();
- }
- } catch (Exception e) {
- reconnectAttemptsLeft--;
- cleanUp();
- if (reconnectAttemptsLeft <= 0) {
- stopReconnectionService();
- }
- }
- }
- };
- private void stopReconnectionService() {
- reconnectAttemptsLeft = MAX_RECONNECT_ATTEMPTS;
- reConnectFuture.cancel(true);
- }
- private void cleanUp() {
- if (inputStream != null) {
- try {
- inputStream.close();
- } catch (IOException i) {
- Gdx.app.log(TAG, "Could not close client - server input stream");
- }
- inputStream = null;
- }
- if (outputStream != null) {
- try {
- outputStream.close();
- } catch (IOException e) {
- Gdx.app.log(TAG, "Could not close client - server output stream");
- }
- outputStream = null;
- }
- if (serverClientInputStream != null) {
- try {
- serverClientInputStream.close();
- } catch (IOException e) {
- Gdx.app.log(TAG, "Could not close server - client input stream");
- }
- serverClientInputStream = null;
- }
- if (serverClientOutputStream != null) {
- try {
- serverClientOutputStream.close();
- } catch (IOException e) {
- Gdx.app.log(TAG, "Could not close server - client output stream");
- }
- serverClientOutputStream = null;
- }
- if (clientSocket != null) {
- clientSocket.dispose();
- }
- clientSocket = null;
- if (listenerSocket != null) {
- listenerSocket.dispose();
- }
- listenerSocket = null;
- }
Add Comment
Please, Sign In to add comment