2012-06-14 09:06:06 -04:00
// 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
2012-08-26 12:20:08 -04:00
# include "Defines.h"
2012-06-14 09:06:06 -04:00
# include "Vector3d.h"
2012-09-23 17:23:33 -04:00
# include "OSSupport/SocketThreads.h"
2012-06-14 09:06:06 -04:00
# include "ChunkDef.h"
2012-08-17 06:18:07 -04:00
# include "ByteBuffer.h"
2012-06-14 09:06:06 -04:00
2012-08-27 13:31:16 -04:00
class cChunkDataSerializer ;
2012-08-19 07:51:17 -04:00
class cInventory ;
2012-08-27 13:31:16 -04:00
class cMonster ;
2012-08-19 17:14:45 -04:00
class cPawn ;
2012-08-24 03:58:26 -04:00
class cPickup ;
2012-08-27 13:31:16 -04:00
class cPlayer ;
class cProtocol ;
class cRedstone ;
class cWindow ;
2012-12-26 04:12:00 -05:00
class cFallingBlock ;
2013-01-11 23:46:01 -05:00
class cItemHandler ;
2012-06-14 09:06:06 -04:00
2012-12-24 17:09:01 -05:00
2012-06-14 09:06:06 -04:00
class cClientHandle : // tolua_export
public cSocketThreads : : cCallback
{ // tolua_export
public :
enum ENUM_PRIORITY
{
E_PRIORITY_LOW ,
E_PRIORITY_NORMAL
} ;
static const int MAXBLOCKCHANGEINTERACTIONS = 20 ; // 5 didn't help, 10 still doesn't work in Creative, 20 seems to have done the trick
2012-09-29 09:41:47 -04:00
# if defined(ANDROID_NDK)
static const int DEFAULT_VIEW_DISTANCE = 4 ; // The default ViewDistance (used when no value is set in Settings.ini)
# else
static const int DEFAULT_VIEW_DISTANCE = 9 ;
# endif
2012-06-14 09:06:06 -04:00
static const int MAX_VIEW_DISTANCE = 10 ;
static const int MIN_VIEW_DISTANCE = 4 ;
2012-09-25 04:23:19 -04:00
cClientHandle ( const cSocket * a_Socket , int a_ViewDistance ) ;
virtual ~ cClientHandle ( ) ;
2012-06-14 09:06:06 -04:00
2012-09-25 04:23:19 -04:00
const AString & GetIPString ( void ) const { return m_IPString ; }
2012-06-14 09:06:06 -04:00
cPlayer * GetPlayer ( ) { return m_Player ; } // tolua_export
2013-01-11 23:46:01 -05:00
void Kick ( const AString & a_Reason ) ; // tolua_export
2012-06-14 09:06:06 -04:00
void Authenticate ( void ) ; // Called by cAuthenticator when the user passes authentication
void StreamChunks ( void ) ;
// Removes the client from all chunks. Used when switching worlds or destroying the player
void RemoveFromAllChunks ( void ) ;
2012-11-11 09:00:58 -05:00
inline bool IsLoggedIn ( void ) const { return ( m_State > = csAuthenticating ) ; }
2012-06-14 09:06:06 -04:00
void Tick ( float a_Dt ) ;
2012-11-11 09:00:58 -05:00
void Destroy ( void ) ;
2012-06-14 09:06:06 -04:00
2012-11-11 09:00:58 -05:00
bool IsPlaying ( void ) const { return ( m_State = = csPlaying ) ; }
bool IsDestroyed ( void ) const { return ( m_State = = csDestroyed ) ; }
bool IsDestroying ( void ) const { return ( m_State = = csDestroying ) ; }
2012-06-14 09:06:06 -04:00
2012-10-21 03:46:28 -04:00
void SendBlockAction ( int a_BlockX , int a_BlockY , int a_BlockZ , char a_Byte1 , char a_Byte2 , BLOCKTYPE a_BlockType ) ;
void SendBlockChange ( int a_BlockX , int a_BlockY , int a_BlockZ , BLOCKTYPE a_BlockType , NIBBLETYPE a_BlockMeta ) ;
void SendBlockChanges ( int a_ChunkX , int a_ChunkZ , const sSetBlockVector & a_Changes ) ;
void SendChat ( const AString & a_Message ) ;
void SendChunkData ( int a_ChunkX , int a_ChunkZ , cChunkDataSerializer & a_Serializer ) ;
void SendCollectPickup ( const cPickup & a_Pickup , const cPlayer & a_Player ) ;
void SendDestroyEntity ( const cEntity & a_Entity ) ;
void SendDisconnect ( const AString & a_Reason ) ;
void SendEntHeadLook ( const cEntity & a_Entity ) ;
void SendEntLook ( const cEntity & a_Entity ) ;
void SendEntRelMove ( const cEntity & a_Entity , char a_RelX , char a_RelY , char a_RelZ ) ;
void SendEntRelMoveLook ( const cEntity & a_Entity , char a_RelX , char a_RelY , char a_RelZ ) ;
void SendEntityEquipment ( const cEntity & a_Entity , short a_SlotNum , const cItem & a_Item ) ;
void SendEntityStatus ( const cEntity & a_Entity , char a_Status ) ;
void SendGameMode ( eGameMode a_GameMode ) ;
void SendHealth ( void ) ;
void SendInventoryProgress ( char a_WindowID , short a_Progressbar , short a_Value ) ;
void SendInventorySlot ( char a_WindowID , short a_SlotNum , const cItem & a_Item ) ;
void SendMetadata ( const cPawn & a_Entity ) ;
void SendPickupSpawn ( const cPickup & a_Pickup ) ;
void SendPlayerAnimation ( const cPlayer & a_Player , char a_Animation ) ;
void SendPlayerListItem ( const cPlayer & a_Player , bool a_IsOnline ) ;
void SendPlayerMoveLook ( void ) ;
void SendPlayerPosition ( void ) ;
void SendPlayerSpawn ( const cPlayer & a_Player ) ;
void SendRespawn ( void ) ;
void SendSoundEffect ( const AString & a_SoundName , int a_SrcX , int a_SrcY , int a_SrcZ , float a_Volume , float a_Pitch ) ; // a_Src coords are Block * 8
void SendSoundParticleEffect ( int a_EffectID , int a_SrcX , int a_SrcY , int a_SrcZ , int a_Data ) ;
void SendBlockBreakAnim ( int a_entityID , int a_blockX , int a_blockY , int a_blockZ , char a_stage ) ;
2012-12-26 04:12:00 -05:00
void SendSpawnFallingBlock ( const cFallingBlock & a_FallingBlock ) ;
2012-10-21 03:46:28 -04:00
void SendSpawnMob ( const cMonster & a_Mob ) ;
2013-02-21 16:55:36 -05:00
void SendSpawnObject ( const cEntity & a_Entity , char a_ObjectType , int a_ObjectData , Byte a_Yaw , Byte a_Pitch ) ;
2013-02-18 11:48:50 -05:00
void SendSpawnVehicle ( const cEntity & a_Vehicle , char a_VehicleType ) ;
2012-10-21 03:46:28 -04:00
void SendTeleportEntity ( const cEntity & a_Entity ) ;
void SendThunderbolt ( int a_BlockX , int a_BlockY , int a_BlockZ ) ;
2012-11-01 17:38:20 -04:00
void SendTimeUpdate ( Int64 a_WorldAge , Int64 a_TimeOfDay ) ;
2012-10-21 03:46:28 -04:00
void SendUnloadChunk ( int a_ChunkX , int a_ChunkZ ) ;
void SendUpdateSign ( int a_BlockX , int a_BlockY , int a_BlockZ , const AString & a_Line1 , const AString & a_Line2 , const AString & a_Line3 , const AString & a_Line4 ) ;
void SendWeather ( eWeather a_Weather ) ;
void SendWholeInventory ( const cInventory & a_Inventory ) ;
void SendWholeInventory ( const cWindow & a_Window ) ;
void SendWindowClose ( char a_WindowID ) ;
void SendWindowOpen ( char a_WindowID , char a_WindowType , const AString & a_WindowTitle , char a_NumSlots ) ;
void SendUseBed ( const cEntity & a_Entity , int a_BlockX , int a_BlockY , int a_BlockZ ) ;
2012-06-14 09:06:06 -04:00
2013-01-11 23:46:01 -05:00
const AString & GetUsername ( void ) const ; // tolua_export
void SetUsername ( const AString & a_Username ) ; // tolua_export
2012-06-14 09:06:06 -04:00
2013-01-11 23:46:01 -05:00
inline short GetPing ( void ) const { return m_Ping ; } // tolua_export
2012-06-14 09:06:06 -04:00
2012-11-11 09:00:58 -05:00
void SetViewDistance ( int a_ViewDistance ) ; // tolua_export
int GetViewDistance ( void ) const { return m_ViewDistance ; } // tolua_export
2012-06-14 09:06:06 -04:00
2013-01-11 23:46:01 -05:00
int GetUniqueID ( ) const { return m_UniqueID ; } // tolua_export
2012-06-14 09:06:06 -04:00
/// Returns true if the client wants the chunk specified to be sent (in m_ChunksToSend)
bool WantsSendChunk ( int a_ChunkX , int a_ChunkY , int a_ChunkZ ) ;
/// Adds the chunk specified to the list of chunks wanted for sending (m_ChunksToSend)
void AddWantedChunk ( int a_ChunkX , int a_ChunkZ ) ;
2012-08-27 13:31:16 -04:00
// Calls that cProtocol descendants use to report state:
void PacketBufferFull ( void ) ;
void PacketUnknown ( unsigned char a_PacketType ) ;
void PacketError ( unsigned char a_PacketType ) ;
2012-06-14 09:06:06 -04:00
2012-08-27 13:31:16 -04:00
// Calls that cProtocol descendants use for handling packets:
2012-08-30 04:19:19 -04:00
void HandlePing ( void ) ;
2012-08-27 13:31:16 -04:00
void HandleCreativeInventory ( short a_SlotNum , const cItem & a_HeldItem ) ;
void HandlePlayerPos ( double a_PosX , double a_PosY , double a_PosZ , double a_Stance , bool a_IsOnGround ) ;
2013-01-11 23:46:01 -05:00
void HandleLeftClick ( int a_BlockX , int a_BlockY , int a_BlockZ , char a_BlockFace , char a_Status ) ;
void HandleRightClick ( int a_BlockX , int a_BlockY , int a_BlockZ , char a_BlockFace , int a_CursorX , int a_CursorY , int a_CursorZ , const cItem & a_HeldItem ) ;
2012-08-27 13:31:16 -04:00
void HandleChat ( const AString & a_Message ) ;
void HandlePlayerLook ( float a_Rotation , float a_Pitch , bool a_IsOnGround ) ;
void HandlePlayerMoveLook ( double a_PosX , double a_PosY , double a_PosZ , double a_Stance , float a_Rotation , float a_Pitch , bool a_IsOnGround ) ; // While m_bPositionConfirmed (normal gameplay)
void HandleAnimation ( char a_Animation ) ;
void HandleSlotSelected ( short a_SlotNum ) ;
void HandleWindowClose ( char a_WindowID ) ;
void HandleWindowClick ( char a_WindowID , short a_SlotNum , bool a_IsRightClick , bool a_IsShiftPressed , const cItem & a_HeldItem ) ;
void HandleUpdateSign (
int a_BlockX , int a_BlockY , int a_BlockZ ,
const AString & a_Line1 , const AString & a_Line2 ,
const AString & a_Line3 , const AString & a_Line4
) ;
void HandleUseEntity ( int a_TargetEntityID , bool a_IsLeftClick ) ;
void HandleRespawn ( void ) ;
void HandleDisconnect ( const AString & a_Reason ) ;
void HandleKeepAlive ( int a_KeepAliveID ) ;
2012-09-05 16:30:27 -04:00
bool HandleHandshake ( const AString & a_Username ) ;
2012-09-29 16:43:42 -04:00
void HandleEntityAction ( int a_EntityID , char a_ActionID ) ;
2012-08-27 13:31:16 -04:00
2012-08-30 04:19:19 -04:00
/** Called when the protocol has finished logging the user in.
Return true to allow the user in ; false to kick them .
*/
bool HandleLogin ( int a_ProtocolVersion , const AString & a_Username ) ;
2012-08-27 13:31:16 -04:00
void SendData ( const char * a_Data , int a_Size ) ;
2012-06-14 09:06:06 -04:00
private :
int m_ViewDistance ; // Number of chunks the player can see in each direction; 4 is the minimum ( http://wiki.vg/Protocol_FAQ#.E2.80.A6all_connecting_clients_spasm_and_jerk_uncontrollably.21 )
static const int GENERATEDISTANCE = 2 ; // Server generates this many chunks AHEAD of player sight. 2 is the minimum, since foliage is generated 1 step behind chunk terrain generation
2012-09-25 04:23:19 -04:00
AString m_IPString ;
2012-06-14 09:06:06 -04:00
2012-08-17 06:18:07 -04:00
int m_ProtocolVersion ;
2012-06-14 09:06:06 -04:00
AString m_Username ;
AString m_Password ;
cCriticalSection m_CSChunkLists ;
cChunkCoordsList m_LoadedChunks ; // Chunks that the player belongs to
cChunkCoordsList m_ChunksToSend ; // Chunks that need to be sent to the player (queued because they weren't generated yet or there's not enough time to send them)
2012-08-27 13:31:16 -04:00
cProtocol * m_Protocol ;
cCriticalSection m_CSOutgoingData ;
cByteBuffer m_OutgoingData ;
2012-09-06 04:33:43 -04:00
AString m_OutgoingDataOverflow ; //< For data that didn't fit into the m_OutgoingData ringbuffer temporarily
2012-06-14 09:06:06 -04:00
cCriticalSection m_CriticalSection ;
Vector3d m_ConfirmPosition ;
cPlayer * m_Player ;
2013-01-05 14:05:15 -05:00
bool m_HasSentDC ;
2012-06-14 09:06:06 -04:00
// Chunk position when the last StreamChunks() was called; used to avoid re-streaming while in the same chunk
int m_LastStreamedChunkX ;
int m_LastStreamedChunkZ ;
2012-11-01 17:38:20 -04:00
/// Seconds since the last packet data was received (updated in Tick(), reset in DataReceived())
float m_TimeSinceLastPacket ;
2012-06-14 09:06:06 -04:00
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)
2012-09-25 05:54:36 -04:00
// Values required for block dig animation
2013-01-11 23:46:01 -05:00
int m_BlockDigAnimStage ; // Current stage of the animation; -1 if not digging
2012-09-25 05:54:36 -04:00
int m_BlockDigAnimSpeed ; // Current speed of the animation (units ???)
2013-01-11 23:46:01 -05:00
int m_BlockDigAnimX ;
int m_BlockDigAnimY ;
int m_BlockDigAnimZ ;
2012-06-14 09:06:06 -04:00
2013-01-11 23:46:01 -05:00
// To avoid dig/aim bug in the client, store the last position given in a DIG_START packet and compare to that when processing the DIG_FINISH packet:
bool m_HasStartedDigging ;
int m_LastDigBlockX ;
int m_LastDigBlockY ;
int m_LastDigBlockZ ;
2012-11-12 06:10:01 -05:00
2012-06-14 09:06:06 -04:00
enum eState
{
csConnected , // The client has just connected, waiting for their handshake / login
csAuthenticating , // The client has logged in, waiting for external authentication
csDownloadingWorld , // The client is waiting for chunks, we're waiting for the loader to provide and send them
csConfirmingPos , // The client has been sent the position packet, waiting for them to repeat the position back
csPlaying , // Normal gameplay
2012-11-11 09:00:58 -05:00
csDestroying , // The client is being destroyed, don't queue any more packets / don't add to chunks
csDestroyed , // The client has been destroyed, the destructor is to be called from the owner thread
2012-06-14 09:06:06 -04:00
2012-11-11 09:00:58 -05:00
// TODO: Add Kicking here as well
2012-06-14 09:06:06 -04:00
} ;
eState m_State ;
bool m_bKeepThreadGoing ;
2012-08-31 12:26:03 -04:00
/// If set to true during csDownloadingWorld, the tick thread calls CheckIfWorldDownloaded()
bool m_ShouldCheckDownloaded ;
2012-06-14 09:06:06 -04:00
/// Returns true if the rate block interactions is within a reasonable limit (bot protection)
bool CheckBlockInteractionsRate ( void ) ;
/// Checks whether all loaded chunks have been sent to the client; if so, sends the position to confirm
void CheckIfWorldDownloaded ( void ) ;
/// Sends the PlayerMoveLook packet that the client needs to reply to for the game to start
void SendConfirmPosition ( void ) ;
/// Adds a single chunk to be streamed to the client; used by StreamChunks()
void StreamChunk ( int a_ChunkX , int a_ChunkY , int a_ChunkZ ) ;
2013-01-11 23:46:01 -05:00
/// Handles the DIG_STARTED dig packet:
void HandleBlockDigStarted ( int a_BlockX , int a_BlockY , int a_BlockZ , char a_BlockFace , BLOCKTYPE a_OldBlock , NIBBLETYPE a_OldMeta ) ;
/// Handles the DIG_FINISHED dig packet:
void HandleBlockDigFinished ( int a_BlockX , int a_BlockY , int a_BlockZ , char a_BlockFace , BLOCKTYPE a_OldBlock , NIBBLETYPE a_OldMeta ) ;
/// Handles the block placing packet when it is a real block placement (not block-using, item-using or eating)
void HandlePlaceBlock ( int a_BlockX , int a_BlockY , int a_BlockZ , char a_BlockFace , int a_CursorX , int a_CursorY , int a_CursorZ , cItemHandler & a_ItemHandler ) ;
2012-06-14 09:06:06 -04:00
// cSocketThreads::cCallback overrides:
virtual void DataReceived ( const char * a_Data , int a_Size ) override ; // Data is received from the client
virtual void GetOutgoingData ( AString & a_Data ) override ; // Data can be sent to client
virtual void SocketClosed ( void ) override ; // The socket has been closed for any reason
static int s_ClientCount ;
int m_UniqueID ;
} ; // tolua_export
# endif // CCLIENTHANDLE_H_INCLUDED