project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/Netplay.java
author Medo <smaxein@googlemail.com>
Thu, 26 Jul 2012 11:01:32 +0200
changeset 7358 57a508884052
child 7461 38acbfdb484f
permissions -rw-r--r--
Hedgeroid: Major overhaul of the network connection code. Now it is threaded and errors are handled properly.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7358
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
     1
package org.hedgewars.hedgeroid.netplay;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
     2
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
     3
import java.io.File;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
     4
import java.io.FileNotFoundException;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
     5
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
     6
import org.hedgewars.hedgeroid.R;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
     7
import org.hedgewars.hedgeroid.Utils;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
     8
import org.hedgewars.hedgeroid.netplay.JnaFrontlib.IntStrCallback;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
     9
import org.hedgewars.hedgeroid.netplay.JnaFrontlib.MetaschemePtr;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    10
import org.hedgewars.hedgeroid.netplay.JnaFrontlib.NetconnPtr;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    11
import org.hedgewars.hedgeroid.netplay.JnaFrontlib.RoomArrayPtr;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    12
import org.hedgewars.hedgeroid.netplay.JnaFrontlib.RoomCallback;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    13
import org.hedgewars.hedgeroid.netplay.JnaFrontlib.RoomListCallback;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    14
import org.hedgewars.hedgeroid.netplay.JnaFrontlib.RoomPtr;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    15
import org.hedgewars.hedgeroid.netplay.JnaFrontlib.StrCallback;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    16
import org.hedgewars.hedgeroid.netplay.JnaFrontlib.StrRoomCallback;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    17
import org.hedgewars.hedgeroid.netplay.JnaFrontlib.StrStrCallback;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    18
import org.hedgewars.hedgeroid.netplay.JnaFrontlib.VoidCallback;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    19
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
import android.content.Context;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
import android.content.Intent;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
import android.content.res.Resources;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
import android.os.Handler;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
import android.os.HandlerThread;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
import android.os.Looper;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
import android.os.Message;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
import android.support.v4.content.LocalBroadcastManager;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
import android.util.Log;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
import android.util.Pair;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
import com.sun.jna.Pointer;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    33
/**
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
 * This class manages the application's networking state.
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
 */
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
public class Netplay {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
	public static enum State { NOT_CONNECTED, CONNECTING, LOBBY, ROOM, INGAME }
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    38
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
	// Extras in broadcasts
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
	public static final String EXTRA_PLAYERNAME = "playerName";
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
	public static final String EXTRA_MESSAGE = "message";
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
	public static final String EXTRA_HAS_ERROR = "hasError";
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
	private static final String ACTIONPREFIX = "org.hedgewars.hedgeroid.netconn.";
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
	public static final String ACTION_DISCONNECTED = ACTIONPREFIX+"DISCONNECTED";
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
	public static final String ACTION_CONNECTED = ACTIONPREFIX+"CONNECTED";
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
	public static final String ACTION_PASSWORD_REQUESTED = ACTIONPREFIX+"PASSWORD_REQUESTED";
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
	public static final String DEFAULT_SERVER = "netserver.hedgewars.org";
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
	public static final int DEFAULT_PORT = 46631;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
	private final Context appContext;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    53
	private final LocalBroadcastManager broadcastManager;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    54
	private final FromNetHandler fromNetHandler = new FromNetHandler();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
	private State state;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
	private int foregroundUsers = 0;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
	// null if there is no running connection (==state is NOT_CONNECTED)
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
	private ThreadedNetConnection connection;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    61
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
	public final PlayerList playerList = new PlayerList();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
	public final RoomList roomList = new RoomList();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
	public final MessageLog lobbyChatlog;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
	public final MessageLog roomChatlog;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
	public Netplay(Context appContext) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
		this.appContext = appContext;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
		broadcastManager = LocalBroadcastManager.getInstance(appContext);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
		lobbyChatlog = new MessageLog(appContext);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
		roomChatlog = new MessageLog(appContext);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
		state = State.NOT_CONNECTED;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    74
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
	private void clearState() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
		playerList.clear();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
		roomList.clear();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
		lobbyChatlog.clear();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
		roomChatlog.clear();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    81
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
	public void connectToDefaultServer(String playerName) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    83
		connect(playerName, DEFAULT_SERVER, DEFAULT_PORT);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    84
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    85
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    86
	/**
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    87
	 * Establish a new connection. Only call if the current state is NOT_CONNECTED.
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    88
	 * 
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    89
	 * The state will switch to CONNECTING immediately. After that, it can asynchronously change to any other state.
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    90
	 * State changes are indicated by broadcasts. In particular, if an error occurs while trying to connect, the state
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    91
	 * will change back to NOT_CONNECTED and an ACTION_DISCONNECTED broadcast is sent.
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    92
	 */
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    93
	public void connect(String name, String host, int port) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    94
		if(state != State.NOT_CONNECTED) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    95
			throw new IllegalStateException("Attempt to start a new connection while the old one was still running.");
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    96
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    97
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    98
		clearState();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
    99
		state = State.CONNECTING;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   100
		connection = ThreadedNetConnection.startConnection(appContext, fromNetHandler, name, host, port);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   101
		connection.setFastTickRate(foregroundUsers > 0);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   102
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   103
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   104
	public void sendNick(String nick) { sendToNet(ThreadedNetConnection.ToNetHandler.MSG_SEND_NICK, nick); }
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   105
	public void sendPassword(String password) { sendToNet(ThreadedNetConnection.ToNetHandler.MSG_SEND_PASSWORD, password); }
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   106
	public void sendQuit(String message) { sendToNet(ThreadedNetConnection.ToNetHandler.MSG_SEND_QUIT, message); }
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   107
	public void sendRoomlistRequest() { sendToNet(ThreadedNetConnection.ToNetHandler.MSG_SEND_ROOMLIST_REQUEST); }
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   108
	public void sendPlayerInfoQuery(String name) { sendToNet(ThreadedNetConnection.ToNetHandler.MSG_SEND_PLAYER_INFO_REQUEST, name); }
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   109
	public void sendChat(final String s) { sendToNet(ThreadedNetConnection.ToNetHandler.MSG_SEND_CHAT, s); }
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   110
	public void disconnect() { sendToNet(ThreadedNetConnection.ToNetHandler.MSG_DISCONNECT, "User Quit"); }
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   111
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   112
	private static Netplay instance;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   113
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   114
	/**
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   115
	 * Retrieve the single app-wide instance of the netplay interface, creating it if it
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   116
	 * does not exist yet.
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   117
	 * 
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   118
	 * @param applicationContext
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   119
	 * @return
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   120
	 */
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   121
	public static Netplay getAppInstance(Context applicationContext) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   122
		if(instance == null) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   123
			// We'll just do it here and never quit it again...
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   124
			if(Flib.INSTANCE.flib_init() != 0) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   125
				throw new RuntimeException("Unable to start frontlib");
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   126
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   127
			instance = new Netplay(applicationContext);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   128
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   129
		return instance;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   130
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   131
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   132
	public State getState() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   133
		return state;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   134
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   135
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   136
	/**
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   137
	 * Indicate that you want network messages to be checked regularly (several times per second).
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   138
	 * As long as nobody requests fast ticks, the network is only checked once every few seconds
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   139
	 * to conserve battery power.
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   140
	 * Once you no longer need fast updates, call unrequestFastTicks.
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   141
	 */
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   142
	public void requestFastTicks() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   143
		if(foregroundUsers == Integer.MAX_VALUE) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   144
			throw new RuntimeException("Reference counter overflow");
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   145
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   146
		if(foregroundUsers == 0 && connection != null) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   147
			connection.setFastTickRate(true);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   148
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   149
		foregroundUsers++;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   150
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   151
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   152
	public void unrequestFastTicks() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   153
		if(foregroundUsers == 0) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   154
			throw new RuntimeException("Reference counter underflow");
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   155
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   156
		foregroundUsers--;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   157
		if(foregroundUsers == 0 && connection != null) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   158
			connection.setFastTickRate(false);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   159
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   160
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   161
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   162
	private boolean sendToNet(int what) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   163
		if(connection != null) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   164
			Handler handler = connection.toNetHandler;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   165
			return handler.sendMessage(handler.obtainMessage(what));
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   166
		} else {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   167
			return false;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   168
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   169
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   170
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   171
	private boolean sendToNet(int what, Object obj) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   172
		if(connection != null) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   173
			Handler handler = connection.toNetHandler;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   174
			return handler.sendMessage(handler.obtainMessage(what, obj));
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   175
		} else {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   176
			return false;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   177
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   178
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   179
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   180
	private MessageLog getCurrentLog() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   181
		if(state == State.ROOM || state == State.INGAME) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   182
			return roomChatlog;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   183
		} else {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   184
			return lobbyChatlog;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   185
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   186
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   187
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   188
	/**
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   189
	 * Processes messages from the networking system. Always runs on the main thread.
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   190
	 */
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   191
	final class FromNetHandler extends Handler {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   192
		public static final int MSG_LOBBY_JOIN = 0;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   193
		public static final int MSG_LOBBY_LEAVE = 1;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   194
		public static final int MSG_CHAT = 2;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   195
		public static final int MSG_MESSAGE = 3;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   196
		public static final int MSG_ROOM_ADD = 4;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   197
		public static final int MSG_ROOM_UPDATE = 5;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   198
		public static final int MSG_ROOM_DELETE = 6;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   199
		public static final int MSG_ROOMLIST = 7;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   200
		public static final int MSG_CONNECTED = 8;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   201
		public static final int MSG_DISCONNECTED = 9;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   202
		public static final int MSG_PASSWORD_REQUEST = 10;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   203
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   204
		public FromNetHandler() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   205
			super(Looper.getMainLooper());
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   206
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   207
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   208
		@SuppressWarnings("unchecked")
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   209
		@Override
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   210
		public void handleMessage(Message msg) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   211
			switch(msg.what) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   212
			case MSG_LOBBY_JOIN: {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   213
				String name = (String)msg.obj;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   214
				playerList.addPlayerWithNewId(name);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   215
				lobbyChatlog.appendPlayerJoin(name);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   216
				break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   217
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   218
			case MSG_LOBBY_LEAVE: {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   219
				Pair<String, String> args = (Pair<String, String>)msg.obj;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   220
				playerList.removePlayer(args.first);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   221
				lobbyChatlog.appendPlayerLeave(args.first, args.second);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   222
				break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   223
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   224
			case MSG_CHAT: {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   225
				Pair<String, String> args = (Pair<String, String>)msg.obj;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   226
				getCurrentLog().appendChat(args.first, args.second);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   227
				break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   228
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   229
			case MSG_MESSAGE: {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   230
				getCurrentLog().appendMessage(msg.arg1, (String)msg.obj);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   231
				break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   232
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   233
			case MSG_ROOM_ADD: {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   234
				roomList.addRoomWithNewId((Room)msg.obj);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   235
				break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   236
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   237
			case MSG_ROOM_UPDATE: {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   238
				Pair<String, Room> args = (Pair<String, Room>)msg.obj;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   239
				roomList.updateRoom(args.first, args.second);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   240
				break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   241
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   242
			case MSG_ROOM_DELETE: {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   243
				roomList.removeRoom((String)msg.obj);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   244
				break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   245
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   246
			case MSG_ROOMLIST: {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   247
				roomList.updateList((Room[])msg.obj);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   248
				break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   249
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   250
			case MSG_CONNECTED: {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   251
				state = State.LOBBY;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   252
				broadcastManager.sendBroadcast(new Intent(ACTION_CONNECTED));
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   253
				break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   254
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   255
			case MSG_DISCONNECTED: {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   256
				Pair<Boolean, String> args = (Pair<Boolean, String>)msg.obj;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   257
				state = State.NOT_CONNECTED;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   258
				connection = null;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   259
				Intent intent = new Intent(ACTION_DISCONNECTED);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   260
				intent.putExtra(EXTRA_HAS_ERROR, args.first);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   261
				intent.putExtra(EXTRA_MESSAGE, args.second);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   262
				broadcastManager.sendBroadcastSync(intent);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   263
				break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   264
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   265
			case MSG_PASSWORD_REQUEST: {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   266
				Intent intent = new Intent(ACTION_PASSWORD_REQUESTED);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   267
				intent.putExtra(EXTRA_PLAYERNAME, (String)msg.obj);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   268
				broadcastManager.sendBroadcast(intent);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   269
				break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   270
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   271
			default: {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   272
				Log.e("FromNetHandler", "Unknown message type: "+msg.what);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   273
				break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   274
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   275
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   276
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   277
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   278
	
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   279
	/**
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   280
	 * This class handles the actual communication with the networking library, on a separate thread.
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   281
	 */
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   282
	private static class ThreadedNetConnection {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   283
		private static final long TICK_INTERVAL_FAST = 100;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   284
		private static final long TICK_INTERVAL_SLOW = 5000;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   285
		private static final JnaFrontlib FLIB = Flib.INSTANCE;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   286
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   287
		public final ToNetHandler toNetHandler;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   288
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   289
		private final Context appContext;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   290
		private final FromNetHandler fromNetHandler;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   291
		private final TickHandler tickHandler;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   292
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   293
		/**
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   294
		 * conn can only be null while connecting (the first thing in the thread), and directly after disconnecting,
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   295
		 * in the same message (the looper is shut down on disconnect, so there will be no messages after that).
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   296
		 */
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   297
		private NetconnPtr conn;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   298
		private String playerName;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   299
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   300
		private ThreadedNetConnection(Context appContext, FromNetHandler fromNetHandler) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   301
			this.appContext = appContext;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   302
			this.fromNetHandler = fromNetHandler;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   303
			
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   304
			HandlerThread thread = new HandlerThread("NetThread");
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   305
			thread.start();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   306
			toNetHandler = new ToNetHandler(thread.getLooper());
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   307
			tickHandler = new TickHandler(thread.getLooper(), TICK_INTERVAL_FAST, tickCb);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   308
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   309
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   310
		private void connect(final String name, final String host, final int port) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   311
			toNetHandler.post(new Runnable() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   312
				public void run() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   313
					playerName = name == null ? "Player" : name;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   314
					MetaschemePtr meta = null;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   315
					File dataPath;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   316
					try {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   317
						dataPath = Utils.getDataPathFile(appContext);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   318
					} catch (FileNotFoundException e) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   319
						shutdown(true, appContext.getString(R.string.sdcard_not_mounted));
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   320
						return;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   321
					}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   322
					String metaschemePath = new File(dataPath, "metasettings.ini").getAbsolutePath();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   323
					meta = FLIB.flib_metascheme_from_ini(metaschemePath);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   324
					if(meta == null) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   325
						shutdown(true, appContext.getString(R.string.error_unexpected, "Missing metasettings.ini"));
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   326
						return;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   327
					}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   328
					conn = FLIB.flib_netconn_create(playerName, meta, dataPath.getAbsolutePath(), host, port);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   329
					if(conn == null) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   330
						shutdown(true, appContext.getString(R.string.error_connection_failed));
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   331
						return;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   332
					}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   333
					FLIB.flib_netconn_onLobbyJoin(conn, lobbyJoinCb, null);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   334
					FLIB.flib_netconn_onLobbyLeave(conn, lobbyLeaveCb, null);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   335
					FLIB.flib_netconn_onChat(conn, chatCb, null);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   336
					FLIB.flib_netconn_onMessage(conn, messageCb, null);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   337
					FLIB.flib_netconn_onRoomAdd(conn, roomAddCb, null);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   338
					FLIB.flib_netconn_onRoomUpdate(conn, roomUpdateCb, null);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   339
					FLIB.flib_netconn_onRoomDelete(conn, roomDeleteCb, null);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   340
					FLIB.flib_netconn_onConnected(conn, connectedCb, null);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   341
					FLIB.flib_netconn_onRoomlist(conn, roomlistCb, null);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   342
					FLIB.flib_netconn_onDisconnected(conn, disconnectCb, null);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   343
					FLIB.flib_netconn_onPasswordRequest(conn, passwordRequestCb, null);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   344
					FLIB.flib_metascheme_release(meta);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   345
					tickHandler.start();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   346
				}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   347
			});
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   348
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   349
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   350
		public static ThreadedNetConnection startConnection(Context appContext, FromNetHandler fromNetHandler, String playerName, String host, int port) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   351
			ThreadedNetConnection result = new ThreadedNetConnection(appContext, fromNetHandler);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   352
			result.connect(playerName, host, port);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   353
			return result;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   354
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   355
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   356
		public void setFastTickRate(boolean fastTickRate) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   357
			tickHandler.setInterval(fastTickRate ? TICK_INTERVAL_FAST : TICK_INTERVAL_SLOW);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   358
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   359
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   360
		private final Runnable tickCb = new Runnable() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   361
			public void run() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   362
				FLIB.flib_netconn_tick(conn);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   363
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   364
		};
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   365
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   366
		private final StrCallback lobbyJoinCb = new StrCallback() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   367
			public void callback(Pointer context, String name) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   368
				sendFromNet(FromNetHandler.MSG_LOBBY_JOIN, name);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   369
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   370
		};
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   371
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   372
		private final StrStrCallback lobbyLeaveCb = new StrStrCallback() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   373
			public void callback(Pointer context, String name, String msg) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   374
				sendFromNet(FromNetHandler.MSG_LOBBY_LEAVE, Pair.create(name, msg));
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   375
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   376
		};
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   377
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   378
		private final StrStrCallback chatCb = new StrStrCallback() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   379
			public void callback(Pointer context, String name, String msg) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   380
				sendFromNet(FromNetHandler.MSG_CHAT, Pair.create(name, msg));
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   381
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   382
		};
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   383
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   384
		private final IntStrCallback messageCb = new IntStrCallback() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   385
			public void callback(Pointer context, int type, String msg) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   386
				sendFromNet(FromNetHandler.MSG_MESSAGE, type, msg);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   387
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   388
		};
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   389
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   390
		private final RoomCallback roomAddCb = new RoomCallback() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   391
			public void callback(Pointer context, RoomPtr roomPtr) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   392
				sendFromNet(FromNetHandler.MSG_ROOM_ADD, roomPtr.deref());
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   393
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   394
		};
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   395
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   396
		private final StrRoomCallback roomUpdateCb = new StrRoomCallback() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   397
			public void callback(Pointer context, String name, RoomPtr roomPtr) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   398
				sendFromNet(FromNetHandler.MSG_ROOM_UPDATE, Pair.create(name, roomPtr.deref()));
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   399
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   400
		};
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   401
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   402
		private final StrCallback roomDeleteCb = new StrCallback() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   403
			public void callback(Pointer context, final String name) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   404
				sendFromNet(FromNetHandler.MSG_ROOM_DELETE, name);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   405
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   406
		};
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   407
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   408
		private final RoomListCallback roomlistCb = new RoomListCallback() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   409
			public void callback(Pointer context, RoomArrayPtr arg1, int count) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   410
				sendFromNet(FromNetHandler.MSG_ROOMLIST, arg1.getRooms(count));
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   411
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   412
		};
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   413
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   414
		private final VoidCallback connectedCb = new VoidCallback() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   415
			public void callback(Pointer context) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   416
				FLIB.flib_netconn_send_request_roomlist(conn);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   417
				playerName = FLIB.flib_netconn_get_playername(conn);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   418
				sendFromNet(FromNetHandler.MSG_CONNECTED, playerName);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   419
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   420
		};
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   421
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   422
		private final StrCallback passwordRequestCb = new StrCallback() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   423
			public void callback(Pointer context, String nickname) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   424
				sendFromNet(FromNetHandler.MSG_PASSWORD_REQUEST, playerName);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   425
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   426
		};
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   427
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   428
		private void shutdown(boolean error, String message) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   429
			if(conn != null) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   430
				FLIB.flib_netconn_destroy(conn);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   431
				conn = null;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   432
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   433
			tickHandler.stop();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   434
			toNetHandler.getLooper().quit();
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   435
			sendFromNet(FromNetHandler.MSG_DISCONNECTED, Pair.create(error, message));
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   436
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   437
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   438
		private final IntStrCallback disconnectCb = new IntStrCallback() {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   439
			public void callback(Pointer context, int reason, String message) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   440
				Boolean error = reason != JnaFrontlib.NETCONN_DISCONNECT_NORMAL;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   441
				String messageForUser = createDisconnectUserMessage(appContext.getResources(), reason, message);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   442
				shutdown(error, messageForUser);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   443
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   444
		};
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   445
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   446
		private static String createDisconnectUserMessage(Resources res, int reason, String message) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   447
			switch(reason) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   448
			case JnaFrontlib.NETCONN_DISCONNECT_AUTH_FAILED:
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   449
				return res.getString(R.string.error_auth_failed);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   450
			case JnaFrontlib.NETCONN_DISCONNECT_CONNLOST:
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   451
				return res.getString(R.string.error_connection_lost, message);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   452
			case JnaFrontlib.NETCONN_DISCONNECT_INTERNAL_ERROR:
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   453
				return res.getString(R.string.error_unexpected, message);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   454
			case JnaFrontlib.NETCONN_DISCONNECT_SERVER_TOO_OLD:
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   455
				return res.getString(R.string.error_server_too_old);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   456
			default:
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   457
				return message;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   458
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   459
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   460
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   461
		private boolean sendFromNet(int what, Object obj) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   462
			return fromNetHandler.sendMessage(fromNetHandler.obtainMessage(what, obj));
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   463
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   464
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   465
		private boolean sendFromNet(int what, int arg1, Object obj) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   466
			return fromNetHandler.sendMessage(fromNetHandler.obtainMessage(what, arg1, 0, obj));
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   467
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   468
		
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   469
		/**
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   470
		 * Processes messages to the networking system. Runs on a non-main thread.
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   471
		 */
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   472
		public final class ToNetHandler extends Handler {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   473
			public static final int MSG_SEND_NICK = 0;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   474
			public static final int MSG_SEND_PASSWORD = 1;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   475
			public static final int MSG_SEND_QUIT = 2;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   476
			public static final int MSG_SEND_ROOMLIST_REQUEST = 3;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   477
			public static final int MSG_SEND_PLAYER_INFO_REQUEST = 4;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   478
			public static final int MSG_SEND_CHAT = 5;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   479
			public static final int MSG_DISCONNECT = 6;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   480
			
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   481
			public ToNetHandler(Looper looper) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   482
				super(looper);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   483
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   484
			
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   485
			@Override
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   486
			public void handleMessage(Message msg) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   487
				switch(msg.what) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   488
				case MSG_SEND_NICK: {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   489
					FLIB.flib_netconn_send_nick(conn, (String)msg.obj);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   490
					break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   491
				}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   492
				case MSG_SEND_PASSWORD: {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   493
					FLIB.flib_netconn_send_password(conn, (String)msg.obj);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   494
					break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   495
				}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   496
				case MSG_SEND_QUIT: {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   497
					FLIB.flib_netconn_send_quit(conn, (String)msg.obj);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   498
					break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   499
				}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   500
				case MSG_SEND_ROOMLIST_REQUEST: {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   501
					FLIB.flib_netconn_send_request_roomlist(conn); // TODO restrict to lobby state?
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   502
					break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   503
				}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   504
				case MSG_SEND_PLAYER_INFO_REQUEST: {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   505
					FLIB.flib_netconn_send_playerInfo(conn, (String)msg.obj);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   506
					break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   507
				}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   508
				case MSG_SEND_CHAT: {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   509
					if(FLIB.flib_netconn_send_chat(conn, (String)msg.obj) == 0) {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   510
						sendFromNet(FromNetHandler.MSG_CHAT, Pair.create(playerName, (String)msg.obj));
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   511
					}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   512
					break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   513
				}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   514
				case MSG_DISCONNECT: {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   515
					FLIB.flib_netconn_send_quit(conn, (String)msg.obj);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   516
					shutdown(false, "User quit");
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   517
					break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   518
				}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   519
				default: {
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   520
					Log.e("ToNetHandler", "Unknown message type: "+msg.what);
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   521
					break;
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   522
				}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   523
				}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   524
			}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   525
		}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   526
	}
57a508884052 Hedgeroid: Major overhaul of the network connection code. Now it is threaded and
Medo <smaxein@googlemail.com>
parents:
diff changeset
   527
}