project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/ThreadedNetConnection.java
author koda
Tue, 21 Jan 2014 22:43:06 +0100
changeset 10017 de822cd3df3a
parent 7691 55c0a856ecd0
permissions -rw-r--r--
fixwhitespace and dos2unix
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7584
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     1
/*
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     2
 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     4
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     5
 * This program is free software; you can redistribute it and/or
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     6
 * modify it under the terms of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     8
 * of the License, or (at your option) any later version.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
     9
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    13
 * GNU General Public License for more details.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    14
 *
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    15
 * You should have received a copy of the GNU General Public License
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    16
 * along with this program; if not, write to the Free Software
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    18
 */
7831c84cc644 License change: With the agreement of Xeli, I changed the Hedgeroid license to
Medo <smaxein@googlemail.com>
parents: 7582
diff changeset
    19
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    20
package org.hedgewars.hedgeroid.netplay;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    21
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    22
import static org.hedgewars.hedgeroid.netplay.Netplay.FromNetMsgType.*;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    23
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    24
import java.io.File;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    25
import java.io.FileNotFoundException;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    26
import java.util.Arrays;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    27
import java.util.Collections;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    28
import java.util.List;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    29
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    30
import org.hedgewars.hedgeroid.R;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    31
import org.hedgewars.hedgeroid.Datastructures.MapRecipe;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    32
import org.hedgewars.hedgeroid.Datastructures.Scheme;
7568
75ba91f14ed5 Hedgeroid:
Medo <smaxein@googlemail.com>
parents: 7558
diff changeset
    33
import org.hedgewars.hedgeroid.Datastructures.TeamInGame;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    34
import org.hedgewars.hedgeroid.Datastructures.Weaponset;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    35
import org.hedgewars.hedgeroid.frontlib.Flib;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    36
import org.hedgewars.hedgeroid.frontlib.Frontlib;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    37
import org.hedgewars.hedgeroid.frontlib.Frontlib.BoolCallback;
7588
27e5857da6af Hedgeroid improvements:
Medo <smaxein@googlemail.com>
parents: 7584
diff changeset
    38
import org.hedgewars.hedgeroid.frontlib.Frontlib.ByteArrayPtr;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    39
import org.hedgewars.hedgeroid.frontlib.Frontlib.BytesCallback;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    40
import org.hedgewars.hedgeroid.frontlib.Frontlib.GameSetupPtr;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    41
import org.hedgewars.hedgeroid.frontlib.Frontlib.IntStrCallback;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    42
import org.hedgewars.hedgeroid.frontlib.Frontlib.MapIntCallback;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    43
import org.hedgewars.hedgeroid.frontlib.Frontlib.MapRecipePtr;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    44
import org.hedgewars.hedgeroid.frontlib.Frontlib.NetconnPtr;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    45
import org.hedgewars.hedgeroid.frontlib.Frontlib.RoomArrayPtr;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    46
import org.hedgewars.hedgeroid.frontlib.Frontlib.RoomCallback;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    47
import org.hedgewars.hedgeroid.frontlib.Frontlib.RoomListCallback;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    48
import org.hedgewars.hedgeroid.frontlib.Frontlib.RoomPtr;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    49
import org.hedgewars.hedgeroid.frontlib.Frontlib.SchemeCallback;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    50
import org.hedgewars.hedgeroid.frontlib.Frontlib.SchemePtr;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    51
import org.hedgewars.hedgeroid.frontlib.Frontlib.StrCallback;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    52
import org.hedgewars.hedgeroid.frontlib.Frontlib.StrIntCallback;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    53
import org.hedgewars.hedgeroid.frontlib.Frontlib.StrRoomCallback;
7691
55c0a856ecd0 frontlib+Hedgeroid: Added support for the new client flags (chief, admin, reg)
Medo <smaxein@googlemail.com>
parents: 7588
diff changeset
    54
