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