]> git.saurik.com Git - wxWidgets.git/blame - include/wx/unix/gsockunx.h
temporary fix the linking problem due to making GSocket::Detected_Read/Write() non...
[wxWidgets.git] / include / wx / unix / gsockunx.h
CommitLineData
dbd300df 1/* -------------------------------------------------------------------------
99d80019
JS
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$
dbd300df
GL
8 * -------------------------------------------------------------------------
9 */
483249fc 10
2804f77d
VZ
11#ifndef _WX_UNIX_GSOCKUNX_H_
12#define _WX_UNIX_GSOCKUNX_H_
d422d01e 13
5e1eac14
VZ
14class wxGSocketIOHandler;
15
f0db5d75 16class GSocket : public GSocketBase
ba2a81d7
DE
17{
18public:
53a161e1
VZ
19 GSocket(wxSocketBase& wxsocket);
20 virtual ~GSocket();
21
eb97543d 22 virtual void Shutdown();
53a161e1
VZ
23 virtual GSocket *WaitConnection(wxSocketBase& wxsocket);
24
09e6e5ec 25 GSocketError SetServer();
948c96ef 26 bool SetReusable();
60edcf45
VZ
27 bool SetBroadcast();
28 bool DontDoBind();
09e6e5ec
DE
29 GSocketError Connect(GSocketStream stream);
30 GSocketError SetNonOriented();
31 int Read(char *buffer, int size);
32 int Write(const char *buffer, int size);
948c96ef 33 void SetNonBlocking(bool non_block);
ba2a81d7 34 GSocketError WXDLLIMPEXP_NET GetError();
b082b524
DE
35 GSocketError GetSockOpt(int level, int optname, void *optval, int *optlen);
36 GSocketError SetSockOpt(int level, int optname,
37 const void *optval, int optlen);
2804f77d
VZ
38 //attach or detach from main loop
39 void Notify(bool flag);
eb97543d
VZ
40 void Detected_Read();
41 void Detected_Write();
8c029a5b 42
f0fbbe23
VZ
43private:
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
09e6e5ec
DE
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);
09e6e5ec 78public:
b6db2e91
SN
79 /* DFE: We can't protect these data member until the GUI code is updated */
80 /* protected: */
5e1eac14 81 wxGSocketIOHandler *m_handler;
a324a7bc 82
2804f77d
VZ
83 // true if socket should fire events
84 bool m_use_events;
85
2804f77d
VZ
86 // pointer for storing extra (usually GUI-specific) data
87 void *m_gui_dependent;
53a161e1
VZ
88
89private:
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);
a324a7bc
GL
93};
94
2804f77d
VZ
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.
99class GSocketFDBasedManager : public GSocketManager
100{
101public:
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);
5c33522f 110 int * const fds = static_cast<int *>(socket->m_gui_dependent);
2804f77d
VZ
111
112 fds[0] = -1;
113 fds[1] = -1;
9bf10d6b 114
2804f77d
VZ
115 return true;
116 }
d422d01e 117
f0fbbe23 118 virtual void Close_Socket(GSocket *socket)
2804f77d
VZ
119 {
120 Uninstall_Callback(socket, GSOCK_INPUT);
121 Uninstall_Callback(socket, GSOCK_OUTPUT);
f0fbbe23
VZ
122
123 close(socket->m_fd);
124 }
125
126 virtual void Destroy_Socket(GSocket *socket)
127 {
128 free(socket->m_gui_dependent);
2804f77d
VZ
129 }
130
131protected:
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 {
5c33522f 168 return static_cast<int *>(socket->m_gui_dependent)[d];
2804f77d
VZ
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
174class GSocketInputBasedManager : public GSocketFDBasedManager
175{
176public:
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
203private:
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};
d422d01e 209
2804f77d 210#endif /* _WX_UNIX_GSOCKUNX_H_ */