52 #ifndef GPG_MULTIPLAYER_H
53 #define GPG_MULTIPLAYER_H
66 typedef std::pair<std::string, std::vector<uint8_t>> SenderAndMessage;
68 enum MultiplayerState {
74 kAdvertisingPromptedUser = 2,
78 kDiscoveringPromptedUser = 4,
80 kDiscoveringWaitingForHost = 5,
84 kConnectedWithDisconnections = 7,
105 bool Initialize(
const std::string& service_id);
109 void AddAppIdentifier(
const std::string& identifier);
116 void StartAdvertising();
119 void StopAdvertising();
123 void StartDiscovery();
125 void StopDiscovery();
128 void DisconnectInstance(
const std::string& instance_id);
130 void DisconnectAll();
138 void set_my_instance_name(
const std::string& my_instance_name) {
139 my_instance_name_ = my_instance_name;
143 MultiplayerState state()
const {
return state_; }
146 bool IsConnected()
const {
147 return state() == kConnected || state() == kConnectedWithDisconnections;
151 bool IsAdvertising()
const {
152 return state() == kAdvertising || state() == kAdvertisingPromptedUser;
155 bool IsDiscovering()
const {
156 return state() == kDiscovering || state() == kDiscoveringPromptedUser ||
157 state() == kDiscoveringWaitingForHost;
160 bool HasError()
const {
return state() == kError; }
164 int GetNumConnectedPlayers();
167 bool is_hosting()
const {
return is_hosting_; }
172 std::string GetInstanceIdByPlayerNumber(
unsigned int player);
178 int GetPlayerNumberByInstanceId(
const std::string& instance_id);
182 bool SendMessage(
const std::string& instance_id,
183 const std::vector<uint8_t>& payload,
bool reliable);
186 void BroadcastMessage(
const std::vector<uint8_t>& payload,
bool reliable);
194 SenderAndMessage GetNextMessage();
197 bool HasReconnectedPlayer();
202 int GetReconnectedPlayer();
207 void set_max_connected_players_allowed(
int players) {
208 max_connected_players_allowed_ = players;
210 int max_connected_players_allowed()
const {
211 return max_connected_players_allowed_;
216 void set_auto_connect(
bool b) { auto_connect_ = b; }
219 bool auto_connect()
const {
return auto_connect_; }
224 void set_allow_reconnecting(
bool b) { allow_reconnecting_ = b; }
227 bool allow_reconnecting()
const {
return allow_reconnecting_; }
230 typedef std::queue<SenderAndMessage> MessageQueue;
233 class DiscoveryListener :
public gpg::IEndpointDiscoveryListener {
235 explicit DiscoveryListener(
236 std::function<
void(gpg::EndpointDetails
const&)> endpointfound_callback,
237 std::function<
void(
const std::string&)> endpointremoved_callback)
238 : endpointfound_callback_(endpointfound_callback),
239 endpointremoved_callback_(endpointremoved_callback) {}
240 void OnEndpointFound(int64_t ,
241 gpg::EndpointDetails
const& endpoint_details) {
243 endpointfound_callback_(endpoint_details);
245 void OnEndpointLost(int64_t ,
const std::string& instance_id) {
247 endpointremoved_callback_(instance_id);
251 std::function<void(gpg::EndpointDetails const&)> endpointfound_callback_;
252 std::function<void(const std::string&)> endpointremoved_callback_;
256 class MessageListener :
public gpg::IMessageListener {
258 explicit MessageListener(
259 std::function<
void(
const std::string&, std::vector<uint8_t>,
bool)>
260 message_received_callback,
261 std::function<
void(
const std::string&)> disconnected_callback)
262 : message_received_callback_(message_received_callback),
263 disconnected_callback_(disconnected_callback) {}
264 void OnMessageReceived(int64_t ,
265 const std::string& instance_id,
266 std::vector<uint8_t>
const& payload,
269 message_received_callback_(instance_id, payload, is_reliable);
271 void OnDisconnected(int64_t ,
272 const std::string& instance_id) {
274 disconnected_callback_(instance_id);
278 std::function<void(const std::string&, std::vector<uint8_t>, bool)>
279 message_received_callback_;
280 std::function<void(const std::string&)> disconnected_callback_;
284 void TransitionState(MultiplayerState old_state, MultiplayerState new_state);
287 void QueueNextState(MultiplayerState next_state);
290 void SendConnectionRequest(
const std::string& host_instance_id);
292 void AcceptConnectionRequest(
const std::string& client_instance_id);
294 void RejectConnectionRequest(
const std::string& client_instance_id);
296 void RejectAllConnectionRequests();
299 void StartAdvertisingCallback(gpg::StartAdvertisingResult
const& info);
300 void ConnectionRequestCallback(
301 gpg::ConnectionRequest
const& connection_request);
302 void DiscoveryEndpointFoundCallback(
303 gpg::EndpointDetails
const& endpoint_details);
304 void DiscoveryEndpointLostCallback(
const std::string& instance_id);
305 void ConnectionResponseCallback(gpg::ConnectionResponse
const& response);
306 void MessageReceivedCallback(
const std::string& instance_id,
307 std::vector<uint8_t>
const& payload,
309 void DisconnectedCallback(
const std::string& instance_id);
312 DialogResponse GetConnectionDialogResponse();
313 bool DisplayConnectionDialog(
const char* title,
const char* question_text,
314 const char* yes_text,
const char* no_text);
318 void UpdateConnectedInstances();
323 int AddNewConnectedInstance(
const std::string& instance_id);
327 void ClearDisconnectedInstances();
330 std::unique_ptr<gpg::NearbyConnections> nearby_connections_;
332 std::unique_ptr<DiscoveryListener> discovery_listener_;
334 std::unique_ptr<MessageListener> message_listener_;
336 std::string service_id_;
337 std::vector<gpg::AppIdentifier> app_identifiers_;
341 std::vector<std::string> connected_instances_;
344 std::map<std::string, int> connected_instances_reverse_;
347 std::list<std::string> pending_instances_;
350 std::list<std::string> discovered_instances_;
353 std::map<std::string, std::string> instance_names_;
357 std::map<std::string, int> disconnected_instances_;
360 std::queue<int> reconnected_players_;
363 MessageQueue incoming_messages_;
366 MultiplayerState state_;
368 std::queue<MultiplayerState> next_states_;
370 std::string my_instance_name_;
371 int max_connected_players_allowed_;
374 pthread_mutex_t message_mutex_;
378 pthread_mutex_t instance_mutex_;
382 pthread_mutex_t state_mutex_;
387 bool allow_reconnecting_;
394 #endif // GPG_MULTIPLAYER_H
Definition: gpg_multiplayer.h:63