Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

renamed EntityManager to NetworkGameManager

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