replace wx_{const,static,reinterpret}_cast with their standard C++ equivalents
[wxWidgets.git] / include / wx / unix / gsockunx.h
1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
3 * Name: gsockunx.h
4 * Copyright: (c) Guilhem Lavaux
5 * Licence: wxWindows Licence
6 * Purpose: GSocket Unix header
7 * CVSID: $Id$
8 * -------------------------------------------------------------------------
9 */
10
11 #ifndef _WX_UNIX_GSOCKUNX_H_
12 #define _WX_UNIX_GSOCKUNX_H_
13
14 class wxGSocketIOHandler;
15
16 class GSocket
17 {
18 public:
19 GSocket();
20 virtual ~GSocket();
21 bool IsOk() { return m_ok; }
22 void Close();
23 void Shutdown();
24 GSocketError SetLocal(GAddress *address);
25 GSocketError SetPeer(GAddress *address);
26 GAddress *GetLocal();
27 GAddress *GetPeer();
28 GSocketError SetServer();
29 GSocket *WaitConnection();
30 bool SetReusable();
31 bool SetBroadcast();
32 bool DontDoBind();
33 GSocketError Connect(GSocketStream stream);
34 GSocketError SetNonOriented();
35 int Read(char *buffer, int size);
36 int Write(const char *buffer, int size);
37 GSocketEventFlags Select(GSocketEventFlags flags);
38 void SetNonBlocking(bool non_block);
39 void SetTimeout(unsigned long millisec);
40 GSocketError WXDLLIMPEXP_NET GetError();
41 void SetCallback(GSocketEventFlags flags,
42 GSocketCallback callback, char *cdata);
43 void UnsetCallback(GSocketEventFlags flags);
44 GSocketError GetSockOpt(int level, int optname, void *optval, int *optlen);
45 GSocketError SetSockOpt(int level, int optname,
46 const void *optval, int optlen);
47 //attach or detach from main loop
48 void Notify(bool flag);
49 virtual void Detected_Read();
50 virtual void Detected_Write();
51 void SetInitialSocketBuffers(int recv, int send)
52 {
53 m_initialRecvBufferSize = recv;
54 m_initialSendBufferSize = send;
55 }
56
57 protected:
58 //enable or disable event callback using gsocket gui callback table
59 void EnableEvents(bool flag = true);
60 void DisableEvents() { EnableEvents(false); }
61 void Enable(GSocketEvent event);
62 void Disable(GSocketEvent event);
63 GSocketError Input_Timeout();
64 GSocketError Output_Timeout();
65 int Recv_Stream(char *buffer, int size);
66 int Recv_Dgram(char *buffer, int size);
67 int Send_Stream(const char *buffer, int size);
68 int Send_Dgram(const char *buffer, int size);
69 bool m_ok;
70 int m_initialRecvBufferSize;
71 int m_initialSendBufferSize;
72 public:
73 /* DFE: We can't protect these data member until the GUI code is updated */
74 /* protected: */
75 int m_fd;
76 wxGSocketIOHandler *m_handler;
77 GAddress *m_local;
78 GAddress *m_peer;
79 GSocketError m_error;
80
81 bool m_non_blocking;
82 bool m_server;
83 bool m_stream;
84 bool m_establishing;
85 bool m_reusable;
86 bool m_broadcast;
87 bool m_dobind;
88 unsigned long m_timeout;
89
90 // true if socket should fire events
91 bool m_use_events;
92
93 /* Callbacks */
94 GSocketEventFlags m_detected;
95 GSocketCallback m_cbacks[GSOCK_MAX_EVENT];
96 char *m_data[GSOCK_MAX_EVENT];
97
98 // pointer for storing extra (usually GUI-specific) data
99 void *m_gui_dependent;
100 };
101
102 // A version of GSocketManager which uses FDs for socket IO
103 //
104 // This class uses GSocket::m_gui_dependent field to store the 2 (for input and
105 // output) FDs associated with the socket.
106 class GSocketFDBasedManager : public GSocketManager
107 {
108 public:
109 // no special initialization/cleanup needed when using FDs
110 virtual bool OnInit() { return true; }
111 virtual void OnExit() { }
112
113 // allocate/free the storage we need
114 virtual bool Init_Socket(GSocket *socket)
115 {
116 socket->m_gui_dependent = malloc(sizeof(int)*2);
117 int * const fds = static_cast<int *>(socket->m_gui_dependent);
118
119 fds[0] = -1;
120 fds[1] = -1;
121
122 return true;
123 }
124 virtual void Destroy_Socket(GSocket *socket)
125 {
126 free(socket->m_gui_dependent);
127 }
128
129 virtual void Enable_Events(GSocket *socket)
130 {
131 Install_Callback(socket, GSOCK_INPUT);
132 Install_Callback(socket, GSOCK_OUTPUT);
133 }
134 virtual void Disable_Events(GSocket *socket)
135 {
136 Uninstall_Callback(socket, GSOCK_INPUT);
137 Uninstall_Callback(socket, GSOCK_OUTPUT);
138 }
139
140 protected:
141 // identifies either input or output direction
142 //
143 // NB: the values of this enum shouldn't change
144 enum SocketDir
145 {
146 FD_INPUT,
147 FD_OUTPUT
148 };
149
150 // get the FD index corresponding to the given GSocketEvent
151 SocketDir GetDirForEvent(GSocket *socket, GSocketEvent event)
152 {
153 switch ( event )
154 {
155 default:
156 wxFAIL_MSG( "unexpected socket event" );
157 // fall through
158
159 case GSOCK_LOST:
160 // fall through
161
162 case GSOCK_INPUT:
163 return FD_INPUT;
164
165 case GSOCK_OUTPUT:
166 return FD_OUTPUT;
167
168 case GSOCK_CONNECTION:
169 // FIXME: explain this?
170 return socket->m_server ? FD_INPUT : FD_OUTPUT;
171 }
172 }
173
174 // access the FDs we store
175 int& FD(GSocket *socket, SocketDir d)
176 {
177 return static_cast<int *>(socket->m_gui_dependent)[d];
178 }
179 };
180
181 // Common base class for all ports using X11-like (and hence implemented in
182 // X11, Motif and GTK) AddInput() and RemoveInput() functions
183 class GSocketInputBasedManager : public GSocketFDBasedManager
184 {
185 public:
186 virtual void Install_Callback(GSocket *socket, GSocketEvent event)
187 {
188 wxCHECK_RET( socket->m_fd != -1,
189 "shouldn't be called on invalid socket" );
190
191 const SocketDir d = GetDirForEvent(socket, event);
192
193 int& fd = FD(socket, d);
194 if ( fd != -1 )
195 RemoveInput(fd);
196
197 fd = AddInput(socket, d);
198 }
199
200 virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event)
201 {
202 const SocketDir d = GetDirForEvent(socket, event);
203
204 int& fd = FD(socket, d);
205 if ( fd != -1 )
206 {
207 RemoveInput(fd);
208 fd = -1;
209 }
210 }
211
212 private:
213 // these functions map directly to XtAdd/RemoveInput() or
214 // gdk_input_add/remove()
215 virtual int AddInput(GSocket *socket, SocketDir d) = 0;
216 virtual void RemoveInput(int fd) = 0;
217 };
218
219 #endif /* _WX_UNIX_GSOCKUNX_H_ */