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 | |
---|
12 | ### File Specific: |
---|
13 | main-programmer: Silvan Nellen |
---|
14 | co-programmer: Benjamin Wuest |
---|
15 | */ |
---|
16 | |
---|
17 | #define DEBUG_MODULE_NETWORK |
---|
18 | |
---|
19 | #include "synchronizeable.h" |
---|
20 | |
---|
21 | #include "netdefs.h" |
---|
22 | #include "network_manager.h" |
---|
23 | #include "network_game_manager.h" |
---|
24 | #include "network_stream.h" |
---|
25 | |
---|
26 | #include "assert.h" |
---|
27 | |
---|
28 | |
---|
29 | /** |
---|
30 | * default constructor |
---|
31 | */ |
---|
32 | Synchronizeable::Synchronizeable() |
---|
33 | { |
---|
34 | this->setClassID(CL_SYNCHRONIZEABLE, "Synchronizeable"); |
---|
35 | this->owner = 0; |
---|
36 | this->state = 0; |
---|
37 | this->hostID = NetworkManager::getInstance()->getHostID(); |
---|
38 | this->setIsServer(this->hostID == 0); |
---|
39 | this->uniqueID = -1; |
---|
40 | this->networkStream = NULL; |
---|
41 | this->setRequestedSync( false ); |
---|
42 | this->setIsOutOfSync( !(this->isServer()) ); |
---|
43 | |
---|
44 | this->bSynchronize = false; |
---|
45 | |
---|
46 | NetworkStream* nd = NetworkManager::getInstance()->getDefaultSyncStream(); |
---|
47 | assert(nd != NULL); |
---|
48 | nd->connectSynchronizeable(*this); |
---|
49 | this->setUniqueID(NetworkManager::getInstance()->getNewUniqueID()); |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | |
---|
54 | /** |
---|
55 | * default destructor deletes all unneded stuff |
---|
56 | */ |
---|
57 | Synchronizeable::~Synchronizeable() |
---|
58 | { |
---|
59 | if ( this->networkStream ) |
---|
60 | this->networkStream->disconnectSynchronizeable(*this); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | /** |
---|
65 | * write data to NetworkStream |
---|
66 | */ |
---|
67 | int Synchronizeable::writeBytes(const byte* data, int length, int sender) |
---|
68 | { |
---|
69 | PRINTF(5)("Synchronizeable::writeBytes was called\n"); |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | /** |
---|
74 | * read data from NetworkStream |
---|
75 | */ |
---|
76 | int Synchronizeable::readBytes(byte* data, int maxLength, int * reciever) |
---|
77 | { |
---|
78 | PRINTF(5)("Synchronizeable::readBytes was called\n"); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | void Synchronizeable::writeDebug() const |
---|
83 | {} |
---|
84 | |
---|
85 | |
---|
86 | void Synchronizeable::readDebug() const |
---|
87 | {} |
---|
88 | |
---|
89 | |
---|
90 | /** |
---|
91 | * Sets the server flag to a given value |
---|
92 | * @param isServer: the boolean value which the server flag is to set to |
---|
93 | */ |
---|
94 | void Synchronizeable::setIsServer(bool isServer) |
---|
95 | { |
---|
96 | if( isServer ) |
---|
97 | this->state = this->state | STATE_SERVER; |
---|
98 | else |
---|
99 | this->state = this->state & (~STATE_SERVER); |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | /** |
---|
104 | * Sets the outofsync flag to a given value |
---|
105 | * @param outOfSync: the boolean value which the outofsync flag is to set to |
---|
106 | */ |
---|
107 | void Synchronizeable::setIsOutOfSync(bool outOfSync) |
---|
108 | { |
---|
109 | if( outOfSync ) |
---|
110 | this->state = this->state | STATE_OUTOFSYNC; |
---|
111 | else |
---|
112 | this->state = this->state & (~STATE_OUTOFSYNC); |
---|
113 | //PRINTF(0)("isoutofsync %s %d\n", this->getClassName(), state); |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | /** |
---|
118 | * Determines if the server flag is set |
---|
119 | * @return true, if the server flag is true, false else |
---|
120 | */ |
---|
121 | bool Synchronizeable::isServer() |
---|
122 | { |
---|
123 | return (this->state & STATE_SERVER) >0; |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | /** |
---|
128 | * Determines if the outofsync flag is set |
---|
129 | * @return true, if the outofsync flag is true, false else |
---|
130 | */ |
---|
131 | bool Synchronizeable::isOutOfSync() |
---|
132 | { |
---|
133 | return (this->state & STATE_OUTOFSYNC) >0; |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | /** |
---|
138 | * Determines if the requestedSync flag is set |
---|
139 | * @return true, if the requestedSync flag is true, false else |
---|
140 | */ |
---|
141 | bool Synchronizeable::requestedSync() |
---|
142 | { |
---|
143 | return (this->state & STATE_REQUESTEDSYNC) >0; |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | /** |
---|
148 | * Sets the requestedsync flag to a given value |
---|
149 | * @param requestedSync: the boolean value which the requestedsync flag is to set to |
---|
150 | */ |
---|
151 | void Synchronizeable::setRequestedSync( bool requestedSync ) |
---|
152 | { |
---|
153 | if( requestedSync ) |
---|
154 | this->state = this->state | STATE_REQUESTEDSYNC; |
---|
155 | else |
---|
156 | this->state = this->state & (~STATE_REQUESTEDSYNC); |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | |
---|