Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/network/handshake.cc @ 6115

Last change on this file since 6115 was 6115, checked in by rennerc, 18 years ago

handshake now sends NetworkGameManagerId

File size: 5.1 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: christoph
13   co-programmer:
14*/
15
16
17/* this is for debug output. It just says, that all calls to PRINT() belong to the DEBUG_MODULE_NETWORK module
18   For more information refere to https://www.orxonox.net/cgi-bin/trac.cgi/wiki/DebugOutput
19*/
20#define DEBUG_MODULE_NETWORK
21
22#include "handshake.h"
23
24Handshake::Handshake( bool server, int clientId, int entityManagerId )
25{
26  /* set the class id for the base object */
27  this->setClassID(CL_HANDSHAKE, "Handshake");
28
29  if ( clientId > 255 )
30  {
31    PRINTF(1)("clientId is too big for type byte (%d)! connection to client %d will not work!", clientId, clientId);
32  }
33
34  if ( entityManagerId > 255 )
35  {
36    PRINTF(1)("entityManagerId is too big for type byte (%d)! connection to client %d will not work!", entityManagerId, clientId);
37  }
38
39  this->setIsServer(server);
40  this->clientId = clientId;
41  this->entityManagerId = entityManagerId;
42  this->state = 0;
43  this->isOk = false;
44  this->setOwner(0);
45
46  PRINTF(5)("Handshake created clientId = %d\n", clientId);
47}
48
49void Handshake::writeBytes( const byte * data, int length )
50{
51  PRINTF(5)("Handshake::writeBytes states = %d %d %d %d (%d)\n", hasState( HS_RECVD_INIT ), hasState( HS_RECVD_VER ), hasState( HS_RECVD_HID ), hasState( HS_COMPLETED ), state);
52
53  if ( hasState( HS_COMPLETED ) )
54       return;
55
56  if ( !hasState( HS_RECVD_INIT ) )
57  {
58    if ( length != _INITIAL_DATA_LENGTH )
59    {
60      PRINTF(0)("initial packet has wrong size %d instead of %d\n", length, _INITIAL_DATA_LENGTH);
61      setState( HS_COMPLETED );
62      return;
63    }
64
65    if ( strncmp((char*)data, _INITIAL_DATA, length) )
66    {
67      PRINTF(0)("initial packed does not match\n");
68      setState( HS_COMPLETED );
69      return;
70    }
71
72    setState( HS_RECVD_INIT );
73    PRINTF(0)("got valid initial packet from client %d\n", clientId);
74    return;
75  }
76
77  if ( hasState( HS_RECVD_INIT ) && !hasState( HS_RECVD_VER ) )
78  {
79    if ( length != _ORXONOX_VERSION_LENGTH )
80    {
81      PRINTF(0)("version number packet has wrong size %d instead of %d\n", length, _ORXONOX_VERSION_LENGTH);
82      setState( HS_COMPLETED );
83      return;
84    }
85
86    if ( strncmp((char*)data, _ORXONOX_VERSION, length) )
87    {
88      PRINTF(0)("versions do not match\n");
89      setState( HS_COMPLETED );
90      return;
91    }
92
93    setState( HS_RECVD_VER );
94
95    PRINTF(0)("client %d's version does match\n", clientId);
96    return;
97  }
98
99  if ( !isServer() && hasState( HS_RECVD_VER ) && !hasState( HS_RECVD_HID ) )
100  {
101    if ( length != 2 )
102    {
103      PRINTF(0)("hostID packet has wrong size %d instead of %d\n", length, 1);
104      setState( HS_COMPLETED );
105      return;
106    }
107
108    setState( HS_COMPLETED );
109    setState( HS_RECVD_HID );
110    this->isOk = true;
111    this->newHostId = data[0];
112    this->newEntityManagerId = data[1];
113
114    if ( newHostId == 0 )
115    {
116      setState( HS_WAS_REJECT );
117      isOk = false;
118      PRINTF(0)("Server did not accept handshake!\n");
119    }
120    else
121    {
122      PRINTF(0)("got my hostID: %d\n", newHostId);
123    }
124    return;
125  }
126
127}
128
129int Handshake::readBytes( byte * data, int maxLength, int * reciever )
130{
131  PRINTF(5)("Handshake::readBytes states = %d %d %d %d (%d)\n", hasState( HS_SENT_INIT ), hasState( HS_SENT_VER ), hasState( HS_SENT_HID ), hasState( HS_COMPLETED ), state);
132
133  if ( hasState( HS_COMPLETED ) )
134    return 0;
135
136  if ( !hasState( HS_SENT_INIT ) )
137  {
138    if ( maxLength < _INITIAL_DATA_LENGTH )
139    {
140      PRINTF(0)("buffer too small for _INITIAL_DATA");
141      setState( HS_COMPLETED );
142      return 0;
143    }
144
145    setState( HS_SENT_INIT );
146    memcpy(data, _INITIAL_DATA, _INITIAL_DATA_LENGTH);
147    if ( this->isServer() )
148      *reciever = clientId;
149    return _INITIAL_DATA_LENGTH;
150  }
151
152  if ( hasState( HS_RECVD_INIT ) && hasState( HS_SENT_INIT ) && !hasState( HS_SENT_VER ) )
153  {
154    if ( maxLength < _ORXONOX_VERSION_LENGTH )
155    {
156      PRINTF(0)("buffer too small for version number");
157      setState( HS_COMPLETED );
158      return 0;
159    }
160
161    setState( HS_SENT_VER );
162    memcpy(data, _ORXONOX_VERSION, _ORXONOX_VERSION_LENGTH);
163    if ( this->isServer() )
164      *reciever = clientId;
165    return _ORXONOX_VERSION_LENGTH;
166  }
167
168  if ( isServer() && hasState( HS_RECVD_VER) && hasState( HS_SENT_VER ) && !hasState( HS_SENT_HID ) )
169  {
170    if ( maxLength < 2 )
171    {
172      PRINTF(0)("buffer too small for ID");
173      setState( HS_COMPLETED );
174      return 0;
175    }
176
177    setState( HS_SENT_HID );
178    setState( HS_COMPLETED );
179
180    if ( hasState( HS_DO_REJECT ) )
181    {
182      isOk = false;
183      //memcpy(data, (byte*)0, 4);
184      data[0] = 0;
185    }
186    else
187    {
188      isOk = true;
189      //memcpy(data, &clientId, 4);
190      data[0] = (byte)clientId;
191      data[1] = (byte)entityManagerId;
192    }
193    *reciever = clientId;
194    return 2;
195  }
196
197  return 0;
198}
199
200void Handshake::writeDebug( ) const
201{
202}
203
204void Handshake::readDebug( ) const
205{
206}
Note: See TracBrowser for help on using the repository browser.