~stratagus/stratagus/bos

« back to all changes in this revision

Viewing changes to bos/tags/boswars-2.7/engine/include/netconnect.h

  • Committer: feb
  • Date: 2014-02-09 12:03:34 UTC
  • Revision ID: svn-v4:34fe1b89-62eb-0310-b8fd-a8472fcdd8b9::10227
Create tag for boswars 2.7.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//     ____                _       __               
 
2
//    / __ )____  _____   | |     / /___ ___________
 
3
//   / __  / __ \/ ___/   | | /| / / __ `/ ___/ ___/
 
4
//  / /_/ / /_/ (__  )    | |/ |/ / /_/ / /  (__  ) 
 
5
// /_____/\____/____/     |__/|__/\__,_/_/  /____/  
 
6
//                                              
 
7
//       A futuristic real-time strategy game.
 
8
//          This file is part of Bos Wars.
 
9
//
 
10
/**@name netconnect.h - The network connection setup header file. */
 
11
//
 
12
//      (c) Copyright 1998-2008 by Lutz Sammer, Andreas Arens, and Jimmy Salmon
 
13
//
 
14
//      This program is free software; you can redistribute it and/or modify
 
15
//      it under the terms of the GNU General Public License as published by
 
16
//      the Free Software Foundation; only version 2 of the License.
 
17
//
 
18
//      This program is distributed in the hope that it will be useful,
 
19
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
//      GNU General Public License for more details.
 
22
//
 
23
//      You should have received a copy of the GNU General Public License
 
24
//      along with this program; if not, write to the Free Software
 
25
//      Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
26
//      02111-1307, USA.
 
27
 
 
28
#ifndef __NETCONNECT_H__
 
29
#define __NETCONNECT_H__
 
30
 
 
31
//@{
 
32
 
 
33
#include <string>
 
34
#include "SDL.h"
 
35
 
 
36
/*----------------------------------------------------------------------------
 
37
--  Defines
 
38
----------------------------------------------------------------------------*/
 
39
 
 
40
        /// Network protocol major version
 
41
#define NetworkProtocolMajorVersion StratagusMajorVersion
 
42
        /// Network protocol minor version (maximal 99)
 
43
#define NetworkProtocolMinorVersion StratagusMinorVersion
 
44
        /// Network protocol patch level (maximal 99)
 
45
#define NetworkProtocolPatchLevel   StratagusPatchLevel
 
46
        /// Network protocol version (1,2,3) -> 10203
 
47
#define NetworkProtocolVersion \
 
48
        (NetworkProtocolMajorVersion * 10000 + NetworkProtocolMinorVersion * 100 + \
 
49
                NetworkProtocolPatchLevel)
 
50
 
 
51
        /// Network protocol printf format string
 
52
#define NetworkProtocolFormatString "%d.%d.%d"
 
53
        /// Network protocol printf format arguments
 
54
#define NetworkProtocolFormatArgs(v) (v) / 10000, ((v) / 100) % 100, (v) % 100
 
55
 
 
56
#define NetworkDefaultPort 6660  /// Default communication port
 
57
 
 
58
/*----------------------------------------------------------------------------
 
59
--  Declarations
 
60
----------------------------------------------------------------------------*/
 
61
 
 
62
/**
 
63
 * Number of bytes in the name of a network player,
 
64
 * including the terminating null character.
 
65
 */
 
66
#define NetPlayerNameSize 16
 
67
 
 
68
/**
 
69
**  Network systems active in current game.
 
70
*/
 
71
class CNetworkHost {
 
72
public:
 
73
        unsigned char *Serialize() const;
 
74
        void Deserialize(const unsigned char *p);
 
75
        static size_t Size() { return 4+2+2+NetPlayerNameSize; }
 
76
 
 
77
        Uint32 Host;         /// Host address
 
78
        Uint16 Port;         /// Port on host
 
79
        Uint16 PlyNr;        /// Player nummer
 
80
        char   PlyName[NetPlayerNameSize];  /// Name of player
 
81
};
 
82
 
 
83
/**
 
84
**  Connect state information of network systems active in current game.
 
85
*/
 
86
typedef struct _network_state_ {
 
87
        unsigned char  State;   /// Menu: ConnectState
 
88
        unsigned short MsgCnt;  /// Menu: Counter for state msg of same type (detect unreachable)
 
89
        // Fill in here...
 
90
} NetworkState;
 
