]> git.saurik.com Git - wxWidgets.git/blob - include/wx/unix/gsockunx.h
trying to resolve GSocketManager API mess: the meaning of Install/Uninstall_Callback...
[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 : public GSocketBase
17 {
18 public:
19 GSocket(wxSocketBase& wxsocket);
20 virtual ~GSocket();
21
22 virtual void Shutdown();
23 virtual GSocket *WaitConnection(wxSocketBase& wxsocket);
24
25 GSocketError SetServer();
26 bool SetReusable();
27 bool SetBroadcast();
28 bool DontDoBind();
29 GSocketError Connect(GSocketStream stream);
30 GSocketError SetNonOriented();
31 int Read(char *buffer, int size);
32 int Write(const char *buffer, int size);
33 void SetNonBlocking(bool non_block);
34 GSocketError WXDLLIMPEXP_NET GetError();
35 GSocketError GetSockOpt(int level, int optname, void *optval, int *optlen);
36 GSocketError SetSockOpt(int level, int optname,
37 const void *optval, int optlen);
38 //attach or detach from main loop
39 void Notify(bool flag);
40 void Detected_Read();
41 void Detected_Write();
42
43 private:
44 // enable or disable notifications for socket input/output events but only
45 // if m_use_events is true; do nothing otherwise
46 void EnableEvents()
47 {
48 if ( m_use_events )
49 DoEnableEvents(true);
50 }
51
52 void DisableEvents()
53 {
54 if ( m_use_events )
55 DoEnableEvents(false);
56 }
57
58 // really enable or disable socket input/output events, regardless of
59 // m_use_events value
60 void DoEnableEvents(bool enable);
61
62
63 // enable or disable events for the given event if m_use_events; do nothing
64 // otherwise
65 //
66 // notice that these functions also update m_detected: EnableEvent() clears
67 // the corresponding bit in it and DisableEvent() sets it
68 void EnableEvent(GSocketEvent event);
69 void DisableEvent(GSocketEvent event);
70
71
72 GSocketError Input_Timeout();
73 GSocketError Output_Timeout();
74 int Recv_Stream(char *buffer, int size);
75 int Recv_Dgram(char *buffer, int size);
76 int Send_Stream(const char *buffer, int size);
77 int Send_Dgram(const char *buffer, int size);
78 public:
79 /* DFE: We can't protect these data member until the GUI code is updated */
80 /* protected: */
81 wxGSocketIOHandler *m_handler;
82
83 // true if socket should fire events
84 bool m_use_events;
85
86 // pointer for storing extra (usually GUI-specific) data
87 void *m_gui_dependent;
88
89 private:
90 // notify the associated wxSocket about a change in socket state and shut
91 // down the socket if the event is GSOCK_LOST
92 void OnStateChange(GSocketEvent event);
93 };
94
95 // A version of GSocketManager which uses FDs for socket IO
96 //
97 // This class uses GSocket::m_gui_dependent field to store the 2 (for input and
98 // output) FDs associated with the socket.
99 class GSocketFDBasedManager : public GSocketManager
100 {
101 public:
102 // no special initialization/cleanup needed when using FDs
103 virtual bool OnInit() { return true; }
104 virtual void OnExit() { }
105
106 // allocate/free the storage we need
107 virtual bool Init_Socket(GSocket *socket)
108 {
109 socket->m_gui_dependent = malloc(sizeof(int)*2);
110 int * const fds = static_cast<int *>(socket->m_gui_dependent);
111
112 fds[0] = -1;
113 fds[1] = -1;
114
115 return true;
116 }
117
118 virtual void Close_Socket(GSocket *socket)
119 {
120 Uninstall_Callback(socket, GSOCK_INPUT);
121 Uninstall_Callback(socket, GSOCK_OUTPUT);
122
123 close(socket->m_fd);
124 }
125
126 virtual void Destroy_Socket(GSocket *socket)
127 {
128 free(socket->m_gui_dependent);
129 }
130
131 protected:
132 // identifies either input or output direction
133 //
134 // NB: the values of this enum shouldn't change
135 enum SocketDir
136 {
137 FD_INPUT,
138 FD_OUTPUT
139 };
140
141 // get the FD index corresponding to the given GSocketEvent
142 SocketDir GetDirForEvent(GSocket *socket, GSocketEvent event)
143 {
144 switch ( event )
145 {
146 default:
147 wxFAIL_MSG( "unexpected socket event" );
148 // fall through
149
150 case GSOCK_LOST:
151 // fall through
152
153 case GSOCK_INPUT:
154 return FD_INPUT;
155
156 case GSOCK_OUTPUT:
157 return FD_OUTPUT;
158
159 case GSOCK_CONNECTION:
160 // FIXME: explain this?
161 return socket->m_server ? FD_INPUT : FD_OUTPUT;
162 }
163 }
164
165 // access the FDs we store
166 int& FD(GSocket *socket, SocketDir d)
167 {
168 return static_cast<int *>(socket->m_gui_dependent)[d];
169 }
170 };
171
172 // Common base class for all ports using X11-like (and hence implemented in
173 // X11, Motif and GTK) AddInput() and RemoveInput() functions
174 class GSocketInputBasedManager : public GSocketFDBasedManager
175 {
176 public:
177 virtual void Install_Callback(GSocket *socket, GSocketEvent event)
178 {
179 wxCHECK_RET( socket->m_fd != -1,
180 "shouldn't be called on invalid socket" );
181
182 const SocketDir d = GetDirForEvent(socket, event);
183
184 int& fd = FD(socket, d);
185 if ( fd != -1 )
186 RemoveInput(fd);
187
188 fd = AddInput(socket, d);
189 }
190
191 virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event)
192 {
193 const SocketDir d = GetDirForEvent(socket, event);
194
195 int& fd = FD(socket, d);
196 if ( fd != -1 )
197 {
198 RemoveInput(fd);
199 fd = -1;
200 }
201 }
202
203 private:
204 // these functions map directly to XtAdd/RemoveInput() or
205 // gdk_input_add/remove()
206 virtual int AddInput(GSocket *socket, SocketDir d) = 0;
207 virtual void RemoveInput(int fd) = 0;
208 };
209
210 #endif /* _WX_UNIX_GSOCKUNX_H_ */