Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6483 was 6341, checked in by bensch, 19 years ago

orxonox/trunk: merged the network branche back to the trunk, so we do not get away from each other to fast

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