~ubuntu-branches/debian/squeeze/openttd/squeeze

« back to all changes in this revision

Viewing changes to src/network/network_gamelist.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jordi Mallach, Matthijs Kooijman, Jordi Mallach
  • Date: 2009-04-15 18:22:10 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090415182210-22ktb8kdbp2tf3bm
[ Matthijs Kooijman ]
* New upstream release.
* Remove Debian specific desktop file, upstream provides one now. 
* Add debian/watch file.

[ Jordi Mallach ]
* Bump Standards-Version to 3.8.1, with no changes required.
* Move to debhelper compat 7. Bump Build-Depends accordingly.
* Use dh_prep.
* Add "set -e" to config script.
* Remove a few extra doc files that get installed by upstream Makefile.
* Add more complete copyright information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: network_gamelist.cpp 11691 2007-12-25 09:48:53Z rubidium $ */
 
1
/* $Id: network_gamelist.cpp 15299 2009-01-31 20:16:06Z smatz $ */
2
2
 
3
3
/**
4
4
 * @file network_gamelist.cpp This file handles the GameList
9
9
 
10
10
#include "../stdafx.h"
11
11
#include "../debug.h"
12
 
#include "../newgrf_config.h"
13
12
#include "../core/alloc_func.hpp"
14
 
#include "core/game.h"
 
13
#include "../thread.h"
 
14
#include "../string_func.h"
 
15
#include "network_internal.h"
15
16
#include "network_udp.h"
16
17
#include "network_gamelist.h"
17
 
#include "network_gui.h"
18
18
 
19
19
NetworkGameList *_network_game_list = NULL;
20
20
 
 
21
ThreadMutex *_network_game_list_mutex = ThreadMutex::New();
 
22
NetworkGameList *_network_game_delayed_insertion_list = NULL;
 
23
 
 
24
/** Add a new item to the linked gamelist, but do it delayed in the next tick
 
25
 * or so to prevent race conditions.
 
26
 * @param item the item to add. Will be freed once added.
 
27
 */
 
28
void NetworkGameListAddItemDelayed(NetworkGameList *item)
 
29
{
 
30
        _network_game_list_mutex->BeginCritical();
 
31
        item->next = _network_game_delayed_insertion_list;
 
32
        _network_game_delayed_insertion_list = item;
 
33
        _network_game_list_mutex->EndCritical();
 
34
}
 
35
 
 
36
/** Perform the delayed (thread safe) insertion into the game list */
 
37
static void NetworkGameListHandleDelayedInsert()
 
38
{
 
39
        _network_game_list_mutex->BeginCritical();
 
40
        while (_network_game_delayed_insertion_list != NULL) {
 
41
                NetworkGameList *ins_item = _network_game_delayed_insertion_list;
 
42
                _network_game_delayed_insertion_list = ins_item->next;
 
43
 
 
44
                NetworkGameList *item = NetworkGameListAddItem(ins_item->ip, ins_item->port);
 
45
 
 
46
                if (item != NULL) {
 
47
                        if (StrEmpty(item->info.server_name)) {
 
48
                                memset(&item->info, 0, sizeof(item->info));
 
49
                                strecpy(item->info.server_name, ins_item->info.server_name, lastof(item->info.server_name));
 
50
                                strecpy(item->info.hostname, ins_item->info.hostname, lastof(item->info.hostname));
 
51
                                item->online = false;
 
52
                        }
 
53
                        item->manually = ins_item->manually;
 
54
                        UpdateNetworkGameWindow(false);
 
55
                }
 
56
                free(ins_item);
 
57
        }
 
58
        _network_game_list_mutex->EndCritical();
 
59
}
 
60
 
21
61
/** Add a new item to the linked gamelist. If the IP and Port match
22
62
 * return the existing item instead of adding it again
23
63
 * @param ip the IP-address (inet_addr) of the to-be added item
25
65
 * @return a point to the newly added or already existing item */
26
66
NetworkGameList *NetworkGameListAddItem(uint32 ip, uint16 port)
27
67
{
 
68
        if (ip == 0) return NULL;
 
69
 
28
70
        NetworkGameList *item, *prev_item;
29
71
 
30
72
        prev_item = NULL;
33
75
                prev_item = item;
34
76
        }
35
77
 
36
 
        item = MallocT<NetworkGameList>(1);
37
 
        memset(item, 0, sizeof(*item));
 
78
        item = CallocT<NetworkGameList>(1);
38
79
        item->next = NULL;
39
80
        item->ip = ip;
40
81
        item->port = port;
88
129
/** Requeries the (game) servers we have not gotten a reply from */
89
130
void NetworkGameListRequery()
90
131
{
 
132
        NetworkGameListHandleDelayedInsert();
 
133
 
91
134
        static uint8 requery_cnt = 0;
92
135
 
93
136
        if (++requery_cnt < REQUERY_EVERY_X_GAMELOOPS) return;
94
137
        requery_cnt = 0;
95
138
 
96
 
        struct in_addr ip;
97
 
        NetworkGameList *item;
98
 
 
99
 
        for (item = _network_game_list; item != NULL; item = item->next) {
 
139
        for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
100
140
                item->retries++;
101
141
                if (item->retries < REFRESH_GAMEINFO_X_REQUERIES && (item->online || item->retries >= MAX_GAME_LIST_REQUERY_COUNT)) continue;
102
142
 
103
 
                ip.s_addr = item->ip;
104
 
 
105
143
                /* item gets mostly zeroed by NetworkUDPQueryServer */
106
144
                uint8 retries = item->retries;
107
 
                NetworkUDPQueryServer(inet_ntoa(ip), item->port);
 
145
                NetworkUDPQueryServer(NetworkAddress(item->ip, item->port));
108
146
                item->retries = (retries >= REFRESH_GAMEINFO_X_REQUERIES) ? 0 : retries;
109
147
        }
110
148
}