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
RevLine 
[6043]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
[6053]29  this->setIsServer(server);
[6043]30  this->clientId = clientId;
[6053]31  this->state = 0;
[6043]32  this->isOk = false;
[6053]33  this->setOwner(0);
34
[6090]35  PRINTF(5)("Handshake created clientId = %d\n", clientId);
[6043]36}
37
38void Handshake::writeBytes( const byte * data, int length )
39{
[6062]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);
[6053]41
42  if ( hasState( HS_COMPLETED ) )
43       return;
44
45  if ( !hasState( HS_RECVD_INIT ) )
[6043]46  {
47    if ( length != _INITIAL_DATA_LENGTH )
48    {
[6053]49      PRINTF(0)("initial packet has wrong size %d instead of %d\n", length, _INITIAL_DATA_LENGTH);
50      setState( HS_COMPLETED );
[6043]51      return;
52    }
53
54    if ( strncmp((char*)data, _INITIAL_DATA, length) )
55    {
[6053]56      PRINTF(0)("initial packed does not match\n");
57      setState( HS_COMPLETED );
[6043]58      return;
59    }
60
[6053]61    setState( HS_RECVD_INIT );
[6043]62    PRINTF(0)("got valid initial packet from client %d\n", clientId);
63    return;
64  }
65
[6053]66  if ( hasState( HS_RECVD_INIT ) && !hasState( HS_RECVD_VER ) )
[6043]67  {
68    if ( length != _ORXONOX_VERSION_LENGTH )
69    {
[6053]70      PRINTF(0)("version number packet has wrong size %d instead of %d\n", length, _ORXONOX_VERSION_LENGTH);
71      setState( HS_COMPLETED );
[6043]72      return;
73    }
74
75    if ( strncmp((char*)data, _ORXONOX_VERSION, length) )
76    {
[6053]77      PRINTF(0)("versions do not match\n");
78      setState( HS_COMPLETED );
[6043]79      return;
80    }
81
[6053]82    setState( HS_RECVD_VER );
[6043]83
84    PRINTF(0)("client %d's version does match\n", clientId);
85    return;
86  }
87
[6062]88  if ( !isServer() && hasState( HS_RECVD_VER ) && !hasState( HS_RECVD_HID ) )
[6043]89  {
[6090]90    if ( length != 1 )
[6043]91    {
[6090]92      PRINTF(0)("hostID packet has wrong size %d instead of %d\n", length, 1);
[6053]93      setState( HS_COMPLETED );
[6043]94      return;
95    }
96
[6053]97    setState( HS_COMPLETED );
98    setState( HS_RECVD_HID );
[6043]99    this->isOk = true;
[6090]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    }
[6043]112    return;
113  }
114
115}
116
[6053]117int Handshake::readBytes( byte * data, int maxLength, int * reciever )
[6043]118{
[6062]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);
[6053]120
121  if ( hasState( HS_COMPLETED ) )
122    return 0;
123
124  if ( !hasState( HS_SENT_INIT ) )
[6043]125  {
126    if ( maxLength < _INITIAL_DATA_LENGTH )
127    {
[6053]128      PRINTF(0)("buffer too small for _INITIAL_DATA");
129      setState( HS_COMPLETED );
[6043]130      return 0;
131    }
132
[6053]133    setState( HS_SENT_INIT );
[6043]134    memcpy(data, _INITIAL_DATA, _INITIAL_DATA_LENGTH);
135    if ( this->isServer() )
[6053]136      *reciever = clientId;
[6043]137    return _INITIAL_DATA_LENGTH;
138  }
139
[6053]140  if ( hasState( HS_RECVD_INIT ) && hasState( HS_SENT_INIT ) && !hasState( HS_SENT_VER ) )
[6043]141  {
142    if ( maxLength < _ORXONOX_VERSION_LENGTH )
143    {
[6053]144      PRINTF(0)("buffer too small for version number");
145      setState( HS_COMPLETED );
[6043]146      return 0;
147    }
148
[6053]149    setState( HS_SENT_VER );
[6043]150    memcpy(data, _ORXONOX_VERSION, _ORXONOX_VERSION_LENGTH);
151    if ( this->isServer() )
[6053]152      *reciever = clientId;
[6043]153    return _ORXONOX_VERSION_LENGTH;
154  }
155
[6053]156  if ( isServer() && hasState( HS_RECVD_VER) && hasState( HS_SENT_VER ) && !hasState( HS_SENT_HID ) )
[6043]157  {
158    if ( maxLength < 4 )
159    {
[6053]160      PRINTF(0)("buffer too small for ID");
161      setState( HS_COMPLETED );
[6043]162      return 0;
163    }
164
[6053]165    setState( HS_SENT_HID );
166    setState( HS_COMPLETED );
[6090]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;
[6043]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.