version 0.0.1
[fms.git] / include / freenet / fcpv2.h
1 /*\r
2 \r
3         FCPv2 C++ library\r
4         \r
5         link with ws2_32.lib in Windows\r
6 \r
7 */\r
8 \r
9 #ifndef _fcpv2_\r
10 #define _fcpv2_\r
11 \r
12 #ifdef _WIN32\r
13         #include <winsock2.h>\r
14         #include <windows.h>\r
15 #else\r
16         #include <sys/types.h>\r
17         #include <sys/socket.h>\r
18         #include <netinet/in.h>\r
19         #include <arpa/inet.h>\r
20 #endif\r
21 \r
22 #include <string>\r
23 #include <vector>\r
24 #include <map>\r
25 \r
26 \r
27 class FCPMessage:public std::map<std::string, std::string >\r
28 {\r
29 public:\r
30         FCPMessage() {};\r
31         FCPMessage(const std::string &name) {m_name=name;}\r
32 \r
33         const std::string GetName() const { return m_name; }\r
34         void SetName(const std::string &name) { m_name=name; }\r
35         \r
36         void Reset() { m_name=""; clear(); }\r
37 \r
38 protected:\r
39         std::string m_name;\r
40 };\r
41 \r
42 class FCPv2\r
43 {\r
44 public:\r
45         FCPv2();\r
46         ~FCPv2();\r
47 \r
48         const bool Connect(const char *host, const int port);\r
49         const bool Disconnect();\r
50 \r
51         const bool Connected() const { return m_serversocket!=-1 ? true : false ; }\r
52 \r
53         const bool Update(const long waittime);\r
54 \r
55         const int SendMessage(const char *messagename, const int fieldcount, ...);\r
56         const int SendMessage(FCPMessage &message);\r
57         const int SendRaw(const char *data, const int datalen);\r
58         const std::vector<char>::size_type SendBufferSize()     const { return m_sendbuffer.size(); }\r
59 \r
60         FCPMessage ReceiveMessage();\r
61         const long ReceiveRaw(char *data, long &datalen);       // data must be preallocated, with datalen being max length of data.  Returns length of data received\r
62         const std::vector<char>::size_type ReceiveBufferSize() const { return m_receivebuffer.size(); }\r
63 \r
64 private:\r
65         \r
66         void SocketReceive();\r
67         void SocketSend();\r
68 \r
69         void SendBufferedText(const char *text);                // puts text on send buffer\r
70         void SendBufferedRaw(const char *data, const long len); // puts raw data on send buffer\r
71 \r
72         int FindOnReceiveBuffer(const char *text);              // finds text string on receive buffer and returns index to first char position, -1 if not found\r
73 \r
74 \r
75 #ifdef _WIN32\r
76         static bool m_wsastartup;\r
77 #endif\r
78 \r
79         int m_serversocket;\r
80 \r
81         char *m_tempbuffer;                     // temp buffer used for recv\r
82 \r
83         std::vector<char> m_sendbuffer;\r
84         std::vector<char> m_receivebuffer;\r
85 \r
86         fd_set m_readfs;\r
87         fd_set m_writefs;\r
88         struct timeval m_timeval;\r
89 \r
90 };\r
91 \r
92 #endif  // _fcpv2_\r