-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cpp
172 lines (121 loc) · 3.69 KB
/
server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
SERVER.CPP v0.15
serverMain().
Copyright (c) 2004 Francesco Orsenigo <francesco.orsenigo@gmail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "main.h"
#include "game.h"
#include "colors.h"
#include "net.h"
#include "cmd.h"
/*=============================================================================
* GAME THINK
* Will make everyone call gameThink().
*/
int serverGameThink()
{
cmdSelectBrain(0);
cmdFromClient("think");
}
/*=============================================================================
* INIT PLAYER
*/
int serverInitPlayer()
{
char bf[100];
// serverThink() has already called cmdSelectBrain()
sprintf(bf, "name %s", net_connections[cmdGetBrainId()].name);
cmdFromClient(bf);
// apply a color
int c = time(0) % 8;
while(!game->IsPlayerColorAvailable(colorGet(c))) c++;
cmdGetBrain()->Color = colorGet(c);
//@@ as chunks:
//@@ send game string
//@@ send map....
// send all brains data to all brains
for(brainC* b = game->Brains; b < game->BrainsEnd; b++) if(b->Status != BRAIN_UNUSED)
{
cmdSelectBrain(b->Id);
sprintf(bf, "name %s", cmdGetBrain()->Name);
cmdFromClient(bf); // fake: send names as if they were sent by client!
sprintf(bf, "color %s", cmdGetBrain()->Color->Name);
cmdFromClient(bf); // fake: send colors as if they were sent by client!
}
}
/*=============================================================================
* THINK
*/
static tempoC serverGameThinkTempo(100);
bool serverThink()
{
connectionC* c;
char* p;
int brainID;
// poll network
if(netLoop() == -1) return false;
// handle all incoming data
for(c = net_connections; c < net_connections_end; c++) if(c->fast_p > c->fast_bf)
{
brainID = c - net_connections;
cmdSelectBrain(brainID);
if(cmdGetBrain()->Status != BRAIN_UNUSED)
for(p = c->fast_bf; p = netStringShift(p, c); p += strlen(p) + 1)
cmdFromClient(p); // check, execute & route
else
{
// a new player awaits initialization
serverInitPlayer();
c->fast_p = c->fast_bf;
}
}
// check that there are still players in the game
// @@this check should be performed only by spawned server
// @@ (not dedicated server)
// @@ and only when a connection is lost
int cnt = 0;
for(c = net_connections; c < net_connections_end; c++)
if(c->socket_safe != -1) cnt++;
if(game->Brains->Status != BRAIN_UNUSED && !cnt)
{
printf("All players gone.\n");
return false;
}
// time to close frame?
if(game) if(serverGameThinkTempo()) serverGameThink();
return true;
}
/*=============================================================================
* SERVER MAIN LOOP
*/
void serverMainLoop()
{
consoleSetProcName("sv");
atexit(netReset);
// set up listening
if(netListen() == -1) exit(-1);
// and prepare for a new game
int serverCreateNewGameData();
if(serverCreateNewGameData()) exit(-1);
// loop
while(serverThink());
pf("quit.\n\n");
// serverMainLoop() must NEVER return!!
exit(0);
}
//EOF =========================================================================