Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7202 was 6815, checked in by bensch, 19 years ago

orxonox/trunk: merged branches/network back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/network . -r6774:HEAD

no conflicts…
thats what i call orthogonal work

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