summaryrefslogtreecommitdiff
path: root/src/network
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2008-12-23 08:39:30 +0000
committerrubidium <rubidium@openttd.org>2008-12-23 08:39:30 +0000
commitb4734ee9a4d352785f1332f59c363186abe29695 (patch)
tree195e0e3d8e52be7e4c308d394d03d4468a967c67 /src/network
parent94e7f41a9d54a6c1015778f2a84815c28f8b8461 (diff)
downloadopenttd-b4734ee9a4d352785f1332f59c363186abe29695.tar.xz
(svn r14719) -Codechange: replace DEREF_CLIENT with an instance function and replace looping socket structs with info structs when the loop is only interested in the info structs (i.e. not derefing the info from sockets when one can loop info directly and the socket isn't used)
Diffstat (limited to 'src/network')
-rw-r--r--src/network/core/tcp.h7
-rw-r--r--src/network/network.cpp21
-rw-r--r--src/network/network_client.cpp6
-rw-r--r--src/network/network_func.h2
-rw-r--r--src/network/network_gui.cpp6
-rw-r--r--src/network/network_internal.h2
-rw-r--r--src/network/network_server.cpp42
7 files changed, 41 insertions, 45 deletions
diff --git a/src/network/core/tcp.h b/src/network/core/tcp.h
index 1bafbf26e..f956aa105 100644
--- a/src/network/core/tcp.h
+++ b/src/network/core/tcp.h
@@ -110,6 +110,13 @@ public:
bool IsPacketQueueEmpty();
Packet *Recv_Packet(NetworkRecvStatus *status);
+
+ inline NetworkClientInfo *GetInfo() const
+ {
+ extern NetworkClientInfo _network_client_info[MAX_CLIENT_INFO];
+ extern NetworkTCPSocketHandler _clients[MAX_CLIENTS];
+ return &_network_client_info[this - _clients];
+ }
};
#endif /* ENABLE_NETWORK */
diff --git a/src/network/network.cpp b/src/network/network.cpp
index 2983446fc..0b0bfb98e 100644
--- a/src/network/network.cpp
+++ b/src/network/network.cpp
@@ -163,9 +163,9 @@ NetworkTCPSocketHandler *NetworkFindClientStateFromClientID(ClientID client_id)
// if the user did not send it yet, Client #<no> is used.
void NetworkGetClientName(char *client_name, size_t size, const NetworkTCPSocketHandler *cs)
{
- const NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
+ const NetworkClientInfo *ci = cs->GetInfo();
- if (ci->client_name[0] == '\0') {
+ if (StrEmpty(ci->client_name)) {
snprintf(client_name, size, "Client #%4d", cs->client_id);
} else {
ttd_strlcpy(client_name, ci->client_name, size);
@@ -174,11 +174,11 @@ void NetworkGetClientName(char *client_name, size_t size, const NetworkTCPSocket
byte NetworkSpectatorCount()
{
- NetworkTCPSocketHandler *cs;
+ const NetworkClientInfo *ci;
byte count = 0;
- FOR_ALL_CLIENTS(cs) {
- if (DEREF_CLIENT_INFO(cs)->client_playas == COMPANY_SPECTATOR) count++;
+ FOR_ALL_CLIENT_INFOS(ci) {
+ if (ci->client_playas == COMPANY_SPECTATOR) count++;
}
return count;
@@ -360,11 +360,10 @@ char* GetNetworkErrorMsg(char* buf, NetworkErrorCode err, const char* last)
/* Count the number of active clients connected */
static uint NetworkCountActiveClients()
{
- NetworkTCPSocketHandler *cs;
+ const NetworkClientInfo *ci;
uint count = 0;
- FOR_ALL_CLIENTS(cs) {
- const NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
+ FOR_ALL_CLIENT_INFOS(ci) {
if (IsValidCompanyID(ci->client_playas)) count++;
}
@@ -439,7 +438,7 @@ static NetworkTCPSocketHandler *NetworkAllocClient(SOCKET s)
cs->last_frame_server = _frame_counter;
if (_network_server) {
- NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
+ NetworkClientInfo *ci = cs->GetInfo();
memset(ci, 0, sizeof(*ci));
cs->client_id = _network_client_id++;
@@ -495,7 +494,7 @@ void NetworkCloseClient(NetworkTCPSocketHandler *cs)
cs->Destroy();
// Close the gap in the client-list
- ci = DEREF_CLIENT_INFO(cs);
+ ci = cs->GetInfo();
if (_network_server) {
// We just lost one client :(
@@ -617,7 +616,7 @@ static void NetworkAcceptClients()
// the client stays inactive
cs->status = STATUS_INACTIVE;
- DEREF_CLIENT_INFO(cs)->client_ip = sin.sin_addr.s_addr; // Save the IP of the client
+ cs->GetInfo()->client_ip = sin.sin_addr.s_addr; // Save the IP of the client
}
}
diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp
index 987614ca7..d6cd1af43 100644
--- a/src/network/network_client.cpp
+++ b/src/network/network_client.cpp
@@ -967,7 +967,7 @@ bool NetworkClientPreferTeamChat(const NetworkClientInfo *cio)
if (!_settings_client.gui.prefer_teamchat || !IsValidCompanyID(cio->client_playas)) return false;
const NetworkClientInfo *ci;
- FOR_ALL_ACTIVE_CLIENT_INFOS(ci) {
+ FOR_ALL_CLIENT_INFOS(ci) {
if (ci->client_playas == cio->client_playas && ci != cio) return true;
}
@@ -979,8 +979,8 @@ bool NetworkClientPreferTeamChat(const NetworkClientInfo *cio)
*/
void NetworkPrintClients()
{
- NetworkClientInfo *ci;
- FOR_ALL_ACTIVE_CLIENT_INFOS(ci) {
+ const NetworkClientInfo *ci;
+ FOR_ALL_CLIENT_INFOS(ci) {
IConsolePrintF(CC_INFO, "Client #%1d name: '%s' company: %1d IP: %s",
ci->client_id,
ci->client_name,
diff --git a/src/network/network_func.h b/src/network/network_func.h
index ebc5c9da2..e05d0f952 100644
--- a/src/network/network_func.h
+++ b/src/network/network_func.h
@@ -64,7 +64,7 @@ void CDECL NetworkAddChatMessage(uint16 color, uint8 duration, const char *messa
void NetworkUndrawChatMessage();
void NetworkChatMessageDailyLoop();
-#define FOR_ALL_ACTIVE_CLIENT_INFOS(ci) for (ci = _network_client_info; ci != endof(_network_client_info); ci++) if (ci->client_id != INVALID_CLIENT_ID)
+#define FOR_ALL_CLIENT_INFOS(ci) for (ci = _network_client_info; ci != endof(_network_client_info); ci++) if (ci->client_id != INVALID_CLIENT_ID)
#endif /* ENABLE_NETWORK */
#endif /* NETWORK_FUNC_H */
diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp
index f9d972c6b..d3f7a4ca5 100644
--- a/src/network/network_gui.cpp
+++ b/src/network/network_gui.cpp
@@ -1419,7 +1419,7 @@ static const NetworkClientInfo *NetworkFindClientInfo(byte client_no)
{
const NetworkClientInfo *ci;
- FOR_ALL_ACTIVE_CLIENT_INFOS(ci) {
+ FOR_ALL_CLIENT_INFOS(ci) {
if (client_no == 0) return ci;
client_no--;
}
@@ -1659,7 +1659,7 @@ struct NetworkClientListWindow : Window
const NetworkClientInfo *ci;
/* Should be replaced with a loop through all clients */
- FOR_ALL_ACTIVE_CLIENT_INFOS(ci) {
+ FOR_ALL_CLIENT_INFOS(ci) {
num++;
}
@@ -1689,7 +1689,7 @@ struct NetworkClientListWindow : Window
int y = CLNWND_OFFSET;
- FOR_ALL_ACTIVE_CLIENT_INFOS(ci) {
+ FOR_ALL_CLIENT_INFOS(ci) {
TextColour colour;
if (this->selected_item == i++) { // Selected item, highlight it
GfxFillRect(1, y, 248, y + CLNWND_ROWSIZE - 1, 0);
diff --git a/src/network/network_internal.h b/src/network/network_internal.h
index 4e970f58d..f19d6306e 100644
--- a/src/network/network_internal.h
+++ b/src/network/network_internal.h
@@ -149,8 +149,6 @@ char* GetNetworkErrorMsg(char* buf, NetworkErrorCode err, const char* last);
bool NetworkFindName(char new_name[NETWORK_CLIENT_NAME_LENGTH]);
#define DEREF_CLIENT(i) (&_clients[i])
-// This returns the NetworkClientInfo from a NetworkClientState
-#define DEREF_CLIENT_INFO(cs) (&_network_client_info[cs - _clients])
// Macros to make life a bit more easier
#define DEF_CLIENT_RECEIVE_COMMAND(type) NetworkRecvStatus NetworkPacketReceive_ ## type ## _command(Packet *p)
diff --git a/src/network/network_server.cpp b/src/network/network_server.cpp
index d0f098e1e..0bf9f3a74 100644
--- a/src/network/network_server.cpp
+++ b/src/network/network_server.cpp
@@ -89,7 +89,7 @@ DEF_SERVER_SEND_COMMAND(PACKET_SERVER_COMPANY_INFO)
NetworkGetClientName(client_name, sizeof(client_name), csi);
- ci = DEREF_CLIENT_INFO(csi);
+ ci = csi->GetInfo();
if (ci != NULL && IsValidCompanyID(ci->client_playas)) {
if (!StrEmpty(clients[ci->client_playas])) {
strecat(clients[ci->client_playas], ", ", lastof(clients[ci->client_playas]));
@@ -255,7 +255,7 @@ DEF_SERVER_SEND_COMMAND(PACKET_SERVER_WELCOME)
// Transmit info about all the active clients
FOR_ALL_CLIENTS(new_cs) {
if (new_cs != cs && new_cs->status > STATUS_AUTH)
- SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, DEREF_CLIENT_INFO(new_cs));
+ SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, new_cs->GetInfo());
}
// Also send the info of the server
SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(cs, NetworkFindClientInfoFromClientID(CLIENT_ID_SERVER));
@@ -609,7 +609,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_NEWGRFS_CHECKED)
return;
}
- NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
+ NetworkClientInfo *ci = cs->GetInfo();
/* We now want a password from the client else we do not allow him in! */
if (!StrEmpty(_settings_client.network.server_password)) {
@@ -685,7 +685,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_JOIN)
return;
}
- ci = DEREF_CLIENT_INFO(cs);
+ ci = cs->GetInfo();
strecpy(ci->client_name, name, lastof(ci->client_name));
strecpy(ci->unique_id, unique_id, lastof(ci->unique_id));
@@ -719,7 +719,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_PASSWORD)
return;
}
- ci = DEREF_CLIENT_INFO(cs);
+ ci = cs->GetInfo();
if (IsValidCompanyID(ci->client_playas) && !StrEmpty(_network_company_states[ci->client_playas].password)) {
SEND_COMMAND(PACKET_SERVER_NEED_PASSWORD)(cs, NETWORK_COMPANY_PASSWORD);
@@ -730,7 +730,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_PASSWORD)
SEND_COMMAND(PACKET_SERVER_WELCOME)(cs);
return;
} else if (cs->status == STATUS_AUTHORIZING && type == NETWORK_COMPANY_PASSWORD) {
- ci = DEREF_CLIENT_INFO(cs);
+ ci = cs->GetInfo();
if (strcmp(password, _network_company_states[ci->client_playas].password) != 0) {
// Password is invalid
@@ -797,7 +797,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_MAP_OK)
FOR_ALL_CLIENTS(new_cs) {
if (new_cs->status > STATUS_AUTH) {
- SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(new_cs, DEREF_CLIENT_INFO(cs));
+ SEND_COMMAND(PACKET_SERVER_CLIENT_INFO)(new_cs, cs->GetInfo());
SEND_COMMAND(PACKET_SERVER_JOIN)(new_cs, cs->client_id);
}
}
@@ -873,7 +873,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_COMMAND)
return;
}
- ci = DEREF_CLIENT_INFO(cs);
+ ci = cs->GetInfo();
/* Check if cp->cmd is valid */
if (!IsValidCommand(cp->cmd)) {
@@ -1094,7 +1094,7 @@ void NetworkServerSendChat(NetworkAction action, DestType desttype, int dest, co
/* Find all clients that belong to this company */
ci_to = NULL;
FOR_ALL_CLIENTS(cs) {
- ci = DEREF_CLIENT_INFO(cs);
+ ci = cs->GetInfo();
if (ci->client_playas == (CompanyID)dest) {
SEND_COMMAND(PACKET_SERVER_CHAT)(cs, action, from_id, false, msg);
if (cs->client_id == from_id) show_local = false;
@@ -1160,7 +1160,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_CHAT)
p->Recv_string(msg, NETWORK_CHAT_LENGTH);
- const NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
+ const NetworkClientInfo *ci = cs->GetInfo();
switch (action) {
case NETWORK_ACTION_GIVE_MONEY:
if (!IsValidCompanyID(ci->client_playas)) break;
@@ -1189,7 +1189,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_SET_PASSWORD)
const NetworkClientInfo *ci;
p->Recv_string(password, sizeof(password));
- ci = DEREF_CLIENT_INFO(cs);
+ ci = cs->GetInfo();
if (IsValidCompanyID(ci->client_playas)) {
strecpy(_network_company_states[ci->client_playas].password, password, lastof(_network_company_states[ci->client_playas].password));
@@ -1208,7 +1208,7 @@ DEF_SERVER_RECEIVE_COMMAND(PACKET_CLIENT_SET_NAME)
NetworkClientInfo *ci;
p->Recv_string(client_name, sizeof(client_name));
- ci = DEREF_CLIENT_INFO(cs);
+ ci = cs->GetInfo();
if (cs->has_quit) return;
@@ -1362,7 +1362,6 @@ static void NetworkCheckRestartMap()
(and item 1. happens a year later) */
static void NetworkAutoCleanCompanies()
{
- NetworkTCPSocketHandler *cs;
const NetworkClientInfo *ci;
const Company *c;
bool clients_in_company[MAX_COMPANIES];
@@ -1372,8 +1371,7 @@ static void NetworkAutoCleanCompanies()
memset(clients_in_company, 0, sizeof(clients_in_company));
/* Detect the active companies */
- FOR_ALL_CLIENTS(cs) {
- ci = DEREF_CLIENT_INFO(cs);
+ FOR_ALL_CLIENT_INFOS(ci) {
if (IsValidCompanyID(ci->client_playas)) clients_in_company[ci->client_playas] = true;
}
@@ -1415,7 +1413,6 @@ static void NetworkAutoCleanCompanies()
// and it returns true if that succeeded.
bool NetworkFindName(char new_name[NETWORK_CLIENT_NAME_LENGTH])
{
- NetworkTCPSocketHandler *new_cs;
bool found_name = false;
byte number = 0;
char original_name[NETWORK_CLIENT_NAME_LENGTH];
@@ -1427,8 +1424,7 @@ bool NetworkFindName(char new_name[NETWORK_CLIENT_NAME_LENGTH])
const NetworkClientInfo *ci;
found_name = true;
- FOR_ALL_CLIENTS(new_cs) {
- ci = DEREF_CLIENT_INFO(new_cs);
+ FOR_ALL_CLIENT_INFOS(ci) {
if (strcmp(ci->client_name, new_name) == 0) {
// Name already in use
found_name = false;
@@ -1569,7 +1565,6 @@ void NetworkServerChangeOwner(Owner current_owner, Owner new_owner)
{
/* The server has to handle all administrative issues, for example
* updating and notifying all clients of what has happened */
- NetworkTCPSocketHandler *cs;
NetworkClientInfo *ci = NetworkFindClientInfoFromClientID(CLIENT_ID_SERVER);
/* The server has just changed from owner */
@@ -1579,8 +1574,7 @@ void NetworkServerChangeOwner(Owner current_owner, Owner new_owner)
}
/* Find all clients that were in control of this company, and mark them as new_owner */
- FOR_ALL_CLIENTS(cs) {
- ci = DEREF_CLIENT_INFO(cs);
+ FOR_ALL_CLIENT_INFOS(ci) {
if (current_owner == ci->client_playas) {
ci->client_playas = new_owner;
NetworkUpdateClientInfo(ci->client_id);
@@ -1612,7 +1606,7 @@ void NetworkServerShowStatusToConsole()
NetworkTCPSocketHandler *cs;
FOR_ALL_CLIENTS(cs) {
int lag = NetworkCalculateLag(cs);
- const NetworkClientInfo *ci = DEREF_CLIENT_INFO(cs);
+ const NetworkClientInfo *ci = cs->GetInfo();
const char* status;
status = (cs->status < (ptrdiff_t)lengthof(stat_str) ? stat_str[cs->status] : "unknown");
@@ -1635,10 +1629,8 @@ void NetworkServerSendError(ClientID client_id, NetworkErrorCode error)
bool NetworkCompanyHasClients(CompanyID company)
{
- const NetworkTCPSocketHandler *cs;
const NetworkClientInfo *ci;
- FOR_ALL_CLIENTS(cs) {
- ci = DEREF_CLIENT_INFO(cs);
+ FOR_ALL_CLIENT_INFOS(ci) {
if (ci->client_playas == company) return true;
}
return false;