project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/netplay/Netconn.java
changeset 7352 641f11cdd319
parent 7349 12fdfd2038d4
equal deleted inserted replaced
7349:12fdfd2038d4 7352:641f11cdd319
    31 	private static final String DEFAULT_SERVER = "140.247.62.101";
    31 	private static final String DEFAULT_SERVER = "140.247.62.101";
    32 	private static final int DEFAULT_PORT = 46631;
    32 	private static final int DEFAULT_PORT = 46631;
    33 	
    33 	
    34 	private NetconnPtr conn;
    34 	private NetconnPtr conn;
    35 	private String playerName;
    35 	private String playerName;
       
    36 	private boolean joined; // True once we have been admitted to the lobby
    36 	
    37 	
    37 	public final PlayerList playerList = new PlayerList();
    38 	public final PlayerList playerList = new PlayerList();
    38 	public final RoomList roomList = new RoomList();
    39 	public final RoomList roomList = new RoomList();
    39 	public final MessageLog lobbyLog;
    40 	public final MessageLog lobbyChatlog;
    40 	public final MessageLog roomLog;
    41 	public final MessageLog roomChatlog;
    41 	
    42 	
    42 	private StrCallback lobbyJoinCb = new StrCallback() {
    43 	private StrCallback lobbyJoinCb = new StrCallback() {
    43 		public void callback(Pointer context, String arg1) {
    44 		public void callback(Pointer context, String arg1) {
    44 			playerList.addPlayerWithNewId(arg1);
    45 			playerList.addPlayerWithNewId(arg1);
    45 			lobbyLog.appendPlayerJoin(arg1);
    46 			lobbyChatlog.appendPlayerJoin(arg1);
    46 		}
    47 		}
    47 	};
    48 	};
    48 	
    49 	
    49 	private StrStrCallback lobbyLeaveCb = new StrStrCallback() {
    50 	private StrStrCallback lobbyLeaveCb = new StrStrCallback() {
    50 		public void callback(Pointer context, String name, String msg) {
    51 		public void callback(Pointer context, String name, String msg) {
    51 			playerList.remove(name);
    52 			playerList.removePlayer(name);
    52 			lobbyLog.appendPlayerLeave(name, msg);
    53 			lobbyChatlog.appendPlayerLeave(name, msg);
    53 		}
    54 		}
    54 	};
    55 	};
    55 	
    56 	
    56 	private StrStrCallback chatCb = new StrStrCallback() {
    57 	private StrStrCallback chatCb = new StrStrCallback() {
    57 		public void callback(Pointer context, String name, String msg) {
    58 		public void callback(Pointer context, String name, String msg) {
    77 		}
    78 		}
    78 	};
    79 	};
    79 	
    80 	
    80 	private StrCallback roomDeleteCb = new StrCallback() {
    81 	private StrCallback roomDeleteCb = new StrCallback() {
    81 		public void callback(Pointer context, String name) {
    82 		public void callback(Pointer context, String name) {
    82 			roomList.remove(name);
    83 			roomList.removeRoom(name);
    83 		}
    84 		}
    84 	};
    85 	};
    85 	
    86 	
    86 	private VoidCallback connectedCb = new VoidCallback() {
    87 	private VoidCallback connectedCb = new VoidCallback() {
    87 		public void callback(Pointer context) {
    88 		public void callback(Pointer context) {
    88 			// TODO I guess more needs to happen here...
    89 			// TODO I guess more needs to happen here...
       
    90 			joined = true;
    89 			FLIB.flib_netconn_send_request_roomlist(conn);
    91 			FLIB.flib_netconn_send_request_roomlist(conn);
    90 		}
    92 		}
    91 	};
    93 	};
    92 	
    94 	
    93 	private RoomListCallback roomlistCb = new RoomListCallback() {
    95 	private RoomListCallback roomlistCb = new RoomListCallback() {
    94 		public void callback(Pointer context, RoomArrayPtr arg1, int count) {
    96 		public void callback(Pointer context, RoomArrayPtr arg1, int count) {
    95 			roomList.clear();
    97 			roomList.updateList(arg1.getRooms(count));
    96 			for(RoomPtr roomPtr : arg1.getRooms(count)) {
       
    97 				roomList.addRoomWithNewId(roomPtr);
       
    98 			}
       
    99 		}
    98 		}
   100 	};
    99 	};
   101 	
   100 	
   102 	private IntStrCallback disconnectCb = new IntStrCallback() {
   101 	private IntStrCallback disconnectCb = new IntStrCallback() {
   103 		public void callback(Pointer context, int arg1, String arg2) {
   102 		public void callback(Pointer context, int arg1, String arg2) {
   123 	public Netconn(Context context, String playerName, String host, int port) throws IOException {
   122 	public Netconn(Context context, String playerName, String host, int port) throws IOException {
   124 		if(playerName == null) {
   123 		if(playerName == null) {
   125 			playerName = "Player";
   124 			playerName = "Player";
   126 		}
   125 		}
   127 		this.playerName = playerName;
   126 		this.playerName = playerName;
   128 		this.lobbyLog = new MessageLog(context);
   127 		this.lobbyChatlog = new MessageLog(context);
   129 		this.roomLog = new MessageLog(context);
   128 		this.roomChatlog = new MessageLog(context);
   130 		
   129 		
   131 		MetaschemePtr meta = null;
   130 		MetaschemePtr meta = null;
   132 		File dataPath = Utils.getDataPathFile(context);
   131 		File dataPath = Utils.getDataPathFile(context);
   133 		try {
   132 		try {
   134 			String metaschemePath = new File(dataPath, "metasettings.ini").getAbsolutePath();
   133 			String metaschemePath = new File(dataPath, "metasettings.ini").getAbsolutePath();
   168 	}
   167 	}
   169 	
   168 	
   170 	public void sendChat(String s) {
   169 	public void sendChat(String s) {
   171 		FLIB.flib_netconn_send_chat(conn, s);
   170 		FLIB.flib_netconn_send_chat(conn, s);
   172 		if(FLIB.flib_netconn_is_in_room_context(conn)) {
   171 		if(FLIB.flib_netconn_is_in_room_context(conn)) {
   173 			roomLog.appendChat(playerName, s);
   172 			roomChatlog.appendChat(playerName, s);
   174 		} else {
   173 		} else {
   175 			lobbyLog.appendChat(playerName, s);
   174 			lobbyChatlog.appendChat(playerName, s);
   176 		}
   175 		}
   177 	}
   176 	}
   178 	
   177 	
   179 	private MessageLog getCurrentLog() {
   178 	private MessageLog getCurrentLog() {
   180 		if(FLIB.flib_netconn_is_in_room_context(conn)) {
   179 		if(FLIB.flib_netconn_is_in_room_context(conn)) {
   181 			return roomLog;
   180 			return roomChatlog;
   182 		} else {
   181 		} else {
   183 			return lobbyLog;
   182 			return lobbyChatlog;
   184 		}
   183 		}
   185 	}
   184 	}
   186 	
   185 	
   187 	public void sendNick(String nick) { FLIB.flib_netconn_send_nick(conn, nick); }
   186 	public void sendNick(String nick) { FLIB.flib_netconn_send_nick(conn, nick); }
   188 	public void sendPassword(String password) { FLIB.flib_netconn_send_password(conn, password); }
   187 	public void sendPassword(String password) { FLIB.flib_netconn_send_password(conn, password); }
   189 	public void sendQuit(String message) { FLIB.flib_netconn_send_quit(conn, message); }
   188 	public void sendQuit(String message) { FLIB.flib_netconn_send_quit(conn, message); }
   190 	public void sendRoomlistRequest() { FLIB.flib_netconn_send_request_roomlist(conn); }
   189 	public void sendRoomlistRequest() { if(joined) FLIB.flib_netconn_send_request_roomlist(conn); }
       
   190 	public void sendPlayerInfoQuery(String name) { FLIB.flib_netconn_send_playerInfo(conn, name); }
   191 	
   191 	
   192 	public boolean isConnected() {
   192 	public boolean isConnected() {
   193 		return conn != null;
   193 		return conn != null;
   194 	}
   194 	}
   195 	
   195 	
   198 		if(conn != null) {
   198 		if(conn != null) {
   199 			FLIB.flib_netconn_destroy(conn);
   199 			FLIB.flib_netconn_destroy(conn);
   200 			Log.e("Netconn", "Leaked Netconn object");
   200 			Log.e("Netconn", "Leaked Netconn object");
   201 		}
   201 		}
   202 	}
   202 	}
       
   203 
   203 }
   204 }