import org.hedgewars.hedgeroid.frontlib.Frontlib.StrStrBoolCallback;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    55
import org.hedgewars.hedgeroid.frontlib.Frontlib.StrStrCallback;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    56
import org.hedgewars.hedgeroid.frontlib.Frontlib.TeamCallback;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    57
import org.hedgewars.hedgeroid.frontlib.Frontlib.TeamPtr;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    58
import org.hedgewars.hedgeroid.frontlib.Frontlib.VoidCallback;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    59
import org.hedgewars.hedgeroid.frontlib.Frontlib.WeaponsetCallback;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    60
import org.hedgewars.hedgeroid.frontlib.Frontlib.WeaponsetPtr;
7558
983ff426f91e Hedgeroid: Fixed JNA mapping of size_t
Medo <smaxein@googlemail.com>
parents: 7508
diff changeset
    61
import org.hedgewars.hedgeroid.frontlib.NativeSizeT;
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    62
import org.hedgewars.hedgeroid.netplay.Netplay.FromNetHandler;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    63
import org.hedgewars.hedgeroid.netplay.Netplay.FromNetMsgType;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    64
import org.hedgewars.hedgeroid.util.FileUtils;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    65
import org.hedgewars.hedgeroid.util.TickHandler;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    66
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    67
import android.annotation.SuppressLint;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    68
import android.content.Context;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    69
import android.content.res.Resources;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    70
import android.os.Handler;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    71
import android.os.HandlerThread;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    72
import android.os.Looper;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    73
import android.os.Message;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    74
import android.util.Log;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    75
import android.util.Pair;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    76
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    77
import com.sun.jna.Pointer;
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    78
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    79
/**
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    80
 * This class handles the actual communication with the networking library, running on a separate thread.
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    81
 *
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    82
 * In order to process net messages, this class regularly runs a tick() function on the frontlib. This
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    83
 * usually happens several times per second, but it can be slowed down a lot if no fast reaction to
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    84
 * events is required (e.g. to conserve battery if the application is in the background).
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    85
 */
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
    86
