Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6429 was 6341, checked in by bensch, 19 years ago

orxonox/trunk: merged the network branche back to the trunk, so we do not get away from each other to fast

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