X-Git-Url: https://git.pterodactylus.net/?p=fms.git;a=blobdiff_plain;f=include%2Ffreenet%2Ffcpv2.h;fp=include%2Ffreenet%2Ffcpv2.h;h=1f033a95eea1bfb111497d1b3d7abc3673c5e05f;hp=864b14ffde7b0eb122bbd87ac4b53e04e9a87d0d;hb=4e96d123460d6363cf7274e36bd9357768eb86ad;hpb=e662ea47fba8715474851ceebacba400984ee433 diff --git a/include/freenet/fcpv2.h b/include/freenet/fcpv2.h index 864b14f..1f033a9 100644 --- a/include/freenet/fcpv2.h +++ b/include/freenet/fcpv2.h @@ -1,14 +1,24 @@ -/* +/** + + \mainpage FCPv2 library FCPv2 C++ library - link with ws2_32.lib in Windows + link with ws2_32.lib and iphlpapi.lib in Windows + +*/ +/** + \file fcpv2.h */ #ifndef _fcpv2_ #define _fcpv2_ +#include +#include +#include + #ifdef _WIN32 #include #include @@ -19,74 +29,276 @@ #include #endif -#include -#include -#include +/** + \brief %FCPv2 namespace + This namespace contains the %FCPv2 library +*/ +namespace FCPv2 +{ -class FCPMessage:public std::map +/** + \brief An FCP message + + FCP Messages are comprised of a name and zero or more field/value pairs. +*/ +class Message { public: - FCPMessage() {}; - FCPMessage(const std::string &name) {m_name=name;} + /** + \brief Default constructor + */ + Message(); + /** + \brief Construct message with a specific name + + \param name The name of the message + */ + Message(const std::string &name); + /** + \brief Construct message with a specific name and fields - const std::string GetName() const { return m_name; } - void SetName(const std::string &name) { m_name=name; } + The number of field/value pairs must match the fieldcount parameter. + + \param name The name of the message + \param fieldcount The number of field/value pairs that follow + */ + Message(const std::string &name, const int fieldcount, ...); + + /** + \brief Gets the name of the message + + \return The name of the message + */ + const std::string &GetName() const { return m_name; } + /** + \brief Sets the name of the message + + \param name The name of the message + */ + void SetName(const std::string &name) { m_name=name; } + + /** + \brief Accesses the field/value pairs + + \param field The field to access + \return Reference to the value of the field + */ + std::string &operator[](const std::string &field) { return m_fields[field]; } + + /** + \brief Non-const accessor for field map + + \return field map + */ + std::map &GetFields() { return m_fields; } + /** + \brief Const accessor for field map + + \return field map + */ + const std::map &GetFields() const { return m_fields; } + + /** + \brief Clears the name and fields of the message + */ + void Clear() { m_name=""; m_fields.clear(); } + + const bool operator==(const Message &rhs) const { return (m_name==rhs.m_name && m_fields==rhs.m_fields); } + const bool operator!=(const Message &rhs) const { return !(*this==rhs); } + const bool operator<(const Message &rhs) const { return (m_name(const Message &rhs) const { return !(*this<=rhs); } + const bool operator>=(const Message &rhs) const { return !(*this m_fields; + }; -class FCPv2 +/** + \brief An FCP connection to a Freenet node +*/ +class Connection { public: - FCPv2(); - ~FCPv2(); + /** + \brief Default constructor + */ + Connection(); + /** + \brief Construct connection with an existing socket + + \param sock An existing socket connection to a Freenet node + */ + Connection(const int sock); + /** + \brief Default constructor + + The deconstructor will close the connection if it is open + */ + ~Connection(); + + /** + \brief Creates an FCP connection to a Freenet node + + If the instaciated object has an existing connection open, it will be closed. + + \param fcphost The IP Address, hostname, FQDN, or other resolvable name that points to the Freenet node + \param fcpport The port that the Freenet node is listening for FCP connections on + \return true if the connection was established, false if it was not + */ + const bool Connect(const std::string &fcphost, const int fcpport); + /** + \brief Checks if the connection is currently connected + + \return true if there is a connection, false if there is not + */ + const bool IsConnected() const { return m_socket!=-1; } + /** + \brief Disconnects the connection - const bool Connect(const char *host, const int port); + \return always true + */ const bool Disconnect(); + + /** + \brief Sends and receives data on the connection + + \param ms Maximum number of milliseconds to wait for the send and receive buffers to become available + \return true if the connection remains connected, false if the connection is disconnected + */ + const bool Update(const unsigned long ms); - const bool Connected() const { return m_serversocket!=-1 ? true : false ; } + /** + \brief Checks if an FCP message is ready to be received - const bool Update(const long waittime); + \return true if an FCP message is ready to be received, false otherwise + */ + const bool MessageReady() const; + + /** + \brief Gets the number of bytes on the receive buffer - const int SendMessage(const char *messagename, const int fieldcount, ...); - const int SendMessage(FCPMessage &message); - const int SendRaw(const char *data, const int datalen); - const std::vector::size_type SendBufferSize() const { return m_sendbuffer.size(); } + \return The number of bytes on the receive buffer + */ + const std::vector::size_type ReceiveBufferSize() const { return m_receivebuffer.size(); } + /** + \brief Receives an FCP message + + \param[out] message The FCP message + \return true if an FCP message was received, false otherwise + */ + const bool Receive(Message &message); + /** + \brief Receives raw data + + The received data is inserted at the end of the supplied vector - FCPMessage ReceiveMessage(); - const long ReceiveRaw(char *data, long &datalen); // data must be preallocated, with datalen being max length of data. Returns length of data received - const std::vector::size_type ReceiveBufferSize() const { return m_receivebuffer.size(); } + \param[out] data vector to place received data in + \param len number of bytes to receive + \return true if the bytes were received, false otherwise + */ + const bool Receive(std::vector &data, const std::vector::size_type len); + /** + \brief Receives raw data -private: + \param[out] data char array to place received data in + \param len number of bytes to receive + \return true if the bytes were received, false otherwise + */ + const bool Receive(char *data, const size_t len); + /** + \brief Discards data on receive buffer + + \param len The number of bytes on the receive buffer to discard + \return true if the bytes were discarded, false otherwise + */ + const bool ReceiveIgnore(const size_t len); - void SocketReceive(); - void SocketSend(); + /** + \brief Gets the number of bytes waiting to be sent to the node + + \return The number of bytes waiting to be sent to the node + */ + const std::vector::size_type SendBufferSize() const { return m_sendbuffer.size(); } + /** + \brief Sends an FCP Message + + \param message The Message to send + \return true if the Message was buffered for sending successfully, false otherwise + */ + const bool Send(const Message &message); + /** + \brief Sends raw data - void SendBufferedText(const char *text); // puts text on send buffer - void SendBufferedRaw(const char *data, const long len); // puts raw data on send buffer + \param data A vector of the data to send + \return true if the data was buffered for sending successfully, false otherwise + */ + const bool Send(const std::vector &data); + /** + \brief Sends raw data - int FindOnReceiveBuffer(const char *text); // finds text string on receive buffer and returns index to first char position, -1 if not found + \param data A char array of data to send + \param len The number of bytes on the array to send + \return true if the data was buffered for sending successfully, false otherwise + */ + const bool Send(const char *data, const size_t len); + /** + \brief Gets the socket identifier of the connection + + \return The socket identifier. It will be -1 if the socket is invalid. + */ + const int Socket() { return m_socket; } + + /** + \brief Waits until the receive buffer contains a specified number of bytes + + This will continuously call Update until either the specific number of bytes have been received, + or the connection becomes disconnected + + \param ms The number of milliseconds for each call to Update + \param len The number of bytes to wait for + \return true if the number of bytes is waiting on the receive buffer, false if the connection was closed + */ + const bool WaitForBytes(const unsigned long ms, const size_t len); + +private: + // can't be copied + Connection(const Connection &connection); + Connection &operator=(const Connection &connection); + + const bool MessageReady(std::vector::const_iterator &endpos, std::vector::size_type &endlen) const; + const bool MessageReady(std::vector::iterator &endpos, std::vector::size_type &endlen); + void Split(const std::string &str, const std::string &separators, std::vector &elements); + + void DoSend(); + void DoReceive(); #ifdef _WIN32 static bool m_wsastartup; #endif - int m_serversocket; - - char *m_tempbuffer; // temp buffer used for recv - - std::vector m_sendbuffer; + int m_socket; std::vector m_receivebuffer; - + std::vector m_sendbuffer; + std::vector m_tempbuffer; fd_set m_readfs; fd_set m_writefs; struct timeval m_timeval; - + }; -#endif // _fcpv2_ +} // namespace + +#endif // _fcpv2_connection_