Changeset 6219 in orxonox.OLD for branches/network/src/lib
- Timestamp:
- Dec 21, 2005, 1:39:20 PM (19 years ago)
- Location:
- branches/network/src/lib/network
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/network/src/lib/network/converter.cc
r6197 r6219 54 54 { 55 55 const int mod = 256; // = 2^8 56 57 56 57 58 58 byte* result = new byte[INTSIZE]; 59 59 int sgn; … … 65 65 x = -x; 66 66 } 67 67 68 68 for (int i = 0; i < INTSIZE; i++) 69 69 { … … 71 71 x /= mod; 72 72 } 73 73 74 74 if (sgn == -1) 75 75 result[INTSIZE - 1] += sgnadd; 76 77 76 77 78 78 return result; 79 79 } … … 84 84 * @return: An int that accords the given byte-array 85 85 */ 86 int Converter::byteArrayToInt( byte* a)86 int Converter::byteArrayToInt(const byte* a) 87 87 { 88 88 int mult = 1; … … 102 102 else 103 103 result += a[INTSIZE - 1] * mult; 104 104 105 105 return result; 106 106 } … … 113 113 char* result = new char[200]; 114 114 int pos = 0; 115 115 116 116 if (x < 0) 117 117 { … … 119 119 x = -x; 120 120 } 121 121 122 122 float sub = 1; 123 123 while (sub < x) … … 135 135 pos++; 136 136 sub /= 2; 137 137 138 138 if (sub == 0.5f) 139 139 result[pos++] = '.'; 140 140 } 141 141 142 142 return result; 143 143 } … … 154 154 int mantisse = 0; 155 155 int exponent = 128; 156 156 157 157 int sgn; 158 158 if (x < 0) … … 163 163 else 164 164 sgn = 1; 165 165 166 166 float sub = 1; 167 167 while (sub < x) … … 178 178 x -= sub; 179 179 } 180 180 181 181 mantisse *= 2; 182 182 exponent--; … … 190 190 exponent--; 191 191 } 192 193 //printf("mantisse = %i exponent = %i \n", mantisse, exponent); 194 192 193 //printf("mantisse = %i exponent = %i \n", mantisse, exponent); 194 195 195 mantisse -= expmult; 196 196 197 197 int hx = mantisse + expmult * exponent; 198 198 byte* result = intToByteArray(hx); 199 199 if (sgn == -1) 200 200 result[3] += sgnadd; 201 201 202 202 return result; 203 203 } … … 214 214 int mantisse = hmant % expmult; 215 215 mantisse += expmult; 216 216 217 217 int hexp = a[2] + a[3] * 256; 218 218 int exponent = (hexp / 128) % 256 - 128; 219 219 220 220 int sgn; 221 221 if (a[3] >= sgnadd) … … 223 223 else 224 224 sgn = 1; 225 225 226 226 //printf("mantisse = %i exponent = %i \n", mantisse, exponent); 227 227 228 228 float emult = 1; 229 229 if (exponent > 0) … … 233 233 for (int i = 0; i > exponent; i--) 234 234 emult /= 2; 235 235 236 236 float result = mantisse * emult; 237 237 if (sgn == -1) 238 238 result = -result; 239 239 240 240 return result; 241 241 } -
branches/network/src/lib/network/converter.h
r6197 r6219 3 3 * Is able to convert int to byte-array and vice versa 4 4 */ 5 5 6 6 #ifndef _CONVERTER 7 7 #define _CONVERTER … … 23 23 public: 24 24 static byte* intToByteArray(int x); 25 static int byteArrayToInt( byte* a);26 25 static int byteArrayToInt(const byte* a); 26 27 27 //byte* floatToByteArray(float x); 28 28 //float byteArrayToFloat(byte[] a); 29 29 30 30 //Test 31 31 static char* floatToBinString(float x); 32 32 33 33 static byte* floatToByteArray(float x); 34 34 static float byteArrayToFloat(byte* a); 35 35 36 36 static byte* _floatToByteArray(float x); 37 37 static float _byteArrayToFloat(byte* a); -
branches/network/src/lib/network/network_game_manager.cc
r6214 r6219 73 73 b = data[i++]; 74 74 75 if ( b == REQUEST_CREATE ) 76 { 77 return; 78 } 79 if ( b == REQUEST_REMOVE ) 80 { 81 return; 82 } 83 if ( b == CREATE_ENTITY ) 84 { 85 return; 86 } 87 if ( b == REMOVE_ENTITY ) 88 { 89 return; 90 } 75 if ( isServer() ) 76 { 77 if ( b == REQUEST_CREATE ) 78 { 79 if ( !handleRequestCreate( i, data, length, sender ) ) 80 return; 81 continue; 82 } 83 if ( b == REQUEST_REMOVE ) 84 { 85 if ( !handleRequestRemove( i, data, length, sender ) ) 86 return; 87 continue; 88 } 89 } 90 91 if ( !isServer() ) 92 { 93 if ( b == CREATE_ENTITY ) 94 { 95 if ( !handleCreateEntity( i, data, length, sender ) ) 96 return; 97 continue; 98 } 99 if ( b == REMOVE_ENTITY ) 100 { 101 if ( !handleRemoveEntity( i, data, length, sender ) ) 102 return; 103 continue; 104 } 105 if ( b == CREATE_ENTITY_LIST ) 106 { 107 if ( !handleCreateEntityList( i, data, length, sender ) ) 108 return; 109 continue; 110 } 111 if ( b == REMOVE_ENTITY_LIST ) 112 { 113 if ( !handleRemoveEntityList( i, data, length, sender ) ) 114 return; 115 continue; 116 } 117 if ( b == YOU_ARE_ENTITY ) 118 { 119 if ( !handleYouAreEntity( i, data, length, sender ) ) 120 return; 121 continue; 122 } 123 } 124 91 125 if ( b == REQUEST_SYNC ) 92 126 { 93 return; 94 } 95 if ( b == YOU_ARE_ENTITY ) 96 { 97 return; 98 } 99 if ( b == CREATE_ENTITY_LIST ) 100 { 101 return; 102 } 103 if ( b == REMOVE_ENTITY_LIST ) 104 { 105 return; 106 } 127 if ( !handleRequestSync( i, data, length, sender ) ) 128 return; 129 continue; 130 } 131 132 //if we get her something with data is wrong 133 PRINTF(1)("Data is not in the right format! i=%d\n", i); 134 return; 107 135 } 108 136 } … … 156 184 * @param classID: The ID of the class of which an entity should be created 157 185 */ 158 void NetworkGameManager::createEntity( ClassID classID)186 void NetworkGameManager::createEntity( ClassID classID, int owner ) 159 187 { 160 188 if ( this->isServer() ) … … 166 194 } 167 195 168 this->executeCreateEntity( classID, newUniqueID++ );196 this->executeCreateEntity( classID, newUniqueID++, owner ); 169 197 } 170 198 else 171 199 { 172 this->requestCreateEntity( classID );200 this->requestCreateEntity( classID ); 173 201 } 174 202 } … … 570 598 } 571 599 600 bool NetworkGameManager::handleRequestCreate( int & i, const byte * data, int length, int sender ) 601 { 602 if ( INTSIZE > length-i ) 603 { 604 PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n"); 605 return false; 606 } 607 int classID = Converter::byteArrayToInt( data ); 608 i += INTSIZE; 609 610 createEntity( (ClassID)classID ); 611 612 return true; 613 } 614 615 bool NetworkGameManager::handleRequestRemove( int & i, const byte * data, int length, int sender ) 616 { 617 if ( INTSIZE > length-i ) 618 { 619 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); 620 return false; 621 } 622 int uniqueID = Converter::byteArrayToInt( data ); 623 i += INTSIZE; 624 625 removeEntity( uniqueID ); 626 627 return true; 628 } 629 630 bool NetworkGameManager::handleCreateEntity( int & i, const byte * data, int length, int sender ) 631 { 632 if ( INTSIZE > length-i ) 633 { 634 PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n"); 635 return false; 636 } 637 int classID = Converter::byteArrayToInt( data ); 638 i += INTSIZE; 639 640 if ( INTSIZE > length-i ) 641 { 642 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); 643 return false; 644 } 645 int uniqueID = Converter::byteArrayToInt( data ); 646 i += INTSIZE; 647 648 if ( INTSIZE > length-i ) 649 { 650 PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n"); 651 return false; 652 } 653 int owner = Converter::byteArrayToInt( data ); 654 i += INTSIZE; 655 656 doCreateEntity( (ClassID)classID, uniqueID, owner ); 657 658 return true; 659 } 660 661 bool NetworkGameManager::handleRemoveEntity( int & i, const byte * data, int length, int sender ) 662 { 663 if ( INTSIZE > length-i ) 664 { 665 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); 666 return false; 667 } 668 int uniqueID = Converter::byteArrayToInt( data ); 669 i += INTSIZE; 670 671 doRemoveEntity( uniqueID ); 672 673 return true; 674 } 675 676 bool NetworkGameManager::handleCreateEntityList( int & i, const byte * data, int length, int sender ) 677 { 678 if ( INTSIZE > length-i ) 679 { 680 PRINTF(1)("Cannot read n from buffer! Not enough data left!\n"); 681 return false; 682 } 683 int n = Converter::byteArrayToInt( data ); 684 i += INTSIZE; 685 686 int classID, uniqueID, owner; 687 688 for ( int j = 0; j<n; j++ ) 689 { 690 691 if ( INTSIZE > length-i ) 692 { 693 PRINTF(1)("Cannot read classID from buffer! Not enough data left!\n"); 694 return false; 695 } 696 classID = Converter::byteArrayToInt( data ); 697 i += INTSIZE; 698 699 if ( INTSIZE > length-i ) 700 { 701 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); 702 return false; 703 } 704 uniqueID = Converter::byteArrayToInt( data ); 705 i += INTSIZE; 706 707 if ( INTSIZE > length-i ) 708 { 709 PRINTF(1)("Cannot read owner from buffer! Not enough data left!\n"); 710 return false; 711 } 712 owner = Converter::byteArrayToInt( data ); 713 i += INTSIZE; 714 715 doCreateEntity( (ClassID)classID, uniqueID, owner ); 716 717 } 718 return true; 719 } 720 721 bool NetworkGameManager::handleRemoveEntityList( int & i, const byte * data, int length, int sender ) 722 { 723 if ( INTSIZE > length-i ) 724 { 725 PRINTF(1)("Cannot read n from buffer! Not enough data left!\n"); 726 return false; 727 } 728 int n = Converter::byteArrayToInt( data ); 729 i += INTSIZE; 730 731 int uniqueID; 732 733 for ( int j = 0; j<n; j++ ) 734 { 735 736 if ( INTSIZE > length-i ) 737 { 738 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); 739 return false; 740 } 741 uniqueID = Converter::byteArrayToInt( data ); 742 i += INTSIZE; 743 744 doRemoveEntity( uniqueID ); 745 } 746 747 return true; 748 } 749 750 bool NetworkGameManager::handleYouAreEntity( int & i, const byte * data, int length, int sender ) 751 { 752 if ( INTSIZE > length-i ) 753 { 754 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); 755 return false; 756 } 757 int uniqueID = Converter::byteArrayToInt( data ); 758 i += INTSIZE; 759 760 doYouAre( uniqueID ); 761 762 return true; 763 } 764 765 bool NetworkGameManager::handleRequestSync( int & i, const byte * data, int length, int sender ) 766 { 767 if ( INTSIZE > length-i ) 768 { 769 PRINTF(1)("Cannot read uniqueID from buffer! Not enough data left!\n"); 770 return false; 771 } 772 int uniqueID = Converter::byteArrayToInt( data ); 773 i += INTSIZE; 774 775 doRequestSync( uniqueID, sender ); 776 777 return true; 778 } 779 -
branches/network/src/lib/network/network_game_manager.h
r6214 r6219 69 69 virtual void readDebug() const; 70 70 71 void createEntity( ClassID classID);72 void removeEntity( int uniqueID);71 void createEntity( ClassID classID, int owner = 0 ); 72 void removeEntity( int uniqueID ); 73 73 void sendYouAre( int uniqueID, int userID ); 74 74 … … 96 96 inline bool readFromClientBuffer( clientBuffer &cb, byte*data, int length ); 97 97 98 //helper functions for writeBytes 99 inline bool handleRequestCreate( int& i, const byte* data, int length, int sender ); 100 inline bool handleRequestRemove( int& i, const byte* data, int length, int sender ); 101 inline bool handleCreateEntity( int& i, const byte* data, int length, int sender ); 102 inline bool handleRemoveEntity( int& i, const byte* data, int length, int sender ); 103 inline bool handleCreateEntityList( int& i, const byte* data, int length, int sender ); 104 inline bool handleRemoveEntityList( int& i, const byte* data, int length, int sender ); 105 inline bool handleYouAreEntity( int& i, const byte* data, int length, int sender ); 106 inline bool handleRequestSync( int& i, const byte* data, int length, int sender ); 107 98 108 private: 99 109 std::vector<clientBuffer> outBuffer;
Note: See TracChangeset
for help on using the changeset viewer.