Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

handshake: several clients can connect at the same time now

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