|
1 /* $Id$ */ |
|
2 |
|
3 #ifdef ENABLE_NETWORK |
|
4 |
|
5 #include "../stdafx.h" |
|
6 #include "../debug.h" |
|
7 #include "network_data.h" |
|
8 #include "../newgrf_config.h" |
|
9 |
|
10 // This file handles the GameList |
|
11 // Also, it handles the request to a server for data about the server |
|
12 |
|
13 /** Add a new item to the linked gamelist. If the IP and Port match |
|
14 * return the existing item instead of adding it again |
|
15 * @param ip the IP-address (inet_addr) of the to-be added item |
|
16 * @param port the port the server is running on |
|
17 * @return a point to the newly added or already existing item */ |
|
18 NetworkGameList *NetworkGameListAddItem(uint32 ip, uint16 port) |
|
19 { |
|
20 NetworkGameList *item, *prev_item; |
|
21 |
|
22 prev_item = NULL; |
|
23 for (item = _network_game_list; item != NULL; item = item->next) { |
|
24 if (item->ip == ip && item->port == port) return item; |
|
25 prev_item = item; |
|
26 } |
|
27 |
|
28 item = malloc(sizeof(*item)); |
|
29 memset(item, 0, sizeof(*item)); |
|
30 item->next = NULL; |
|
31 item->ip = ip; |
|
32 item->port = port; |
|
33 |
|
34 if (prev_item == NULL) { |
|
35 _network_game_list = item; |
|
36 } else { |
|
37 prev_item->next = item; |
|
38 } |
|
39 DEBUG(net, 4, "[gamelist] added server to list"); |
|
40 |
|
41 UpdateNetworkGameWindow(false); |
|
42 |
|
43 return item; |
|
44 } |
|
45 |
|
46 /** Remove an item from the gamelist linked list |
|
47 * @param remove pointer to the item to be removed */ |
|
48 void NetworkGameListRemoveItem(NetworkGameList *remove) |
|
49 { |
|
50 NetworkGameList *item, *prev_item; |
|
51 |
|
52 prev_item = NULL; |
|
53 for (item = _network_game_list; item != NULL; item = item->next) { |
|
54 if (remove == item) { |
|
55 if (prev_item == NULL) { |
|
56 _network_game_list = remove->next; |
|
57 } else { |
|
58 prev_item->next = remove->next; |
|
59 } |
|
60 |
|
61 /* Remove GRFConfig information */ |
|
62 ClearGRFConfigList(&remove->info.grfconfig); |
|
63 free(remove); |
|
64 remove = NULL; |
|
65 |
|
66 DEBUG(net, 4, "[gamelist] removed server from list"); |
|
67 UpdateNetworkGameWindow(false); |
|
68 return; |
|
69 } |
|
70 prev_item = item; |
|
71 } |
|
72 } |
|
73 |
|
74 #endif /* ENABLE_NETWORK */ |