Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

handshake works now

File size: 4.3 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(0)("Handshake created clientId = %d\n", clientId);
36}
37
38void Handshake::writeBytes( const byte * data, int length )
39{
40  PRINTF(0)("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 ( hasState( HS_RECVD_VER ) && !hasState( HS_RECVD_HID ) )
89  {
90    if ( length != 4 )
91    {
92      PRINTF(0)("hostID packet has wrong size %d instead of %d\n", length, 4);
93      setState( HS_COMPLETED );
94      return;
95    }
96
97    setState( HS_COMPLETED );
98    setState( HS_RECVD_HID );
99    this->isOk = true;
100    memcpy(&(this->newHostId), data, 4);
101    PRINTF(0)("got my hostID: %d\n", newHostId);
102    return;
103  }
104
105}
106
107int Handshake::readBytes( byte * data, int maxLength, int * reciever )
108{
109  PRINTF(0)("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);
110
111  if ( hasState( HS_COMPLETED ) )
112    return 0;
113
114  if ( !hasState( HS_SENT_INIT ) )
115  {
116    if ( maxLength < _INITIAL_DATA_LENGTH )
117    {
118      PRINTF(0)("buffer too small for _INITIAL_DATA");
119      setState( HS_COMPLETED );
120      return 0;
121    }
122
123    setState( HS_SENT_INIT );
124    memcpy(data, _INITIAL_DATA, _INITIAL_DATA_LENGTH);
125    if ( this->isServer() )
126      *reciever = clientId;
127    return _INITIAL_DATA_LENGTH;
128  }
129
130  if ( hasState( HS_RECVD_INIT ) && hasState( HS_SENT_INIT ) && !hasState( HS_SENT_VER ) )
131  {
132    if ( maxLength < _ORXONOX_VERSION_LENGTH )
133    {
134      PRINTF(0)("buffer too small for version number");
135      setState( HS_COMPLETED );
136      return 0;
137    }
138
139    setState( HS_SENT_VER );
140    memcpy(data, _ORXONOX_VERSION, _ORXONOX_VERSION_LENGTH);
141    if ( this->isServer() )
142      *reciever = clientId;
143    return _ORXONOX_VERSION_LENGTH;
144  }
145
146  if ( isServer() && hasState( HS_RECVD_VER) && hasState( HS_SENT_VER ) && !hasState( HS_SENT_HID ) )
147  {
148    if ( maxLength < 4 )
149    {
150      PRINTF(0)("buffer too small for ID");
151      setState( HS_COMPLETED );
152      return 0;
153    }
154
155    setState( HS_SENT_HID );
156    setState( HS_COMPLETED );
157    isOk = true;
158    memcpy(data, (byte*)&clientId, 4);
159    if ( this->isServer() )
160      *reciever = clientId;
161    return 4;
162  }
163
164  return 0;
165}
166
167void Handshake::writeDebug( ) const
168{
169}
170
171void Handshake::readDebug( ) const
172{
173}
Note: See TracBrowser for help on using the repository browser.