File: network.h

package info (click to toggle)
warmux 1%3A11.04.1%2Brepack2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 126,388 kB
  • sloc: cpp: 186,040; xml: 8,909; sh: 3,358; makefile: 1,052; ansic: 713
file content (166 lines) | stat: -rw-r--r-- 5,401 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/******************************************************************************
 *  Warmux is a convivial mass murder game.
 *  Copyright (C) 2001-2011 Warmux Team.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 ******************************************************************************
 * Network layer for Warmux.
 *****************************************************************************/

#ifndef NETWORK_H
#define NETWORK_H
//-----------------------------------------------------------------------------
#include <list>
#include <string>
#include <WARMUX_network.h>
#include <WARMUX_player.h>
#include <WARMUX_singleton.h>
#include <WARMUX_base.h>
//-----------------------------------------------------------------------------

// Use this debug to store network communication to a file
//#define LOG_NETWORK
// Factorize some declarations for Visual C++
#ifdef _MSC_VER
#  define S_IRUSR _S_IREAD
#  define S_IWUSR _S_IWRITE
#endif
#ifdef WIN32
#  define S_IRGRP 0
#  define O_SYNC  O_BINARY
#endif

// Some forward declarations
struct SDL_Thread;
class Action;
class DistantComputer;
class NetworkServer;
class NetworkMenu;
class WSocketSet;


class NetworkThread
{
  static SDL_Thread* thread; // network thread, where we receive data from network
  static bool stop_thread;

  static void ReceiveActions();
  static int ThreadRun(void* no_param);
public:
  static void Start();
  static void Stop() { stop_thread = true; }

  static bool Continue() { return !stop_thread; }
  static void Wait();
};


class Network : public Singleton<Network>
{
  static int num_objects;

  std::list<DistantComputer*> cpu; // list of the connected computer
  SDL_mutex *cpus_lock;

  std::string game_name;
  std::string password;

  Player player;
  bool turn_master_player;

protected:
  bool game_master_player;
  WNet::net_game_state_t state;

  Network(const std::string& game_name, const std::string& password); // pattern singleton

  WSocketSet* socket_set;

#ifdef LOG_NETWORK
  int fout;
  int fin;
#endif

  virtual void SendAction(const Action& a, DistantComputer* client, bool clt_as_rcver) const;

  void DisconnectNetwork();

  void SetGameName(const std::string& _game_name) { game_name = _game_name; }
public:
  NetworkMenu* network_menu;

  virtual ~Network();

  static Network* GetInstance();
  static NetworkServer* GetInstanceServer(); // WARNING: return NULL if not server!!

  // Start a client
  static connection_state_t ClientStart(const std::string &host, const std::string &port,
                                        const std::string& password);

  // Start a server
  static connection_state_t ServerStart(const std::string &port,
                                        const std::string& game_name,
                                        const std::string& password);

  static void Disconnect();
  static bool IsConnected() { return !GetInstance()->IsLocal() && NetworkThread::Continue(); }

  virtual bool IsLocal() const { return false; }
  virtual bool IsServer() const { return false; }
  virtual bool IsClient() const { return false; }

  void SetGameMaster(); // useful when we re-electing a game master
  bool IsGameMaster() const { return game_master_player; }
  const std::string& GetGameName() const { return game_name; }
  const std::string& GetPassword() const { return password; }
  Player& GetPlayer() { return player; }
  const Player& GetPlayer() const { return player; }
  Player * LockRemoteHostsAndGetPlayer(uint player_id);

  std::list<DistantComputer*>& GetRemoteHosts() { return cpu; }

  std::list<DistantComputer*>& LockRemoteHosts();
  const std::list<DistantComputer*>& LockRemoteHosts() const;
  void UnlockRemoteHosts() const;
  void AddRemoteHost(DistantComputer *host);
  void RemoveRemoteHost(std::list<DistantComputer*>::iterator host_it);

  int CheckActivity(int timeout);
  virtual void HandleAction(Action* a, DistantComputer* sender) = 0;
  virtual void WaitActionSleep() = 0;
  virtual void CloseConnection(std::list<DistantComputer*>::iterator closed) = 0;

  // Action handling
  void SendActionToAll(const Action& action) const;
  void SendActionToOne(const Action& action, DistantComputer* client) const;
  void SendActionToAllExceptOne(const Action& action, DistantComputer* client) const;

  // Manage network state
  void SetState(WNet::net_game_state_t state);
  WNet::net_game_state_t GetState() const { return state; }
  void SendNetworkState();

  bool IsTurnMaster() const;

  uint GetNbPlayersConnected() const;
  uint GetNbPlayersWithState(Player::State player_state) const;
  std::vector<uint> GetCommonMaps();

  void SendMapsList();
};

//-----------------------------------------------------------------------------
#endif