summaryrefslogtreecommitdiffstats
path: root/source/cClientHandle.h
blob: 21f97f6c3dec76f12d709020671b6e4ab2b8c817 (plain) (blame)
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

// cClientHandle.h

// Interfaces to the cClientHandle class representing a client connected to this server. The client need not be a player yet





#pragma once
#ifndef CCLIENTHANDLE_H_INCLUDED
#define CCLIENTHANDLE_H_INCLUDED

#include "Packets/cPacket.h"
#include "Vector3d.h"





// class Game;
class cChunk;
class cPlayer;
class cRedstone;





class cClientHandle							// tolua_export
{											// tolua_export
public:
	enum ENUM_PRIORITY
	{
		E_PRIORITY_LOW,
		E_PRIORITY_NORMAL
	};

	static const int MAXBLOCKCHANGEINTERACTIONS = 10; // 5 didn't help, 10 seems to have done the trick

	cClientHandle(const cSocket & a_Socket);
	~cClientHandle();

	static const int VIEWDISTANCE = 17; // MUST be odd number or CRASH!
	static const int GENERATEDISTANCE = 2; // Server generates this many chunks AHEAD of player sight.

	const cSocket & GetSocket();
	cPlayer* GetPlayer() { return m_Player; }	// tolua_export

	void Kick(const AString & a_Reason); //tolua_export
	void Authenticate(void);  // Called by cAuthenticator when the user passes authentication

	void AddPacket( cPacket * a_Packet );
	void HandlePendingPackets();

	void StreamChunks();
	void StreamChunksSmart( cChunk** a_Chunks, unsigned int a_NumChunks );
	void RemoveFromAllChunks();

	inline void SetLoggedIn( bool a_bLoggedIn ) { m_bLoggedIn = a_bLoggedIn; }
	inline bool IsLoggedIn() { return m_bLoggedIn; }

	void Tick(float a_Dt);

	bool IsDestroyed() { return m_bDestroyed; }
	void Destroy();

	cChunk* m_LoadedChunks[VIEWDISTANCE*VIEWDISTANCE];

	void Send( const cPacket & a_Packet, ENUM_PRIORITY a_Priority = E_PRIORITY_NORMAL );

	static void SendThread( void *lpParam );
	static void ReceiveThread( void *lpParam );

	const AString & GetUsername(void) const;
	
	inline short GetPing() { return m_Ping; }
private:
	void HandlePacket( cPacket* a_Packet );
	void RemovePacket( cPacket * a_Packet );

	void SendLoginResponse();

	int mProtocolVersion;
	AString mUsername;
	AString mPassword;

	PacketList mPendingParsePackets;
	PacketList mPendingNrmSendPackets;
	PacketList mPendingLowSendPackets;

	cThread* pReceiveThread;
	cThread* pSendThread;

	cSocket mSocket;

	cCriticalSection mCriticalSection;
	cCriticalSection mSendCriticalSection;
	cCriticalSection mSocketCriticalSection;
	cSemaphore       mSemaphore;

	Vector3d mConfirmPosition;

	cPacket * mPacketMap[256];

	bool      m_bDestroyed;
	cPlayer * m_Player;
	bool      m_bKicking;

	float m_TimeLastPacket;

	short m_Ping;
	int m_PingID;
	long long m_PingStartTime;
	long long m_LastPingTime;
	static const unsigned short PING_TIME_MS = 1000; //minecraft sends 1 per 20 ticks (1 second or every 1000 ms)

	bool m_bLoggedIn;
	bool m_bPositionConfirmed;
	bool m_bSendLoginResponse;

	bool m_bKeepThreadGoing;
};										// tolua_export




#endif  // CCLIENTHANDLE_H_INCLUDED