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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
// Emacs style mode select -*- C++ -*-
//---------------------------------------------------------------------------
//
// $Id: swasynio.c,v 1.4.2.1 2003/06/08 18:16:38 fraggle Exp $
//
// Copyright(C) 1984-2000 David L. Clark
// Copyright(C) 2001-2003 Simon Howard
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at your
// option) any later version. 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; if not, write to the Free Software Foundation, Inc., 59 Temple
// Place - Suite 330, Boston, MA 02111-1307, USA.
//
//---------------------------------------------------------------------------
//
// swasynio - SW asynchrounous communications I/O
//
//---------------------------------------------------------------------------
#include <ctype.h>
#include "tcpcomm.h"
#include "timer.h"
#include "video.h"
#include "sw.h"
#include "swasynio.h"
#include "swdisp.h"
#include "swend.h"
#include "swgames.h"
#include "swinit.h"
#include "swmain.h"
#include "swmisc.h"
#include "swmove.h"
#include "swsound.h"
#include "swtitle.h"
asynmode_t asynmode;
char asynhost[128];
static int lastkey = 0; /* Always behind one character */
static int timeout_time;
static void settimeout(int ms)
{
timeout_time = Timer_GetMS() + ms;
}
static BOOL timeout()
{
return Timer_GetMS() >= timeout_time;
}
static inline void sendshort(int s)
{
commout(s & 0xff);
commout((s >> 8) & 0xff);
}
static inline int readshort()
{
int s, t;
while ((s = commin()) < 0) {
if (timeout()) {
fprintf(stderr, "readshort: timeout on read\n");
exit(-1);
}
}
while ((t = commin()) < 0) {
if (timeout()) {
fprintf(stderr, "readshort: timeout on read\n");
exit(-1);
}
}
return (t << 8) + s;
}
static int asynin()
{
register int c;
settimeout(500);
for (;;) {
if ((c = commin()) >= 0)
return c;
if (ctlbreak())
swend(NULL, NO);
if (timeout())
return -1;
}
}
int asynget(OBJECTS * ob)
{
register int key;
if (ob->ob_index == player) {
key = lastkey;
lastkey = 0;
} else {
settimeout(1000);
key = readshort();
}
return key;
}
void asynput(int movekey)
{
static BOOL first = TRUE;
if (first)
first = FALSE;
else
swflush();
sendshort(movekey);
}
char *asynclos()
{
commterm();
return NULL;
}
// this function is called by the multiplayer planes
BOOL movemult(OBJECTS * obp)
{
register OBJECTS *ob;
int cmd;
plyrplane = compplane = FALSE;
endstat = endsts[currobx = (ob = obp)->ob_index];
cmd = asynget(ob);
interpret(ob, cmd);
/*
if (dispcnt) {
ob->ob_flaps = 0;
ob->ob_bombing = FALSE;
}
*/
if ((ob->ob_state == CRASHED || ob->ob_state == GHOSTCRASHED)
&& ob->ob_hitcount <= 0) {
if (ob->ob_life > QUIT) {
// sdh 25/10/2001: infinite lives in multiplayer
//++ob->ob_crashcnt;
initpln(ob);
}
}
return movepln(ob);
}
#define PROTOHEADER "SDLSOPWITH" VERSION
static void synchronize()
{
// check for header
// send the header ourselves first
char *buf;
char *p;
buf = malloc(sizeof(PROTOHEADER) + 5);
sprintf(buf, PROTOHEADER "%i", player);
for (p = buf; *p; ++p)
commout(*p);
// now listen for response
sprintf(buf, PROTOHEADER "%i", !player);
settimeout(2000);
for (p = buf; *p;) {
int c;
if (timeout()) {
fprintf(stderr,
"synchronize: timeout on connect\n");
exit(-1);
}
c = commin();
if (c >= 0) {
if (c == *p) {
++p;
} else {
fprintf(stderr,
"synchronize: invalid protocol header received!\n");
exit(-1);
}
}
}
free(buf);
if (player) {
settimeout(1000);
explseed = readshort();
printf("random seed: %i\n", explseed);
settimeout(1000);
conf_missiles = readshort() != 0;
conf_wounded = readshort() != 0;
conf_animals = readshort() != 0;
} else {
// send settings
sendshort(explseed);
printf("random seed: %i\n", explseed);
sendshort(conf_missiles);
sendshort(conf_wounded);
sendshort(conf_animals);
}
}
// setup tcp loop
static void tcploop_connect()
{
int time;
clrprmpt();
swputs(" Attempting to connect to\n ");
swputs(asynhost);
swputs(" ...");
Vid_Update();
commconnect(asynhost);
clrprmpt();
swputs(" Connected, waiting for other player\n");
Vid_Update();
// for the first 5 seconds, listen to see if theres another player
// there
for (time = Timer_GetMS() + 5000; Timer_GetMS() < time; ) {
int c;
if (ctlbreak()) {
fprintf(stderr, "tcploop_connect: user aborted\n");
exit(-1);
}
c = commin();
if (c >= 0) {
if (c == '?') {
commout('!');
player = 1;
return;
} else {
fprintf(stderr,
"tcploop_connect: invalid"
"char received\n");
exit(-1);
}
}
}
// now send a ? every second until we get a ! response
time = Timer_GetMS() + 1000;
for (;;) {
int c;
if (ctlbreak()) {
fprintf(stderr, "tcploop_connect: user aborted\n");
exit(-1);
}
c = commin();
if (c >= 0) {
if (c == '!') {
player = 0;
return;
} else {
fprintf(stderr,
"tcploop_connect: invalid"
"char received\n");
exit(-1);
}
}
if (Timer_GetMS() > time) {
commout('?');
time = Timer_GetMS() + 1000;
}
}
}
// setup connection
static void asyninit()
{
if (asynmode == ASYN_TCPLOOP) {
tcploop_connect();
} else if (asynmode == ASYN_LISTEN) {
swtitln();
clrprmpt();
swputs(" Listening for connection...");
Vid_Update();
commlisten();
player = 0;
} else if (asynmode == ASYN_CONNECT) {
swtitln();
clrprmpt();
swputs(" Attempting to connect to \n ");
swputs(asynhost);
swputs(" ...");
Vid_Update();
commconnect(asynhost);
player = 1;
} else {
fprintf(stderr, "unknown asynmode mode\n");
exit(-1);
}
}
void init1asy()
{
#ifndef TCPIP
fprintf(stderr, "TCP/IP support not compiled into binary!\n");
return;
#else
asyninit();
clrprmpt();
swputs(" Waiting for other player");
synchronize();
currgame = &swgames[0];
#endif
}
void init2asy()
{
register OBJECTS *ob;
OBJECTS *initpln();
if (!player)
initplyr(NULL);
ob = initpln(NULL);
ob->ob_drawf = dispmult;
ob->ob_movef = movemult;
ob->ob_clr = ob->ob_index % 2 + 1;
ob->ob_owner = ob;
ob->ob_state = FLYING;
oobjects[ob->ob_index] = *ob;
// movmem(ob, &oobjects[ob->ob_index], sizeof(OBJECTS));
if (player)
initplyr(NULL);
}
//---------------------------------------------------------------------------
//
// $Log: swasynio.c,v $
// Revision 1.4.2.1 2003/06/08 18:16:38 fraggle
// Fix networking and some compile bugs
//
// Revision 1.4 2003/04/05 22:55:11 fraggle
// Remove the FOREVER macro and some unused stuff from std.h
//
// Revision 1.3 2003/04/05 22:44:04 fraggle
// Remove some useless functions from headers, make them static if they
// are not used by other files
//
// Revision 1.2 2003/04/05 22:31:29 fraggle
// Remove PLAYMODE_MULTIPLE and swnetio.c
//
// Revision 1.1.1.1 2003/02/14 19:03:08 fraggle
// Initial Sourceforge CVS import
//
//
// sdh 14/2/2003: change license header to GPL
// sdh 26/03/2002: changed CGA_ to Vid_
// sdh 16/11/2001: TCPIP #define to disable TCP/IP support
// sdh 29/10/2001: harrykeys
// sdh 21/10/2001: rearranged file headers, added cvs tags
// sdh 21/10/2001: reformatted with indent, adjusted some code by hand
// to make more readable
// sdh 20/10/2001: rearranged netgame syncronisation, take game settings
// from the first (listening) player
// sdh 19/10/2001: removed all externs, this is done with headers now
// sdh 18/10/2001: converted all functions to ansi-style arguments
//
// 96-12-26 Remove IMAGINET network card address in "multaddr".
// 87-03-12 Allow asynch loopback for debugging.
// 87-03-09 Microsoft compiler.
// 85-04-03 Development
//
//---------------------------------------------------------------------------
|