Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/network/network_protocol.cc @ 6060

Last change on this file since 6060 was 6060, checked in by bwuest, 18 years ago

Network changes

File size: 3.4 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/* using namespace std is default, this needs to be here */
33using namespace std;
34
35
36/**
37  standard constructor
38*/
39NetworkProtocol::NetworkProtocol()
40{
41  /* set the class id for the base object */
42  this->setClassID(CL_NETWORK_PROTOCOL, "NetworkProtocol");
43  this->headerLength = sizeof(Header);
44}
45
46/**
47  standard destructor
48*/
49NetworkProtocol::~NetworkProtocol()
50{}
51
52/**
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*/
62int NetworkProtocol::createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source)
63{
64  PRINTF(0)("create header length = %i, bufferLength = %i\n", length, bufferLength);
65  //If there isn't enough space for the header return -1
66  if (length + this->headerLength > bufferLength)
67    return -1;
68
69//   for(int i = 0; i < length; i++)
70//     printf("send byte[%i]=%u\n", i, data[i]);
71
72
73  //Create space for the header
74  for( int i = length - 1; i >= 0; i--)
75    data[i + this->headerLength] = data[i];
76
77  //Now create the header
78  /* protocol identifier */
79  data[0] = 255;
80  /* version number */
81  data[1] = 0;
82  /* sender ID: FIXME: there will be a better ID (for example unique:D)*/
83  data[2] = (byte)(source.getUniqueID());
84  /* data length*/
85  data[3] = length;
86
87
88  return length + this->headerLength;
89}
90
91/**
92    * extracts the header from the binary data stream
93    * @arg data: the binary data with the header
94    * @arg length: the length of the binary data (including header)
95    * @return: a Header struct with the header information and the binary data
96*/
97Header NetworkProtocol::extractHeader(byte* data, int length)
98{
99  PRINTF(0)("extract Header\n");
100  //Test if received data can contain a header
101  if (length < headerLength)
102  {
103    PRINTF(0)("Received data is to short; it can't contain a header!\n");
104    Header h;
105    h.protocol = 0;
106    return h;
107  }
108
109  //Extract header
110  Header h;
111  //&h = data;
112
113  h.protocol = data[0];
114  /* version number */
115  h.version = data[1];
116  /* unique ID */
117  h.synchronizeableID = data[2];
118  /* data length*/
119  h.length = data[3];
120
121
122//   for(int i = 0; i < length; i++)
123//     printf("recS byte[%i]=%u\n", i, data[i]);
124
125  //Remove header
126//   for (int i = headerLength; i < length; i++)
127//     data[i - headerLength] = data[i];
128
129  return h;
130}
Note: See TracBrowser for help on using the repository browser.