Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/network/network_protocol.cc @ 7929

Last change on this file since 7929 was 6341, checked in by bensch, 19 years ago

orxonox/trunk: merged the network branche back to the trunk, so we do not get away from each other to fast

File size: 3.3 KB
Line 
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.
10
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
26/* include the synchronizeable object */
27#include "synchronizeable.h"
28
29/* include this file for debugging */
30#include "debug.h"
31
32#include "converter.h"
33
34/* using namespace std is default, this needs to be here */
35using namespace std;
36
37
38/**
39  standard constructor
40*/
41NetworkProtocol::NetworkProtocol()
42{
43  /* set the class id for the base object */
44  this->setClassID(CL_NETWORK_PROTOCOL, "NetworkProtocol");
45  this->headerLength = INTSIZE+FLOATSIZE;
46}
47
48/**
49  standard destructor
50*/
51NetworkProtocol::~NetworkProtocol()
52{}
53
54/**
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*/
64int NetworkProtocol::createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source)
65{
66  PRINTF(5)("create header length = %i, bufferLength = %i\n", length, bufferLength);
67  //If there isn't enough space for the header return -1
68  if (length + 2*INTSIZE > bufferLength)
69    return -1;
70
71
72  // FIXME: without move Create space for the header
73  for( int i = length - 1; i >= 0; i--)
74    data[i + INTSIZE+INTSIZE] = data[i];
75
76  //Now create the header
77  /* sender ID: FIXME: there will be a better ID (for example unique:D)*/
78  //data[0] = (byte)(source.getUniqueID());
79  int res = Converter::intToByteArray( source.getUniqueID(), data, bufferLength );
80
81  /* data length*/
82  //data[1] = length;
83  res += Converter::intToByteArray( length, data+res, bufferLength-res );
84
85
86  return length + res;
87}
88
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
94*/
95Header NetworkProtocol::extractHeader(byte* data, int length)
96{
97  PRINTF(5)("extract Header\n");
98  //Test if received data can contain a header
99  if (length < 2*INTSIZE)
100  {
101    PRINTF(1)("Received data is to short; it can't contain a header!\n");
102    Header h;
103    h.length = 0;
104    return h;
105  }
106
107  //Extract header
108  Header h;
109  //&h = data;
110
111  /* unique ID */
112  //h.synchronizeableID = data[0];
113  int res = Converter::byteArrayToInt( data, &(h.synchronizeableID) );
114  /* data length*/
115  //h.length = data[1];
116  Converter::byteArrayToInt( data+res, &(h.length) );
117
118
119  return h;
120}
Note: See TracBrowser for help on using the repository browser.