Added 'fr::Packetable' to allow custom objects to be packed into packets

Can be used by inheriting the 'pack' and 'unpack' members of fr::Packetable. The 'pack' function should add your class members to the packet, and the 'unpack' function should take them back out.
This commit is contained in:
Cloaked9000 2017-02-02 11:51:31 +00:00
parent 15d20388c1
commit 5c94b3f6df
3 changed files with 61 additions and 1 deletions

View File

@ -17,7 +17,7 @@ endif()
set( INCLUDE_PATH "${PROJECT_SOURCE_DIR}/include" ) set( INCLUDE_PATH "${PROJECT_SOURCE_DIR}/include" )
set( SOURCE_PATH "${PROJECT_SOURCE_DIR}/src" ) set( SOURCE_PATH "${PROJECT_SOURCE_DIR}/src" )
set(SOURCE_FILES main.cpp src/TcpSocket.cpp include/frnetlib/TcpSocket.h src/TcpListener.cpp include/frnetlib/TcpListener.h src/Socket.cpp include/frnetlib/Socket.h src/Packet.cpp include/frnetlib/Packet.h include/frnetlib/NetworkEncoding.h src/SocketSelector.cpp include/frnetlib/SocketSelector.h src/HttpSocket.cpp include/frnetlib/HttpSocket.h src/HttpRequest.cpp include/frnetlib/HttpRequest.h src/HttpResponse.cpp include/frnetlib/HttpResponse.h src/Http.cpp include/frnetlib/Http.h src/SSLSocket.cpp include/frnetlib/SSLSocket.h src/SSLListener.cpp include/frnetlib/SSLListener.h include/frnetlib/SSLContext.h src/SocketReactor.cpp include/frnetlib/SocketReactor.h) set(SOURCE_FILES main.cpp src/TcpSocket.cpp include/frnetlib/TcpSocket.h src/TcpListener.cpp include/frnetlib/TcpListener.h src/Socket.cpp include/frnetlib/Socket.h src/Packet.cpp include/frnetlib/Packet.h include/frnetlib/NetworkEncoding.h src/SocketSelector.cpp include/frnetlib/SocketSelector.h src/HttpSocket.cpp include/frnetlib/HttpSocket.h src/HttpRequest.cpp include/frnetlib/HttpRequest.h src/HttpResponse.cpp include/frnetlib/HttpResponse.h src/Http.cpp include/frnetlib/Http.h src/SSLSocket.cpp include/frnetlib/SSLSocket.h src/SSLListener.cpp include/frnetlib/SSLListener.h include/frnetlib/SSLContext.h src/SocketReactor.cpp include/frnetlib/SocketReactor.h include/frnetlib/Packetable.h)
include_directories(include) include_directories(include)

View File

@ -9,6 +9,7 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include "NetworkEncoding.h" #include "NetworkEncoding.h"
#include "Packetable.h"
namespace fr namespace fr
{ {
@ -409,6 +410,33 @@ namespace fr
return *this; return *this;
} }
/*!
* Should be called to pack an object that inherits
* fr::Packetable to the packet.
*
* @param object The class to pack
* @return The current packet object for chaining
*/
inline Packet &operator<<(const fr::Packetable &object)
{
object.pack(*this);
return *this;
}
/*!
* Should be called to unpack an object that inherits
* fr::Packetable.
*
* @param object The object to unpack into
* @return The current packet object for chaining
*/
inline Packet &operator>>(fr::Packetable &object)
{
object.unpack(*this);
return *this;
}
/*! /*!
* Sets the internal data buffer * Sets the internal data buffer
* *

View File

@ -0,0 +1,32 @@
//
// Created by fred on 02/02/17.
//
#ifndef FRNETLIB_PACKETABLE_H
#define FRNETLIB_PACKETABLE_H
#include <string>
namespace fr
{
class Packet;
class Packetable
{
public:
/*!
* Called to pack class data into the 'destination'
* packet.
*
* @param destination Where you should store the class data
*/
virtual void pack(fr::Packet &destination) const=0;
/*!
* Called to unpack class data from the 'source' packet.
*
* @param source Where to retreive the class data from.
*/
virtual void unpack(fr::Packet &source)=0;
};
}
#endif //FRNETLIB_PACKETABLE_H