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