Pyrogenesis  trunk
NetHost.h
Go to the documentation of this file.
1 /* Copyright (C) 2016 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef NETHOST_H
19 #define NETHOST_H
20 
21 #include "ps/CStr.h"
22 
23 #include <map>
24 
25 /**
26  * @file
27  * Various declarations shared by networking code.
28  */
29 
30 typedef struct _ENetPeer ENetPeer;
31 typedef struct _ENetPacket ENetPacket;
32 typedef struct _ENetHost ENetHost;
33 class CNetMessage;
34 
36 {
37  /**
38  * Whether the player is currently connected and active.
39  * (We retain information on disconnected players to support rejoining,
40  * but don't transmit these to other clients.)
41  */
42  bool m_Enabled;
43 
44  /// Player name
45  CStrW m_Name;
46 
47  /// The player that the given host controls, or -1 if none (observer)
49 
50  /// Status - Ready or not: 0 for not ready, 1 for ready
52 };
53 
54 typedef std::map<CStr, PlayerAssignment> PlayerAssignmentMap; // map from GUID -> assignment
55 
56 /**
57  * Reasons sent by server to clients in disconnection messages.
58  * Must be kept in sync with binaries/data/mods/public/gui/common/network.js
59  */
61 {
72 };
73 
74 class CNetHost
75 {
76 public:
77  static const int DEFAULT_CHANNEL = 0;
78 
79  /**
80  * Transmit a message to the given peer.
81  * @param message message to send
82  * @param peer peer to send to
83  * @param peerName name of peer for debug logs
84  * @return true on success, false on failure
85  */
86  static bool SendMessage(const CNetMessage* message, ENetPeer* peer, const char* peerName);
87 
88  /**
89  * Construct an ENet packet by serialising the given message.
90  * @return NULL on failure
91  */
92  static ENetPacket* CreatePacket(const CNetMessage* message);
93 
94  /**
95  * Initialize ENet.
96  * This must be called before any other networking code.
97  */
98  static void Initialize();
99 
100  /**
101  * Deinitialize ENet.
102  */
103  static void Deinitialize();
104 };
105 
106 #endif // NETHOST_H
Definition: NetHost.h:62
Definition: NetHost.h:67
std::map< CStr, PlayerAssignment > PlayerAssignmentMap
Definition: NetHost.h:54
Definition: NetHost.h:65
CStrW m_Name
Player name.
Definition: NetHost.h:45
Definition: NetHost.h:69
i32 m_PlayerID
The player that the given host controls, or -1 if none (observer)
Definition: NetHost.h:48
Definition: NetHost.h:74
NetDisconnectReason
Reasons sent by server to clients in disconnection messages.
Definition: NetHost.h:60
struct _ENetPeer ENetPeer
Definition: NetHost.h:30
uint8_t u8
Definition: types.h:37
Definition: NetHost.h:70
Definition: NetHost.h:35
Definition: NetHost.h:71
The base class for all network messages exchanged within the game.
Definition: NetMessage.h:32
int32_t i32
Definition: types.h:34
struct _ENetPacket ENetPacket
Definition: NetHost.h:31
bool m_Enabled
Whether the player is currently connected and active.
Definition: NetHost.h:42
Definition: NetHost.h:64
Definition: NetHost.h:68
struct _ENetHost ENetHost
Definition: NetHost.h:32
u8 m_Status
Status - Ready or not: 0 for not ready, 1 for ready.
Definition: NetHost.h:51
Definition: NetHost.h:63
Definition: NetHost.h:66