class ThreadedNetConnection {
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    87
    private static final long TICK_INTERVAL_FAST = 100;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    88
    private static final long TICK_INTERVAL_SLOW = 5000;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    89
    private static final Frontlib FLIB = Flib.INSTANCE;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    90
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    91
    public final ToNetHandler toNetHandler;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    92
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    93
    private final Context appContext;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    94
    private final FromNetHandler fromNetHandler;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    95
    private final TickHandler tickHandler;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    96
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    97
    /**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    98
     * conn can only be null while connecting (the first thing in the thread), and directly after disconnecting,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
    99
     * in the same message (the looper is shut down on disconnect, so there will be no messages after that).
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   100
     */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   101
    private NetconnPtr conn;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   102
    private String playerName;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   103
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   104
    private ThreadedNetConnection(Context appContext, FromNetHandler fromNetHandler) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   105
        this.appContext = appContext;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   106
        this.fromNetHandler = fromNetHandler;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   107
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   108
        HandlerThread thread = new HandlerThread("NetThread");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   109
        thread.start();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   110
        toNetHandler = new ToNetHandler(thread.getLooper());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   111
        tickHandler = new TickHandler(thread.getLooper(), TICK_INTERVAL_FAST, tickCb);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   112
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   113
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   114
    private void connect(final String name, final String host, final int port) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   115
        toNetHandler.post(new Runnable() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   116
            public void run() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   117
                playerName = name == null ? "Player" : name;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   118
                File dataPath;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   119
                try {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   120
                    dataPath = FileUtils.getDataPathFile(appContext);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   121
                } catch (FileNotFoundException e) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   122
                    shutdown(true, appContext.getString(R.string.sdcard_not_mounted));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   123
                    return;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   124
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   125
                conn = FLIB.flib_netconn_create(playerName, dataPath.getAbsolutePath()+"/", host, port);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   126
                if(conn == null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   127
                    shutdown(true, appContext.getString(R.string.error_connection_failed));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   128
                    return;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   129
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   130
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   131
                FLIB.flib_netconn_onSchemeChanged(conn, cfgSchemeCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   132
                FLIB.flib_netconn_onClientFlags(conn, clientFlagsCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   133
                FLIB.flib_netconn_onChat(conn, chatCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   134
                FLIB.flib_netconn_onConnected(conn, connectedCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   135
                FLIB.flib_netconn_onDisconnected(conn, disconnectCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   136
                FLIB.flib_netconn_onEngineMessage(conn, engineMessageCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   137
                FLIB.flib_netconn_onEnterRoom(conn, enterRoomCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   138
                FLIB.flib_netconn_onHogCountChanged(conn, hogCountChangedCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   139
                FLIB.flib_netconn_onLeaveRoom(conn, leaveRoomCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   140
                FLIB.flib_netconn_onLobbyJoin(conn, lobbyJoinCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   141
                FLIB.flib_netconn_onLobbyLeave(conn, lobbyLeaveCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   142
                FLIB.flib_netconn_onMapChanged(conn, mapChangedCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   143
                FLIB.flib_netconn_onMessage(conn, messageCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   144
                FLIB.flib_netconn_onPasswordRequest(conn, passwordRequestCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   145
                FLIB.flib_netconn_onRoomAdd(conn, roomAddCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   146
                FLIB.flib_netconn_onRoomDelete(conn, roomDeleteCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   147
                FLIB.flib_netconn_onRoomJoin(conn, roomJoinCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   148
                FLIB.flib_netconn_onRoomLeave(conn, roomLeaveCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   149
                FLIB.flib_netconn_onRoomlist(conn, roomlistCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   150
                FLIB.flib_netconn_onRoomUpdate(conn, roomUpdateCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   151
                FLIB.flib_netconn_onRunGame(conn, runGameCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   152
                FLIB.flib_netconn_onScriptChanged(conn, scriptChangedCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   153
                // FLIB.flib_netconn_onServerVar(conn, serverVarCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   154
                FLIB.flib_netconn_onTeamAccepted(conn, teamAcceptedCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   155
                FLIB.flib_netconn_onTeamAdd(conn, teamAddedCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   156
                FLIB.flib_netconn_onTeamColorChanged(conn, teamColorChangedCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   157
                FLIB.flib_netconn_onTeamDelete(conn, teamDeletedCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   158
                FLIB.flib_netconn_onWeaponsetChanged(conn, weaponsetChangedCb, null);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   159
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   160
                tickHandler.start();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   161
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   162
        });
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   163
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   164
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   165
    public static ThreadedNetConnection startConnection(Context appContext, FromNetHandler fromNetHandler, String playerName, String host, int port) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   166
        ThreadedNetConnection result = new ThreadedNetConnection(appContext, fromNetHandler);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   167
        result.connect(playerName, host, port);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   168
        return result;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   169
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   170
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   171
    public void setFastTickRate(boolean fastTickRate) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   172
        tickHandler.setInterval(fastTickRate ? TICK_INTERVAL_FAST : TICK_INTERVAL_SLOW);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   173
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   174
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   175
    private final Runnable tickCb = new Runnable() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   176
        public void run() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   177
            FLIB.flib_netconn_tick(conn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   178
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   179
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   180
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   181
    private final SchemeCallback cfgSchemeCb = new SchemeCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   182
        public void callback(Pointer context, SchemePtr schemePtr) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   183
            sendFromNet(MSG_SCHEME_CHANGED, schemePtr.deref());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   184
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   185
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   186
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   187
    private final MapIntCallback mapChangedCb = new MapIntCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   188
        public void callback(Pointer context, MapRecipePtr mapPtr, int updateType) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   189
            sendFromNet(MSG_MAP_CHANGED, updateType, mapPtr.deref());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   190
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   191
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   192
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   193
    private final StrCallback scriptChangedCb = new StrCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   194
        public void callback(Pointer context, String script) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   195
            sendFromNet(MSG_SCRIPT_CHANGED, script);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   196
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   197
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   198
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   199
    private final WeaponsetCallback weaponsetChangedCb = new WeaponsetCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   200
        public void callback(Pointer context, WeaponsetPtr weaponsetPtr) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   201
            sendFromNet(MSG_WEAPONSET_CHANGED, weaponsetPtr.deref());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   202
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   203
    };
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   204
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   205
    private final StrCallback lobbyJoinCb = new StrCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   206
        public void callback(Pointer context, String name) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   207
            sendFromNet(MSG_LOBBY_JOIN, name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   208
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   209
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   210
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   211
    private final StrStrCallback lobbyLeaveCb = new StrStrCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   212
        public void callback(Pointer context, String name, String msg) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   213
            sendFromNet(MSG_LOBBY_LEAVE, Pair.create(name, msg));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   214
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   215
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   216
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   217
    private final StrCallback roomJoinCb = new StrCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   218
        public void callback(Pointer context, String name) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   219
            sendFromNet(MSG_ROOM_JOIN, name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   220
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   221
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   222
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   223
    private final StrStrCallback roomLeaveCb = new StrStrCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   224
        public void callback(Pointer context, String name, String message) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   225
            sendFromNet(MSG_ROOM_LEAVE, Pair.create(name, message));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   226
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   227
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   228
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   229
    private final StrStrBoolCallback clientFlagsCb = new StrStrBoolCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   230
        public void callback(Pointer context, String nick, String flags, boolean newFlagsState) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   231
            sendFromNet(MSG_CLIENT_FLAGS, new ClientFlagsUpdate(nick, flags, newFlagsState));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   232
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   233
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   234
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   235
    private final StrStrCallback chatCb = new StrStrCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   236
        public void callback(Pointer context, String name, String msg) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   237
            sendFromNet(MSG_CHAT, Pair.create(name, msg));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   238
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   239
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   240
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   241
    private final IntStrCallback messageCb = new IntStrCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   242
        public void callback(Pointer context, int type, String msg) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   243
            sendFromNet(MSG_MESSAGE, type, msg);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   244
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   245
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   246
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   247
    private final RoomCallback roomAddCb = new RoomCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   248
        public void callback(Pointer context, RoomPtr roomPtr) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   249
            sendFromNet(MSG_ROOM_ADD, roomPtr.deref());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   250
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   251
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   252
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   253
    private final StrRoomCallback roomUpdateCb = new StrRoomCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   254
        public void callback(Pointer context, String name, RoomPtr roomPtr) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   255
            sendFromNet(MSG_ROOM_UPDATE, Pair.create(name, roomPtr.deref()));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   256
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   257
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   258
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   259
    private final StrCallback roomDeleteCb = new StrCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   260
        public void callback(Pointer context, final String name) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   261
            sendFromNet(MSG_ROOM_DELETE, name);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   262
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   263
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   264
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   265
    private final RoomListCallback roomlistCb = new RoomListCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   266
        public void callback(Pointer context, RoomArrayPtr arg1, int count) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   267
            sendFromNet(MSG_ROOMLIST, arg1.getRooms(count));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   268
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   269
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   270
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   271
    private final VoidCallback connectedCb = new VoidCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   272
        public void callback(Pointer context) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   273
            FLIB.flib_netconn_send_request_roomlist(conn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   274
            playerName = FLIB.flib_netconn_get_playername(conn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   275
            sendFromNet(MSG_CONNECTED, playerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   276
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   277
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   278
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   279
    private final StrCallback passwordRequestCb = new StrCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   280
        public void callback(Pointer context, String nickname) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   281
            sendFromNet(MSG_PASSWORD_REQUEST, playerName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   282
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   283
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   284
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   285
    private final BoolCallback enterRoomCb = new BoolCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   286
        public void callback(Pointer context, boolean isChief) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   287
            sendFromNet(MSG_ENTER_ROOM_FROM_LOBBY, isChief);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   288
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   289
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   290
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   291
    private final IntStrCallback leaveRoomCb = new IntStrCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   292
        public void callback(Pointer context, int reason, String message) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   293
            sendFromNet(MSG_LEAVE_ROOM, reason, message);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   294
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   295
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   296
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   297
    private final TeamCallback teamAddedCb = new TeamCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   298
        public void callback(Pointer context, TeamPtr team) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   299
            sendFromNet(MSG_TEAM_ADDED, team.deref());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   300
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   301
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   302
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   303
    private final StrCallback teamDeletedCb = new StrCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   304
        public void callback(Pointer context, String teamName) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   305
            sendFromNet(MSG_TEAM_DELETED, teamName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   306
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   307
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   308
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   309
    private final StrCallback teamAcceptedCb = new StrCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   310
        public void callback(Pointer context, String teamName) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   311
            sendFromNet(MSG_TEAM_ACCEPTED, teamName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   312
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   313
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   314
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   315
    private final StrIntCallback teamColorChangedCb = new StrIntCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   316
        public void callback(Pointer context, String teamName, int colorIndex) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   317
            sendFromNet(MSG_TEAM_COLOR_CHANGED, colorIndex, teamName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   318
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   319
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   320
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   321
    private final StrIntCallback hogCountChangedCb = new StrIntCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   322
        public void callback(Pointer context, String teamName, int hogCount) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   323
            sendFromNet(MSG_HOG_COUNT_CHANGED, hogCount, teamName);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   324
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   325
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   326
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   327
    private final BytesCallback engineMessageCb = new BytesCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   328
        public void callback(Pointer context, ByteArrayPtr buffer, NativeSizeT size) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   329
            sendFromNet(MSG_ENGINE_MESSAGE, buffer.deref(size.intValue()));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   330
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   331
    };
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   332
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   333
    private final VoidCallback runGameCb = new VoidCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   334
        public void callback(Pointer context) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   335
            GameSetupPtr configPtr = FLIB.flib_netconn_create_gamesetup(conn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   336
            sendFromNet(MSG_RUN_GAME, configPtr.deref());
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   337
            FLIB.flib_gamesetup_destroy(configPtr);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   338
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   339
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   340
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   341
    private void shutdown(boolean error, String message) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   342
        if(conn != null) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   343
            FLIB.flib_netconn_destroy(conn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   344
            conn = null;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   345
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   346
        tickHandler.stop();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   347
        toNetHandler.getLooper().quit();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   348
        sendFromNet(MSG_DISCONNECTED, Pair.create(error, message));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   349
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   350
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   351
    private final IntStrCallback disconnectCb = new IntStrCallback() {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   352
        public void callback(Pointer context, int reason, String message) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   353
            Boolean error = reason != Frontlib.NETCONN_DISCONNECT_NORMAL;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   354
            String messageForUser = createDisconnectUserMessage(appContext.getResources(), reason, message);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   355
            shutdown(error, messageForUser);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   356
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   357
    };
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   358
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   359
    private static String createDisconnectUserMessage(Resources res, int reason, String message) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   360
        switch(reason) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   361
        case Frontlib.NETCONN_DISCONNECT_AUTH_FAILED:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   362
            return res.getString(R.string.error_auth_failed);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   363
        case Frontlib.NETCONN_DISCONNECT_CONNLOST:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   364
            return res.getString(R.string.error_connection_lost);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   365
        case Frontlib.NETCONN_DISCONNECT_INTERNAL_ERROR:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   366
            return res.getString(R.string.error_unexpected, message);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   367
        case Frontlib.NETCONN_DISCONNECT_SERVER_TOO_OLD:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   368
            return res.getString(R.string.error_server_too_old);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   369
        default:
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   370
            return message;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   371
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   372
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   373
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   374
    private boolean sendFromNet(FromNetMsgType what, Object obj) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   375
        return fromNetHandler.sendMessage(fromNetHandler.obtainMessage(what.ordinal(), obj));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   376
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   377
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   378
    private boolean sendFromNet(FromNetMsgType what, int arg1, Object obj) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   379
        return fromNetHandler.sendMessage(fromNetHandler.obtainMessage(what.ordinal(), arg1, 0, obj));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   380
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   381
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   382
    static enum ToNetMsgType {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   383
        MSG_SEND_NICK,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   384
        MSG_SEND_PASSWORD,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   385
        MSG_SEND_QUIT,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   386
        MSG_SEND_ROOMLIST_REQUEST,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   387
        MSG_SEND_PLAYER_INFO_REQUEST,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   388
        MSG_SEND_CHAT,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   389
        MSG_SEND_TEAMCHAT,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   390
        MSG_SEND_FOLLOW_PLAYER,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   391
        MSG_SEND_JOIN_ROOM,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   392
        MSG_SEND_CREATE_ROOM,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   393
        MSG_SEND_LEAVE_ROOM,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   394
        MSG_SEND_KICK,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   395
        MSG_SEND_ADD_TEAM,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   396
        MSG_SEND_REMOVE_TEAM,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   397
        MSG_DISCONNECT,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   398
        MSG_SEND_TEAM_COLOR_INDEX,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   399
        MSG_SEND_TEAM_HOG_COUNT,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   400
        MSG_SEND_ENGINE_MESSAGE,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   401
        MSG_SEND_ROUND_FINISHED,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   402
        MSG_SEND_TOGGLE_READY,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   403
        MSG_SEND_START_GAME,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   404
        MSG_SEND_WEAPONSET,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   405
        MSG_SEND_MAP,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   406
        MSG_SEND_MAP_NAME,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   407
        MSG_SEND_MAP_GENERATOR,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   408
        MSG_SEND_MAP_TEMPLATE,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   409
        MSG_SEND_MAZE_SIZE,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   410
        MSG_SEND_MAP_SEED,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   411
        MSG_SEND_MAP_THEME,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   412
        MSG_SEND_MAP_DRAWDATA,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   413
        MSG_SEND_GAMESTYLE,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   414
        MSG_SEND_SCHEME;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   415
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   416
        static final List<ThreadedNetConnection.ToNetMsgType> values = Collections.unmodifiableList(Arrays.asList(ToNetMsgType.values()));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   417
    }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   418
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   419
    /**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   420
     * Processes messages to the networking system. Runs on a non-main thread.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   421
     */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   422
    @SuppressLint("HandlerLeak")
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   423
    public final class ToNetHandler extends Handler {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   424
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   425
        public ToNetHandler(Looper looper) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   426
            super(looper);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   427
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   428
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   429
        @Override
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   430
        public void handleMessage(Message msg) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   431
            switch(ToNetMsgType.values.get(msg.what)) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   432
            case MSG_SEND_NICK: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   433
                FLIB.flib_netconn_send_nick(conn, (String)msg.obj);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   434
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   435
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   436
            case MSG_SEND_PASSWORD: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   437
                FLIB.flib_netconn_send_password(conn, (String)msg.obj);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   438
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   439
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   440
            case MSG_SEND_QUIT: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   441
                FLIB.flib_netconn_send_quit(conn, (String)msg.obj);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   442
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   443
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   444
            case MSG_SEND_ROOMLIST_REQUEST: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   445
                FLIB.flib_netconn_send_request_roomlist(conn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   446
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   447
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   448
            case MSG_SEND_PLAYER_INFO_REQUEST: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   449
                FLIB.flib_netconn_send_playerInfo(conn, (String)msg.obj);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   450
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   451
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   452
            case MSG_SEND_CHAT: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   453
                if(FLIB.flib_netconn_send_chat(conn, (String)msg.obj) == 0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   454
                    sendFromNet(MSG_CHAT, Pair.create(playerName, (String)msg.obj));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   455
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   456
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   457
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   458
            case MSG_SEND_TEAMCHAT: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   459
                FLIB.flib_netconn_send_teamchat(conn, (String)msg.obj);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   460
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   461
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   462
            case MSG_SEND_FOLLOW_PLAYER: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   463
                FLIB.flib_netconn_send_playerFollow(conn, (String)msg.obj);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   464
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   465
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   466
            case MSG_SEND_JOIN_ROOM: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   467
                FLIB.flib_netconn_send_joinRoom(conn, (String)msg.obj);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   468
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   469
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   470
            case MSG_SEND_CREATE_ROOM: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   471
                FLIB.flib_netconn_send_createRoom(conn, (String)msg.obj);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   472
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   473
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   474
            case MSG_SEND_LEAVE_ROOM: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   475
                if(FLIB.flib_netconn_send_leaveRoom(conn, (String)msg.obj) == 0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   476
                    sendFromNet(MSG_LEAVE_ROOM, -1, "");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   477
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   478
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   479
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   480
            case MSG_SEND_KICK: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   481
                FLIB.flib_netconn_send_kick(conn, (String)msg.obj);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   482
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   483
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   484
            case MSG_SEND_ADD_TEAM: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   485
                FLIB.flib_netconn_send_addTeam(conn, TeamPtr.createJavaOwned((TeamInGame)msg.obj));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   486
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   487
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   488
            case MSG_SEND_REMOVE_TEAM: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   489
                if(FLIB.flib_netconn_send_removeTeam(conn, (String)msg.obj)==0) {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   490
                    sendFromNet(MSG_TEAM_DELETED, msg.obj);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   491
                }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   492
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   493
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   494
            case MSG_DISCONNECT: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   495
                FLIB.flib_netconn_send_quit(conn, (String)msg.obj);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   496
                shutdown(false, "User quit");
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   497
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   498
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   499
            case MSG_SEND_TEAM_COLOR_INDEX: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   500
                FLIB.flib_netconn_send_teamColor(conn, (String)msg.obj, msg.arg1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   501
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   502
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   503
            case MSG_SEND_TEAM_HOG_COUNT: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   504
                FLIB.flib_netconn_send_teamHogCount(conn, (String)msg.obj, msg.arg1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   505
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   506
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   507
            case MSG_SEND_ENGINE_MESSAGE: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   508
                byte[] message = (byte[])msg.obj;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   509
                ByteArrayPtr ptr = ByteArrayPtr.createJavaOwned(message);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   510
                FLIB.flib_netconn_send_engineMessage(conn, ptr, NativeSizeT.valueOf(message.length));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   511
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   512
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   513
            case MSG_SEND_ROUND_FINISHED: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   514
                FLIB.flib_netconn_send_roundfinished(conn, (Boolean)msg.obj);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   515
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   516
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   517
            case MSG_SEND_TOGGLE_READY: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   518
                FLIB.flib_netconn_send_toggleReady(conn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   519
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   520
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   521
            case MSG_SEND_START_GAME: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   522
                FLIB.flib_netconn_send_startGame(conn);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   523
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   524
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   525
            case MSG_SEND_WEAPONSET: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   526
                FLIB.flib_netconn_send_weaponset(conn, WeaponsetPtr.createJavaOwned((Weaponset)msg.obj));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   527
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   528
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   529
            case MSG_SEND_MAP: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   530
                FLIB.flib_netconn_send_map(conn, MapRecipePtr.createJavaOwned((MapRecipe)msg.obj));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   531
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   532
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   533
            case MSG_SEND_MAP_NAME: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   534
                FLIB.flib_netconn_send_mapName(conn, (String)msg.obj);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   535
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   536
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   537
            case MSG_SEND_MAP_GENERATOR: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   538
                FLIB.flib_netconn_send_mapGen(conn, msg.arg1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   539
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   540
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   541
            case MSG_SEND_MAP_TEMPLATE: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   542
                FLIB.flib_netconn_send_mapTemplate(conn, msg.arg1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   543
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   544
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   545
            case MSG_SEND_MAZE_SIZE: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   546
                FLIB.flib_netconn_send_mapMazeSize(conn, msg.arg1);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   547
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   548
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   549
            case MSG_SEND_MAP_SEED: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   550
                FLIB.flib_netconn_send_mapSeed(conn, (String) msg.obj);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   551
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   552
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   553
            case MSG_SEND_MAP_THEME: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   554
                FLIB.flib_netconn_send_mapTheme(conn, (String) msg.obj);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   555
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   556
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   557
            case MSG_SEND_MAP_DRAWDATA: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   558
                byte[] message = (byte[])msg.obj;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   559
                ByteArrayPtr ptr = ByteArrayPtr.createJavaOwned(message);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   560
                FLIB.flib_netconn_send_mapDrawdata(conn, ptr, NativeSizeT.valueOf(message.length));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   561
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   562
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   563
            case MSG_SEND_GAMESTYLE: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   564
                FLIB.flib_netconn_send_script(conn, (String) msg.obj);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   565
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   566
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   567
            case MSG_SEND_SCHEME: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   568
                FLIB.flib_netconn_send_scheme(conn, SchemePtr.createJavaOwned((Scheme) msg.obj));
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   569
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   570
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   571
            default: {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   572
                Log.e("ToNetHandler", "Unknown message type: "+msg.what);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   573
                break;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   574
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   575
            }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   576
        }
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7691
diff changeset
   577
    }
7508
763d3961400b Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
diff changeset
   578
}