1 | #include "server2.hpp" |
---|
2 | #include "nfserver.hpp" |
---|
3 | #include "nfclient.hpp" |
---|
4 | #include "dprint.hpp" |
---|
5 | #include "view.hpp" |
---|
6 | #include "jrand.hpp" |
---|
7 | #include "objects.hpp" |
---|
8 | #include "level.hpp" |
---|
9 | #include "dev.hpp" |
---|
10 | |
---|
11 | char *get_username(); |
---|
12 | int start_running=0; |
---|
13 | |
---|
14 | class client_descriptor |
---|
15 | { |
---|
16 | public : |
---|
17 | client_descriptor(int client_number); |
---|
18 | view *player; // has this player been inducted yet? |
---|
19 | client_descriptor *next; |
---|
20 | long requested_join,cx1,cy1,cx2,cy2; |
---|
21 | int cnum; |
---|
22 | } ; |
---|
23 | |
---|
24 | client_descriptor::client_descriptor(int client_number) |
---|
25 | { |
---|
26 | player=NULL; |
---|
27 | next=NULL; |
---|
28 | requested_join=0; |
---|
29 | cnum=client_number; |
---|
30 | } |
---|
31 | |
---|
32 | game_server *local_server=NULL; // created on server machine, NULL on all others |
---|
33 | |
---|
34 | |
---|
35 | game_server::game_server(int argc, char **argv, int port) |
---|
36 | { |
---|
37 | client_list=NULL; |
---|
38 | sync_check=0; // should we send sync packets to client? |
---|
39 | |
---|
40 | for (int i=1;i<argc;i++) |
---|
41 | { |
---|
42 | if (!strcmp(argv[i],"-sync")) |
---|
43 | sync_check=1; |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | |
---|
49 | |
---|
50 | |
---|
51 | |
---|
52 | |
---|
53 | game_server::~game_server() |
---|
54 | { |
---|
55 | for (client_descriptor *p=client_list;p;) |
---|
56 | { |
---|
57 | client_descriptor *q=p; |
---|
58 | p=p->next; |
---|
59 | delete q; |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | void game_server::receive_inputs() // reads inputs from all non-local clients |
---|
66 | { |
---|
67 | /* client_descriptor *last=NULL; |
---|
68 | packet pk; |
---|
69 | for (client_descriptor *p=client_list;p;) |
---|
70 | { |
---|
71 | int delete_me=0; |
---|
72 | if (p->connection) |
---|
73 | { |
---|
74 | if (p->player) // clients with players are required to send input |
---|
75 | { |
---|
76 | int error=!get_pkt(p->connection,pk); |
---|
77 | if (!error) |
---|
78 | pk.insert_into(next_out); |
---|
79 | else // on error delete the client |
---|
80 | delete_me=1; |
---|
81 | } else |
---|
82 | { |
---|
83 | if (p->connection->ready_to_read()) |
---|
84 | { |
---|
85 | if (!p->connection->get(pk)) |
---|
86 | delete_me=1; |
---|
87 | else |
---|
88 | { |
---|
89 | uchar cmd; |
---|
90 | long cx1,cy1,cx2,cy2; |
---|
91 | if (!pk.read((uchar *)&cmd,1) || cmd!=SCMD_JOIN_GAME || |
---|
92 | pk.read((uchar *)&cx1,4)!=4 || |
---|
93 | pk.read((uchar *)&cy1,4)!=4 || |
---|
94 | pk.read((uchar *)&cx2,4)!=4 || |
---|
95 | pk.read((uchar *)&cy2,4)!=4) |
---|
96 | delete_me=1; |
---|
97 | else |
---|
98 | { |
---|
99 | p->cx1=lltl(cx1); |
---|
100 | p->cy1=lltl(cy1); |
---|
101 | p->cx2=lltl(cx2); |
---|
102 | p->cy2=lltl(cy2); |
---|
103 | p->requested_join=1; // mark this client as wanting to join |
---|
104 | pk.insert_into(next_out); |
---|
105 | } |
---|
106 | |
---|
107 | } |
---|
108 | } |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | if (delete_me) |
---|
113 | { |
---|
114 | client_descriptor *del_me=p; |
---|
115 | p=p->next; |
---|
116 | if (last) |
---|
117 | last->next=p; |
---|
118 | else client_list=p; |
---|
119 | delete del_me; |
---|
120 | } else { last=p; p=p->next; } |
---|
121 | |
---|
122 | |
---|
123 | }*/ |
---|
124 | |
---|
125 | } |
---|
126 | |
---|
127 | void game_server::send_inputs() // pass collected inputs to all non-local clients |
---|
128 | { |
---|
129 | /* client_descriptor *last=NULL; |
---|
130 | if (sync_check) |
---|
131 | { |
---|
132 | next_out.write_byte(SCMD_SYNC); |
---|
133 | next_out.write_long(make_sync_long()); |
---|
134 | } |
---|
135 | |
---|
136 | next_out.write_byte(SCMD_END_OF_PACKET); // so clients knows when to stop reading |
---|
137 | |
---|
138 | for (client_descriptor *p=client_list;p;) |
---|
139 | { |
---|
140 | if (p->connection && p->player) |
---|
141 | { |
---|
142 | int error=!p->connection->send(next_out); |
---|
143 | if (error) |
---|
144 | { |
---|
145 | client_descriptor *del_me=p; |
---|
146 | p=p->next; |
---|
147 | if (last) |
---|
148 | last->next=p; |
---|
149 | else client_list=p; |
---|
150 | delete del_me; |
---|
151 | } else { last=p; p=p->next; } |
---|
152 | } else |
---|
153 | { |
---|
154 | last=p; |
---|
155 | p=p->next; |
---|
156 | } |
---|
157 | } */ |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | void game_server::check_for_clients() |
---|
162 | { |
---|
163 | |
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | void game_server::join_new_players() |
---|
168 | { |
---|
169 | /* int wait=0; |
---|
170 | client_descriptor *p=client_list; |
---|
171 | for (;p;p=p->next) |
---|
172 | if (p->requested_join) |
---|
173 | { |
---|
174 | |
---|
175 | view *f=player_list; |
---|
176 | for (;f && f->next;f=f->next); // find last player, add one for pn |
---|
177 | int i,st=0; |
---|
178 | for (i=0;i<total_objects;i++) |
---|
179 | if (!strcmp(object_names[i],"START")) |
---|
180 | st=i; |
---|
181 | |
---|
182 | game_object *o=create(current_start_type,0,0); |
---|
183 | game_object *start=current_level->get_random_start(320,NULL); |
---|
184 | if (start) { o->x=start->x; o->y=start->y; } |
---|
185 | else { o->x=100; o->y=100; } |
---|
186 | |
---|
187 | f->next=new view(o,NULL,f->player_number+1); |
---|
188 | o->set_controller(f->next); |
---|
189 | |
---|
190 | if (start) |
---|
191 | current_level->add_object_after(o,start); |
---|
192 | else |
---|
193 | current_level->add_object(o); |
---|
194 | |
---|
195 | view *v=f->next; |
---|
196 | |
---|
197 | v->cx1=p->cx1; |
---|
198 | v->cy1=p->cy1; |
---|
199 | v->cx2=p->cx2; |
---|
200 | v->cy2=p->cy2; |
---|
201 | p->player=v; |
---|
202 | v->Drawable=p->cnum; |
---|
203 | |
---|
204 | wait=1; |
---|
205 | p->requested_join=0; |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | if (wait) // wait for acknowedgement from everone then delete net file |
---|
210 | { |
---|
211 | packet pk; |
---|
212 | current_level->save("netstart.spe",1); |
---|
213 | printf("%d sync for save\n",make_sync_long()); |
---|
214 | |
---|
215 | client_descriptor *last=NULL; |
---|
216 | for (p=client_list;p;p=p->next) |
---|
217 | { |
---|
218 | if (p->player) |
---|
219 | { |
---|
220 | pk.write_byte(SCMD_JOIN_START); |
---|
221 | int error=!p->connection->send(pk); |
---|
222 | if (!error) |
---|
223 | { |
---|
224 | while (!p->connection->ready_to_read()) |
---|
225 | service_net_request(); |
---|
226 | } |
---|
227 | |
---|
228 | if (error || !p->connection->get(pk)) |
---|
229 | { |
---|
230 | if (!last) |
---|
231 | client_list=client_list->next; |
---|
232 | else last->next=p->next; |
---|
233 | delete p; |
---|
234 | } else |
---|
235 | last=p; |
---|
236 | } |
---|
237 | } |
---|
238 | unlink("netstart.spe"); |
---|
239 | }*/ |
---|
240 | } |
---|
241 | |
---|
242 | |
---|
243 | |
---|
244 | /* |
---|
245 | |
---|
246 | server/client interaction |
---|
247 | |
---|
248 | |
---|
249 | Client - get/send commands |
---|
250 | |
---|
251 | Server - receive inputs |
---|
252 | check for join request |
---|
253 | if join request add SCMD_JOIN_GAME to out packet |
---|
254 | send inputs |
---|
255 | |
---|
256 | Client - read commands from server |
---|
257 | process commands |
---|
258 | tick_game |
---|
259 | draw |
---|
260 | |
---|
261 | |
---|
262 | Server (if join request) : |
---|
263 | create new_player |
---|
264 | save level to netstart.spe |
---|
265 | wait for all clients with views to send SCMD_NEW_ACK |
---|
266 | new player should read entire level, while old |
---|
267 | clients seek to "player_info" and read this. |
---|
268 | |
---|
269 | |
---|
270 | |
---|
271 | |
---|
272 | |
---|
273 | |
---|
274 | */ |
---|