HoverRace  2.0
NetworkSession.h
Go to the documentation of this file.
1 #ifdef HR_LEGACY_BUILD
2 // NetworkSession.h
3 //
4 //
5 // Copyright (c) 1995-1998 - Richard Langlois and Grokksoft Inc.
6 //
7 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
8 // you may not use this file except in compliance with the License.
9 //
10 // A copy of the license should have been attached to the package from which
11 // you have taken this file. If you can not find the license you can not use
12 // this file.
13 //
14 //
15 // The author makes no representations about the suitability of
16 // this software for any purpose. It is provided "as is" "AS IS",
17 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
18 // implied.
19 //
20 // See the License for the specific language governing permissions
21 // and limitations under the License.
22 //
23 //
24 
25 #pragma once
26 
27 #include "ClientSession.h"
28 #include "RoomList.h"
29 #include "NetInterface.h"
30 
31 namespace HoverRace {
32 namespace Client {
33 
39 class NetworkSession : public ClientSession
40 {
41  typedef ClientSession SUPER;
42  protected:
43  class PlayerResult
44  {
45  public:
46  int mPlayerIndex; // -1 = CurrentPlayer
47  int mPlayerId;
48  int mCraftModel;
49  int mNbCompletedLap;
50  MR_SimulationTime mFinishTime;
51  MR_SimulationTime mBestLap;
52 
53  PlayerResult *mNext;
54 
55  // Hit Result
56  int mNbTimeHit; // Nb of missiles and mines hit
57  int mNbGoodShot; // nb of missiles that hit someone else
58 
59  PlayerResult *mNextHitResult; // Create a second list from the initial structure
60  };
61 
62  BOOL mMasterMode;
63  NetworkInterface mNetInterface;
64  BOOL mTimeToSendCharacterCreation; // 0 mean sended
65  BOOL mSended12SecClockUpdate; // User by server to adjust client clock
66  BOOL mSended8SecClockUpdate;
67  int mMajorID;
68  int mMinorID;
69 
70  RoomListPtr roomList;
71 
72  int mSendedPlayerStats;
73  MR_FreeElementHandle mClient[NetworkInterface::eMaxClient];
74  MainCharacter::MainCharacter *mClientCharacter[NetworkInterface::eMaxClient];
75 
76  int mLastSendElemStateFuncTime;
77  int mLastSendElemStateTime[NetworkInterface::eMaxClient];
78 
79  PlayerResult *mResultList;
80  PlayerResult *mHitList;
81 
82  char mChatEditBuffer[120];
83 
84  BOOL mInternetGame;
85  HWND mWindow;
86 
87  // Awfull Ladder patch
88  int mOpponendMajorID;
89  int mOpponendMinorID;
90 
91  // Network functions
92  // Must be called only once
93  void BroadcastMainElementCreation(const Util::ObjectFromFactoryId &pId, const Model::ElementNetState &pState, int pRoom, int pHoverId);
94  // Creation of autonomous elements
95  void BroadcastAutoElementCreation(const Util::ObjectFromFactoryId &pId, const Model::ElementNetState &pState, int pRoom);
96  void BroadcastPermElementState(int pPermId, const Model::ElementNetState &pState, int pRoom);
97  void BroadcastMainElementState(const Model::ElementNetState &pState);
98  void BroadcastMainElementStats(MR_SimulationTime pFinishTime, MR_SimulationTime pBestLap, int pNbLap);
99  void BroadcastChatMessage(const char *pMessage);
100  void BroadcastTime();
101  void BroadcastHit(int pHoverIdSrc);
102 
103  void AddChatMessage(int pPlayerIndex, const char *Message, int pMessageLen);
104  void AddResultEntry(int pPlayerIndex, MR_SimulationTime pFinishTime, MR_SimulationTime pBestLap, int pNbLap);
105  void AddHitEntry(int pPlayerIndex, int pPlayerFromID);
106  // helper
107  void InsertHitEntry(PlayerResult *pEntry);
108 
109  void ReadNet();
110  void WriteNet();
111 
112  static void ElementCreationHook(Model::FreeElement * pElement, int pRoom, void *pThis);
113  static void PermElementStateHook(Model::FreeElement * pElement, int pRoom, int pPermId, void *pThis);
114 
115  public:
116  // Creation and destruction
117  NetworkSession(BOOL pInternetGame, int pMajorID, int pMinorID, HWND pWindow);
118  ~NetworkSession();
119 
120  // Simulation control
121  void Process();
122 
123  BOOL LoadNew(const char *pTitle, HoverRace::Parcel::RecordFilePtr pMazeFile, int pNbLap, char pGameOpts, VideoServices::VideoBuffer * pVideo);
124 
125  BOOL CreateMainCharacter();
126 
127  void SetSimulationTime(MR_SimulationTime pTime);
128 
129  void SetPlayerName(const char *pPlayerName);
130  const char *GetPlayerName() const;
131  void SetRoomList(RoomListPtr roomList);
132  BOOL WaitConnections(HWND pWindow, const char *pTrackName, BOOL pPromptForPort = TRUE,
133  unsigned pDefaultPort = HoverRace::Util::Config::GetInstance()->net.tcpServPort,
134  HWND * pModalessDlg = NULL, int pReturnMessage = 0);
135  BOOL PreConnectToServer(HWND pWindow, std::string &pTrackName);
136  BOOL ConnectToServer(HWND pWindow, const char *pServerIP = NULL,
137  unsigned pPort = HoverRace::Util::Config::GetInstance()->net.tcpServPort,
138  const char *pGameName = NULL, HWND * pModalessDlg = NULL,
139  int pReturnMessage = 0);
140 
141  int ResultAvaillable() const; // Return the number of players desc avail
142  void GetResult(int pPosition, const char *&pPlayerName, int &pId, BOOL &pConnected, int &pNbLap, MR_SimulationTime & pFinishTime, MR_SimulationTime & pBestLap) const;
143  void GetHitResult(int pPosition, const char *&pPlayerName, int &pId, BOOL &pConnected, int &pNbHitOther, int &pNbHitHimself) const;
144 
145  int GetNbPlayers() const;
146  int GetRank(const MainCharacter::MainCharacter * pPlayer) const;
147 
148  void AddMessageKey(char pKey);
149  void GetCurrentMessage(char *pDest) const;
150 
151  MainCharacter::MainCharacter *GetPlayer(int pPlayerIndex) const;
152 };
153 
154 } // namespace Client
155 } // namespace HoverRace
156 #endif
MR_Int32 MR_SimulationTime
Definition: WorldCoordinates.h:106
std::shared_ptr< RoomList > RoomListPtr
Definition: RoomList.h:96
static Config * GetInstance()
Definition: Config.h:151
MR_FreeElementHandleClass * MR_FreeElementHandle
Definition: MazeElement.h:51
Definition: Announcement.h:24