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 \return The FCP message string
\r
121 const std::string GetFCPString() const;
\r
125 std::string m_name;
\r
126 std::map<std::string,std::string> m_fields;
\r
131 \brief An FCP connection to a Freenet node
\r
137 \brief Default constructor
\r
141 \brief Construct connection with an existing socket
\r
143 \param sock An existing open socket connection to a Freenet node
\r
145 Connection(const int sock);
\r
147 \brief Default destructor
\r
149 The destructor will close the connection if it is open
\r
154 \brief Creates an FCP connection to a Freenet node
\r
156 If the instaciated object has an existing connection open, it will be closed.
\r
158 \param fcphost The IP Address, hostname, FQDN, or other resolvable name that points to the Freenet node
\r
159 \param fcpport The port that the Freenet node is listening for FCP connections on
\r
160 \return true if the connection was established, false if it was not
\r
162 const bool Connect(const std::string &fcphost, const int fcpport);
\r
164 \brief Checks if the connection is currently connected
\r
166 \return true if there is a connection, false if there is not
\r
168 const bool IsConnected() const { return m_socket!=-1; }
\r
170 \brief Disconnects the connection
\r
172 \return always true
\r
174 const bool Disconnect();
\r
177 \brief Sends and receives data on the connection
\r
179 \param ms Maximum number of milliseconds to wait for the send and receive buffers to become available
\r
180 \return true if the connection remains connected, false if the connection is disconnected
\r
182 const bool Update(const unsigned long ms);
\r
185 \brief Checks if an FCP message is ready to be received
\r
187 \return true if an FCP message is ready to be received, false otherwise
\r
189 const bool MessageReady() const;
\r
192 \brief Gets the number of bytes on the receive buffer
\r
194 \return The number of bytes on the receive buffer
\r
196 const std::vector<char>::size_type ReceiveBufferSize() const { return m_receivebuffer.size(); }
\r
198 \brief Receives an FCP message
\r
200 \param[out] message The FCP message
\r
201 \return true if an FCP message was received, false otherwise
\r
203 const bool Receive(Message &message);
\r
205 \brief Receives raw data
\r
207 The received data is inserted at the end of the supplied vector
\r
209 \param[out] data vector to place received data in
\r
210 \param len number of bytes to receive
\r
211 \return true if the bytes were received, false otherwise
\r
213 const bool Receive(std::vector<char> &data, const std::vector<char>::size_type len);
\r
215 \brief Receives raw data
\r
217 \param[out] data char array to place received data in
\r
218 \param len number of bytes to receive
\r
219 \return true if the bytes were received, false otherwise
\r
221 const bool Receive(char *data, const size_t len);
\r
223 \brief Discards data on receive buffer
\r
225 \param len The number of bytes on the receive buffer to discard
\r
226 \return true if the bytes were discarded, false otherwise
\r
228 const bool ReceiveIgnore(const size_t len);
\r
231 \brief Gets the number of bytes waiting to be sent to the node
\r
233 \return The number of bytes waiting to be sent to the node
\r
235 const std::vector<char>::size_type SendBufferSize() const { return m_sendbuffer.size(); }
\r
237 \brief Sends an FCP Message
\r
239 \param message The Message to send
\r
240 \return true if the Message was buffered for sending successfully, false otherwise
\r
242 const bool Send(const Message &message);
\r
244 \brief Sends raw data
\r
246 \param data A vector of the data to send
\r
247 \return true if the data was buffered for sending successfully, false otherwise
\r
249 const bool Send(const std::vector<char> &data);
\r
251 \brief Sends raw data
\r
253 \param data A char array of data to send
\r
254 \param len The number of bytes on the array to send
\r
255 \return true if the data was buffered for sending successfully, false otherwise
\r
257 const bool Send(const char *data, const size_t len);
\r
260 \brief Gets the socket identifier of the connection
\r
262 \return The socket identifier. It will be -1 if the socket is invalid.
\r
264 const int Socket() { return m_socket; }
\r
267 \brief Waits until the receive buffer contains a specified number of bytes
\r
269 This will continuously call Update until either the specific number of bytes have been received,
\r
270 or the connection becomes disconnected.
\r
272 \param ms The number of milliseconds for each call to Update
\r
273 \param len The number of bytes to wait for
\r
274 \return true if the number of bytes is waiting on the receive buffer, false if the connection was closed
\r
276 const bool WaitForBytes(const unsigned long ms, const size_t len);
\r
280 Connection(const Connection &connection);
\r
281 Connection &operator=(const Connection &connection);
\r
283 const bool MessageReady(std::vector<char>::const_iterator &endpos, std::vector<char>::size_type &endlen) const;
\r
284 const bool MessageReady(std::vector<char>::iterator &endpos, std::vector<char>::size_type &endlen);
\r
285 void Split(const std::string &str, const std::string &separators, std::vector<std::string> &elements);
\r
291 static bool m_wsastartup;
\r
295 std::vector<char> m_receivebuffer;
\r
296 std::vector<char> m_sendbuffer;
\r
297 std::vector<char> m_tempbuffer;
\r
300 struct timeval m_timeval;
\r
306 #endif // _fcpv2_connection_
\r