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 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
|
/*
* Created by Hassan "Karajorma" Kazmi for the FreeSpace2 Source Code Project.
* You may not sell or otherwise commercially exploit the source or things you
* create based on the source.
*/
#include "network/multi_sexp.h"
#include "network/psnet2.h"
#include "network/multimsgs.h"
#include "parse/sexp.h"
#include "network/multi.h"
#include "network/multiutil.h"
#define CALLBACK_TERMINATOR 255
int TEMP_DATA_SIZE = -1;
#define TYPE_NOT_DATA 255
#define TYPE_SEXP_OPERATOR 0
#define TYPE_ARGUMENT_COUNT 1
#define TYPE_DATA_TERMINATES 2
#define TYPE_INT 3
#define TYPE_SHIP 4
#define TYPE_STRING 5
#define TYPE_PARSE_OBJECT 6
#define TYPE_BOOLEAN 7
#define TYPE_FLOAT 8
#define TYPE_SHORT 9
#define TYPE_USHORT 10
#define TYPE_OBJECT 11
// the type array holds information on the type of date held at the same index of the data array
// types are not sent to the client and the entire array could be replaced with a couple of variables indexing the end of
// the previous SEXP. However it is much more helpful when debugging to have the array
ubyte type[MAX_PACKET_SIZE];
int argument_count_index = -1; // index in the type and data arrays for the argument count
int current_argument_count = 0; // number of bytes the data for this SEXP currently takes up
// these 3 variable names must remain the same as those used in multimsgs.h in order for the macros to work
ubyte data[MAX_PACKET_SIZE];
int packet_size = 0;
int offset = 0;
bool callback_started = false;
int Multi_sexp_bytes_left = 0; // number of bytes in incoming packet that still require processing
int op_num = -1;
bool packet_flagged_invalid = false;
//forward declarations
void multi_sexp_ensure_space_remains(int data_size);
/**************************
HOST SIDE PACKET FUNCTIONS
*************************/
/**
* Set up the SEXP packet every frame when the game starts processing SEXPs.
*/
void initalise_sexp_packet()
{
if (!MULTIPLAYER_MASTER) {
return;
}
memset(data, 0, MAX_PACKET_SIZE);
memset(type, -1, MAX_PACKET_SIZE);
packet_size = 0;
argument_count_index = -1;
current_argument_count = 0;
}
/**
* Called when a server is currently processing a SEXP that needs to send an update to the clients.
*/
void multi_start_callback()
{
if (!MULTIPLAYER_MASTER) {
return;
}
callback_started = true;
//Write OP into the Type buffer.
type[packet_size] = TYPE_SEXP_OPERATOR;
//Write the SEXP_Operator number into the data buffer.
Assert (!Current_sexp_operator.empty());
ADD_INT(Current_sexp_operator.back());
//Store the next data index as we'll need it later to write the COUNT.
argument_count_index = packet_size;
// store an invalid count, we'll come back and store the correct value once we know what it is.
type[packet_size] = TYPE_ARGUMENT_COUNT;
ADD_INT(TEMP_DATA_SIZE);
}
/**
* Called when a server has written all the data it needs to write for this SEXP.
*/
void multi_end_callback()
{
if (!MULTIPLAYER_MASTER) {
return;
}
callback_started = false;
// something is wrong with the packet, blast it clean and start again
if (packet_flagged_invalid) {
initalise_sexp_packet();
packet_flagged_invalid = false;
return;
}
//write TERMINATOR into the Type and data buffers
type[packet_size] = TYPE_DATA_TERMINATES;
ubyte b = CALLBACK_TERMINATOR;
ADD_DATA(b);
//Write the COUNT into the data buffer at the index we saved earlier.
int temp_packet_size = packet_size;
packet_size = argument_count_index;
ADD_INT(current_argument_count);
packet_size = temp_packet_size;
current_argument_count = 0;
}
/**
* Convenience function that simply calls the two functions above. Used when the server merely needs to signal the clients that a
* SEXP has been processed but no additional data needs to be sent.
*/
void multi_do_callback()
{
multi_start_callback();
multi_end_callback();
}
/**
* Checks if there is enough space in the packet currently being stuffed for the data that is about to be written into it
*
* If there is not enough space, it will send everything in the packet apart from any data from the SEXP currently being processed
* and then create a new packet containing the data for this SEXP only.
*/
void multi_sexp_ensure_space_remains(int data_size)
{
if (!MULTIPLAYER_MASTER) {
return;
}
int packet_end = 0;
int sub_packet_size = 0;
int i, j;
//If the index of the data buffer isn't high enough yet, bail
if (packet_size + data_size + HEADER_LENGTH < MAX_PACKET_SIZE) {
return;
}
//iterate back through the types array until we find a TERMINATOR and store the corresponding data index
for (i = packet_size-1; i > 0; i--) {
if ( type[i] == TYPE_DATA_TERMINATES) {
packet_end = i;
break;
}
}
// we want the number of bytes not the index of the last one
sub_packet_size = packet_end + 1;
// At very least must include OP, COUNT, TERMINATOR
if (packet_end < 9 && !packet_flagged_invalid) {
Warning(LOCATION, "Sexp %s has attempted to write too much data to a single packet. It is advised that you split this SEXP up into smaller ones", Operators[Current_sexp_operator.back()].text );
packet_flagged_invalid = true;
return;
}
send_sexp_packet(data, sub_packet_size);
j = 0;
//Slide down any entries after the stored index to the start of the array.
for (i = sub_packet_size; i < packet_size; i++) {
data[j] = data[i];
type[j] = type[i];
j++;
}
packet_size = j;
// flush the remaining type buffer
for (i = j ; i < MAX_PACKET_SIZE ; i++) {
type[i] = (ubyte) -1;
}
// if we have an existing argument count we need to update where to put it too
if (current_argument_count) {
argument_count_index = argument_count_index - sub_packet_size;
}
Assert(argument_count_index >=0);
}
/**
* Flushes out the SEXP packet and sends any data still in there. Called when the game finishes processing SEXPs.
*/
void multi_sexp_flush_packet()
{
if (!MULTIPLAYER_MASTER) {
return;
}
// possible to get here when there is nothing in the packet to send
if (packet_size == 0){
return;
}
Assert (type[packet_size -1] == TYPE_DATA_TERMINATES);
Assert (!packet_flagged_invalid);
send_sexp_packet(data, packet_size);
initalise_sexp_packet();
}
/**
* Checks if the SEXP packet is ready to recieve data.
*/
bool cannot_send_data()
{
if (!MULTIPLAYER_MASTER || packet_flagged_invalid ) {
return true;
}
if (!callback_started) {
Warning (LOCATION, "Attempt to send data in multi_sexp.cpp without first starting a callback");
return true;
}
return false;
}
/********************************
HOST SIDE DATA WRAPPER FUNCTIONS
*******************************/
/**
* Add an int to the SEXP packet.
*/
void multi_send_int(int value)
{
if (cannot_send_data()) {
return;
}
multi_sexp_ensure_space_remains(sizeof(value));
//Write INT into the Type buffer.
type[packet_size] = TYPE_INT;
//Write the int into the data buffer
ADD_INT(value);
//Increment the COUNT by 4 (i.e the size of an int).
current_argument_count += sizeof(int);
}
/**
* Adds a ship's net sig to the SEXP packet.
*/
void multi_send_ship(int shipnum)
{
if (cannot_send_data()) {
return;
}
multi_sexp_ensure_space_remains(sizeof(shipnum));
multi_send_ship(&Ships[shipnum]);
}
/**
* Adds a ship's net sig to the SEXP packet.
*/
void multi_send_ship(ship *shipp)
{
if (cannot_send_data()) {
return;
}
multi_sexp_ensure_space_remains(sizeof(ushort));
//write into the Type buffer.
type[packet_size] = TYPE_SHIP;
//write the into the data buffer
ADD_USHORT(Objects[shipp->objnum].net_signature);
current_argument_count += sizeof(ushort);
}
/**
* Add the net sig of an object to the SEXP packet.
*/
void multi_send_object(object *objp)
{
if (cannot_send_data()) {
return;
}
multi_sexp_ensure_space_remains(sizeof(ushort));
//write into the Type buffer.
type[packet_size] = TYPE_OBJECT;
//write the into the data buffer
ADD_USHORT(objp->net_signature);
current_argument_count += sizeof(ushort);
}
/**
* Add the net sig of a parse object to the SEXP packet.
*/
void multi_send_parse_object(p_object *pobjp)
{
if (cannot_send_data()) {
return;
}
multi_sexp_ensure_space_remains(sizeof(ushort));
//write into the Type buffer.
type[packet_size] = TYPE_PARSE_OBJECT;
//write the into the data buffer
ADD_USHORT(pobjp->net_signature);
current_argument_count += sizeof(ushort);
}
/**
* Add a string to the SEXP packet. Should only be used for strings TOKEN_LENGTH in size or smaller.
*/
void multi_send_string(char *string)
{
if (cannot_send_data()) {
return;
}
multi_sexp_ensure_space_remains(strlen(string)+4);
int start_size = packet_size;
//write into the Type buffer.
type[packet_size] = TYPE_STRING;
//write the into the data buffer
ADD_STRING(string);
current_argument_count += packet_size - start_size;
}
void multi_send_string(const SCP_string &string)
{
if (cannot_send_data()) {
return;
}
multi_sexp_ensure_space_remains(string.length()+4);
int start_size = packet_size;
//write into the Type buffer.
type[packet_size] = TYPE_STRING;
//write the into the data buffer
ADD_STRING(string.c_str());
current_argument_count += packet_size - start_size;
}
/**
* Add a boolean to the SEXP packet.
*/
void multi_send_bool(bool value)
{
if (cannot_send_data()) {
return;
}
multi_sexp_ensure_space_remains(sizeof(value));
//write into the Type buffer.
type[packet_size] = TYPE_BOOLEAN;
//Write the value into the data buffer
ADD_DATA(value);
//Increment the COUNT
current_argument_count += sizeof(value);
}
/**
* Add a float to the SEXP packet.
*/
void multi_send_float(float value)
{
if (cannot_send_data()) {
return;
}
multi_sexp_ensure_space_remains(sizeof(value));
//write into the Type buffer.
type[packet_size] = TYPE_FLOAT;
//Write the value into the data buffer
ADD_FLOAT(value);
//Increment the COUNT
current_argument_count += sizeof(float);
}
/**
* Add a short to the SEXP packet.
*/
void multi_send_short(short value)
{
if (cannot_send_data()) {
return;
}
multi_sexp_ensure_space_remains(sizeof(value));
//Write the type into the Type buffer.
type[packet_size] = TYPE_SHORT;
//Write the value into the data buffer
ADD_SHORT(value);
//Increment the COUNT
current_argument_count += sizeof(short);
}
/**
* Add an unsigned short to the SEXP packet.
*/
void multi_send_ushort(ushort value)
{
if (cannot_send_data()) {
return;
}
multi_sexp_ensure_space_remains(sizeof(value));
//Write the type into the Type buffer.
type[packet_size] = TYPE_USHORT;
//Write the value into the data buffer
ADD_USHORT(value);
//Increment the COUNT
current_argument_count += sizeof(ushort);
}
/****************************
CLIENT SIDE PACKET FUNCTIONS
***************************/
/**
* Called when the client recieves a SEXP packet from the server. Removes the data from the packet that underlying code uses and puts
* into a new array that will work with the rest of the client-side code.
*/
void sexp_packet_received(ubyte *received_packet, int num_ubytes)
{
int i;
offset = 0;
op_num = -1;
for (i=0; i < MAX_PACKET_SIZE; i++) {
data[i] = received_packet[i];
}
Multi_sexp_bytes_left = num_ubytes;
// start working through the packet
multi_sexp_eval();
}
/**
* Checks that the previous SEXP in the packet has correctly removed all its data from the packet. Attempts to fix it if it hasn't.
*/
bool argument_count_is_valid()
{
if (current_argument_count != 0) {
// we have a problem here, either the argument count is wrong or the last SEXP didn't remove all its data from the packet
ubyte possible_terminator;
bool terminator_found = false;
for (int i=0; i < current_argument_count ; i++) {
GET_DATA(possible_terminator);
Multi_sexp_bytes_left--;
if (possible_terminator == CALLBACK_TERMINATOR) {
Warning(LOCATION, "%s has returned to multi_sexp_eval() claiming %d arguments left. %d actually found. Trace out and fix this!", Operators[op_num].text, current_argument_count, i);
terminator_found = true;
break;
}
}
// if we still haven't found the terminator it probably means the last SEXP didn't remove all its data from the packet
if (!terminator_found) {
GET_DATA(possible_terminator);
Multi_sexp_bytes_left--;
if (possible_terminator != CALLBACK_TERMINATOR) {
// discard remainder of packet if we still haven't found the terminator as it is hopelessly corrupt
Warning(LOCATION, "%s has returned to multi_sexp_eval() without finding the terminator. Discarding packet! Trace out and fix this!", Operators[op_num].text);
Multi_sexp_bytes_left = 0;
return false;
}
else {
// the previous SEXP hasn't removed all it's data from the packet correctly but it appears we've managed to fix it
Warning(LOCATION, "%s has returned to multi_sexp_eval() without removing all the data the server wrote during its callback. Trace out and fix this!", Operators[op_num].text);
op_num = -1;
}
}
}
return true;
}
/**
* Gets the next operator from the SEXP packet. Returns the number of the operator or -1 if there are no more operators because the
* packet is corrupt.
*/
int multi_sexp_get_next_operator()
{
if (!argument_count_is_valid()) {
return -1;
}
GET_INT(op_num);
Multi_sexp_bytes_left -= sizeof(int);
GET_INT(current_argument_count);
Multi_sexp_bytes_left -= sizeof(int);
Assert (Multi_sexp_bytes_left);
return op_num;
}
/**
* Returns the current operator number but does not touch the SEXP packet.
*/
int multi_sexp_get_operator()
{
return op_num;
}
/**
* Ensures that the variables tracking how much data is left in the packet are updated correctly when data is removed.
*/
void multi_reduce_counts(int amount)
{
Multi_sexp_bytes_left -= amount;
current_argument_count -= amount;
if (Multi_sexp_bytes_left < 0 || current_argument_count < 0) {
Warning(LOCATION, "multi_get_x function call has read an invalid amount of data. Trace out and fix this!");
}
}
/**
* Called when the SEXP code has finished processing the current SEXP.
*/
void multi_finished_callback()
{
ubyte terminator;
Assert(current_argument_count == 0);
// read in the terminator
GET_DATA(terminator);
if (terminator != CALLBACK_TERMINATOR) {
Warning(LOCATION, "multi_get_x function call has been called on an improperly terminated callback. Trace out and fix this!");
// discard remainder of packet
Multi_sexp_bytes_left = 0;
return;
}
Multi_sexp_bytes_left--;
op_num = -1;
}
/**
* Used to discard the rest of a callback on the client machine.
*/
bool multi_sexp_discard_operator()
{
int i;
ubyte dummy;
ubyte terminator;
// read in a number of bytes equal to the count
for (i=0; i<current_argument_count; i++) {
GET_DATA(dummy);
Multi_sexp_bytes_left--;
}
GET_DATA(terminator);
Multi_sexp_bytes_left--;
op_num = -1;
// the operation terminated correctly, probably a new SEXP that this version doesn't support.
if (terminator == CALLBACK_TERMINATOR)
return true;
// packet is probably corrupt
else
return false;
}
/**********************************
CLIENT SIDE DATA WRAPPER FUNCTIONS
*********************************/
/**
* Attempts to remove an int from the SEXP packet and assign it to the value parameter. Returns false if it is unable to do so.
*/
bool multi_get_int(int &value)
{
if (!Multi_sexp_bytes_left || !current_argument_count) {
return false;
}
GET_INT(value);
multi_reduce_counts(sizeof(int));
return true;
}
/**
* Attempts to get an index for the Ships array based on the net sig it removes from the SEXP packet. Returns it as the value
* parameter. Returns false if unable to do so.
*/
bool multi_get_ship(int &value)
{
ushort netsig;
object *objp;
if (!Multi_sexp_bytes_left || !current_argument_count) {
return false;
}
// get the net signature of the ship
GET_USHORT(netsig);
multi_reduce_counts(sizeof(ushort));
// lookup the object
objp = multi_get_network_object(netsig);
if((objp != NULL) && (objp->type == OBJ_SHIP) && (objp->instance >=0)){
value = objp->instance;
return true;
}
Warning(LOCATION, "multi_get_ship called for object %d even though it is not a ship", objp->instance);
return false;
}
/**
* Attempts to get a ship pointer based on the net sig it removes from the SEXP packet. Returns it as the value parameter.
* Returns false if unable to do so.
*/
bool multi_get_ship(ship* &shipp)
{
int shipnum;
if (multi_get_ship(shipnum)) {
shipp = &Ships[shipnum];
return true;
}
return false;
}
/**
* Attempts to get an object pointer based on the net sig it removes from the SEXP packet. Returns it as the value parameter.
* Returns false if unable to do so.
*/
bool multi_get_object(object*& objp)
{
ushort netsig;
if (!Multi_sexp_bytes_left || !current_argument_count) {
return false;
}
// get the net signature of the ship
GET_USHORT(netsig);
multi_reduce_counts(sizeof(ushort));
// lookup the object
objp = multi_get_network_object(netsig);
if((objp != NULL) && (objp->instance >=0)){
return true;
}
Warning(LOCATION, "multi_get_object called for non-existent object");
return false;
}
/**
* Attempts to get a parse objects pointer based on the net sig it removes from the SEXP packet. Returns it as the value parameter.
* Returns false if unable to do so.
*/
bool multi_get_parse_object(p_object*& pobjp)
{
ushort netsig;
if (!Multi_sexp_bytes_left || !current_argument_count) {
return false;
}
// get the net signature of the ship
GET_USHORT(netsig);
multi_reduce_counts(sizeof(ushort));
// lookup the object
pobjp = mission_parse_get_arrival_ship(netsig);
if(pobjp != NULL){
return true;
}
return false;
}
/**
* Attempts to remove a string from the SEXP packet and assign it to the value parameter. Returns false if it is unable to do so.
*/
bool multi_get_string(char *buffer)
{
int starting_offset = offset;
if (!Multi_sexp_bytes_left || !current_argument_count) {
return false;
}
GET_STRING(buffer);
multi_reduce_counts(offset - starting_offset);
return true;
}
bool multi_get_string(SCP_string &buffer)
{
char tempstring[TOKEN_LENGTH];
int starting_offset = offset;
if (!Multi_sexp_bytes_left || !current_argument_count) {
return false;
}
GET_STRING(tempstring);
buffer = tempstring;
multi_reduce_counts(offset - starting_offset);
return true;
}
/**
* Attempts to remove a boolean from the SEXP packet and assign it to the value parameter. Returns false if it is unable to do so.
*/
bool multi_get_bool(bool &value)
{
if (!Multi_sexp_bytes_left || !current_argument_count) {
return false;
}
GET_DATA(value);
multi_reduce_counts(sizeof(value));
return true;
}
/**
* Attempts to remove a float from the SEXP packet and assign it to the value parameter. Returns false if it is unable to do so.
*/
bool multi_get_float(float &value)
{
if (!Multi_sexp_bytes_left || !current_argument_count) {
return false;
}
GET_FLOAT(value);
multi_reduce_counts(sizeof(float));
return true;
}
/**
* Attempts to remove a short from the SEXP packet and assign it to the value parameter. Returns false if it is unable to do so.
*/
bool multi_get_short(short &value)
{
if (!Multi_sexp_bytes_left || !current_argument_count) {
return false;
}
GET_SHORT(value);
multi_reduce_counts(sizeof(short));
return true;
}
/**
* Attempts to remove an unsigned short from the SEXP packet and assign it to the value parameter. Returns false if it is unable to do so.
*/
bool multi_get_ushort(ushort &value)
{
if (!Multi_sexp_bytes_left || !current_argument_count) {
return false;
}
GET_USHORT(value);
multi_reduce_counts(sizeof(ushort));
return true;
}
/**
* attempts to remove all remaining data for the current operator.
*/
void multi_discard_remaining_callback_data()
{
if (!multi_sexp_discard_operator()) {
Warning(LOCATION, "Attempt to discard remaining data failed! Callback for operator %d lacks proper termination. Entire packet may be corrupt. Discarding remaining packet");
}
}
|