author | unc0rr |
Sun, 14 Oct 2018 23:16:11 +0200 | |
changeset 13937 | cd437d76978a |
parent 10017 | de822cd3df3a |
permissions | -rw-r--r-- |
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
1 |
/* |
7584
7831c84cc644
License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents:
7582
diff
changeset
|
2 |
* Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game |
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
3 |
* Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com> |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
4 |
* |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
5 |
* This program is free software; you can redistribute it and/or |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
6 |
* modify it under the terms of the GNU General Public License |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
7 |
* as published by the Free Software Foundation; either version 2 |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
8 |
* of the License, or (at your option) any later version. |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
9 |
* |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
10 |
* This program is distributed in the hope that it will be useful, |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
13 |
* GNU General Public License for more details. |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
14 |
* |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
16 |
* along with this program; if not, write to the Free Software |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
17 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
18 |
*/ |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
19 |
|
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
20 |
package org.hedgewars.hedgeroid; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
21 |
|
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
22 |
import java.net.ConnectException; |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
23 |
|
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
24 |
import org.hedgewars.hedgeroid.Datastructures.GameConfig; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
25 |
import org.hedgewars.hedgeroid.frontlib.Flib; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
26 |
import org.hedgewars.hedgeroid.frontlib.Frontlib; |
7588 | 27 |
import org.hedgewars.hedgeroid.frontlib.Frontlib.ByteArrayPtr; |
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
28 |
import org.hedgewars.hedgeroid.frontlib.Frontlib.BytesCallback; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
29 |
import org.hedgewars.hedgeroid.frontlib.Frontlib.GameSetupPtr; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
30 |
import org.hedgewars.hedgeroid.frontlib.Frontlib.GameconnPtr; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
31 |
import org.hedgewars.hedgeroid.frontlib.Frontlib.IntCallback; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
32 |
import org.hedgewars.hedgeroid.frontlib.Frontlib.StrBoolCallback; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
33 |
import org.hedgewars.hedgeroid.frontlib.Frontlib.StrCallback; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
34 |
import org.hedgewars.hedgeroid.frontlib.Frontlib.VoidCallback; |
7558
983ff426f91e
Hedgeroid: Fixed JNA mapping of size_t
Medo <smaxein@googlemail.com>
parents:
7508
diff
changeset
|
35 |
import org.hedgewars.hedgeroid.frontlib.NativeSizeT; |
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
36 |
import org.hedgewars.hedgeroid.netplay.GameMessageListener; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
37 |
import org.hedgewars.hedgeroid.netplay.Netplay; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
38 |
import org.hedgewars.hedgeroid.util.TickHandler; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
39 |
|
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
40 |
import android.os.Handler; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
41 |
import android.os.HandlerThread; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
42 |
import android.os.Looper; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
43 |
import android.util.Log; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
44 |
|
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
45 |
import com.sun.jna.Pointer; |
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
46 |
|
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
47 |
/** |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
48 |
* This class handles both talking to the engine (IPC) for running a game, and |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
49 |
* coordinating with the netconn if it is a netgame, using the frontlib for the |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
50 |
* actual IPC networking communication. |
10017 | 51 |
* |
7582
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
52 |
* After creating the GameConnection object, it will communicate with the engine |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
53 |
* on its own thread. It shuts itself down as soon as the connection to the engine |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
54 |
* is lost. |
714310efad8f
Hedgeroid: Final sprint to the deadline
Medo <smaxein@googlemail.com>
parents:
7558
diff
changeset
|
55 |
*/ |
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
56 |
public final class GameConnection { |
10017 | 57 |
private static final Handler mainHandler = new Handler(Looper.getMainLooper()); |
58 |
||
59 |
public final int port; |
|
60 |
private final HandlerThread thread; |
|
61 |
private final Handler handler; |
|
62 |
private TickHandler tickHandler; |
|
63 |
private final Netplay netplay; // ==null if not a netgame |
|
64 |
private GameconnPtr conn; |
|
65 |
||
66 |
private GameConnection(GameconnPtr conn, Netplay netplay) { |
|
67 |
this.conn = conn; |
|
68 |
this.port = Flib.INSTANCE.flib_gameconn_getport(conn); |
|
69 |
this.netplay = netplay; |
|
70 |
this.thread = new HandlerThread("IPCThread"); |
|
71 |
thread.start(); |
|
72 |
this.handler = new Handler(thread.getLooper()); |
|
73 |
} |
|
74 |
||
75 |
private void setupConnection() { |
|
76 |
tickHandler = new TickHandler(thread.getLooper(), 50, tickCb); |
|
77 |
tickHandler.start(); |
|
78 |
||
79 |
if(netplay != null) { |
|
80 |
mainHandler.post(new Runnable() { |
|
81 |
public void run() { |
|
82 |
netplay.registerGameMessageListener(gameMessageListener); |
|
83 |
} |
|
84 |
}); |
|
85 |
Flib.INSTANCE.flib_gameconn_onChat(conn, chatCb, null); |
|
86 |
Flib.INSTANCE.flib_gameconn_onEngineMessage(conn, engineMessageCb, null); |
|
87 |
} |
|
88 |
Flib.INSTANCE.flib_gameconn_onConnect(conn, connectCb, null); |
|
89 |
Flib.INSTANCE.flib_gameconn_onDisconnect(conn, disconnectCb, null); |
|
90 |
Flib.INSTANCE.flib_gameconn_onErrorMessage(conn, errorMessageCb, null); |
|
91 |
} |
|
92 |
||
93 |
/** |
|
94 |
* Start a new IPC server to communicate with the engine. |
|
95 |
* Performs networking operations, don't run on the UI thread. |
|
96 |
* @throws ConnectException if we can't set up the IPC server |
|
97 |
*/ |
|
98 |
public static GameConnection forNetgame(final GameConfig config, Netplay netplay) throws ConnectException { |
|
99 |
final String playerName = netplay.getPlayerName(); |
|
100 |
GameconnPtr conn = Flib.INSTANCE.flib_gameconn_create(playerName, GameSetupPtr.createJavaOwned(config), true); |
|
101 |
if(conn == null) { |
|
102 |
throw new ConnectException(); |
|
103 |
} |
|
104 |
GameConnection result = new GameConnection(conn, netplay); |
|
105 |
result.setupConnection(); |
|
106 |
return result; |
|
107 |
} |
|
108 |
||
109 |
/** |
|
110 |
* Start a new IPC server to communicate with the engine. |
|
111 |
* Performs networking operations, don't run on the UI thread. |
|
112 |
* @throws ConnectException if we can't set up the IPC server |
|
113 |
*/ |
|
114 |
public static GameConnection forLocalGame(final GameConfig config) throws ConnectException { |
|
115 |
GameconnPtr conn = Flib.INSTANCE.flib_gameconn_create("Player", GameSetupPtr.createJavaOwned(config), false); |
|
116 |
if(conn == null) { |
|
117 |
throw new ConnectException(); |
|
118 |
} |
|
119 |
GameConnection result = new GameConnection(conn, null); |
|
120 |
result.setupConnection(); |
|
121 |
return result; |
|
122 |
} |
|
123 |
||
124 |
private final Runnable tickCb = new Runnable() { |
|
125 |
public void run() { |
|
126 |
Flib.INSTANCE.flib_gameconn_tick(conn); |
|
127 |
} |
|
128 |
}; |
|
129 |
||
130 |
// runs on the IPCThread |
|
131 |
private void shutdown() { |
|
132 |
tickHandler.stop(); |
|
133 |
thread.quit(); |
|
134 |
Flib.INSTANCE.flib_gameconn_destroy(conn); |
|
135 |
conn = null; |
|
136 |
if(netplay != null) { |
|
137 |
mainHandler.post(new Runnable() { |
|
138 |
public void run() { |
|
139 |
netplay.unregisterGameMessageListener(gameMessageListener); |
|
140 |
} |
|
141 |
}); |
|
142 |
} |
|
143 |
} |
|
144 |
||
145 |
// runs on the IPCThread |
|
146 |
private final StrBoolCallback chatCb = new StrBoolCallback() { |
|
147 |
public void callback(Pointer context, String message, boolean teamChat) { |
|
148 |
if(teamChat) { |
|
149 |
netplay.sendTeamChat(message); |
|
150 |
} else { |
|
151 |
netplay.sendChat(message); |
|
152 |
} |
|
153 |
} |
|
154 |
}; |
|
155 |
||
156 |
// runs on the IPCThread |
|
157 |
private final VoidCallback connectCb = new VoidCallback() { |
|
158 |
public void callback(Pointer context) { |
|
159 |
Log.i("GameConnection", "Connected"); |
|
160 |
} |
|
161 |
}; |
|
162 |
||
163 |
// runs on the IPCThread |
|
164 |
private final IntCallback disconnectCb = new IntCallback() { |
|
165 |
public void callback(Pointer context, int reason) { |
|
166 |
if(netplay != null) { |
|
167 |
netplay.sendRoundFinished(reason==Frontlib.GAME_END_FINISHED); |
|
168 |
} |
|
169 |
shutdown(); |
|
170 |
} |
|
171 |
}; |
|
172 |
||
173 |
// runs on the IPCThread |
|
174 |
private final BytesCallback engineMessageCb = new BytesCallback() { |
|
175 |
public void callback(Pointer context, ByteArrayPtr buffer, NativeSizeT size) { |
|
176 |
netplay.sendEngineMessage(buffer.deref(size.intValue())); |
|
177 |
} |
|
178 |
}; |
|
179 |
||
180 |
// runs on the IPCThread |
|
181 |
private final StrCallback errorMessageCb = new StrCallback() { |
|
182 |
public void callback(Pointer context, String message) { |
|
183 |
Log.e("GameConnection", message); |
|
184 |
} |
|
185 |
}; |
|
186 |
||
187 |
// runs on any thread |
|
188 |
private final GameMessageListener gameMessageListener = new GameMessageListener() { |
|
189 |
public void onNetDisconnected() { |
|
190 |
handler.post(new Runnable() { |
|
191 |
public void run() { |
|
192 |
Flib.INSTANCE.flib_gameconn_send_quit(conn); |
|
193 |
} |
|
194 |
}); |
|
195 |
} |
|
196 |
||
197 |
public void onMessage(final int type, final String message) { |
|
198 |
handler.post(new Runnable() { |
|
199 |
public void run() { |
|
200 |
Flib.INSTANCE.flib_gameconn_send_textmsg(conn, type, message); |
|
201 |
} |
|
202 |
}); |
|
203 |
} |
|
204 |
||
205 |
public void onEngineMessage(final byte[] em) { |
|
206 |
handler.post(new Runnable() { |
|
207 |
public void run() { |
|
208 |
ByteArrayPtr ptr = ByteArrayPtr.createJavaOwned(em); |
|
209 |
Flib.INSTANCE.flib_gameconn_send_enginemsg(conn, ptr, NativeSizeT.valueOf(em.length)); |
|
210 |
} |
|
211 |
}); |
|
212 |
} |
|
213 |
||
214 |
public void onChatMessage(final String nick, final String message) { |
|
215 |
handler.post(new Runnable() { |
|
216 |
public void run() { |
|
217 |
Flib.INSTANCE.flib_gameconn_send_chatmsg(conn, nick, message); |
|
218 |
} |
|
219 |
}); |
|
220 |
} |
|
221 |
}; |
|
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff
changeset
|
222 |
} |