Debian Codegraph @

CodeLite/SocketAPI/clSocketBase.h

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2014 Eran Ifrah
5 // file name : clSocketBase.h
6 //
7 // -------------------------------------------------------------------------
8 // A
9 // _____ _ _ _ _
10 // / __ \ | | | | (_) |
11 // | / \/ ___ __| | ___| | _| |_ ___
12 // | | / _ \ / _ |/ _ \ | | | __/ _ )
13 // | \__/\ (_) | (_| | __/ |___| | || __/
14 // \____/\___/ \__,_|\___\_____/_|\__\___|
15 //
16 // F i l e
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////////
25
26 #ifndef CLSOCKETBASE_H
27 #define CLSOCKETBASE_H
28
29 #include <string>
30 #include <sys/param.h>
31 #include <wx/msgqueue.h>
32 #include <wx/sharedptr.h>
33 #include <wx/string.h>
34 #if defined(__WXOSX__) || defined(BSD)
35 #include <sys/errno.h>
36 #endif
37 #include "codelite_exports.h"
38
39 #include "wx/defs.h"
40 #ifdef __WINDOWS__ // __WINDOWS__ defined by wx/defs.h
41 // includes windows.h and if wxUSE_WINSOCK2 is true includes winsock2.h
42 #include "wx/msw/wrapwin.h"
43
44 typedef SOCKET socket_t;
45 typedef int socklen_t;
46 #else
47 typedef int socket_t;
48 #define INVALID_SOCKET -1
49 #endif
50
51 class WXDLLIMPEXP_CL clSocketException
52 {
53 std::string m_what;
54
55 public:
56 clSocketException(const std::string& what)
57 : m_what(what)
58 {
59 // trim whitespaces
60 static std::string trimString("\r\n\t\v ");
61 m_what.erase(0, m_what.find_first_not_of(trimString));
62 m_what.erase(m_what.find_last_not_of(trimString) + 1);
63 }
64 ~clSocketException() {}
65 const std::string& what() const { return m_what; }
66 };
67
68 class WXDLLIMPEXP_CL clSocketBase
69 {
70 protected:
71 socket_t m_socket = INVALID_SOCKET;
72 bool m_closeOnExit = true;
73
74 public:
75 typedef wxSharedPtr<clSocketBase> Ptr_t;
76 typedef wxMessageQueue<clSocketBase::Ptr_t> Queue_t;
77
78 enum {
79 kSuccess = 1,
80 kTimeout = 2,
81 kError = 3,
82 };
83
84 #ifdef _WIN32
85 static const int eWouldBlock = WSAEWOULDBLOCK;
86 #else
87 static const int eWouldBlock = EWOULDBLOCK;
88 #endif
89
90 static int GetLastError();
91 static std::string error();
92 static std::string error(const int errorCode);
93
94 public:
95 /**
96 * @brief set the socket into blocking/non-blocking mode
97 * @param blocking
98 */
99 void MakeSocketBlocking(bool blocking);
100
101 clSocketBase(socket_t sockfd = INVALID_SOCKET);
102 virtual ~clSocketBase();
103
104 void SetCloseOnExit(bool closeOnExit) { this->m_closeOnExit = closeOnExit; }
105 bool IsCloseOnExit() const { return m_closeOnExit; }
106 /**
107 * @brief return the descriptor and clear this socket.
108 */
109 socket_t Release();
110
111 /**
112 * @brief initialize the socket library
113 */
114 static void Initialize();
115
116 socket_t GetSocket() const { return m_socket; }
117
118 /**
119 * @brief use user socket handle
120 */
121 void SetSocket(socket_t socket);
122
123 /**
124 * @brief
125 * @param msg
126 */
127 void Send(const std::string& msg);
128 void Send(const wxMemoryBuffer& msg);
129 void Send(const wxString& msg, const wxMBConv& conv = wxConvUTF8);
130
131 /**
132 * @brief
133 * @return
134 */
135 int Read(char* buffer, size_t bufferSize, size_t& bytesRead, long timeout = -1);
136 /**
137 * @brief read string content from remote server
138 * @param content [output]
139 * @param timeout seconds to wait
140 */
141 int Read(wxString& content, const wxMBConv& conv = wxConvUTF8, long timeout = -1);
142
143 /**
144 * @brief read a buffer from the socket
145 * @param content [output]
146 * @param timeout seconds to wait
147 */
148 int Read(wxMemoryBuffer& content, long timeout = -1);
149
150 /**
151 * @brief
152 * @param seconds
153 * @return
154 */
155 int SelectRead(long seconds = -1);
156
157 /**
158 * @brief select for read. Same as above, but use milli seconds instead
159 * @param milliSeconds number of _milliseconds_ to wait
160 * @return
161 */
162 int SelectReadMS(long milliSeconds = -1);
163
164 /**
165 * @brief select for write
166 * @return
167 */
168 int SelectWrite(long seconds = -1);
169
170 /**
171 * @brief select for write (milli seconds version)
172 * @return
173 */
174 int SelectWriteMS(long milliSeconds = -1);
175
176 /**
177 * @brief read a full message that was sent with 'SendMessage'
178 * @param message
179 * @param timeout seconds to wait
180 * @return kSuccess, kTimeout or kError
181 */
182 int ReadMessage(wxString& message, int timeout);
183
184 /**
185 * @brief write a full message
186 * @param message
187 */
188 void WriteMessage(const wxString& message);
189
190 protected:
191 /**
192 * @brief
193 */
194 void DestroySocket();
195 };
196
197 #endif // CLSOCKETBASE_H