2012-09-02 11:38:28 -04:00
// Connection.h
// Interfaces to the cConnection class representing a single pair of connected sockets
# pragma once
# include <time.h>
2012-09-02 17:38:13 -04:00
# include "ByteBuffer.h"
2012-09-02 11:38:28 -04:00
class cServer ;
class cConnection
{
2013-06-02 05:56:04 -04:00
AString m_LogNameBase ; ///< Base for the log filename and all files connected to this log
int m_ItemIdx ; ///< Index for the next file into which item metadata should be written (ParseSlot() function)
2012-09-02 11:38:28 -04:00
cCriticalSection m_CSLog ;
FILE * m_LogFile ;
cServer & m_Server ;
SOCKET m_ClientSocket ;
SOCKET m_ServerSocket ;
clock_t m_BeginTick ; // Tick when the relative time was first retrieved (used for GetRelativeTime())
2012-09-02 17:38:13 -04:00
enum eConnectionState
{
csUnencrypted , // The connection is not encrypted. Packets must be decoded in order to be able to start decryption.
csEncryptedUnderstood , // The communication is encrypted and so far all packets have been understood, so they can be still decoded
csEncryptedUnknown , // The communication is encrypted, but an unknown packet has been received, so packets cannot be decoded anymore
} ;
eConnectionState m_ClientState ;
eConnectionState m_ServerState ;
int m_Nonce ;
2012-09-02 11:38:28 -04:00
public :
cConnection ( SOCKET a_ClientSocket , cServer & a_Server ) ;
~ cConnection ( ) ;
void Run ( void ) ;
void Log ( const char * a_Format , . . . ) ;
void DataLog ( const void * a_Data , int a_Size , const char * a_Format , . . . ) ;
2013-06-04 10:08:40 -04:00
void LogFlush ( void ) ;
2012-09-02 11:38:28 -04:00
protected :
2012-09-02 17:38:13 -04:00
typedef CFB_Mode < AES > : : Encryption Encryptor ;
typedef CFB_Mode < AES > : : Decryption Decryptor ;
cByteBuffer m_ClientBuffer ;
cByteBuffer m_ServerBuffer ;
Decryptor m_ServerDecryptor ;
Encryptor m_ServerEncryptor ;
Decryptor m_ClientDecryptor ;
Encryptor m_ClientEncryptor ;
2013-05-08 06:27:12 -04:00
/// Set to true when PACKET_PING is received from the client; will cause special parsing for server kick
bool m_HasClientPinged ;
2012-09-02 17:38:13 -04:00
2012-09-02 11:38:28 -04:00
bool ConnectToServer ( void ) ;
/// Relays data from server to client; returns false if connection aborted
bool RelayFromServer ( void ) ;
/// Relays data from client to server; returns false if connection aborted
bool RelayFromClient ( void ) ;
/// Returns the time relative to the first call of this function, in the fractional seconds elapsed
double GetRelativeTime ( void ) ;
2012-09-02 17:38:13 -04:00
/// Sends data to the specified socket. If sending fails, prints a fail message using a_Peer and returns false.
bool SendData ( SOCKET a_Socket , const char * a_Data , int a_Size , const char * a_Peer ) ;
/// Sends data to the specified socket. If sending fails, prints a fail message using a_Peer and returns false.
bool SendData ( SOCKET a_Socket , cByteBuffer & a_Data , const char * a_Peer ) ;
/// Sends data to the specfied socket, after encrypting it using a_Encryptor. If sending fails, prints a fail message using a_Peer and returns false
bool SendEncryptedData ( SOCKET a_Socket , Encryptor & a_Encryptor , const char * a_Data , int a_Size , const char * a_Peer ) ;
/// Sends data to the specfied socket, after encrypting it using a_Encryptor. If sending fails, prints a fail message using a_Peer and returns false
bool SendEncryptedData ( SOCKET a_Socket , Encryptor & a_Encryptor , cByteBuffer & a_Data , const char * a_Peer ) ;
/// Decodes packets coming from the client, sends appropriate counterparts to the server; returns false if the connection is to be dropped
bool DecodeClientsPackets ( const char * a_Data , int a_Size ) ;
/// Decodes packets coming from the server, sends appropriate counterparts to the client; returns false if the connection is to be dropped
bool DecodeServersPackets ( const char * a_Data , int a_Size ) ;
// Packet handling, client-side:
2012-09-07 16:15:06 -04:00
bool HandleClientAnimation ( void ) ;
2012-12-26 04:08:44 -05:00
bool HandleClientBlockDig ( void ) ;
2012-09-07 16:15:06 -04:00
bool HandleClientBlockPlace ( void ) ;
2012-09-14 13:13:56 -04:00
bool HandleClientChatMessage ( void ) ;
2012-09-04 16:21:23 -04:00
bool HandleClientClientStatuses ( void ) ;
2012-09-14 13:13:56 -04:00
bool HandleClientCreativeInventoryAction ( void ) ;
2012-09-04 16:21:23 -04:00
bool HandleClientEncryptionKeyResponse ( void ) ;
2012-10-13 05:40:55 -04:00
bool HandleClientEntityAction ( void ) ;
2012-09-04 16:21:23 -04:00
bool HandleClientHandshake ( void ) ;
2012-09-07 16:15:06 -04:00
bool HandleClientKeepAlive ( void ) ;
2012-09-04 16:21:23 -04:00
bool HandleClientLocaleAndView ( void ) ;
bool HandleClientPing ( void ) ;
2012-09-23 11:56:42 -04:00
bool HandleClientPlayerAbilities ( void ) ;
2012-09-05 15:06:40 -04:00
bool HandleClientPlayerLook ( void ) ;
bool HandleClientPlayerOnGround ( void ) ;
bool HandleClientPlayerPosition ( void ) ;
2012-09-04 16:21:23 -04:00
bool HandleClientPlayerPositionLook ( void ) ;
2012-09-07 16:15:06 -04:00
bool HandleClientSlotSelect ( void ) ;
bool HandleClientUpdateSign ( void ) ;
2012-09-14 13:13:56 -04:00
bool HandleClientUseEntity ( void ) ;
bool HandleClientWindowClick ( void ) ;
bool HandleClientWindowClose ( void ) ;
2012-09-02 17:38:13 -04:00
// Packet handling, server-side:
2012-12-28 01:05:52 -05:00
bool HandleServerAttachEntity ( void ) ;
2012-09-20 07:36:09 -04:00
bool HandleServerBlockAction ( void ) ;
2012-09-06 04:34:08 -04:00
bool HandleServerBlockChange ( void ) ;
2012-09-18 04:13:12 -04:00
bool HandleServerChangeGameState ( void ) ;
2012-09-04 16:21:23 -04:00
bool HandleServerChatMessage ( void ) ;
2012-10-11 15:40:26 -04:00
bool HandleServerCollectPickup ( void ) ;
2012-09-04 16:21:23 -04:00
bool HandleServerCompass ( void ) ;
2012-09-20 07:36:09 -04:00
bool HandleServerDestroyEntities ( void ) ;
2012-09-04 16:21:23 -04:00
bool HandleServerEncryptionKeyRequest ( void ) ;
bool HandleServerEncryptionKeyResponse ( void ) ;
2012-09-18 07:59:41 -04:00
bool HandleServerEntity ( void ) ;
2012-09-04 16:21:23 -04:00
bool HandleServerEntityEquipment ( void ) ;
2013-07-05 16:56:54 -04:00
bool HandleServerEntityHeadLook ( void ) ;
2012-09-18 07:59:41 -04:00
bool HandleServerEntityLook ( void ) ;
2013-07-05 16:56:54 -04:00
bool HandleServerEntityMetadata ( void ) ;
bool HandleServerEntityProperties ( void ) ;
2012-09-18 07:59:41 -04:00
bool HandleServerEntityRelativeMove ( void ) ;
bool HandleServerEntityRelativeMoveLook ( void ) ;
bool HandleServerEntityStatus ( void ) ;
bool HandleServerEntityTeleport ( void ) ;
2012-09-19 03:11:31 -04:00
bool HandleServerEntityVelocity ( void ) ;
2012-12-27 02:11:37 -05:00
bool HandleServerIncrementStatistic ( void ) ;
2012-09-04 16:21:23 -04:00
bool HandleServerKeepAlive ( void ) ;
bool HandleServerKick ( void ) ;
bool HandleServerLogin ( void ) ;
bool HandleServerMapChunk ( void ) ;
2012-09-18 04:35:06 -04:00
bool HandleServerMapChunkBulk ( void ) ;
2012-09-06 04:34:08 -04:00
bool HandleServerMultiBlockChange ( void ) ;
2012-09-20 07:36:09 -04:00
bool HandleServerNamedSoundEffect ( void ) ;
2012-09-04 16:21:23 -04:00
bool HandleServerPlayerAbilities ( void ) ;
2013-06-04 10:08:40 -04:00
bool HandleServerPlayerAnimation ( void ) ;
2012-09-04 16:21:23 -04:00
bool HandleServerPlayerListItem ( void ) ;
bool HandleServerPlayerPositionLook ( void ) ;
2012-09-18 04:17:38 -04:00
bool HandleServerSetExperience ( void ) ;
2012-09-14 13:13:56 -04:00
bool HandleServerSetSlot ( void ) ;
2012-12-26 04:08:44 -05:00
bool HandleServerSlotSelect ( void ) ;
2012-09-20 07:36:09 -04:00
bool HandleServerSoundEffect ( void ) ;
2012-09-19 03:11:31 -04:00
bool HandleServerSpawnMob ( void ) ;
2013-06-04 10:08:40 -04:00
bool HandleServerSpawnNamedEntity ( void ) ;
2012-09-19 03:11:31 -04:00
bool HandleServerSpawnObjectVehicle ( void ) ;
bool HandleServerSpawnPainting ( void ) ;
bool HandleServerSpawnPickup ( void ) ;
2012-09-04 16:21:23 -04:00
bool HandleServerTimeUpdate ( void ) ;
2012-09-05 15:06:40 -04:00
bool HandleServerUpdateHealth ( void ) ;
2012-09-07 16:15:06 -04:00
bool HandleServerUpdateSign ( void ) ;
2012-09-19 03:17:47 -04:00
bool HandleServerUpdateTileEntity ( void ) ;
2012-09-14 13:13:56 -04:00
bool HandleServerWindowClose ( void ) ;
2012-09-04 16:21:23 -04:00
bool HandleServerWindowContents ( void ) ;
2012-09-20 07:36:09 -04:00
bool HandleServerWindowOpen ( void ) ;
2012-09-04 16:21:23 -04:00
/// Parses the slot data in a_Buffer into item description; returns true if successful, false if not enough data
bool ParseSlot ( cByteBuffer & a_Buffer , AString & a_ItemDesc ) ;
2012-09-02 17:38:13 -04:00
2012-09-18 07:59:41 -04:00
/// Parses the metadata in a_Buffer into raw metadata in an AString; returns true if successful, false if not enough data
bool ParseMetadata ( cByteBuffer & a_Buffer , AString & a_Metadata ) ;
2012-12-27 02:11:37 -05:00
/// Logs the contents of the metadata in the AString, using Log(). Assumes a_Metadata is valid (parsed by ParseMetadata()). The log is indented by a_IndentCount spaces
void LogMetadata ( const AString & a_Metadata , size_t a_IndentCount ) ;
2012-09-02 17:38:13 -04:00
/// Send EKResp to the server:
void SendEncryptionKeyResponse ( const AString & a_ServerPublicKey , const AString & a_Nonce ) ;
/// Starts client encryption based on the parameters received
void StartClientEncryption ( const AString & a_EncryptedSecret , const AString & a_EncryptedNonce ) ;
2012-09-02 11:38:28 -04:00
} ;