project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/NetplayService.java
changeset 7330 867e4fda496e
child 7332 3f2e130f9715
equal deleted inserted replaced
7328:cd919a2ead4e 7330:867e4fda496e
       
     1 package org.hedgewars.hedgeroid.netplay;
       
     2 
       
     3 import java.io.File;
       
     4 import java.io.FileNotFoundException;
       
     5 import java.io.IOException;
       
     6 import java.util.Collections;
       
     7 
       
     8 import org.hedgewars.hedgeroid.Utils;
       
     9 import org.hedgewars.hedgeroid.netplay.JnaFrontlib.MetaschemePtr;
       
    10 import org.hedgewars.hedgeroid.netplay.JnaFrontlib.NetconnPtr;
       
    11 
       
    12 import com.sun.jna.Library;
       
    13 import com.sun.jna.Native;
       
    14 
       
    15 import android.app.Service;
       
    16 import android.content.Intent;
       
    17 import android.os.Binder;
       
    18 import android.os.CountDownTimer;
       
    19 import android.os.IBinder;
       
    20 
       
    21 public class NetplayService extends Service {
       
    22 	static {
       
    23 		System.loadLibrary("SDL_net");
       
    24 	}
       
    25 	public static final JnaFrontlib FRONTLIB = (JnaFrontlib)Native.loadLibrary("frontlib", JnaFrontlib.class, Collections.singletonMap(Library.OPTION_TYPE_MAPPER, FrontlibTypeMapper.INSTANCE));
       
    26 
       
    27 	private final NetplayBinder binder = new NetplayBinder();
       
    28 	public NetconnPtr netconn;
       
    29 	private CountDownTimer timer;
       
    30 	private String playerName;
       
    31 	
       
    32 	@Override
       
    33 	public IBinder onBind(Intent intent) {
       
    34 		return binder;
       
    35 	}
       
    36 	
       
    37 	@Override
       
    38 	public void onCreate() {
       
    39 		if(FRONTLIB.flib_init() != 0) {
       
    40 			throw new RuntimeException("Unable to start frontlib");
       
    41 		}
       
    42 	}
       
    43 	
       
    44 	@Override
       
    45 	public void onDestroy() {
       
    46 		disconnect();
       
    47 		FRONTLIB.flib_quit();
       
    48 	}
       
    49 
       
    50 	/**
       
    51 	 * Connect to the official Hedgewars server.
       
    52 	 * 
       
    53 	 * @throws IOException if the metascheme file can't be read or the connection to the server fails
       
    54 	 */
       
    55 	public void connect(String playerName) throws IOException {
       
    56 		connect(playerName, "140.247.62.101", 46631);
       
    57 	}
       
    58 	
       
    59 	/**
       
    60 	 * Connect to the server with the given hostname and port
       
    61 	 * 
       
    62 	 * @throws IOException if the metascheme file can't be read or the connection to the server fails
       
    63 	 */
       
    64 	public void connect(String playerName, String host, int port) throws IOException {
       
    65 		if(playerName == null) {
       
    66 			playerName = "Player";
       
    67 		}
       
    68 		this.playerName = playerName;
       
    69 		MetaschemePtr meta = null;
       
    70 		try {
       
    71 			String metaschemePath = new File(Utils.getDataPathFile(this), "metasettings.ini").getAbsolutePath();
       
    72 			meta = FRONTLIB.flib_metascheme_from_ini(metaschemePath);
       
    73 			if(meta == null) {
       
    74 				throw new RuntimeException("Missing metascheme");
       
    75 			}
       
    76 			netconn = FRONTLIB.flib_netconn_create(playerName, meta, Utils.getDataPathFile(this).getAbsolutePath(), host, port);
       
    77 	    	timer = new CountDownTimer(Long.MAX_VALUE, 50) {
       
    78 				@Override
       
    79 				public void onTick(long millisUntilFinished) {
       
    80 					if(netconn != null) {
       
    81 						FRONTLIB.flib_netconn_tick(netconn);
       
    82 					}
       
    83 				}
       
    84 				
       
    85 				@Override
       
    86 				public void onFinish() {
       
    87 				}
       
    88 			};
       
    89 			timer.start();
       
    90 		} catch(FileNotFoundException e) {
       
    91 			throw new RuntimeException(e);
       
    92 		} finally {
       
    93 			FRONTLIB.flib_metascheme_release(meta);
       
    94 		}
       
    95 	}
       
    96 	
       
    97 	public void disconnect() {
       
    98 		if(timer != null) {
       
    99 			timer.cancel();
       
   100 		}
       
   101 		if(netconn != null) {
       
   102 			FRONTLIB.flib_netconn_send_quit(netconn, "User quit");
       
   103 			FRONTLIB.flib_netconn_destroy(netconn);
       
   104 			netconn = null;
       
   105 		}
       
   106 	}
       
   107 	
       
   108 	public class NetplayBinder extends Binder {
       
   109 		NetplayService getService() {
       
   110             return NetplayService.this;
       
   111         }
       
   112 	}
       
   113 
       
   114 	public String getPlayerName() {
       
   115 		return playerName;
       
   116 	}
       
   117 }