1 package org.hedgewars.hedgeroid.netplay; |
1 package org.hedgewars.hedgeroid.netplay; |
2 |
2 |
3 import java.io.File; |
|
4 import java.io.FileNotFoundException; |
|
5 import java.io.IOException; |
3 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 |
4 |
15 import android.app.Service; |
5 import android.app.Service; |
16 import android.content.Intent; |
6 import android.content.Intent; |
17 import android.os.Binder; |
7 import android.os.Binder; |
18 import android.os.CountDownTimer; |
8 import android.os.CountDownTimer; |
19 import android.os.IBinder; |
9 import android.os.IBinder; |
20 |
10 |
21 public class NetplayService extends Service { |
11 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(); |
12 private final NetplayBinder binder = new NetplayBinder(); |
28 public NetconnPtr netconn; |
13 public Netconn netconn; |
29 private CountDownTimer timer; |
14 private CountDownTimer timer; |
30 private String playerName; |
|
31 |
15 |
32 @Override |
16 @Override |
33 public IBinder onBind(Intent intent) { |
17 public IBinder onBind(Intent intent) { |
34 return binder; |
18 return binder; |
35 } |
19 } |
36 |
20 |
37 @Override |
21 @Override |
38 public void onCreate() { |
22 public void onCreate() { |
39 if(FRONTLIB.flib_init() != 0) { |
23 if(Flib.INSTANCE.flib_init() != 0) { |
40 throw new RuntimeException("Unable to start frontlib"); |
24 throw new RuntimeException("Unable to start frontlib"); |
41 } |
25 } |
|
26 try { |
|
27 netconn = new Netconn(getApplicationContext(), "AndroidTester"); |
|
28 } catch (IOException e) { |
|
29 // TODO better handling |
|
30 throw new RuntimeException("Unable to start frontlib"); |
|
31 } |
|
32 timer = new CountDownTimer(Long.MAX_VALUE, 50) { |
|
33 @Override |
|
34 public void onTick(long millisUntilFinished) { |
|
35 if(netconn != null) { |
|
36 netconn.tick(); |
|
37 } |
|
38 } |
|
39 |
|
40 @Override |
|
41 public void onFinish() { |
|
42 } |
|
43 }; |
|
44 timer.start(); |
42 } |
45 } |
43 |
46 |
44 @Override |
47 @Override |
45 public void onDestroy() { |
48 public void onDestroy() { |
46 disconnect(); |
49 netconn.disconnect(); |
47 FRONTLIB.flib_quit(); |
50 Flib.INSTANCE.flib_quit(); |
48 } |
51 } |
49 |
52 |
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 { |
53 public class NetplayBinder extends Binder { |
109 NetplayService getService() { |
54 Netconn getNetconn() { |
110 return NetplayService.this; |
55 return netconn; |
111 } |
56 } |
112 } |
57 } |
113 |
58 |
114 public String getPlayerName() { |
59 public boolean isConnected() { |
115 return playerName; |
60 return netconn!=null; |
116 } |
61 } |
117 } |
62 } |