91
 
 
92
/**
 
93
**  Multiplayer game setup menu state
 
94
*/
 
95
class CServerSetup {
 
96
public:
 
97
        unsigned char *Serialize() const;
 
98
        void Deserialize(const unsigned char *p);
 
99
        static size_t Size() { return 1+1+1+1+1+1+1+ 1*PlayerMax + 1*PlayerMax + 4*PlayerMax; }
 
100
        void Clear() {
 
101
                ResourcesOption = UnitsOption = FogOfWar = RevealMap =
 
102
                        GameTypeOption = Difficulty = MapRichness = 0;
 
103
                memset(CompOpt, 0, sizeof(CompOpt));
 
104
                memset(Ready, 0, sizeof(Ready));
 
105
                memset(LastFrame, 0, sizeof(LastFrame));
 
106
        }
 
107
 
 
108
        Uint8  ResourcesOption;       /// Resources option
 
109
        Uint8  UnitsOption;           /// Unit # option
 
110
        Uint8  FogOfWar;              /// Fog of war option
 
111
        Uint8  RevealMap;             /// Reveal all the map
 
112
        Uint8  GameTypeOption;        /// Game type option
 
113
        Uint8  Difficulty;            /// Difficulty option
 
114
        Uint8  MapRichness;           /// Map richness option
 
115
        Uint8  CompOpt[PlayerMax];    /// Free slot option selection  {"Available", "Computer", "Closed" }
 
116
        Uint8  Ready[PlayerMax];      /// Client ready state
 
117
        Uint32 LastFrame[PlayerMax];  /// Last message received
 
118
        // Fill in here...
 
119
};
 
120
 
 
121
/**
 
122
**  Network init message.
 
123
**
 
124
**  @todo Transfering the same data in each message is waste of bandwidth.
 
125
**  I mean the versions and the UID ...
 
126
*/
 
127
class CInitMessage {
 
128
public:
 
129
        unsigned char *Serialize() const;
 
130
        void Deserialize(const unsigned char *p);
 
131
        static size_t Size() { return 1+1+4+4+4+4+4+4+1+256; }
 
132
 
 
133
        Uint8  Type;        /// Init message type
 
134
        Uint8  SubType;     /// Init message subtype
 
135
        Sint32 Stratagus;   /// Stratagus engine version
 
136
        Sint32 Version;     /// Network protocol version
 
137
        Uint32 ConfUID;     /// Engine configuration UID (Checksum) FIXME: not available yet
 
138
        Uint32 MapUID;      /// UID of map to play. FIXME: add MAP name, path, etc
 
139
        Sint32 Lag;         /// Lag time
 
140
        Sint32 Updates;     /// Update frequency
 
141
        Uint8  HostsCount;  /// Number of hosts
 
142
 
 
143
        union {
 
144
                CNetworkHost Hosts[PlayerMax]; /// Participant information
 
145
                char         MapPath[256];
 
146
                CServerSetup State;             /// Server Setup State information
 
147
        } u;
 
148
};
 
149
 
 
150
/**
 
151
**  Network init config message subtypes (menu state machine).
 
152
*/
 
153
enum _ic_message_subtype_ {
 
154
        ICMHello,               /// Client Request
 
155
        ICMConfig,              /// Setup message configure clients
 
156
 
 
157
        ICMEngineMismatch,      /// Stratagus engine version doesn't match
 
158
        ICMProtocolMismatch,    /// Network protocol version doesn't match
 
159
        ICMEngineConfMismatch,  /// Engine configuration isn't identical
 
160
        ICMMapUidMismatch,      /// MAP UID doesn't match
 
161
 
 
162
        ICMGameFull,            /// No player slots available
 
163
        ICMWelcome,             /// Acknowledge for new client connections
 
164
 
 
165
        ICMWaiting,             /// Client has received Welcome and is waiting for Map/State
 
166
        ICMMap,                 /// MapInfo (and Mapinfo Ack)
 
167
        ICMState,               /// StateInfo
 
168
        ICMResync,              /// Ack StateInfo change
 
169
 
 
170
        ICMServerQuit,          /// Server has quit game
 
171
        ICMGoodBye,             /// Client wants to leave game
 
172
        ICMSeeYou,              /// Client has left game
 
173
 
 
174
        ICMGo,                  /// Client is ready to run
 
175
 
 
176
        ICMAYT,                 /// Server asks are you there
 
177
        ICMIAH,                 /// Client answers I am here
 
178
};
 
