Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

network_protocol.cc and network_protocol.h changed

File size: 2.9 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 this file, it contains some default definitions */
27#include "netdefs.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}
44
45/**
46  standard destructor
47*/
48NetworkProtocol::~NetworkProtocol()
49{}
50
51/**
52    * creates a new packet header from the function arguments
53    *
54    * @arg data: the binary data without header -> return the data in this binary array
55    * @arg length: the data length without header
56    * @arg bufferLength: the length of the internal buffer
57    * @arg source: reference to the source Synchronizeable object
58    * @arg remoteID: id number of the remote 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*/
62NetworkProtocol::createHeader(byte* data, int length, int bufferLength, const Synchronizeable& source, unsigned int remoteID)
63{
64  //If there isn't enough space for the header return -1
65  if (length + headerLength > bufferLength)
66    return -1;
67 
68  //Create space for the header
69  for (unsigned int i = length - 1; i >= 0; i--)
70    data[i + headerLength] = data[i];
71 
72  //Include header
73  data[0] = 255;
74}
75
76/**
77    * extracts the header from the binary data stream
78    * @arg data: the binary data with the header
79    * @arg length: the length of the binary data (including header)
80    * @return: a Header struct with the header information and the binary data
81*/
82Header NetworkProtocol::ExtractHeader(byte* data, int length)
83{
84  //Test if received data can contain a header
85  if (length < headerLength)
86  {
87    PRINTF(0)("Received data is to short; it can't contain a header!");
88    Header h;
89    h.protocol = -1;
90    return h;
91  }
92 
93  //Extract header
94  Header h;
95  h.protocol = data[0];
96 
97  h.length = length - headerLength;
98  h.data = data;
99 
100  //Remove header
101  for (int i = headerLength; i < length; i++)
102    data[i - headerLength] = data[i];
103 
104  return h;
105}
Note: See TracBrowser for help on using the repository browser.