project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/Netconn.java
changeset 7342 0e29eec2df5c
parent 7332 3f2e130f9715
child 7349 12fdfd2038d4
equal deleted inserted replaced
7340:62043f5f7c67 7342:0e29eec2df5c
     5 
     5 
     6 import org.hedgewars.hedgeroid.Utils;
     6 import org.hedgewars.hedgeroid.Utils;
     7 import org.hedgewars.hedgeroid.netplay.JnaFrontlib.IntStrCallback;
     7 import org.hedgewars.hedgeroid.netplay.JnaFrontlib.IntStrCallback;
     8 import org.hedgewars.hedgeroid.netplay.JnaFrontlib.MetaschemePtr;
     8 import org.hedgewars.hedgeroid.netplay.JnaFrontlib.MetaschemePtr;
     9 import org.hedgewars.hedgeroid.netplay.JnaFrontlib.NetconnPtr;
     9 import org.hedgewars.hedgeroid.netplay.JnaFrontlib.NetconnPtr;
       
    10 import org.hedgewars.hedgeroid.netplay.JnaFrontlib.RoomArrayPtr;
       
    11 import org.hedgewars.hedgeroid.netplay.JnaFrontlib.RoomCallback;
       
    12 import org.hedgewars.hedgeroid.netplay.JnaFrontlib.RoomListCallback;
       
    13 import org.hedgewars.hedgeroid.netplay.JnaFrontlib.RoomPtr;
    10 import org.hedgewars.hedgeroid.netplay.JnaFrontlib.StrCallback;
    14 import org.hedgewars.hedgeroid.netplay.JnaFrontlib.StrCallback;
       
    15 import org.hedgewars.hedgeroid.netplay.JnaFrontlib.StrRoomCallback;
    11 import org.hedgewars.hedgeroid.netplay.JnaFrontlib.StrStrCallback;
    16 import org.hedgewars.hedgeroid.netplay.JnaFrontlib.StrStrCallback;
       
    17 import org.hedgewars.hedgeroid.netplay.JnaFrontlib.VoidCallback;
    12 
    18 
    13 import com.sun.jna.Pointer;
    19 import com.sun.jna.Pointer;
    14 
    20 
    15 import android.content.Context;
    21 import android.content.Context;
    16 import android.util.Log;
    22 import android.util.Log;
    27 	
    33 	
    28 	private NetconnPtr conn;
    34 	private NetconnPtr conn;
    29 	private String playerName;
    35 	private String playerName;
    30 	
    36 	
    31 	public final PlayerList playerList = new PlayerList();
    37 	public final PlayerList playerList = new PlayerList();
       
    38 	public final RoomList roomList = new RoomList();
    32 	public final MessageLog lobbyLog;
    39 	public final MessageLog lobbyLog;
    33 	public final MessageLog roomLog;
    40 	public final MessageLog roomLog;
    34 	
    41 	
    35 	private StrCallback lobbyJoinCb = new StrCallback() {
    42 	private StrCallback lobbyJoinCb = new StrCallback() {
    36 		public void callback(Pointer context, String arg1) {
    43 		public void callback(Pointer context, String arg1) {
    37 			playerList.addPlayer(arg1);
    44 			playerList.addPlayerWithNewId(arg1);
    38 			lobbyLog.appendPlayerJoin(arg1);
    45 			lobbyLog.appendPlayerJoin(arg1);
    39 		}
    46 		}
    40 	};
    47 	};
    41 	
    48 	
    42 	private StrStrCallback lobbyLeaveCb = new StrStrCallback() {
    49 	private StrStrCallback lobbyLeaveCb = new StrStrCallback() {
    43 		public void callback(Pointer context, String name, String msg) {
    50 		public void callback(Pointer context, String name, String msg) {
    44 			playerList.removePlayer(name);
    51 			playerList.remove(name);
    45 			lobbyLog.appendPlayerLeave(name, msg);
    52 			lobbyLog.appendPlayerLeave(name, msg);
    46 		}
    53 		}
    47 	};
    54 	};
    48 	
    55 	
    49 	private StrStrCallback chatCb = new StrStrCallback() {
    56 	private StrStrCallback chatCb = new StrStrCallback() {
    53 	};
    60 	};
    54 	
    61 	
    55 	private IntStrCallback messageCb = new IntStrCallback() {
    62 	private IntStrCallback messageCb = new IntStrCallback() {
    56 		public void callback(Pointer context, int type, String msg) {
    63 		public void callback(Pointer context, int type, String msg) {
    57 			getCurrentLog().appendMessage(type, msg);
    64 			getCurrentLog().appendMessage(type, msg);
       
    65 		}
       
    66 	};
       
    67 	
       
    68 	private RoomCallback roomAddCb = new RoomCallback() {
       
    69 		public void callback(Pointer context, RoomPtr roomPtr) {
       
    70 			roomList.addRoomWithNewId(roomPtr);
       
    71 		}
       
    72 	};
       
    73 	
       
    74 	private StrRoomCallback roomUpdateCb = new StrRoomCallback() {
       
    75 		public void callback(Pointer context, String name, RoomPtr roomPtr) {
       
    76 			roomList.updateRoom(name, roomPtr);
       
    77 		}
       
    78 	};
       
    79 	
       
    80 	private StrCallback roomDeleteCb = new StrCallback() {
       
    81 		public void callback(Pointer context, String name) {
       
    82 			roomList.remove(name);
       
    83 		}
       
    84 	};
       
    85 	
       
    86 	private VoidCallback connectedCb = new VoidCallback() {
       
    87 		public void callback(Pointer context) {
       
    88 			// TODO I guess more needs to happen here...
       
    89 			FLIB.flib_netconn_send_request_roomlist(conn);
       
    90 		}
       
    91 	};
       
    92 	
       
    93 	private RoomListCallback roomlistCb = new RoomListCallback() {
       
    94 		public void callback(Pointer context, RoomArrayPtr arg1, int count) {
       
    95 			roomList.clear();
       
    96 			for(RoomPtr roomPtr : arg1.getRooms(count)) {
       
    97 				roomList.addRoomWithNewId(roomPtr);
       
    98 			}
    58 		}
    99 		}
    59 	};
   100 	};
    60 	
   101 	
    61 	/**
   102 	/**
    62 	 * Connect to the official Hedgewars server.
   103 	 * Connect to the official Hedgewars server.
    94 			}
   135 			}
    95 			FLIB.flib_netconn_onLobbyJoin(conn, lobbyJoinCb, null);
   136 			FLIB.flib_netconn_onLobbyJoin(conn, lobbyJoinCb, null);
    96 			FLIB.flib_netconn_onLobbyLeave(conn, lobbyLeaveCb, null);
   137 			FLIB.flib_netconn_onLobbyLeave(conn, lobbyLeaveCb, null);
    97 			FLIB.flib_netconn_onChat(conn, chatCb, null);
   138 			FLIB.flib_netconn_onChat(conn, chatCb, null);
    98 			FLIB.flib_netconn_onMessage(conn, messageCb, null);
   139 			FLIB.flib_netconn_onMessage(conn, messageCb, null);
       
   140 			FLIB.flib_netconn_onRoomAdd(conn, roomAddCb, null);
       
   141 			FLIB.flib_netconn_onRoomUpdate(conn, roomUpdateCb, null);
       
   142 			FLIB.flib_netconn_onRoomDelete(conn, roomDeleteCb, null);
       
   143 			FLIB.flib_netconn_onConnected(conn, connectedCb, null);
       
   144 			FLIB.flib_netconn_onRoomlist(conn, roomlistCb, null);
    99 		} finally {
   145 		} finally {
   100 			FLIB.flib_metascheme_release(meta);
   146 			FLIB.flib_metascheme_release(meta);
   101 		}
   147 		}
   102 	}
   148 	}
   103 	
   149 	
   131 	}
   177 	}
   132 	
   178 	
   133 	public void sendNick(String nick) { FLIB.flib_netconn_send_nick(conn, nick); }
   179 	public void sendNick(String nick) { FLIB.flib_netconn_send_nick(conn, nick); }
   134 	public void sendPassword(String password) { FLIB.flib_netconn_send_password(conn, password); }
   180 	public void sendPassword(String password) { FLIB.flib_netconn_send_password(conn, password); }
   135 	public void sendQuit(String message) { FLIB.flib_netconn_send_quit(conn, message); }
   181 	public void sendQuit(String message) { FLIB.flib_netconn_send_quit(conn, message); }
       
   182 	public void sendRoomlistRequest() { FLIB.flib_netconn_send_request_roomlist(conn); }
   136 	
   183 	
   137 	public boolean isConnected() {
   184 	public boolean isConnected() {
   138 		return conn != null;
   185 		return conn != null;
   139 	}
   186 	}
   140 	
   187