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