Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6713 was 6695, checked in by patrick, 19 years ago

merged the network branche to the trunk

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