179
 
 
180
/**
 
181
**  Network Client connect states
 
182
*/
 
183
enum _net_client_con_state_ {
 
184
        ccs_unused = 0,           /// Unused.
 
185
        ccs_connecting,           /// New client
 
186
        ccs_connected,            /// Has received slot info
 
187
        ccs_mapinfo,              /// Has received matching map-info
 
188
        ccs_badmap,               /// Has received non-matching map-info
 
189
        ccs_synced,               /// Client is in sync with server
 
190
        ccs_async,                /// Server user has changed selection
 
191
        ccs_changed,              /// Client user has made menu selection
 
192
        ccs_detaching,            /// Client user wants to detach
 
193
        ccs_disconnected,         /// Client has detached
 
194
        ccs_unreachable,          /// Server is unreachable
 
195
        ccs_usercanceled,         /// Connection canceled by user
 
196
        ccs_nofreeslots,          /// Server has no more free slots
 
197
        ccs_serverquits,          /// Server quits
 
198
        ccs_goahead,              /// Server wants to start game
 
199
        ccs_started,              /// Server has started game
 
200
        ccs_incompatibleengine,   /// Incompatible engine version
 
201
        ccs_incompatiblenetwork,  /// Incompatible netowrk version
 
202
};
 
203
 
 
204
/*----------------------------------------------------------------------------
 
205
--  Variables
 
206
----------------------------------------------------------------------------*/
 
207
 
 
208
extern int NetPlayers;                /// Network players
 
209
extern int NetworkPort;               /// Local network port to use
 
210
 
 
211
extern std::string LocalPlayerName;   /// Name of local player
 
212
 
 
213
extern int HostsCount;                /// Number of hosts.
 
214
extern CNetworkHost Hosts[PlayerMax]; /// Host, port, and number of all players.
 
215
 
 
216
extern int NetConnectRunning;              /// Network menu: Setup mode active
 
217
extern NetworkState NetStates[PlayerMax];  /// Network menu: Server: Client Host states
 
218
extern unsigned char NetLocalState;        /// Network menu: Local Server/Client connect state
 
219
extern int NetLocalHostsSlot;              /// Network menu: Slot # in Hosts array of local client
 
220
extern int NetLocalPlayerNumber;           /// Player number of local client
 
221
 
 
222
extern CServerSetup ServerSetupState;      /// Network menu: Multiplayer Server Menu selections state
 
223
extern CServerSetup LocalSetupState;       /// Network menu: Multiplayer Client Menu selections local state
 
224
 
 
225
/*----------------------------------------------------------------------------
 
226
--  Functions
 
227
----------------------------------------------------------------------------*/
 
228
 
 
229
extern void NetworkServerStartGame(void);       /// Server user has finally hit the start game button
 
230
extern void NetworkGamePrepareGameSettings(void);
 
231
extern void NetworkConnectSetupGame(void);      /// Assign Player slot, evaluate Setup state..
 
232
 
 
233
extern void NetworkInitClientConnect(void);     /// Setup network connect state machine for clients
 
234
extern void NetworkExitClientConnect(void);     /// Terminate network connect state machine for clients
 
235
extern void NetworkInitServerConnect(int openslots); /// Setup network connect state machine for the server
 
236
extern void NetworkExitServerConnect(void);     /// Terminate network connect state machine for the server
 
237
extern int NetworkParseSetupEvent(const unsigned char *buf, int size);  /// Parse a network connect event
 
238
extern int NetworkSetupServerAddress(const std::string &serveraddr);  /// Menu: Setup the server IP
 
239
extern void NetworkProcessClientRequest(void);  /// Menu Loop: Send out client request messages
 
240
extern void NetworkProcessServerRequest(void);  /// Menu Loop: Send out server request messages
 
241
extern void NetworkServerResyncClients(void);   /// Menu Loop: Server: Mark clients state to send stateinfo message
 
242
extern void NetworkDetachFromServer(void);      /// Menu Loop: Client: Send GoodBye to the server and detach
 
243
 
 
244
//@}
 
245
 
 
246
#endif // !__NETCONNECT_H__