]>
Commit | Line | Data |
---|---|---|
51fe4b60 | 1 | ///////////////////////////////////////////////////////////////////////////// |
60913641 | 2 | // Name: wx/unix/private/sockunix.h |
51fe4b60 VZ |
3 | // Purpose: wxSocketImpl implementation for Unix systems |
4 | // Authors: Guilhem Lavaux, Vadim Zeitlin | |
5 | // Created: April 1997 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 1997 Guilhem Lavaux | |
8 | // (c) 2008 Vadim Zeitlin | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
483249fc | 11 | |
2804f77d VZ |
12 | #ifndef _WX_UNIX_GSOCKUNX_H_ |
13 | #define _WX_UNIX_GSOCKUNX_H_ | |
d422d01e | 14 | |
36b6a928 | 15 | #include <unistd.h> |
51fe4b60 | 16 | #include <sys/ioctl.h> |
74d30983 VZ |
17 | |
18 | // Under older (Open)Solaris versions FIONBIO is declared in this header only. | |
19 | // In the newer versions it's included by sys/ioctl.h but it's simpler to just | |
20 | // include it always instead of testing for whether it is or not. | |
21 | #ifdef __SOLARIS__ | |
22 | #include <sys/filio.h> | |
23 | #endif | |
24 | ||
6bcc1145 | 25 | #include "wx/private/fdiomanager.h" |
36b6a928 | 26 | |
a9d859df VZ |
27 | class wxSocketImplUnix : public wxSocketImpl, |
28 | public wxFDIOHandler | |
ba2a81d7 DE |
29 | { |
30 | public: | |
9123889f VZ |
31 | wxSocketImplUnix(wxSocketBase& wxsocket) |
32 | : wxSocketImpl(wxsocket) | |
33 | { | |
34 | m_fds[0] = | |
35 | m_fds[1] = -1; | |
9123889f | 36 | } |
53a161e1 | 37 | |
2b036c4b VZ |
38 | virtual wxSocketError GetLastError() const; |
39 | ||
df21920b VZ |
40 | virtual void ReenableEvents(wxSocketEventFlags flags) |
41 | { | |
42 | // enable the notifications about input/output being available again in | |
43 | // case they were disabled by OnRead/WriteWaiting() | |
44 | // | |
45 | // notice that we'd like to enable the events here only if there is | |
46 | // nothing more left on the socket right now as otherwise we're going | |
47 | // to get a "ready for whatever" notification immediately (well, during | |
48 | // the next event loop iteration) and disable the event back again | |
49 | // which is rather inefficient but unfortunately doing it like this | |
50 | // doesn't work because the existing code (e.g. src/common/sckipc.cpp) | |
51 | // expects to keep getting notifications about the data available from | |
52 | // the socket even if it didn't read all the data the last time, so we | |
53 | // absolutely have to continue generating them | |
54 | EnableEvents(flags); | |
55 | } | |
56 | ||
a9d859df VZ |
57 | // wxFDIOHandler methods |
58 | virtual void OnReadWaiting(); | |
59 | virtual void OnWriteWaiting(); | |
60 | virtual void OnExceptionWaiting(); | |
251e98cb | 61 | virtual bool IsOk() const { return m_fd != INVALID_SOCKET; } |
8c029a5b | 62 | |
f0fbbe23 | 63 | private: |
51fe4b60 VZ |
64 | virtual void DoClose() |
65 | { | |
7d66cdcc | 66 | DisableEvents(); |
51fe4b60 VZ |
67 | |
68 | close(m_fd); | |
69 | } | |
70 | ||
71 | virtual void UnblockAndRegisterWithEventLoop() | |
72 | { | |
73 | int trueArg = 1; | |
74 | ioctl(m_fd, FIONBIO, &trueArg); | |
75 | ||
76 | EnableEvents(); | |
77 | } | |
78 | ||
22185a1f | 79 | // enable or disable notifications for socket input/output events |
df21920b VZ |
80 | void EnableEvents(int flags = wxSOCKET_INPUT_FLAG | wxSOCKET_OUTPUT_FLAG) |
81 | { DoEnableEvents(flags, true); } | |
82 | void DisableEvents(int flags = wxSOCKET_INPUT_FLAG | wxSOCKET_OUTPUT_FLAG) | |
83 | { DoEnableEvents(flags, false); } | |
f0fbbe23 | 84 | |
22185a1f | 85 | // really enable or disable socket input/output events |
df21920b | 86 | void DoEnableEvents(int flags, bool enable); |
f0fbbe23 | 87 | |
51fe4b60 | 88 | protected: |
51fe4b60 VZ |
89 | // descriptors for input and output event notification channels associated |
90 | // with the socket | |
91 | int m_fds[2]; | |
53a161e1 VZ |
92 | |
93 | private: | |
94 | // notify the associated wxSocket about a change in socket state and shut | |
51fe4b60 VZ |
95 | // down the socket if the event is wxSOCKET_LOST |
96 | void OnStateChange(wxSocketNotify event); | |
97 | ||
df21920b VZ |
98 | // check if there is any input available, return 1 if yes, 0 if no or -1 on |
99 | // error | |
100 | int CheckForInput(); | |
101 | ||
102 | ||
51fe4b60 VZ |
103 | // give it access to our m_fds |
104 | friend class wxSocketFDBasedManager; | |
a324a7bc GL |
105 | }; |
106 | ||
6bcc1145 VZ |
107 | // A version of wxSocketManager which uses FDs for socket IO: it is used by |
108 | // Unix console applications and some X11-like ports (wxGTK and wxMotif but not | |
109 | // wxX11 currently) which implement their own port-specific wxFDIOManagers | |
51fe4b60 | 110 | class wxSocketFDBasedManager : public wxSocketManager |
2804f77d VZ |
111 | { |
112 | public: | |
6bcc1145 VZ |
113 | wxSocketFDBasedManager() |
114 | { | |
115 | m_fdioManager = NULL; | |
116 | } | |
117 | ||
118 | virtual bool OnInit(); | |
2804f77d VZ |
119 | virtual void OnExit() { } |
120 | ||
4f260c9c VZ |
121 | virtual wxSocketImpl *CreateSocket(wxSocketBase& wxsocket) |
122 | { | |
123 | return new wxSocketImplUnix(wxsocket); | |
124 | } | |
125 | ||
6bcc1145 VZ |
126 | virtual void Install_Callback(wxSocketImpl *socket_, wxSocketNotify event); |
127 | virtual void Uninstall_Callback(wxSocketImpl *socket_, wxSocketNotify event); | |
2804f77d | 128 | |
6bcc1145 | 129 | protected: |
51fe4b60 | 130 | // get the FD index corresponding to the given wxSocketNotify |
6bcc1145 VZ |
131 | wxFDIOManager::Direction |
132 | GetDirForEvent(wxSocketImpl *socket, wxSocketNotify event); | |
2804f77d VZ |
133 | |
134 | // access the FDs we store | |
6bcc1145 | 135 | int& FD(wxSocketImplUnix *socket, wxFDIOManager::Direction d) |
2804f77d | 136 | { |
a9d859df | 137 | return socket->m_fds[d]; |
2804f77d | 138 | } |
a9d859df | 139 | |
6bcc1145 | 140 | wxFDIOManager *m_fdioManager; |
2804f77d | 141 | |
6bcc1145 | 142 | wxDECLARE_NO_COPY_CLASS(wxSocketFDBasedManager); |
2804f77d | 143 | }; |
d422d01e | 144 | |
2804f77d | 145 | #endif /* _WX_UNIX_GSOCKUNX_H_ */ |