Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6661 was 6661, checked in by patrick, 18 years ago

network: dont get the sync process to run again.

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