ScummVM API documentation
net_main.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef SCUMM_HE_NET_MAIN_H
23 #define SCUMM_HE_NET_MAIN_H
24 
25 #include "backends/networking/enet/enet.h"
26 #include "backends/networking/enet/host.h"
27 #include "backends/networking/enet/socket.h"
28 #include "common/formats/json.h"
29 namespace Scumm {
30 
31 class ScummEngine_v90he;
32 
33 class Net {
34 public:
35  Net(ScummEngine_v90he *vm);
36  ~Net();
37 
38 private:
42  struct Address {
43  Common::String host;
44  int port;
45  bool operator==(const Address &other) {
46  return host == other.host && port == other.port;
47  };
48  };
52  struct Session {
53  bool local = false;
54  int id = -1;
55  Common::String host;
56  int port = 0;
57  Common::String name;
58  int players = 0;
59  uint32 timestamp = 0;
60 
61  // For Moonbase map generation:
62  uint8 mapGenerator = 0;
63  int mapSeed = 0;
64  int mapSize = 0;
65  int mapTileset = 0;
66  int mapEnergy = 0;
67  int mapTerrain = 0;
68  int mapWater = 0;
69  Common::String encodedMap;
70  // Used to query map data over LAN.
71  bool getGeneratedMap = false;
72  };
79  Address getAddressFromString(Common::String address);
86  Common::String getStringFromAddress(Address address);
87 
88 public:
97  int hostGame(char *sessionName, char *userName);
108  int joinGame(Common::String IP, char *userName);
109 
118  int addUser(char *shortName, char *longName);
126  int removeUser();
127 
133  int whoSentThis();
139  int whoAmI();
140 
148  int createSession(char *name);
158  int joinSession(int sessionIndex);
168  int joinSessionById(int sessionId);
176  int ifSessionExist(int sessionId);
177 
183  int endSession();
184 
192  void setSessionServer(Common::String sessionServer);
196  void disableSessionJoining();
202  void enableSessionJoining();
203 
209  void setBotsCount(int botsCount);
210 
222  int32 setProviderByName(int32 parameter1, int32 parameter2);
223 
229  void setFakeLatency(int time);
230 
238  bool destroyPlayer(int32 userId);
248  int32 startQuerySessions(bool connectToSessionServer = true);
254  int32 updateQuerySessions();
259  void stopQuerySessions();
265  int querySessions();
266 
272  int queryProviders();
282  int setProvider(int providerIndex);
288  int closeProvider();
289 
298  bool initAll();
305  bool initProvider();
314  bool initSession();
323  bool initUser();
324 
334  void remoteStartScript(int typeOfSend, int sendTypeParam, int priority, int argsCount, int32 *args);
343  void remoteSendArray(int typeOfSend, int sendTypeParam, int priority, int arrayIndex);
344 
359  int remoteStartScriptFunction(int typeOfSend, int sendTypeParam, int priority, int defaultReturnValue, int argsCount, int32 *args);
360 
366  void doNetworkOnceAFrame(int msecs);
367 
368 private:
381  bool connectToSession(Common::String address, int port, bool queryGeneratedMap);
382 
393  int doJoinSession(Session session);
394 
401  void generateMoonbaseMap(Session session);
402 
416  int remoteSendData(int typeOfSend, int sendTypeParam, int type, Common::String data, int priority, int defaultRes = 0, bool wait = false, int callid = 0);
417 
424  bool serviceBroadcast();
432  void handleBroadcastData(Common::String data, Common::String host, int port);
436  void serviceSessionServer();
442  void handleSessionServerData(Common::String data);
446  void remoteReceiveData();
453  void handleGameData(Common::JSONValue *json, int peerIndex);
462  void handleGameDataHost(Common::JSONValue *json, int peerIndex);
463 
464 public:
465  // getters
466 
477  bool getHostName(char *hostname, int length);
489  bool getIPfromName(char *ip, int ipLength, char *nameBuffer);
497  void getSessionName(int sessionNumber, char *buffer, int length);
504  int getSessionPlayerCount(int sessionNumber);
512  void getProviderName(int providerIndex, char *buffer, int length);
513 
514 private:
515  // mostly getters
516 
522  int getTotalPlayers();
523 
524 public:
525  // fields
526  int _latencyTime; // ms
527  bool _fakeLatency;
528 
529  bool _isHost; // true = hosting game, false = joined game.
530 
531  int _myUserId;
532  int _fromUserId;
533 
534  int _sessionId; // Session ID received from the session server.
535 
536 private:
537  ScummEngine_v90he *_vm;
538 
539  Common::String _gameName;
540  Common::String _gameVersion;
541 
542  Networking::ENet::ENet *_enet;
543 
544  byte *_tmpbuffer;
545 
546  int _numUsers;
547  int _numBots;
548  int _maxPlayers;
549  int _userIdCounter;
551  Common::HashMap<int, int> _userIdToPeerIndex;
552  Common::HashMap<int, Common::String> _userIdToAddress;
553  Common::HashMap<Common::String, int> _addressToUserId;
554 
555  Common::String _sessionName;
556  Networking::ENet::Host *_sessionHost;
557 
558  // For Moonbase map generation:
559  uint8 _mapGenerator;
560  int _mapSeed;
561  int _mapSize;
562  int _mapTileset;
563  int _mapEnergy;
564  int _mapTerrain;
565  int _mapWater;
566  Common::String _encodedMap;
567 
568  bool _isShuttingDown;
569 
570  Common::Queue<Common::JSONValue *> _hostDataQueue;
571  Common::Queue<int> _peerIndexQueue;
572 
573  Common::Array<Session> _sessions;
574  int _hostPort;
575 
576  // For broadcasting our game session over LAN.
577  Networking::ENet::Socket *_broadcastSocket;
578 
579  // For creating/joining sessions over the Internet.
580  Networking::ENet::Host *_sessionServerHost;
581  Address _sessionServerAddress;
582  bool _forcedAddress;
583  bool _gotSessions;
584  int _sessionServerPeer;
585  bool _isRelayingGame;
586 };
587 
588 } // End of namespace Scumm
589 
590 #endif
void getSessionName(int sessionNumber, char *buffer, int length)
void getProviderName(int providerIndex, char *buffer, int length)
Get the name of a provider of the given index.
int32 updateQuerySessions()
Definition: str.h:59
Definition: socket.h:41
void enableSessionJoining()
int querySessions()
bool initProvider()
Definition: enet.h:33
void stopQuerySessions()
bool getHostName(char *hostname, int length)
void doNetworkOnceAFrame(int msecs)
int getSessionPlayerCount(int sessionNumber)
int queryProviders()
Definition: net_main.h:33
int whoSentThis()
int whoAmI()
void setSessionServer(Common::String sessionServer)
int endSession()
int setProvider(int providerIndex)
Set the provider by the index.
Definition: json.h:90
Definition: queue.h:42
bool initAll()
int joinSessionById(int sessionId)
bool initSession()
int joinSession(int sessionIndex)
int joinGame(Common::String IP, char *userName)
int ifSessionExist(int sessionId)
bool initUser()
void setFakeLatency(int time)
Sets the fake latency.
void setBotsCount(int botsCount)
Set AI Player count.
int32 setProviderByName(int32 parameter1, int32 parameter2)
Set and initializes the provider given by their name.
void remoteSendArray(int typeOfSend, int sendTypeParam, int priority, int arrayIndex)
int createSession(char *name)
Creates and host a network game session.
void disableSessionJoining()
bool getIPfromName(char *ip, int ipLength, char *nameBuffer)
bool destroyPlayer(int32 userId)
int32 startQuerySessions(bool connectToSessionServer=true)
int addUser(char *shortName, char *longName)
int closeProvider()
Definition: host.h:45
int hostGame(char *sessionName, char *userName)
void remoteStartScript(int typeOfSend, int sendTypeParam, int priority, int argsCount, int32 *args)
int remoteStartScriptFunction(int typeOfSend, int sendTypeParam, int priority, int defaultReturnValue, int argsCount, int32 *args)
Definition: actor.h:30
int removeUser()