OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4Network2Reference.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 #ifndef C4NETWORK2REFERENCE_H
17 #define C4NETWORK2REFERENCE_H
18 
19 #include "C4Version.h"
21 #include "game/C4GameVersion.h"
22 #include "lib/C4InputValidation.h"
23 #include "network/C4Network2.h"
25 
26 const int C4Network2HTTPQueryTimeout = 10; // (s)
27 const uint32_t C4Network2HTTPHappyEyeballsTimeout = 300; // (ms)
28 
29 // Session data
31 {
32 public:
35 
36  // Game parameters
38 
39 private:
40  // General information
41  int32_t Icon{0};
43  StdCopyStrBuf GameMode; // Game mode (e.g. "Melee") to decide the correct league for evaluation
44  C4Network2Status GameStatus;
45  int32_t Time{0};
46  int32_t Frame{0};
47  int32_t StartTime{0};
48  int32_t LeaguePerformance{0}; // custom settlement league performance if scenario doesn't use elapsed frames
50  bool JoinAllowed{true};
51  bool ObservingAllowed{true};
52  bool PasswordNeeded{false};
53  bool OfficialServer{false};
54  bool IsEditor{false};
55  C4NetpuncherID NetpuncherGameID;
56  StdCopyStrBuf NetpuncherAddr;
57  StdCopyStrBuf Statistics;
58 
59  // Engine information
60  C4GameVersion Game;
61 
62  // Network addresses
63  uint8_t iAddrCnt{0};
66 
67 public:
68  const C4Network2Address &getAddr(int i) const { return Addrs[i]; }
69  C4Network2Address &getAddr(int i) { return Addrs[i]; }
70  int getAddrCnt() const { return iAddrCnt; }
71  const char *getTitle() const { return Title.getData(); }
72  int32_t getIcon() const { return Icon; }
73  C4Network2Status getGameStatus() const { return GameStatus; }
74  const char *getComment() const { return Comment.getData(); }
75  const C4GameVersion &getGameVersion() const { return Game; }
76  bool isPasswordNeeded() const { return PasswordNeeded; }
77  bool isJoinAllowed() const { return JoinAllowed; }
78  bool isOfficialServer() const { return OfficialServer; }
79  int32_t getSortOrder() const;
80  int32_t getTime() const { return Time; }
81  int32_t getStartTime() const { return StartTime; }
83  bool isEditor() const { return IsEditor; }
84  C4NetpuncherID getNetpuncherGameID() const { return NetpuncherGameID; }
85  StdStrBuf getNetpuncherAddr() const { return NetpuncherAddr; }
86 
88  const C4NetIO::EndpointAddress &GetSourceAddress() const { return source; }
89 
90  void InitLocal();
91 
92  void SortNullIPsBack();
93 
94  void CompileFunc(StdCompiler *pComp);
95 };
96 
97 // Serves references (mini-HTTP-server)
99 {
100 public:
102  ~C4Network2RefServer() override;
103 
104 private:
105  CStdCSec RefCSec;
106  C4Network2Reference *pReference{nullptr};
107 
108 public:
109  void Clear();
110  void SetReference(C4Network2Reference *pReference);
111 
112 protected:
113  // Overridden
114  void PackPacket(const C4NetIOPacket &rPacket, StdBuf &rOutBuf) override;
115  size_t UnpackPacket(const StdBuf &rInBuf, const C4NetIO::addr_t &addr) override;
116 
117 private:
118  // Responses
119  void RespondNotImplemented(const C4NetIO::addr_t &addr, const char *szMessage);
120  void RespondReference(const C4NetIO::addr_t &addr);
121 
122 };
123 
124 // mini HTTP client
126 {
127 public:
129  ~C4Network2HTTPClient() override;
130 
131 private:
132 
133  // Address information
134  C4NetIO::addr_t ServerAddr, ServerAddrFallback, PeerAddr;
135  StdCopyStrBuf Server, RequestPath;
136 
137  bool fBinary{false};
138  bool fBusy{false}, fSuccess{false}, fConnected{false};
139  size_t iDataOffset{0};
140  StdCopyBuf Request;
141  time_t iRequestTimeout;
142  C4TimeMilliseconds HappyEyeballsTimeout;
143 
144  // Response header data
145  size_t iDownloadedSize{0}, iTotalSize{0};
146  bool fCompressed;
147 
148  // Event queue to use for notify when something happens
149  class C4InteractiveThread *pNotify{nullptr};
150 
151 protected:
152  StdCopyBuf ResultBin; // set if fBinary
153  StdCopyStrBuf ResultString; // set if !fBinary
154 
155 protected:
156 
157  // Overridden
158  void PackPacket(const C4NetIOPacket &rPacket, StdBuf &rOutBuf) override;
159  size_t UnpackPacket(const StdBuf &rInBuf, const C4NetIO::addr_t &addr) override;
160 
161  // Callbacks
162  bool OnConn(const C4NetIO::addr_t &AddrPeer, const C4NetIO::addr_t &AddrConnect, const addr_t *pOwnAddr, C4NetIO *pNetIO) override;
163  void OnDisconn(const C4NetIO::addr_t &AddrPeer, C4NetIO *pNetIO, const char *szReason) override;
164  void OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO) override;
165 
166  void ResetRequestTimeout();
167  virtual int32_t GetDefaultPort() { return 80; }
168 
169 public:
170  bool Query(const StdBuf &Data, bool fBinary);
171  bool Query(const char *szData, bool fBinary) { return Query(StdBuf(szData, SLen(szData)), fBinary); }
172 
173  bool isBusy() const { return fBusy; }
174  bool isSuccess() const { return fSuccess; }
175  bool isConnected() const { return fConnected; }
176  size_t getTotalSize() const { return iTotalSize; }
177  size_t getDownloadedSize() const { return iDownloadedSize; }
178  const StdBuf &getResultBin() const { assert(fBinary); return ResultBin; }
179  const char *getResultString() const { assert(!fBinary); return ResultString.getData(); }
180  const char *getServerName() const { return Server.getData(); }
181  const char *getRequest() const { return RequestPath.getData(); }
182  const C4NetIO::addr_t &getServerAddress() const { return ServerAddr; }
183 
184  void Cancel(const char *szReason);
185  void Clear();
186 
187  bool SetServer(const char *szServerAddress);
188 
189  void SetNotify(class C4InteractiveThread *pnNotify) { pNotify = pnNotify; }
190 
191  // Overridden
192  bool Execute(int iMaxTime, pollfd * readyfds) override { return Execute(iMaxTime); }
193  virtual bool Execute(int iMaxTime = TO_INF);
195 
196 private:
197  bool ReadHeader(StdStrBuf Data);
198  bool Decompress(StdBuf *pData);
199 
200 };
201 
202 // Loads current update url string (mini-HTTP-client)
204 {
205 protected:
206  int32_t GetDefaultPort() override { return C4NetStdPortHTTP; }
207 public:
209 
210  bool QueryUpdateURL();
211  bool GetUpdateURL(StdStrBuf *pUpdateURL);
212  bool GetVersion(StdStrBuf *pVersion);
213 };
214 
215 // Loads references (mini-HTTP-client)
217 {
218 protected:
219  int32_t GetDefaultPort() override { return C4NetStdPortRefServer; }
220 public:
222 
223  bool QueryReferences();
224  bool GetReferences(C4Network2Reference **&rpReferences, int32_t &rRefCount);
225 };
226 
227 #endif // C4NETWORK2REFERENCE_H
const char * getData() const
Definition: StdBuf.h:442
Definition: StdBuf.h:29
void PackPacket(const C4NetIOPacket &rPacket, StdBuf &rOutBuf) override
const char * getResultString() const
size_t UnpackPacket(const StdBuf &rInBuf, const C4NetIO::addr_t &addr) override
const C4Network2Address & getAddr(int i) const
C4TimeMilliseconds GetNextTick(C4TimeMilliseconds tNow) override
const char * getComment() const
virtual int32_t GetDefaultPort()
const int C4Network2HTTPQueryTimeout
C4Network2Status getGameStatus() const
size_t UnpackPacket(const StdBuf &rInBuf, const C4NetIO::addr_t &addr) override
size_t getDownloadedSize() const
int32_t GetDefaultPort() override
C4NetpuncherID getNetpuncherGameID() const
size_t SLen(const char *sptr)
Definition: Standard.h:74
bool GetVersion(StdStrBuf *pVersion)
void Cancel(const char *szReason)
bool GetUpdateURL(StdStrBuf *pUpdateURL)
void CompileFunc(StdCompiler *pComp)
int32_t getTime() const
C4Network2Address & getAddr(int i)
bool isPasswordNeeded() const
const int16_t C4NetStdPortRefServer
Definition: C4Network2.h:32
const char * getTitle() const
bool GetReferences(C4Network2Reference **&rpReferences, int32_t &rRefCount)
size_t getTotalSize() const
const C4NetIO::EndpointAddress & GetSourceAddress() const
const int32_t C4ClientMaxAddr
int32_t getSortOrder() const
int32_t GetDefaultPort() override
void OnDisconn(const C4NetIO::addr_t &AddrPeer, C4NetIO *pNetIO, const char *szReason) override
const int16_t C4NetStdPortHTTP
Definition: C4Network2.h:34
const char * getServerName() const
static const int TO_INF
Definition: C4NetIO.h:66
int32_t getStartTime() const
void OnPacket(const class C4NetIOPacket &rPacket, C4NetIO *pNetIO) override
bool isJoinAllowed() const
const C4NetIO::addr_t & getServerAddress() const
void SetSourceAddress(const C4NetIO::EndpointAddress &ip)
const char * getRequest() const
int32_t getIcon() const
const uint32_t C4Network2HTTPHappyEyeballsTimeout
void SetNotify(class C4InteractiveThread *pnNotify)
bool Query(const char *szData, bool fBinary)
bool OnConn(const C4NetIO::addr_t &AddrPeer, const C4NetIO::addr_t &AddrConnect, const addr_t *pOwnAddr, C4NetIO *pNetIO) override
bool isOfficialServer() const
void PackPacket(const C4NetIOPacket &rPacket, StdBuf &rOutBuf) override
bool Query(const StdBuf &Data, bool fBinary)
const C4GameVersion & getGameVersion() const
bool Execute(int iMaxTime, pollfd *readyfds) override
bool SetServer(const char *szServerAddress)
C4GameParameters Parameters
StdStrBuf getNetpuncherAddr() const
void SetReference(C4Network2Reference *pReference)
~C4Network2HTTPClient() override
StdStrBuf getGameGoalString() const
const StdBuf & getResultBin() const