Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/network/handshake.cc @ 6139

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

trunk: merged branche network with trunk using command: svn merge -r5999:HEAD, conflicts resolved in favor of the trunk bla

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