Fix compile error at macOS (#9)

* Fix compile error at macOS
* If fixing some of the whitespace in this file with this patch, might as well go the whole way?
* > 0 to != NULL
This commit is contained in:
Rongjian Zhang 2018-06-03 15:37:39 +08:00 committed by Brad Smith
parent 458eb30f3d
commit 211c7972a9
1 changed files with 733 additions and 701 deletions

View File

@ -114,7 +114,7 @@ int LoadConfigFile(char *fn)
if(fp=fopen(fn,"rb")) if(fp=fopen(fn,"rb"))
{ {
char buf[256]; char buf[256];
while(fgets(buf, 256, fp) > 0) while(fgets(buf, 256, fp) != NULL)
{ {
if(!strncasecmp(buf,"maxclients",strlen("maxclients"))) if(!strncasecmp(buf,"maxclients",strlen("maxclients")))
sscanf(buf,"%*s %d",&ServerConfig.MaxClients); sscanf(buf,"%*s %d",&ServerConfig.MaxClients);
@ -140,7 +140,6 @@ int LoadConfigFile(char *fn)
} }
} }
} }
else else
{ {
printf("Cannot load configuration file %s.\n", fn); printf("Cannot load configuration file %s.\n", fn);
@ -238,6 +237,7 @@ static int CheckNBTCPReceive(ClientEntry *client) throw(int)
{ {
if(!client->nbtcplen) if(!client->nbtcplen)
throw(1); /* Should not happen. */ throw(1); /* Should not happen. */
int l; int l;
while(l = recv(client->TCPSocket, client->nbtcp + client->nbtcphas, client->nbtcplen - client->nbtcphas, MSG_NOSIGNAL)) while(l = recv(client->TCPSocket, client->nbtcp + client->nbtcphas, client->nbtcplen - client->nbtcphas, MSG_NOSIGNAL))
@ -298,10 +298,16 @@ static int CheckNBTCPReceive(ClientEntry *client) throw(int)
{ {
EndNBTCPReceive(client); EndNBTCPReceive(client);
if(len) if(len)
{
StartNBTCPReceive(client,NBTCP_COMMAND | cmd,len); StartNBTCPReceive(client,NBTCP_COMMAND | cmd,len);
else /* Woops. Client probably tried to send a text message of 0 length. Or maybe a 0-length cheat file? Better be safe! */ }
else
{
/* Woops. Client probably tried to send a text message of 0 length.
Or maybe a 0-length cheat file? Better be safe! */
StartNBTCPReceive(client,NBTCP_UPDATEDATA,client->localplayers); StartNBTCPReceive(client,NBTCP_UPDATEDATA,client->localplayers);
} }
}
else throw(1); else throw(1);
return(1); return(1);
} }
@ -325,12 +331,15 @@ static int CheckNBTCPReceive(ClientEntry *client) throw(int)
free(ma); free(ma);
} }
else else
{
SendToAll((GameEntry*)client->game, tocmd, client->nbtcp, len); SendToAll((GameEntry*)client->game, tocmd, client->nbtcp, len);
}
EndNBTCPReceive(client); EndNBTCPReceive(client);
StartNBTCPReceive(client,NBTCP_UPDATEDATA,client->localplayers); StartNBTCPReceive(client,NBTCP_UPDATEDATA,client->localplayers);
return(1); return(1);
} }
case NBTCP_LOGINLEN:len = de32(client->nbtcp); case NBTCP_LOGINLEN:
len = de32(client->nbtcp);
if(len > 1024 || len < (16 + 16 + 64 + 1)) if(len > 1024 || len < (16 + 16 + 64 + 1))
throw(1); throw(1);
EndNBTCPReceive(client); EndNBTCPReceive(client);
@ -398,6 +407,7 @@ static int CheckNBTCPReceive(ClientEntry *client) throw(int)
GameEntry *tg=(GameEntry *)client->game; GameEntry *tg=(GameEntry *)client->game;
for(x=0; x<tg->MaxPlayers; x++) for(x=0; x<tg->MaxPlayers; x++)
{
if(tg->Players[x] && tg->IsUnique[x]) if(tg->Players[x] && tg->IsUnique[x])
{ {
if(tg->Players[x] != client) if(tg->Players[x] != client)
@ -405,7 +415,8 @@ static int CheckNBTCPReceive(ClientEntry *client) throw(int)
try try
{ {
TextToClient(tg->Players[x], "* Player %s has just connected as: %s",MakeMPS(client),client->nickname); TextToClient(tg->Players[x], "* Player %s has just connected as: %s",MakeMPS(client),client->nickname);
} catch(int i) }
catch(int i)
{ {
KillClient(tg->Players[x]); KillClient(tg->Players[x]);
} }
@ -415,6 +426,7 @@ static int CheckNBTCPReceive(ClientEntry *client) throw(int)
TextToClient(client, "* You(Player %s) have just connected as: %s",MakeMPS(client),client->nickname); TextToClient(client, "* You(Player %s) have just connected as: %s",MakeMPS(client),client->nickname);
} }
} }
}
EndNBTCPReceive(client); EndNBTCPReceive(client);
StartNBTCPReceive(client,NBTCP_UPDATEDATA,client->localplayers); StartNBTCPReceive(client,NBTCP_UPDATEDATA,client->localplayers);
return(1); return(1);
@ -433,6 +445,7 @@ static char *CleanNick(char *nick)
for(x=0; x<strlen(nick); x++) for(x=0; x<strlen(nick); x++)
if(nick[x] == '<' || nick[x] == '>' || nick[x] == '*' || (nick[x] < 0x20)) if(nick[x] == '<' || nick[x] == '>' || nick[x] == '*' || (nick[x] < 0x20))
nick[x] = 0; nick[x] = 0;
if(!strlen(nick)) if(!strlen(nick))
{ {
free(nick); free(nick);
@ -449,8 +462,10 @@ static int NickUnique(ClientEntry *client)
for(x=0; x<game->MaxPlayers; x++) for(x=0; x<game->MaxPlayers; x++)
if(game->Players[x] && client != game->Players[x]) if(game->Players[x] && client != game->Players[x])
if(!strcasecmp(client->nickname, game->Players[x]->nickname)) if(!strcasecmp(client->nickname, game->Players[x]->nickname))
return(0); return(0);
return(1); return(1);
} }
@ -458,6 +473,7 @@ static int MakeSendTCP(ClientEntry *client, uint8 *data, uint32 len) throw(int)
{ {
if(send(client->TCPSocket, data, len, MSG_NOSIGNAL) != len) if(send(client->TCPSocket, data, len, MSG_NOSIGNAL) != len)
throw(1); throw(1);
return(1); return(1);
} }
@ -487,7 +503,6 @@ static void SendToAll(GameEntry *game, int cmd, uint8 *data, uint32 len) throw(i
{ {
KillClient(game->Players[x]); KillClient(game->Players[x]);
} }
} }
} }
@ -563,6 +578,7 @@ static void KillClient(ClientEntry *client)
time_t curtime = time(0); time_t curtime = time(0);
printf("Unassigned client %d disconnected on %s",client->id, ctime(&curtime)); printf("Unassigned client %d disconnected on %s",client->id, ctime(&curtime));
} }
if(client->nbtcp) if(client->nbtcp)
free(client->nbtcp); free(client->nbtcp);
@ -571,6 +587,7 @@ static void KillClient(ClientEntry *client)
if(client->TCPSocket != -1) if(client->TCPSocket != -1)
close(client->TCPSocket); close(client->TCPSocket);
memset(client, 0, sizeof(ClientEntry)); memset(client, 0, sizeof(ClientEntry));
client->TCPSocket = -1; client->TCPSocket = -1;
@ -583,13 +600,14 @@ static void AddClientToGame(ClientEntry *client, uint8 id[16], uint8 extra[64])
int wg; int wg;
GameEntry *game,*fegame; GameEntry *game,*fegame;
retry: retry:
game = NULL; game = NULL;
fegame = NULL; fegame = NULL;
/* First, find an available game. */ /* First, find an available game. */
for(wg=0; wg<ServerConfig.MaxClients; wg++) for(wg=0; wg<ServerConfig.MaxClients; wg++)
{
if(!Games[wg].MaxPlayers && !fegame) if(!Games[wg].MaxPlayers && !fegame)
{ {
if(!fegame) if(!fegame)
@ -600,6 +618,7 @@ static void AddClientToGame(ClientEntry *client, uint8 id[16], uint8 extra[64])
game = &Games[wg]; game = &Games[wg];
break; break;
} }
}
if(!game) /* Hmm, no game found. Guess we'll have to create one. */ if(!game) /* Hmm, no game found. Guess we'll have to create one. */
{ {
@ -611,7 +630,6 @@ static void AddClientToGame(ClientEntry *client, uint8 id[16], uint8 extra[64])
memcpy(game->ExtraInfo, extra, 64); memcpy(game->ExtraInfo, extra, 64);
} }
int n; int n;
for(n = 0; n < game->MaxPlayers; n++) for(n = 0; n < game->MaxPlayers; n++)
if(game->Players[n]) if(game->Players[n])
@ -638,6 +656,7 @@ static void AddClientToGame(ClientEntry *client, uint8 id[16], uint8 extra[64])
int instancecount = client->localplayers; int instancecount = client->localplayers;
for(n=0; n<game->MaxPlayers; n++) for(n=0; n<game->MaxPlayers; n++)
{
if(!game->Players[n]) if(!game->Players[n])
{ {
game->Players[n] = client; game->Players[n] = client;
@ -649,6 +668,7 @@ static void AddClientToGame(ClientEntry *client, uint8 id[16], uint8 extra[64])
if(!instancecount) if(!instancecount)
break; break;
} }
}
/* Game is full. */ /* Game is full. */
if(n == game->MaxPlayers) if(n == game->MaxPlayers)
@ -668,13 +688,13 @@ static void AddClientToGame(ClientEntry *client, uint8 id[16], uint8 extra[64])
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
struct sockaddr_in sockin; struct sockaddr_in sockin;
socklen_t sockin_len; socklen_t sockin_len;
int i; int i;
char* pass = 0; char* pass = 0;
/* If we can't load the default config file, use some defined values */ /* If we can't load the default config file, use some defined values */
if(!LoadConfigFile(DEFAULT_CONFIG)) { if(!LoadConfigFile(DEFAULT_CONFIG))
{
ServerConfig.Port = DEFAULT_PORT; ServerConfig.Port = DEFAULT_PORT;
ServerConfig.MaxClients = DEFAULT_MAX; ServerConfig.MaxClients = DEFAULT_MAX;
ServerConfig.ConnectTimeout = DEFAULT_TIMEOUT; ServerConfig.ConnectTimeout = DEFAULT_TIMEOUT;
@ -682,10 +702,10 @@ int main(int argc, char *argv[])
} }
char* configfile = 0; char* configfile = 0;
for(i=1; i<argc ;i++) for(i=1; i<argc ;i++)
{ {
if(!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) { if(!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h"))
{
printf("Usage: %s [OPTION]...\n" ,argv[0]); printf("Usage: %s [OPTION]...\n" ,argv[0]);
printf("Begins the FCE Ultra game server with given options.\n"); printf("Begins the FCE Ultra game server with given options.\n");
printf("This server will first look in %s for options. If that \nfile does not exist, it will use the defaults given here. Any argument given \ndirectly will override any default values.\n\n", DEFAULT_CONFIG); printf("This server will first look in %s for options. If that \nfile does not exist, it will use the defaults given here. Any argument given \ndirectly will override any default values.\n\n", DEFAULT_CONFIG);
@ -700,7 +720,8 @@ int main(int argc, char *argv[])
printf("-c\t--configfile\tLoads the given configuration file.\n"); printf("-c\t--configfile\tLoads the given configuration file.\n");
return -1; return -1;
} }
if(!strcmp(argv[i], "--port") || !strcmp(argv[i], "-p")) { if(!strcmp(argv[i], "--port") || !strcmp(argv[i], "-p"))
{
i++; i++;
if(argc == i) if(argc == i)
{ {
@ -710,9 +731,11 @@ int main(int argc, char *argv[])
ServerConfig.Port = atoi(argv[i]); ServerConfig.Port = atoi(argv[i]);
continue; continue;
} }
if(!strcmp(argv[i], "--password") || !strcmp(argv[i], "-w")) { if(!strcmp(argv[i], "--password") || !strcmp(argv[i], "-w"))
{
i++; i++;
if(argc == i) { if(argc == i)
{
printf("Please specify a password.\n"); printf("Please specify a password.\n");
return -1; return -1;
} }
@ -725,46 +748,56 @@ int main(int argc, char *argv[])
puts("Password required to log in."); puts("Password required to log in.");
continue; continue;
} }
if(!strcmp(argv[i], "--maxclients") || !strcmp(argv[i], "-m")) { if(!strcmp(argv[i], "--maxclients") || !strcmp(argv[i], "-m"))
{
i++; i++;
if(argc == i) { if(argc == i)
{
printf("Please specify the maximium ammount of clients.\n"); printf("Please specify the maximium ammount of clients.\n");
return -1; return -1;
} }
ServerConfig.MaxClients = atoi(argv[i]); ServerConfig.MaxClients = atoi(argv[i]);
continue; continue;
} }
if(!strcmp(argv[i], "--timeout") || !strcmp(argv[i], "-t")) { if(!strcmp(argv[i], "--timeout") || !strcmp(argv[i], "-t"))
{
i++; i++;
if(argc == i) { if(argc == i)
{
printf("Please specify the conenction timeout time in seconds.\n"); printf("Please specify the conenction timeout time in seconds.\n");
return -1; return -1;
} }
ServerConfig.ConnectTimeout = atoi(argv[i]); ServerConfig.ConnectTimeout = atoi(argv[i]);
continue; continue;
} }
if(!strcmp(argv[i], "--framedivisor") || !strcmp(argv[i], "-f")) { if(!strcmp(argv[i], "--framedivisor") || !strcmp(argv[i], "-f"))
{
i++; i++;
if(argc == i) { if(argc == i)
{
printf("Please specify a framedivisor.\n"); printf("Please specify a framedivisor.\n");
return -1; return -1;
} }
ServerConfig.FrameDivisor = atoi(argv[i]); ServerConfig.FrameDivisor = atoi(argv[i]);
continue; continue;
} }
if(!strcmp(argv[i], "--configfile") || !strcmp(argv[i], "-c")) { if(!strcmp(argv[i], "--configfile") || !strcmp(argv[i], "-c"))
{
i++; i++;
if(argc == i) { if(argc == i)
{
printf("Please specify a configfile to load.\n"); printf("Please specify a configfile to load.\n");
return -1; return -1;
} }
if(!LoadConfigFile(argv[i])) { if(!LoadConfigFile(argv[i]))
{
puts("Error loading configuration file."); puts("Error loading configuration file.");
exit(-1); exit(-1);
} }
continue; continue;
} }
if(!strcmp(argv[i], "--version") || !strcmp(argv[i], "-v")) { if(!strcmp(argv[i], "--version") || !strcmp(argv[i], "-v"))
{
printf("FCE Ultra network server version %s\n", VERSION); printf("FCE Ultra network server version %s\n", VERSION);
return 0; return 0;
} }
@ -774,7 +807,6 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
Games = (GameEntry *)malloc(sizeof(GameEntry) * ServerConfig.MaxClients); Games = (GameEntry *)malloc(sizeof(GameEntry) * ServerConfig.MaxClients);
Clients = (ClientEntry *)malloc(sizeof(ClientEntry) * ServerConfig.MaxClients); Clients = (ClientEntry *)malloc(sizeof(ClientEntry) * ServerConfig.MaxClients);
@ -783,7 +815,6 @@ int main(int argc, char *argv[])
{ {
int x; int x;
for(x=0; x<ServerConfig.MaxClients; x++) for(x=0; x<ServerConfig.MaxClients; x++)
Clients[x].TCPSocket = -1; Clients[x].TCPSocket = -1;
} }
@ -875,6 +906,7 @@ int main(int argc, char *argv[])
{ {
KillClient(&Clients[n]); KillClient(&Clients[n]);
} }
int whichgame; int whichgame;
for(whichgame = 0; whichgame < ServerConfig.MaxClients; whichgame ++) for(whichgame = 0; whichgame < ServerConfig.MaxClients; whichgame ++)
{ {