1
Fork 0
mirror of https://github.com/SapphireServer/Sapphire.git synced 2025-04-22 12:47:45 +00:00
sapphire/src/common/Network/GamePacketNew.h

291 lines
7.2 KiB
C
Raw Normal View History

#ifndef _GAMEPACKET_NEW_H
#define _GAMEPACKET_NEW_H
2017-08-08 13:53:47 +02:00
#include <stdint.h>
#include <iostream>
#include <sstream>
#include <time.h>
2018-06-28 00:07:07 +02:00
#include <boost/make_shared.hpp>
2018-07-03 09:20:09 +02:00
#include <string.h>
#include <memory>
#include "CommonNetwork.h"
#include "PacketDef/Ipcs.h"
2018-06-28 00:07:07 +02:00
2017-08-08 13:53:47 +02:00
namespace Core {
namespace Network {
namespace Packets {
// Must forward define these in order to enable the compiler to produce the
// correct template functions.
2018-06-03 19:31:03 +02:00
template < typename T, typename T1 >
2018-06-28 00:07:07 +02:00
class FFXIVIpcPacket;
2017-08-08 13:53:47 +02:00
2017-11-21 18:43:09 +01:00
template< class T >
2018-06-28 00:07:07 +02:00
using ZoneChannelPacket = FFXIVIpcPacket< T, ServerZoneIpcType >;
2017-11-21 18:43:09 +01:00
template< class T >
2018-06-28 00:07:07 +02:00
using ChatChannelPacket = FFXIVIpcPacket< T, ServerChatIpcType >;
template< class T >
using LobbyChannelPacket = FFXIVIpcPacket< T, ServerLobbyIpcType >;
2018-06-28 00:07:07 +02:00
template< class T, typename... Args >
boost::shared_ptr< ZoneChannelPacket< T > > makeZonePacket( Args... args )
{
return boost::make_shared< ZoneChannelPacket< T > >( args... );
}
template< class T, typename... Args >
boost::shared_ptr< ChatChannelPacket< T > > makeChatPacket( Args... args )
{
return boost::make_shared< ChatChannelPacket< T > >( args... );
}
2017-11-21 18:43:09 +01:00
template< class T, typename... Args >
boost::shared_ptr< LobbyChannelPacket< T > > makeLobbyPacket( Args... args )
{
return boost::make_shared< LobbyChannelPacket< T > >( args... );
}
2017-08-08 13:53:47 +02:00
/**
* The base implementation of a game packet. Needed for parsing packets.
*/
2018-06-03 19:31:03 +02:00
template < typename T1 >
2018-06-07 20:33:06 +02:00
class FFXIVIpcPacketBase
2017-08-08 13:53:47 +02:00
{
public:
2018-06-07 20:33:06 +02:00
virtual ~FFXIVIpcPacketBase() = default;
2017-08-08 13:53:47 +02:00
/**
* @brief Gets the IPC type of this packet. (Useful for determining the
* type of a parsed packet.)
*/
2018-06-07 20:33:06 +02:00
virtual T1 ipcType() = 0;
2017-08-08 13:53:47 +02:00
};
2018-06-28 00:07:07 +02:00
////////////////////////////////////////////////7
class FFXIVPacketBase
2017-08-08 13:53:47 +02:00
{
public:
2018-06-28 00:07:07 +02:00
FFXIVPacketBase() :
m_segmentType( 0 )
2017-08-08 13:53:47 +02:00
{
2018-06-28 00:07:07 +02:00
initializeSegmentHeader();
}
2017-08-08 13:53:47 +02:00
2018-06-28 00:07:07 +02:00
FFXIVPacketBase( uint16_t segmentType, uint32_t sourceActorId, uint32_t targetActorId ) :
m_segmentType( segmentType )
2017-08-08 13:53:47 +02:00
{
2018-06-28 00:07:07 +02:00
initializeSegmentHeader();
setSourceActor( sourceActorId );
setTargetActor( targetActorId );
}
2017-08-08 13:53:47 +02:00
2018-06-28 00:07:07 +02:00
std::size_t getSize() const
2017-08-08 13:53:47 +02:00
{
2018-06-28 00:07:07 +02:00
return m_segHdr.size;
}
2017-08-08 13:53:47 +02:00
2018-06-28 00:07:07 +02:00
virtual std::vector< uint8_t > getData() const
{
return {};
}
protected:
/** The segment header */
FFXIVARR_PACKET_SEGMENT_HEADER m_segHdr;
uint16_t m_segmentType;
2017-08-08 13:53:47 +02:00
public:
2018-06-28 00:07:07 +02:00
virtual uint32_t getContentSize() { return 0; };
virtual std::vector< uint8_t > getContent() { return{}; };
2017-08-08 13:53:47 +02:00
2018-06-28 00:07:07 +02:00
/**
* @brief Gets the segment type of this packet.
*/
uint16_t getSegmentType() const
{
return m_segmentType;
}
2017-08-08 13:53:47 +02:00
/**
2018-06-28 00:07:07 +02:00
* @brief Sets the source actor id for this packet.
2017-08-08 13:53:47 +02:00
* @param actorId The source actor id.
*/
2018-06-28 00:07:07 +02:00
void setSourceActor( uint32_t actorId )
2017-08-08 13:53:47 +02:00
{
m_segHdr.source_actor = actorId;
};
/**
2018-06-28 00:07:07 +02:00
* @brief Gets the source actor id for this packet.
2017-08-08 13:53:47 +02:00
* @return The source actor id.
*/
2018-06-28 00:07:07 +02:00
uint32_t getSourceActor() const
2017-08-08 13:53:47 +02:00
{
return m_segHdr.source_actor;
};
/**
2018-06-28 00:07:07 +02:00
* @brief Sets the target actor id for this packet.
2017-08-08 13:53:47 +02:00
* @param actorId The target actor id.
*/
2018-06-28 00:07:07 +02:00
void setTargetActor( uint32_t actorId )
2017-08-08 13:53:47 +02:00
{
m_segHdr.target_actor = actorId;
};
/**
2018-06-28 00:07:07 +02:00
* @brief Gets the target actor id for this packet.
2017-08-08 13:53:47 +02:00
*/
2018-06-28 00:07:07 +02:00
uint32_t getTargetActor( void ) const
2017-08-08 13:53:47 +02:00
{
return m_segHdr.target_actor;
};
2018-06-28 00:07:07 +02:00
/** Initializes the fields of the segment header structure */
virtual void initializeSegmentHeader( void )
{
// Zero out the structure.
memset( &m_segHdr, 0, sizeof( FFXIVARR_PACKET_SEGMENT_HEADER ) );
2017-08-08 13:53:47 +02:00
2018-06-28 00:07:07 +02:00
// Set the values of static fields.
// The size must be the sum of the segment header and the content
m_segHdr.size = sizeof( FFXIVARR_PACKET_SEGMENT_HEADER ) + getContentSize();
m_segHdr.type = getSegmentType();
}
2017-08-08 13:53:47 +02:00
2018-06-28 00:07:07 +02:00
};
template < typename T, typename T1 >
class FFXIVIpcPacket : public FFXIVIpcPacketBase< T1 >, public FFXIVPacketBase
{
public:
FFXIVIpcPacket< T, T1 >( uint32_t sourceActorId, uint32_t targetActorId ) :
FFXIVPacketBase( 3, sourceActorId, targetActorId )
{
initialize();
};
FFXIVIpcPacket< T, T1 >( uint32_t sourceActorId ) :
FFXIVPacketBase( 3, sourceActorId, sourceActorId )
{
initialize();
2017-08-08 13:53:47 +02:00
};
2018-06-28 00:07:07 +02:00
uint32_t getContentSize() override
{
return sizeof( FFXIVARR_IPC_HEADER ) + sizeof( T );
}
std::vector< uint8_t > getContent() override
{
std::vector< uint8_t > content( getContentSize() );
memcpy( content.data(), &m_ipcHdr, sizeof( FFXIVARR_IPC_HEADER ) );
memcpy( content.data() + sizeof( FFXIVARR_IPC_HEADER ), &m_data, sizeof( T ) );
return content;
}
std::vector< uint8_t > getData() const override
2017-08-08 13:53:47 +02:00
{
2018-06-28 00:07:07 +02:00
auto segmentHeaderSize = sizeof( FFXIVARR_PACKET_SEGMENT_HEADER );
auto ipcHeaderSize = sizeof( FFXIVARR_IPC_HEADER );
auto dataSize = sizeof( m_data );
std::vector< uint8_t > data( segmentHeaderSize + ipcHeaderSize + dataSize );
memcpy( &data[0], &m_segHdr, segmentHeaderSize );
memcpy( &data[segmentHeaderSize], &m_ipcHdr, ipcHeaderSize );
memcpy( &data[segmentHeaderSize + ipcHeaderSize], &m_data, dataSize );
return data;
}
virtual T1 ipcType()
{
return static_cast< T1 >( m_data._ServerIpcType );
};
/** Gets a reference to the underlying IPC data structure. */
T& data() { return m_data; };
protected:
/** Initializes the fields of the header structures */
virtual void initialize()
{
// Zero out the structures.
memset( &m_ipcHdr, 0, sizeof( FFXIVARR_IPC_HEADER ) );
memset( &m_data, 0, sizeof( T ) );
// The IPC type itself.
m_ipcHdr.type = static_cast< ServerZoneIpcType >( m_data._ServerIpcType );
2018-06-28 23:42:12 +02:00
m_segHdr.size = sizeof( T ) + sizeof( FFXIVARR_IPC_HEADER ) + sizeof( FFXIVARR_PACKET_SEGMENT_HEADER );
2017-08-08 13:53:47 +02:00
};
protected:
/** The IPC packet header */
FFXIVARR_IPC_HEADER m_ipcHdr;
/** The underlying data portion of the packet as a structure */
T m_data;
2018-06-28 00:07:07 +02:00
};
class FFXIVRawPacket : public FFXIVPacketBase
{
public:
FFXIVRawPacket( uint16_t type, uint32_t size, uint32_t sourceActorId, uint32_t targetActorId ) :
2018-06-28 23:42:12 +02:00
m_data( std::vector< uint8_t >( size - sizeof( FFXIVARR_PACKET_SEGMENT_HEADER ) ) ),
2018-06-28 00:07:07 +02:00
FFXIVPacketBase( type, sourceActorId, targetActorId )
{
initialize();
m_segHdr.size = size;
};
uint32_t getContentSize() override
{
return m_data.size();
}
std::vector< uint8_t > getContent() override
{
return m_data;
}
virtual std::vector< uint8_t > getData() const override
{
std::vector< uint8_t > data( sizeof( FFXIVARR_PACKET_SEGMENT_HEADER ) + m_data.size() );
2017-08-08 13:53:47 +02:00
2018-06-28 00:07:07 +02:00
memcpy( &data[0], &m_segHdr, sizeof( FFXIVARR_PACKET_SEGMENT_HEADER ) );
memcpy( &data[sizeof( FFXIVARR_PACKET_SEGMENT_HEADER )], &m_data[0], m_data.size() );
return data;
}
/** Gets a reference to the underlying IPC data structure. */
std::vector< uint8_t >& data() { return m_data; };
protected:
/** Initializes the fields of the header structures */
virtual void initialize()
{
// Zero out the structures.
memset( &m_data[0], 0, m_data.size() );
2017-08-08 13:53:47 +02:00
};
2018-06-28 00:07:07 +02:00
protected:
/** The underlying data portion of the packet as a structure */
std::vector< uint8_t > m_data;
2017-08-08 13:53:47 +02:00
};
} /* Packets */
} /* Network */
} /* Core */
2018-07-03 09:20:09 +02:00
#endif /*_CORE_NETWORK_PACKETS_CGAMEPACKETNEW_H*/