Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/network/handshake.cc @ 7618

Last change on this file since 7618 was 6815, checked in by bensch, 19 years ago

orxonox/trunk: merged branches/network back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/network . -r6774:HEAD

no conflicts…
thats what i call orthogonal work

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