[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 | |
---|
[5613] | 32 | /* using namespace std is default, this needs to be here */ |
---|
| 33 | using namespace std; |
---|
| 34 | |
---|
| 35 | |
---|
| 36 | /** |
---|
| 37 | standard constructor |
---|
| 38 | */ |
---|
| 39 | NetworkProtocol::NetworkProtocol() |
---|
| 40 | { |
---|
| 41 | /* set the class id for the base object */ |
---|
| 42 | this->setClassID(CL_NETWORK_PROTOCOL, "NetworkProtocol"); |
---|
[5809] | 43 | this->headerLength = sizeof(Header); |
---|
[5613] | 44 | } |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | standard destructor |
---|
| 48 | */ |
---|
| 49 | NetworkProtocol::~NetworkProtocol() |
---|
| 50 | {} |
---|
| 51 | |
---|
| 52 | /** |
---|
[5735] | 53 | * creates a new packet header from the function arguments |
---|
| 54 | * |
---|
| 55 | * @arg data: the binary data without header -> return the data in this binary array |
---|
| 56 | * @arg length: the data length without header |
---|
| 57 | * @arg bufferLength: the length of the internal buffer |
---|
| 58 | * @arg source: reference to the source Synchronizeable object |
---|
| 59 | * @return: the new data length with header (the header data is included into byte* data) |
---|
| 60 | * -1 if there isn't enough space in the buffer for the header |
---|
| 61 | */ |
---|
[6043] | 62 | int NetworkProtocol::createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source) |
---|
[5735] | 63 | { |
---|
[6090] | 64 | PRINTF(5)("create header length = %i, bufferLength = %i\n", length, bufferLength); |
---|
[5735] | 65 | //If there isn't enough space for the header return -1 |
---|
[5809] | 66 | if (length + this->headerLength > bufferLength) |
---|
[5735] | 67 | return -1; |
---|
[5802] | 68 | |
---|
[5805] | 69 | |
---|
[6061] | 70 | // FIXME: without move Create space for the header |
---|
[5752] | 71 | for( int i = length - 1; i >= 0; i--) |
---|
[5809] | 72 | data[i + this->headerLength] = data[i]; |
---|
[5802] | 73 | |
---|
[5809] | 74 | //Now create the header |
---|
| 75 | /* sender ID: FIXME: there will be a better ID (for example unique:D)*/ |
---|
[6061] | 76 | data[0] = (byte)(source.getUniqueID()); |
---|
[5809] | 77 | /* data length*/ |
---|
[6061] | 78 | data[1] = length; |
---|
[5809] | 79 | |
---|
| 80 | |
---|
| 81 | return length + this->headerLength; |
---|
[5735] | 82 | } |
---|
[5613] | 83 | |
---|
[5735] | 84 | /** |
---|
| 85 | * extracts the header from the binary data stream |
---|
| 86 | * @arg data: the binary data with the header |
---|
| 87 | * @arg length: the length of the binary data (including header) |
---|
| 88 | * @return: a Header struct with the header information and the binary data |
---|
[5613] | 89 | */ |
---|
[5738] | 90 | Header NetworkProtocol::extractHeader(byte* data, int length) |
---|
[5613] | 91 | { |
---|
[6090] | 92 | PRINTF(5)("extract Header\n"); |
---|
[5735] | 93 | //Test if received data can contain a header |
---|
| 94 | if (length < headerLength) |
---|
| 95 | { |
---|
[6090] | 96 | PRINTF(1)("Received data is to short; it can't contain a header!\n"); |
---|
[5735] | 97 | Header h; |
---|
[6061] | 98 | h.length = 0; |
---|
[5735] | 99 | return h; |
---|
| 100 | } |
---|
[5802] | 101 | |
---|
[5735] | 102 | //Extract header |
---|
| 103 | Header h; |
---|
[5809] | 104 | //&h = data; |
---|
| 105 | |
---|
[6043] | 106 | /* unique ID */ |
---|
[6061] | 107 | h.synchronizeableID = data[0]; |
---|
[5809] | 108 | /* data length*/ |
---|
[6061] | 109 | h.length = data[1]; |
---|
[5802] | 110 | |
---|
| 111 | |
---|
[5735] | 112 | return h; |
---|
[5613] | 113 | } |
---|