project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/GameConnection.java
changeset 10017 de822cd3df3a
parent 7588 27e5857da6af
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/GameConnection.java	Tue Jan 21 22:38:13 2014 +0100
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/GameConnection.java	Tue Jan 21 22:43:06 2014 +0100
@@ -48,175 +48,175 @@
  * This class handles both talking to the engine (IPC) for running a game, and
  * coordinating with the netconn if it is a netgame, using the frontlib for the
  * actual IPC networking communication.
- * 
+ *
  * After creating the GameConnection object, it will communicate with the engine
  * on its own thread. It shuts itself down as soon as the connection to the engine
  * is lost.
  */
 public final class GameConnection {
-	private static final Handler mainHandler = new Handler(Looper.getMainLooper());
-	
-	public final int port;
-	private final HandlerThread thread;
-	private final Handler handler;
-	private TickHandler tickHandler;
-	private final Netplay netplay; // ==null if not a netgame
-	private GameconnPtr conn;
-	
-	private GameConnection(GameconnPtr conn, Netplay netplay) {
-		this.conn = conn;
-		this.port = Flib.INSTANCE.flib_gameconn_getport(conn);
-		this.netplay = netplay;
-		this.thread = new HandlerThread("IPCThread");
-		thread.start();
-		this.handler = new Handler(thread.getLooper());
-	}
-	
-	private void setupConnection() {
-		tickHandler = new TickHandler(thread.getLooper(), 50, tickCb);
-		tickHandler.start();
-		
-		if(netplay != null) {
-			mainHandler.post(new Runnable() {
-				public void run() { 
-					netplay.registerGameMessageListener(gameMessageListener);
-				}
-			});
-			Flib.INSTANCE.flib_gameconn_onChat(conn, chatCb, null);
-			Flib.INSTANCE.flib_gameconn_onEngineMessage(conn, engineMessageCb, null);
-		}
-		Flib.INSTANCE.flib_gameconn_onConnect(conn, connectCb, null);
-		Flib.INSTANCE.flib_gameconn_onDisconnect(conn, disconnectCb, null);
-		Flib.INSTANCE.flib_gameconn_onErrorMessage(conn, errorMessageCb, null);
-	}
-	
-	/**
-	 * Start a new IPC server to communicate with the engine.
-	 * Performs networking operations, don't run on the UI thread.
-	 * @throws ConnectException if we can't set up the IPC server
-	 */
-	public static GameConnection forNetgame(final GameConfig config, Netplay netplay) throws ConnectException {
-		final String playerName = netplay.getPlayerName();
-		GameconnPtr conn = Flib.INSTANCE.flib_gameconn_create(playerName, GameSetupPtr.createJavaOwned(config), true);
-		if(conn == null) {
-			throw new ConnectException();
-		}
-		GameConnection result = new GameConnection(conn, netplay);
-		result.setupConnection();
-		return result;
-	}
-	
-	/**
-	 * Start a new IPC server to communicate with the engine.
-	 * Performs networking operations, don't run on the UI thread.
-	 * @throws ConnectException if we can't set up the IPC server
-	 */
-	public static GameConnection forLocalGame(final GameConfig config) throws ConnectException {
-		GameconnPtr conn = Flib.INSTANCE.flib_gameconn_create("Player", GameSetupPtr.createJavaOwned(config), false);
-		if(conn == null) {
-			throw new ConnectException();
-		}
-		GameConnection result = new GameConnection(conn, null);
-		result.setupConnection();
-		return result;
-	}
-	
-	private final Runnable tickCb = new Runnable() {
-		public void run() {
-			Flib.INSTANCE.flib_gameconn_tick(conn);
-		}
-	};
-	
-	// runs on the IPCThread
-	private void shutdown() {
-		tickHandler.stop();
-		thread.quit();
-		Flib.INSTANCE.flib_gameconn_destroy(conn);
-		conn = null;
-		if(netplay != null) {
-			mainHandler.post(new Runnable() {
-				public void run() {
-					netplay.unregisterGameMessageListener(gameMessageListener);
-				}
-			});
-		}
-	}
-	
-	// runs on the IPCThread
-	private final StrBoolCallback chatCb = new StrBoolCallback() {
-		public void callback(Pointer context, String message, boolean teamChat) {
-			if(teamChat) {
-				netplay.sendTeamChat(message);
-			} else {
-				netplay.sendChat(message);
-			}
-		}
-	};
-	
-	// runs on the IPCThread
-	private final VoidCallback connectCb = new VoidCallback() {
-		public void callback(Pointer context) {
-			Log.i("GameConnection", "Connected");
-		}
-	};
-	
-	// runs on the IPCThread
-	private final IntCallback disconnectCb = new IntCallback() {
-		public void callback(Pointer context, int reason) {
-			if(netplay != null) {
-				netplay.sendRoundFinished(reason==Frontlib.GAME_END_FINISHED);
-			}
-			shutdown();
-		}
-	};
-	
-	// runs on the IPCThread
-	private final BytesCallback engineMessageCb = new BytesCallback() {
-		public void callback(Pointer context, ByteArrayPtr buffer, NativeSizeT size) {
-			netplay.sendEngineMessage(buffer.deref(size.intValue()));
-		}
-	};
-	
-	// runs on the IPCThread
-	private final StrCallback errorMessageCb = new StrCallback() {
-		public void callback(Pointer context, String message) {
-			Log.e("GameConnection", message);
-		}
-	};
-	
-	// runs on any thread
-	private final GameMessageListener gameMessageListener = new GameMessageListener() {
-		public void onNetDisconnected() {
-			handler.post(new Runnable() {
-				public void run() {
-					Flib.INSTANCE.flib_gameconn_send_quit(conn);
-				}
-			});
-		}
-		
-		public void onMessage(final int type, final String message) {
-			handler.post(new Runnable() {
-				public void run() {
-					Flib.INSTANCE.flib_gameconn_send_textmsg(conn, type, message);
-				}
-			});
-		}
-		
-		public void onEngineMessage(final byte[] em) {
-			handler.post(new Runnable() {
-				public void run() {
-					ByteArrayPtr ptr = ByteArrayPtr.createJavaOwned(em);
-					Flib.INSTANCE.flib_gameconn_send_enginemsg(conn, ptr, NativeSizeT.valueOf(em.length));
-				}
-			});
-		}
-		
-		public void onChatMessage(final String nick, final String message) {
-			handler.post(new Runnable() {
-				public void run() {
-					Flib.INSTANCE.flib_gameconn_send_chatmsg(conn, nick, message);
-				}
-			});
-		}
-	};
+    private static final Handler mainHandler = new Handler(Looper.getMainLooper());
+
+    public final int port;
+    private final HandlerThread thread;
+    private final Handler handler;
+    private TickHandler tickHandler;
+    private final Netplay netplay; // ==null if not a netgame
+    private GameconnPtr conn;
+
+    private GameConnection(GameconnPtr conn, Netplay netplay) {
+        this.conn = conn;
+        this.port = Flib.INSTANCE.flib_gameconn_getport(conn);
+        this.netplay = netplay;
+        this.thread = new HandlerThread("IPCThread");
+        thread.start();
+        this.handler = new Handler(thread.getLooper());
+    }
+
+    private void setupConnection() {
+        tickHandler = new TickHandler(thread.getLooper(), 50, tickCb);
+        tickHandler.start();
+
+        if(netplay != null) {
+            mainHandler.post(new Runnable() {
+                public void run() {
+                    netplay.registerGameMessageListener(gameMessageListener);
+                }
+            });
+            Flib.INSTANCE.flib_gameconn_onChat(conn, chatCb, null);
+            Flib.INSTANCE.flib_gameconn_onEngineMessage(conn, engineMessageCb, null);
+        }
+        Flib.INSTANCE.flib_gameconn_onConnect(conn, connectCb, null);
+        Flib.INSTANCE.flib_gameconn_onDisconnect(conn, disconnectCb, null);
+        Flib.INSTANCE.flib_gameconn_onErrorMessage(conn, errorMessageCb, null);
+    }
+
+    /**
+     * Start a new IPC server to communicate with the engine.
+     * Performs networking operations, don't run on the UI thread.
+     * @throws ConnectException if we can't set up the IPC server
+     */
+    public static GameConnection forNetgame(final GameConfig config, Netplay netplay) throws ConnectException {
+        final String playerName = netplay.getPlayerName();
+        GameconnPtr conn = Flib.INSTANCE.flib_gameconn_create(playerName, GameSetupPtr.createJavaOwned(config), true);
+        if(conn == null) {
+            throw new ConnectException();
+        }
+        GameConnection result = new GameConnection(conn, netplay);
+        result.setupConnection();
+        return result;
+    }
+
+    /**
+     * Start a new IPC server to communicate with the engine.
+     * Performs networking operations, don't run on the UI thread.
+     * @throws ConnectException if we can't set up the IPC server
+     */
+    public static GameConnection forLocalGame(final GameConfig config) throws ConnectException {
+        GameconnPtr conn = Flib.INSTANCE.flib_gameconn_create("Player", GameSetupPtr.createJavaOwned(config), false);
+        if(conn == null) {
+            throw new ConnectException();
+        }
+        GameConnection result = new GameConnection(conn, null);
+        result.setupConnection();
+        return result;
+    }
+
+    private final Runnable tickCb = new Runnable() {
+        public void run() {
+            Flib.INSTANCE.flib_gameconn_tick(conn);
+        }
+    };
+
+    // runs on the IPCThread
+    private void shutdown() {
+        tickHandler.stop();
+        thread.quit();
+        Flib.INSTANCE.flib_gameconn_destroy(conn);
+        conn = null;
+        if(netplay != null) {
+            mainHandler.post(new Runnable() {
+                public void run() {
+                    netplay.unregisterGameMessageListener(gameMessageListener);
+                }
+            });
+        }
+    }
+
+    // runs on the IPCThread
+    private final StrBoolCallback chatCb = new StrBoolCallback() {
+        public void callback(Pointer context, String message, boolean teamChat) {
+            if(teamChat) {
+                netplay.sendTeamChat(message);
+            } else {
+                netplay.sendChat(message);
+            }
+        }
+    };
+
+    // runs on the IPCThread
+    private final VoidCallback connectCb = new VoidCallback() {
+        public void callback(Pointer context) {
+            Log.i("GameConnection", "Connected");
+        }
+    };
+
+    // runs on the IPCThread
+    private final IntCallback disconnectCb = new IntCallback() {
+        public void callback(Pointer context, int reason) {
+            if(netplay != null) {
+                netplay.sendRoundFinished(reason==Frontlib.GAME_END_FINISHED);
+            }
+            shutdown();
+        }
+    };
+
+    // runs on the IPCThread
+    private final BytesCallback engineMessageCb = new BytesCallback() {
+        public void callback(Pointer context, ByteArrayPtr buffer, NativeSizeT size) {
+            netplay.sendEngineMessage(buffer.deref(size.intValue()));
+        }
+    };
+
+    // runs on the IPCThread
+    private final StrCallback errorMessageCb = new StrCallback() {
+        public void callback(Pointer context, String message) {
+            Log.e("GameConnection", message);
+        }
+    };
+
+    // runs on any thread
+    private final GameMessageListener gameMessageListener = new GameMessageListener() {
+        public void onNetDisconnected() {
+            handler.post(new Runnable() {
+                public void run() {
+                    Flib.INSTANCE.flib_gameconn_send_quit(conn);
+                }
+            });
+        }
+
+        public void onMessage(final int type, final String message) {
+            handler.post(new Runnable() {
+                public void run() {
+                    Flib.INSTANCE.flib_gameconn_send_textmsg(conn, type, message);
+                }
+            });
+        }
+
+        public void onEngineMessage(final byte[] em) {
+            handler.post(new Runnable() {
+                public void run() {
+                    ByteArrayPtr ptr = ByteArrayPtr.createJavaOwned(em);
+                    Flib.INSTANCE.flib_gameconn_send_enginemsg(conn, ptr, NativeSizeT.valueOf(em.length));
+                }
+            });
+        }
+
+        public void onChatMessage(final String nick, final String message) {
+            handler.post(new Runnable() {
+                public void run() {
+                    Flib.INSTANCE.flib_gameconn_send_chatmsg(conn, nick, message);
+                }
+            });
+        }
+    };
 }