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 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
|
/*
* XEvil(TM) Copyright (C) 1994,2000 Steve Hardt and Michael Judge
* http://www.xevil.com
* satan@xevil.com
*
* 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, the file "gpl.txt"; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA, or visit http://www.gnu.org.
*/
// "xetp.cpp"
#if X11
#ifndef NO_PRAGMAS
#pragma implementation "xetp.h"
#endif
#endif
#include "stdafx.h"
#include <iostream>
#if X11
#include <sstream>
#endif
#if WIN32
#include <strstrea.h>
#endif
// Is this include file necessary??
#if WIN32
#include <signal.h>
#endif
#include "utils.h"
#include "intel.h"
#include "streams.h"
#include "physical.h"
#include "world.h"
#include "locator.h"
#include "xetp.h"
using namespace std;
void XETP::check_sizes() {
XETPBasic::check_sizes();
assert(sizeof(ClassId) == sizeof(int));
assert(sizeof(Dir) == sizeof(int));
assert(sizeof(Health) == sizeof(int));
assert(sizeof(ColorNum) == sizeof(int));
assert(sizeof(ITcommand) == sizeof(int));
assert(sizeof(Mass) == sizeof(int));
assert(sizeof(Turn) == sizeof(int));
assert(sizeof(GameStyleType) == sizeof(int));
assert(sizeof(SoundName) == sizeof(int));
assert(sizeof(ABBitField) == sizeof(int));
// Make sure the element fits in a PtrList.
// >= instead of == because on an Alpha,
// void* is 8 while int and TickType are 4.
assert(sizeof(void *) >= sizeof(TickType));
assert(sizeof(void *) >= sizeof(int));
}
void XETP::send_tcp_connect(OutStreamP out,u_short udpPort,char *humanName,
const ViewportInfo &vInfo,int skip,Boolean wantSounds) {
assert(humanName); // can still be "".
u_int len =
sizeof(u_short) + // udpPort
Utils::get_string_write_length(humanName) + // humanName
ViewportInfo::get_write_length(); // vInfo
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->prepare_packet(XETP::add_header(len));
}
send_header(out,TCP_CONNECT,len);
out->write_short(udpPort);
Utils::string_write(out,humanName);
vInfo.write(out);
out->write_int(skip);
out->write_char((char)wantSounds);
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->done_packet();
}
}
void XETP::send_human(OutStreamP out,HumanP human) {
u_int len =
Identifier::get_write_length() + // intelId
human->get_write_length(); // human data
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->prepare_packet(XETP::add_header(len));
}
send_header(out,HUMAN,len);
IntelId intelId = human->get_intel_id();
intelId.write(out);
human->write(out);
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->done_packet();
}
}
void XETP::send_object(OutStreamP out,PhysicalP p,Turn turn,TickType tt) {
u_int len = compute_object_length(p);
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->prepare_packet(XETP::add_header(len));
}
_send_object(out,p,turn,tt,len);
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->done_packet();
}
}
void XETP::send_objects(OutStreamP out,const PtrList &physicals,
Turn turn,const PtrList &tickTypes) {
assert(physicals.length() == tickTypes.length());
u_int totalLen = 0;
int n;
for (n = 0; n < physicals.length(); n++) {
totalLen +=
XETP::add_header(compute_object_length((PhysicalP)physicals.get(n)));
}
// length passed into UDP prepare_packet is bigger than the lengths
// passed into each XETP packet.
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->prepare_packet(totalLen);
}
// Send each object in turn.
for (n = 0; n < physicals.length(); n++) {
PhysicalP p = (PhysicalP)physicals.get(n);
// computing object_length twice, but who cares.
_send_object(out,p,turn,(long)tickTypes.get(n),
compute_object_length(p));
}
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->done_packet();
}
}
u_int XETP::compute_object_length(PhysicalP p) {
u_int len = sizeof(u_short) + // classId
Identifier::get_write_length() + // id
sizeof(Turn) + // turn
sizeof(char) + // TickType
p->get_write_length(); // object data
return len;
}
void XETP::_send_object(OutStreamP out,PhysicalP p,Turn turn,
TickType tt,u_int len) {
// len does not include the XETP header.
const PhysicalContext *cx = p->get_context();
// Only send objects that can be read back in.
assert(cx->create_from_stream);
Id id = p->get_id();
send_header(out,OBJECT,len);
assert(p->get_class_id() <= USHRT_MAX);
out->write_short((u_short)p->get_class_id());
id.write(out);
out->write_int((int)turn);
assert(tt <= UCHAR_MAX);
out->write_char((char)tt);
p->write(out);
}
void XETP::send_new_world(OutStreamP out,WorldP world,int worldVersion) {
u_int len = sizeof(int) + // version
world->get_write_length(); // world data
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->prepare_packet(XETP::add_header(len));
}
send_header(out,NEW_WORLD,len);
out->write_int(worldVersion);
world->write(out);
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->done_packet();
}
}
void XETP::send_world_room(OutStreamP out,WorldP world,
const RoomIndex &idx,int worldVersion) {
u_int len = sizeof(int) + // version
Rooms::get_write_length() + // rooms
RoomIndex::get_write_length() + // roomIndex
Dim::get_write_length() + // dim
world->get_write_length(idx); // world room data
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->prepare_packet(XETP::add_header(len));
}
send_header(out,WORLD_ROOM,len);
out->write_int(worldVersion);
Rooms rooms = world->get_rooms();
rooms.write(out);
idx.write(out);
Dim dim = world->get_dim();
dim.write(out);
world->write(out,idx); // write one room
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->done_packet();
}
}
void XETP::send_rooms_known(OutStreamP out,const Rooms &rooms,
int worldVersion,Boolean *map) {
int mapSize = rooms.acrossMax * rooms.downMax;
u_int len = sizeof(int) + // version
Rooms::get_write_length() + // rooms
sizeof(char) * mapSize; // map
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->prepare_packet(XETP::add_header(len));
}
send_header(out,ROOMS_KNOWN,len);
out->write_int(worldVersion);
rooms.write(out);
for (int n = 0; n < mapSize; n++) {
out->write_char((u_char)map[n]);
}
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->done_packet();
}
}
void XETP::send_message(OutStreamP out,const char *msg) {
assert(msg);
u_int len = strlen(msg);
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->prepare_packet(XETP::add_header(len));
}
send_header(out,MESSAGE,len);
// signed/unsigned shouldn't matter here, ASCII only.
out->write((u_char *)msg,len);
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->done_packet();
}
}
void XETP::send_arena_message(OutStreamP out,int time,const char *msg) {
assert(msg);
u_int len = sizeof(int) + // time
strlen(msg); // string body
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->prepare_packet(XETP::add_header(len));
}
send_header(out,ARENA_MESSAGE,len);
out->write_int(time);
// signed/unsigned shouldn't matter here.
out->write(msg,len - sizeof(int));
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->done_packet();
}
}
void XETP::send_command(OutStreamP out,
const IntelId &iId,ITcommand command) {
u_int len =
Identifier::get_write_length() + // intelId
sizeof(char); // command
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->prepare_packet(XETP::add_header(len));
}
send_header(out,COMMAND,len);
iId.write(out);
assert(command <= UCHAR_MAX);
out->write_char((u_char)command);
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->done_packet();
}
}
void XETP::send_delete_object(OutStreamP out,const Id &id) {
u_int len =
Identifier::get_write_length(); // Id
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->prepare_packet(XETP::add_header(len));
}
send_header(out,DELETE_OBJECT,len);
id.write(out);
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->done_packet();
}
}
void XETP::send_timer_pong(OutStreamP out,Turn pingTurn,Turn localTurn) {
u_int len =
2 * sizeof(Turn);
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->prepare_packet(XETP::add_header(len));
}
send_header(out,TIMER_PONG,len);
out->write_int((int)pingTurn);
out->write_int((int)localTurn);
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->done_packet();
}
}
void XETP::send_sound_request(OutStreamP out,const SoundRequest &request) {
u_int len =
SoundRequest::get_write_length();
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->prepare_packet(XETP::add_header(len));
}
send_header(out,SOUND_REQUEST,len);
request.write(out);
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->done_packet();
}
}
void XETP::send_server_pong(OutStreamP out,GameStyleType gameStyle,
int enemiesNum,int humansNum,
const char* version,
const char* names[],const char* clientNames[],
int humanKills[],
int enemyKills[],const Id ids[]) {
u_int len =
sizeof(u_char) + // gameStyle
sizeof(u_int) + // enemiesNum
sizeof(u_short) + // humansNum
Utils::get_string_write_length(version); // version
int n;
for (n = 0; n < humansNum; n++) {
len += Utils::get_string_write_length(names[n]); // name
len += Utils::get_string_write_length(clientNames[n]); // name
len += sizeof(u_int); // humanKills
len += sizeof(u_int); // enemyKills
len += Id::get_write_length(); // Id
}
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->prepare_packet(XETP::add_header(len));
}
assert(gameStyle < 256 && enemiesNum >= 0 && humansNum >= 0 &&
(((u_int)humansNum & 0xffff0000) == 0));
send_header(out,SERVER_PONG,len);
out->write_char((u_char)gameStyle);
out->write_int((u_int)enemiesNum);
out->write_short((u_short)humansNum);
Utils::string_write(out,version);
// Write out data for each human.
for (n = 0; n < humansNum; n++) {
Utils::string_write(out,names[n]);
Utils::string_write(out,clientNames[n]);
out->write_int((u_int)humanKills[n]);
out->write_int((u_int)enemyKills[n]);
ids[n].write(out);
}
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->done_packet();
}
}
void XETP::send_chat_request(OutStreamP out,const char* receiver,
const char* message) {
u_int len =
Utils::get_string_write_length(receiver) + // receiverName
Utils::get_string_write_length(message); // message
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->prepare_packet(XETP::add_header(len));
}
send_header(out,CHAT_REQUEST,len);
Utils::string_write(out,receiver);
Utils::string_write(out,message);
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->done_packet();
}
}
void XETP::send_chat(OutStreamP out,const char* sender,
const IntelId& senderId,const char* message) {
u_int len =
Utils::get_string_write_length(sender) + // sender
IntelId::get_write_length() + // senderId
Utils::get_string_write_length(message); // message
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->prepare_packet(XETP::add_header(len));
}
send_header(out,CHAT,len);
Utils::string_write(out,sender);
senderId.write(out);
Utils::string_write(out,message);
if (out->get_protocol() == GenericStream::UDP) {
((UDPOutStreamP)out)->done_packet();
}
}
|