Pol  Revision:f37d500
client.h
Go to the documentation of this file.
1 
22 #ifndef __CLIENT_H
23 #define __CLIENT_H
24 
25 #include <atomic>
26 #include <boost/noncopyable.hpp>
27 #include <cstring>
28 #include <memory>
29 #include <mutex>
30 #include <queue>
31 #include <string>
32 
33 #include "../../clib/rawtypes.h"
34 #include "../../clib/spinlock.h"
35 #include "../../clib/wallclock.h"
36 #include "../../clib/weakptr.h"
37 #include "../crypt/cryptkey.h"
38 #include "../pktdef.h"
39 #include "../pktin.h"
40 #include "../polclock.h"
41 #include "../sockets.h"
42 #include "../uconst.h"
43 
44 namespace Pol
45 {
46 namespace Bscript
47 {
48 class BObjectImp;
49 class BStruct;
50 }
51 namespace Core
52 {
53 class MessageTypeFilter;
54 struct XmitBuffer;
55 }
56 namespace Accounts
57 {
58 class Account;
59 }
60 namespace Mobile
61 {
62 class Character;
63 }
64 namespace Crypt
65 {
66 class CCryptBase;
67 }
68 namespace Network
69 {
70 class ClientGameData;
71 class ClientInterface;
72 
73 const u16 T2A = 0x01;
74 const u16 LBR = 0x02;
75 const u16 AOS = 0x04;
76 const u16 SE = 0x08; // set AOS-Flag in send_feature_enable() too for needed checks
77 const u16 ML = 0x10; // set SE- and AOS-Flag in send_feature_enable() too for needed checks
78 const u16 KR =
79  0x20; // set KR- and ML- and SE- and AOS-Flag in send_feature_enable() too for needed checks
80 const u16 SA =
81  0x40; // set SA- and KR- and SE- and AOS-Flag in send_feature_enable() too for needed checks
82 const u16 HSA = 0x80; // set HSA- and SA- and KR- and SE- and AOS-Flag in send_feature_enable() too
83  // for needed checks
84 const u16 TOL =
85  0x100; // set TOL- and HSA- and SA- and KR- and SE- and AOS-Flag in send_feature_enable() too
86  // for needed checks
87 
88 const u8 FLAG_GENDER = 0x01;
89 const u8 FLAG_RACE = 0x02;
90 
92 {
93  int major;
94  int minor;
95  int rev;
96  int patch;
97 };
98 
99 const struct VersionDetailStruct CLIENT_VER_4000 = {4, 0, 0, 0};
100 const struct VersionDetailStruct CLIENT_VER_4070 = {4, 0, 7, 0};
101 const struct VersionDetailStruct CLIENT_VER_5000 = {5, 0, 0, 0};
102 const struct VersionDetailStruct CLIENT_VER_5020 = {5, 0, 2, 0};
103 const struct VersionDetailStruct CLIENT_VER_6017 = {6, 0, 1, 7};
104 const struct VersionDetailStruct CLIENT_VER_60142 = {6, 0, 14, 2};
105 const struct VersionDetailStruct CLIENT_VER_7000 = {7, 0, 0, 0};
106 const struct VersionDetailStruct CLIENT_VER_7090 = {7, 0, 9, 0};
107 const struct VersionDetailStruct CLIENT_VER_70130 = {7, 0, 13, 0};
108 const struct VersionDetailStruct CLIENT_VER_70300 = {7, 0, 30, 0};
109 const struct VersionDetailStruct CLIENT_VER_70331 = {7, 0, 33, 1};
110 
112 {
113  CLIENTTYPE_4000 = 0x1, // 4.0.0a (new spellbookcontent packet 0xbf:0x1b)
114  CLIENTTYPE_4070 = 0x2, // 4.0.7a (new damage packet 0x0b instead of 0xbf:0x22)
115  CLIENTTYPE_5000 = 0x4, // 5.0.0a (compressed gumps)
116  CLIENTTYPE_5020 = 0x8, // 5.0.2a (Buff/Debuff 0xdf)
117  CLIENTTYPE_6017 = 0x10, // 6.0.1.7 (Grid locs)
118  CLIENTTYPE_60142 = 0x20, // 6.0.14.2 (feature enable 0xb9 size change)
120  CLIENTTYPE_7000 = 0x80, // 7.0.0.0 (Gargoyle race)
122  CLIENTTYPE_7090 = 0x200, // 7.0.9.0 (High Sea Adventures)
123  CLIENTTYPE_70130 = 0x400, // 7.0.13.0 (New 0xA9 packet)
124  CLIENTTYPE_70300 = 0x800, // 7.0.30.0 (New Status entries for classic client)
125  CLIENTTYPE_70331 = 0x1000 // 7.0.33.1 new mobile incoming
126 };
127 
128 typedef struct
129 {
130  unsigned char pktbuffer[PKTIN_02_SIZE];
132 
133 class Client : boost::noncopyable
134 {
135 public:
136  Client( ClientInterface& aInterface, Crypt::TCryptInfo& encryption );
137  static void Delete( Client* client );
138  size_t estimatedSize() const;
139 
140 private:
141  void PreDelete();
142  virtual ~Client();
144  bool disconnect; // if 1, disconnect this client
145 
146 public:
147  void Disconnect();
148  void forceDisconnect();
149  bool isActive() const;
150  bool isReallyConnected() const;
151  bool isConnected() const;
152 
153  void unregister(); // removes updater for vitals and takes client away from clientlist
154  void closeConnection();
155  void transmit( const void* data, int len,
156  bool needslock = false ); // for entire message or header only
157  void transmitmore( const void* data, int len ); // for stuff after a header
158 
159  void recv_remaining( int total_expected );
160  void recv_remaining_nocrypt( int total_expected );
161 
162  void setversion( const std::string& ver ) { version_ = ver; }
163  const std::string& getversion() const { return version_; }
164  VersionDetailStruct getversiondetail() const { return versiondetail_; }
165  void setversiondetail( VersionDetailStruct& detail ) { versiondetail_ = detail; }
166  static void itemizeclientversion( const std::string& ver, VersionDetailStruct& detail );
167  bool compareVersion( const std::string& ver );
168  bool compareVersion( const VersionDetailStruct& ver2 );
169  void setClientType( ClientTypeFlag type );
170  bool IsUOKRClient();
171 
172  // Tells whether we should use a different packet handler for this client version
173  bool might_use_v2_handler() const;
174 
175  void setclientinfo( const Core::PKTIN_D9* msg )
176  {
177  memcpy( &clientinfo_, msg, sizeof( clientinfo_ ) );
178  }
179  Bscript::BStruct* getclientinfo() const;
180 
184 
185  bool ready; // all initialization stuff has been sent, ready for general use.
186 
187 
188  //
189  bool have_queued_data() const;
190  void send_queued_data();
191 
192  SOCKET csocket; // socket to client ACK - requires header inclusion.
193  static std::mutex _SocketMutex;
194  unsigned short listen_port;
195  bool aosresist; // UOClient.Cfg Entry
196 
197 
199  {
204  RECV_STATE_CLIENTVERSION_WAIT
205  } recv_state;
206 
207  unsigned char bufcheck1_AA;
208  unsigned char buffer[MAXBUFFER];
209  unsigned char bufcheck2_55;
210  unsigned int bytes_received; // how many bytes have been received into the buffer.
211  unsigned int message_length; // how many bytes are expected for this message
212 
213  sockaddr ipaddr;
214 
216 
217  bool encrypt_server_stream; // encrypt the server stream (data sent to client)?
218 
220 
222  std::string fpLog;
223 
224  std::string status() const;
225 
226  void send_pause();
227  void send_restart();
228 
229  void pause();
230  void restart();
231  std::atomic<int> pause_count;
232 
233  std::string ipaddrAsString() const;
234 
235  bool SpeedHackPrevention( bool add = true );
236  Bscript::BObjectImp* make_ref();
237  weak_ptr<Client> getWeakPtr() const;
238 
239 protected:
242  int n_queued;
243  int queued_bytes_counter; // only used for monitoring
244 
245  // we may want to track how many bytes total are outstanding,
246  // and boot clients that are too far behind.
247  void queue_data( const void* data, unsigned short datalen );
248  void transmit_encrypted( const void* data, int len );
249  void xmit( const void* data, unsigned short datalen );
250 
251 public:
253  unsigned int instance_;
254  static unsigned int instance_counter_;
255  int checkpoint; // CNXBUG
256  unsigned char last_msgtype;
257  size_t thread_pid;
261  std::queue<PacketThrottler> movementqueue;
264  // Will be set by clientthread
267 
268 private:
269  struct
270  {
271  unsigned int bytes_transmitted;
272  unsigned int bytes_received;
273  } counters;
274  std::string version_;
276  bool paused_;
279 };
280 
281 inline bool Client::have_queued_data() const
282 {
283  return ( first_xmit_buffer != NULL );
284 }
285 
286 
287 // Disconnects client. Might lose packets that were not sent by packetqueue.
289 {
290  this->disconnect = true;
291  closeConnection();
292 }
293 
294 // Checks whether the client is disconnected, and not only marked for disconnection
295 inline bool Client::isReallyConnected() const
296 {
297  return !( this->disconnect );
298 }
299 
300 // Checks for both planned and executed disconnections
301 inline bool Client::isConnected() const
302 {
303  return !( this->preDisconnect || this->disconnect );
304 }
305 
306 inline bool Client::isActive() const
307 {
308  return this->ready && this->isConnected();
309 }
310 
311 inline bool Client::might_use_v2_handler() const
312 {
313  return ( this->ClientType & Network::CLIENTTYPE_6017 ) != 0;
314 }
315 }
316 }
317 #endif
unsigned char u8
Definition: rawtypes.h:25
const u16 HSA
Definition: client.h:82
VersionDetailStruct getversiondetail() const
Definition: client.h:164
bool have_queued_data() const
Definition: client.h:281
std::string version_
Definition: client.h:274
#define PKTIN_02_SIZE
Definition: pktdef.h:77
const struct VersionDetailStruct CLIENT_VER_60142
Definition: client.h:104
void forceDisconnect()
Definition: client.h:288
const u8 FLAG_GENDER
Definition: client.h:88
const struct VersionDetailStruct CLIENT_VER_70130
Definition: client.h:107
const struct VersionDetailStruct CLIENT_VER_5020
Definition: client.h:102
const struct VersionDetailStruct CLIENT_VER_7090
Definition: client.h:106
const std::string & getversion() const
Definition: client.h:163
const u8 FLAG_RACE
Definition: client.h:89
sockaddr ipaddr
Definition: client.h:213
int SOCKET
Definition: wnsckt.h:10
Core::polclock_t last_packet_at
Definition: client.h:266
const u16 TOL
Definition: client.h:84
std::string fpLog
Definition: client.h:222
const u16 KR
Definition: client.h:78
Accounts::Account * acct
Definition: client.h:181
std::atomic< int > pause_count
Definition: client.h:231
const u16 ML
Definition: client.h:77
unsigned int wallclock_t
Definition: wallclock.h:8
const struct VersionDetailStruct CLIENT_VER_4070
Definition: client.h:100
void transmit(Client *client, const void *data, int len)
Definition: clientio.cpp:291
const u16 SE
Definition: client.h:76
unsigned int bytes_transmitted
Definition: client.h:271
unsigned int bytes_received
Definition: client.h:210
unsigned char bufcheck2_55
Definition: client.h:209
Mobile::Character * chr
Definition: client.h:182
Clib::wallclock_t next_movement
Definition: client.h:262
unsigned short u16
Definition: rawtypes.h:26
unsigned char buffer[10000]
Definition: UoToolMain.cpp:109
unsigned int u32
Definition: rawtypes.h:27
const u16 AOS
Definition: client.h:75
bool isConnected() const
Definition: client.h:301
Core::PKTIN_D9 clientinfo_
Definition: client.h:275
unsigned int message_length
Definition: client.h:211
#define MAXBUFFER
Definition: uconst.h:88
std::queue< PacketThrottler > movementqueue
Definition: client.h:261
int queued_bytes_counter
Definition: client.h:243
ClientInterface & Interface
Definition: client.h:183
unsigned char bufcheck1_AA
Definition: client.h:207
const struct VersionDetailStruct CLIENT_VER_6017
Definition: client.h:103
const struct VersionDetailStruct CLIENT_VER_4000
Definition: client.h:99
const struct VersionDetailStruct CLIENT_VER_70331
Definition: client.h:109
const struct VersionDetailStruct CLIENT_VER_70300
Definition: client.h:108
int polclock_t
Definition: polclock.h:26
static unsigned int instance_counter_
Definition: client.h:254
Core::XmitBuffer * first_xmit_buffer
Definition: client.h:240
ClientGameData * gd
Definition: client.h:252
bool encrypt_server_stream
Definition: client.h:217
VersionDetailStruct versiondetail_
Definition: client.h:277
bool isActive() const
Definition: client.h:306
static std::mutex _SocketMutex
Definition: client.h:193
const u16 T2A
Definition: client.h:73
std::unordered_map< u64, ScriptDiffData > data
Definition: osmod.cpp:966
weak_ptr_owner< Client > weakptr
Definition: client.h:278
void setversiondetail(VersionDetailStruct &detail)
Definition: client.h:165
bool might_use_v2_handler() const
Definition: client.h:311
Crypt::CCryptBase * cryptengine
Definition: client.h:215
void setversion(const std::string &ver)
Definition: client.h:162
unsigned short listen_port
Definition: client.h:194
const struct VersionDetailStruct CLIENT_VER_5000
Definition: client.h:101
void setclientinfo(const Core::PKTIN_D9 *msg)
Definition: client.h:175
const Core::MessageTypeFilter * msgtype_filter
Definition: client.h:219
unsigned char last_msgtype
Definition: client.h:256
bool isReallyConnected() const
Definition: client.h:295
const u16 SA
Definition: client.h:80
u32 UOExpansionFlagClient
Definition: client.h:259
Core::polclock_t last_activity_at
Definition: client.h:265
void Delete(T *p)
Definition: pol.cpp:665
const struct VersionDetailStruct CLIENT_VER_7000
Definition: client.h:105
bool compareVersion(int ver1major, int ver1minor, int ver1build, int ver2major, int ver2minor, int ver2build)
Definition: cryptkey.cpp:88
Clib::SpinLock _fpLog_lock
Definition: client.h:221
Definition: berror.cpp:12
Core::XmitBuffer * last_xmit_buffer
Definition: client.h:241
unsigned int instance_
Definition: client.h:253
const u16 LBR
Definition: client.h:74