From 5c94b3f6df40cec271ead435542fa1bc1e3efad1 Mon Sep 17 00:00:00 2001 From: Cloaked9000 Date: Thu, 2 Feb 2017 11:51:31 +0000 Subject: [PATCH] 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. --- CMakeLists.txt | 2 +- include/frnetlib/Packet.h | 28 ++++++++++++++++++++++++++++ include/frnetlib/Packetable.h | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 include/frnetlib/Packetable.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 9771edf..8e5b179 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,7 @@ endif() set( INCLUDE_PATH "${PROJECT_SOURCE_DIR}/include" ) 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) diff --git a/include/frnetlib/Packet.h b/include/frnetlib/Packet.h index 5919bc1..70bd03a 100644 --- a/include/frnetlib/Packet.h +++ b/include/frnetlib/Packet.h @@ -9,6 +9,7 @@ #include #include #include "NetworkEncoding.h" +#include "Packetable.h" namespace fr { @@ -409,6 +410,33 @@ namespace fr 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 * diff --git a/include/frnetlib/Packetable.h b/include/frnetlib/Packetable.h new file mode 100644 index 0000000..d782882 --- /dev/null +++ b/include/frnetlib/Packetable.h @@ -0,0 +1,32 @@ +// +// Created by fred on 02/02/17. +// + +#ifndef FRNETLIB_PACKETABLE_H +#define FRNETLIB_PACKETABLE_H +#include + +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