3 \mainpage FCPv2 library
\r
7 link with ws2_32.lib and iphlpapi.lib in Windows
\r
23 #include <winsock2.h>
\r
24 #include <windows.h>
\r
26 #include <sys/types.h>
\r
27 #include <sys/socket.h>
\r
28 #include <netinet/in.h>
\r
29 #include <arpa/inet.h>
\r
33 \brief %FCPv2 namespace
\r
35 This namespace contains the %FCPv2 library
\r
41 \brief An FCP message
\r
43 FCP Messages are comprised of a name and zero or more field/value pairs.
\r
49 \brief Default constructor
\r
53 \brief Construct message with a specific name
\r
55 \param name The name of the message
\r
57 Message(const std::string &name);
\r
59 \brief Construct message with a specific name and fields
\r
61 The number of field/value pairs must match the fieldcount parameter.
\r
63 \param name The name of the message
\r
64 \param fieldcount The number of field/value pairs that follow
\r
66 Message(const std::string &name, const int fieldcount, ...);
\r
69 \brief Gets the name of the message
\r
71 \return The name of the message
\r
73 const std::string &GetName() const { return m_name; }
\r
75 \brief Sets the name of the message
\r
77 \param name The name of the message
\r
79 void SetName(const std::string &name) { m_name=name; }
\r
82 \brief Accesses the field/value pairs
\r
84 \param field The field to access
\r
85 \return Reference to the value of the field
\r
87 std::string &operator[](const std::string &field) { return m_fields[field]; }
\r
90 \brief Non-const accessor for field map
\r
94 std::map<std::string,std::string> &GetFields() { return m_fields; }
\r
96 \brief Const accessor for field map
\r
100 const std::map<std::string,std::string> &GetFields() const { return m_fields; }
\r
103 \brief Clears the name and fields of the message
\r
105 void Clear() { m_name=""; m_fields.clear(); }
\r
107 const bool operator==(const Message &rhs) const { return (m_name==rhs.m_name && m_fields==rhs.m_fields); }
\r
108 const bool operator!=(const Message &rhs) const { return !(*this==rhs); }
\r
109 const bool operator<(const Message &rhs) const { return (m_name<rhs.m_name || (m_name==rhs.m_name && m_fields<rhs.m_fields)); }
\r
110 const bool operator<=(const Message &rhs) const { return (*this==rhs || *this<rhs); }
\r
111 const bool operator>(const Message &rhs) const { return !(*this<=rhs); }
\r
112 const bool operator>=(const Message &rhs) const { return !(*this<rhs); }
\r
115 \brief Gets the string representing this message in FCP
\r
117 This string is what is actually sent/received from the node through FCP
\r
119 const std::string GetFCPString() const;
\r
123 std::string m_name;
\r
124 std::map<std::string,std::string> m_fields;
\r
129 \brief An FCP connection to a Freenet node
\r
135 \brief Default constructor
\r
139 \brief Construct connection with an existing socket
\r
141 \param sock An existing socket connection to a Freenet node
\r
143 Connection(const int sock);
\r
145 \brief Default constructor
\r
147 The deconstructor will close the connection if it is open
\r
152 \brief Creates an FCP connection to a Freenet node
\r
154 If the instaciated object has an existing connection open, it will be closed.
\r
156 \param fcphost The IP Address, hostname, FQDN, or other resolvable name that points to the Freenet node
\r
157 \param fcpport The port that the Freenet node is listening for FCP connections on
\r
158 \return true if the connection was established, false if it was not
\r
160 const bool Connect(const std::string &fcphost, const int fcpport);
\r
162 \brief Checks if the connection is currently connected
\r
164 \return true if there is a connection, false if there is not
\r
166 const bool IsConnected() const { return m_socket!=-1; }
\r
168 \brief Disconnects the connection
\r
170 \return always true
\r
172 const bool Disconnect();
\r
175 \brief Sends and receives data on the connection
\r
177 \param ms Maximum number of milliseconds to wait for the send and receive buffers to become available
\r
178 \return true if the connection remains connected, false if the connection is disconnected
\r
180 const bool Update(const unsigned long ms);
\r
183 \brief Checks if an FCP message is ready to be received
\r
185 \return true if an FCP message is ready to be received, false otherwise
\r
187 const bool MessageReady() const;
\r
190 \brief Gets the number of bytes on the receive buffer
\r
192 \return The number of bytes on the receive buffer
\r
194 const std::vector<char>::size_type ReceiveBufferSize() const { return m_receivebuffer.size(); }
\r
196 \brief Receives an FCP message
\r
198 \param[out] message The FCP message
\r
199 \return true if an FCP message was received, false otherwise
\r
201 const bool Receive(Message &message);
\r
203 \brief Receives raw data
\r
205 The received data is inserted at the end of the supplied vector
\r
207 \param[out] data vector to place received data in
\r
208 \param len number of bytes to receive
\r
209 \return true if the bytes were received, false otherwise
\r
211 const bool Receive(std::vector<char> &data, const std::vector<char>::size_type len);
\r
213 \brief Receives raw data
\r
215 \param[out] data char array to place received data in
\r
216 \param len number of bytes to receive
\r
217 \return true if the bytes were received, false otherwise
\r
219 const bool Receive(char *data, const size_t len);
\r
221 \brief Discards data on receive buffer
\r
223 \param len The number of bytes on the receive buffer to discard
\r
224 \return true if the bytes were discarded, false otherwise
\r
226 const bool ReceiveIgnore(const size_t len);
\r
229 \brief Gets the number of bytes waiting to be sent to the node
\r
231 \return The number of bytes waiting to be sent to the node
\r
233 const std::vector<char>::size_type SendBufferSize() const { return m_sendbuffer.size(); }
\r
235 \brief Sends an FCP Message
\r
237 \param message The Message to send
\r
238 \return true if the Message was buffered for sending successfully, false otherwise
\r
240 const bool Send(const Message &message);
\r
242 \brief Sends raw data
\r
244 \param data A vector of the data to send
\r
245 \return true if the data was buffered for sending successfully, false otherwise
\r
247 const bool Send(const std::vector<char> &data);
\r
249 \brief Sends raw data
\r
251 \param data A char array of data to send
\r
252 \param len The number of bytes on the array to send
\r
253 \return true if the data was buffered for sending successfully, false otherwise
\r
255 const bool Send(const char *data, const size_t len);
\r
258 \brief Gets the socket identifier of the connection
\r
260 \return The socket identifier. It will be -1 if the socket is invalid.
\r
262 const int Socket() { return m_socket; }
\r
265 \brief Waits until the receive buffer contains a specified number of bytes
\r
267 This will continuously call Update until either the specific number of bytes have been received,
\r
268 or the connection becomes disconnected
\r
270 \param ms The number of milliseconds for each call to Update
\r
271 \param len The number of bytes to wait for
\r
272 \return true if the number of bytes is waiting on the receive buffer, false if the connection was closed
\r
274 const bool WaitForBytes(const unsigned long ms, const size_t len);
\r
278 Connection(const Connection &connection);
\r
279 Connection &operator=(const Connection &connection);
\r
281 const bool MessageReady(std::vector<char>::const_iterator &endpos, std::vector<char>::size_type &endlen) const;
\r
282 const bool MessageReady(std::vector<char>::iterator &endpos, std::vector<char>::size_type &endlen);
\r
283 void Split(const std::string &str, const std::string &separators, std::vector<std::string> &elements);
\r
289 static bool m_wsastartup;
\r
293 std::vector<char> m_receivebuffer;
\r
294 std::vector<char> m_sendbuffer;
\r
295 std::vector<char> m_tempbuffer;
\r
298 struct timeval m_timeval;
\r
304 #endif // _fcpv2_connection_
\r