[5613] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
[5802] | 10 | |
---|
[5613] | 11 | ### File Specific: |
---|
| 12 | main-programmer: Benjamin Wuest |
---|
| 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | /* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module |
---|
| 17 | For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput |
---|
| 18 | */ |
---|
| 19 | #define DEBUG_MODULE_NETWORK |
---|
| 20 | |
---|
| 21 | //#include ... |
---|
| 22 | |
---|
| 23 | /* include my own header */ |
---|
| 24 | #include "network_protocol.h" |
---|
| 25 | |
---|
[5736] | 26 | /* include the synchronizeable object */ |
---|
| 27 | #include "synchronizeable.h" |
---|
[5613] | 28 | |
---|
[5735] | 29 | /* include this file for debugging */ |
---|
| 30 | #include "debug.h" |
---|
| 31 | |
---|
[6341] | 32 | #include "converter.h" |
---|
| 33 | |
---|
[5613] | 34 | /* using namespace std is default, this needs to be here */ |
---|
| 35 | using namespace std; |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | standard constructor |
---|
| 40 | */ |
---|
| 41 | NetworkProtocol::NetworkProtocol() |
---|
| 42 | { |
---|
| 43 | /* set the class id for the base object */ |
---|
| 44 | this->setClassID(CL_NETWORK_PROTOCOL, "NetworkProtocol"); |
---|
[6341] | 45 | this->headerLength = INTSIZE+FLOATSIZE; |
---|
[5613] | 46 | } |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | standard destructor |
---|
| 50 | */ |
---|
| 51 | NetworkProtocol::~NetworkProtocol() |
---|
| 52 | {} |
---|
| 53 | |
---|
| 54 | /** |
---|
[5735] | 55 | * creates a new packet header from the function arguments |
---|
| 56 | * |
---|
| 57 | * @arg data: the binary data without header -> return the data in this binary array |
---|
| 58 | * @arg length: the data length without header |
---|
| 59 | * @arg bufferLength: the length of the internal buffer |
---|
| 60 | * @arg source: reference to the source Synchronizeable object |
---|
| 61 | * @return: the new data length with header (the header data is included into byte* data) |
---|
| 62 | * -1 if there isn't enough space in the buffer for the header |
---|
| 63 | */ |
---|
[6139] | 64 | int NetworkProtocol::createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source) |
---|
[5735] | 65 | { |
---|
[6139] | 66 | PRINTF(5)("create header length = %i, bufferLength = %i\n", length, bufferLength); |
---|
[5735] | 67 | //If there isn't enough space for the header return -1 |
---|
[6341] | 68 | if (length + 2*INTSIZE > bufferLength) |
---|
[5735] | 69 | return -1; |
---|
[5802] | 70 | |
---|
[5805] | 71 | |
---|
[6139] | 72 | // FIXME: without move Create space for the header |
---|
[5752] | 73 | for( int i = length - 1; i >= 0; i--) |
---|
[6341] | 74 | data[i + INTSIZE+INTSIZE] = data[i]; |
---|
[5802] | 75 | |
---|
[5809] | 76 | //Now create the header |
---|
| 77 | /* sender ID: FIXME: there will be a better ID (for example unique:D)*/ |
---|
[6341] | 78 | //data[0] = (byte)(source.getUniqueID()); |
---|
| 79 | int res = Converter::intToByteArray( source.getUniqueID(), data, bufferLength ); |
---|
| 80 | |
---|
[5809] | 81 | /* data length*/ |
---|
[6341] | 82 | //data[1] = length; |
---|
| 83 | res += Converter::intToByteArray( length, data+res, bufferLength-res ); |
---|
[5809] | 84 | |
---|
| 85 | |
---|
[6341] | 86 | return length + res; |
---|
[5735] | 87 | } |
---|
[5613] | 88 | |
---|
[5735] | 89 | /** |
---|
| 90 | * extracts the header from the binary data stream |
---|
| 91 | * @arg data: the binary data with the header |
---|
| 92 | * @arg length: the length of the binary data (including header) |
---|
| 93 | * @return: a Header struct with the header information and the binary data |
---|
[5613] | 94 | */ |
---|
[5738] | 95 | Header NetworkProtocol::extractHeader(byte* data, int length) |
---|
[5613] | 96 | { |
---|
[6139] | 97 | PRINTF(5)("extract Header\n"); |
---|
[5735] | 98 | //Test if received data can contain a header |
---|
[6341] | 99 | if (length < 2*INTSIZE) |
---|
[5735] | 100 | { |
---|
[6139] | 101 | PRINTF(1)("Received data is to short; it can't contain a header!\n"); |
---|
[5735] | 102 | Header h; |
---|
[6139] | 103 | h.length = 0; |
---|
[5735] | 104 | return h; |
---|
| 105 | } |
---|
[5802] | 106 | |
---|
[5735] | 107 | //Extract header |
---|
| 108 | Header h; |
---|
[5809] | 109 | //&h = data; |
---|
| 110 | |
---|
[6139] | 111 | /* unique ID */ |
---|
[6341] | 112 | //h.synchronizeableID = data[0]; |
---|
| 113 | int res = Converter::byteArrayToInt( data, &(h.synchronizeableID) ); |
---|
[5809] | 114 | /* data length*/ |
---|
[6341] | 115 | //h.length = data[1]; |
---|
| 116 | Converter::byteArrayToInt( data+res, &(h.length) ); |
---|
[5802] | 117 | |
---|
| 118 | |
---|
[5735] | 119 | return h; |
---|
[5613] | 120 | } |
---|