Attempt to prevent oom after restarting too many times hedgeroid
authorXeli
Sun, 25 Sep 2011 18:22:12 +0200
branchhedgeroid
changeset 5943 5b2b86a37089
parent 5941 db4751cb4f53
child 6023 a28be05b20bc
Attempt to prevent oom after restarting too many times
project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/EngineProtocol/EngineProtocolNetwork.java
project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/SDLActivity.java
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/EngineProtocol/EngineProtocolNetwork.java	Sun Sep 25 18:19:45 2011 +0200
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/EngineProtocol/EngineProtocolNetwork.java	Sun Sep 25 18:22:12 2011 +0200
@@ -26,7 +26,7 @@
 import java.net.Socket;
 import java.net.UnknownHostException;
 
-public class EngineProtocolNetwork implements Runnable{
+public class EngineProtocolNetwork extends Thread{
 
 	public static final String GAMEMODE_LOCAL = "TL";
 	public static final String GAMEMODE_DEMO = "TD";
@@ -43,6 +43,7 @@
 	private OutputStream output;
 	public int port;
 	private final GameConfig config;
+	private boolean clientQuit = false;
 
 	public EngineProtocolNetwork(GameConfig _config){
 		config = _config;
@@ -64,12 +65,12 @@
 	}
 	
 	private void gameIPC(){
+		Socket sock = null;
 		try{
-			Socket sock = serverSocket.accept();
+			sock = serverSocket.accept();
 			input = sock.getInputStream();
 			output = sock.getOutputStream();
 			
-			boolean clientQuit = false;
 			int msgSize = 0;
 			byte[] buffer = new byte[BUFFER_SIZE];
 
@@ -80,7 +81,7 @@
 				msgSize = buffer[0];
 
 				input.read(buffer, 0, msgSize);
-
+				System.out.println("IPC" + (char)buffer[0] + " : " + new String(buffer, 1,msgSize-1, "US_ASCII"));
 				switch(buffer[0]){
 				case 'C'://game init
 					config.sendToEngine(this);
@@ -88,11 +89,8 @@
 				case '?'://ping - pong
 					sendToEngine("!");
 					break;
-				case 'E'://error - quits game
-
-					break;
-				case 'e':
-
+				case 'e'://Send protocol version
+					System.out.println(new String(buffer));
 					break;
 				case 'i'://game statistics
 					switch(buffer[1]){
@@ -117,21 +115,29 @@
 					case 'B'://turn skipped
 						break;
 					default:
-
 					}
 					break;
+				case 'E'://error - quits game
+					System.out.println(new String(buffer));
+					return;
 				case 'q'://game ended remove save file
 
-					break;
+				    return;
 				case 'Q'://game ended but not finished
 
-					break;
+					return;
 				}
 
 			}
-
 		}catch(IOException e){
 			e.printStackTrace();
+		}finally{
+			try {
+				if(sock != null) sock.close();
+			} catch (IOException e) {}
+			try{
+				if(serverSocket != null) serverSocket.close();
+			} catch (IOException e) {}
 		}
 	}
 
@@ -144,8 +150,10 @@
 		} catch (IOException e) {
 			e.printStackTrace();
 		}
-		
-		
+	}
+	
+	public void quitIPC(){
+		clientQuit = true;
 	}
 	
 }
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/SDLActivity.java	Sun Sep 25 18:19:45 2011 +0200
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/mobile/SDLActivity.java	Sun Sep 25 18:22:12 2011 +0200
@@ -285,6 +285,12 @@
 				String.valueOf(surfaceWidth), String.valueOf(surfaceHeight),
 				"0", "null", "xeli", "1", "1", "1", "0", "", path });
 
+		try {
+			ipc.quitIPC();
+			ipc.join();
+		} catch (InterruptedException e) {
+			e.printStackTrace();
+		}
 		//Log.v("SDL", "SDL thread terminated");
 		SDLActivity.mSingleton.finish();
 	}
@@ -351,7 +357,7 @@
 			}
 			mSDLThread = null;
 
-			// Log.v("SDL", "Finished waiting for SDL thread");
+			Log.v("SDL", "Finished waiting for SDL thread");
 		}
 
 		//enableSensor(Sensor.TYPE_ACCELEROMETER, false);