]> git.saurik.com Git - wxWidgets.git/blame - include/wx/unix/private/sockunix.h
wxMessageBox off the main thread lost result code.
[wxWidgets.git] / include / wx / unix / private / sockunix.h
CommitLineData
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
51fe4b60
VZ
6// Copyright: (c) 1997 Guilhem Lavaux
7// (c) 2008 Vadim Zeitlin
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
483249fc 10
2804f77d
VZ
11#ifndef _WX_UNIX_GSOCKUNX_H_
12#define _WX_UNIX_GSOCKUNX_H_
d422d01e 13
36b6a928 14#include <unistd.h>
51fe4b60 15#include <sys/ioctl.h>
74d30983
VZ
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
6bcc1145 24#include "wx/private/fdiomanager.h"
36b6a928 25
a9d859df
VZ
26class wxSocketImplUnix : public wxSocketImpl,
27 public wxFDIOHandler
ba2a81d7
DE
28{
29public:
9123889f
VZ
30 wxSocketImplUnix(wxSocketBase& wxsocket)
31 : wxSocketImpl(wxsocket)
32 {
33 m_fds[0] =
34 m_fds[1] = -1;
9123889f 35 }
53a161e1 36
2b036c4b
VZ
37 virtual wxSocketError GetLastError() const;
38
df21920b
VZ
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
a9d859df
VZ
56 // wxFDIOHandler methods
57 virtual void OnReadWaiting();
58 virtual void OnWriteWaiting();
59 virtual void OnExceptionWaiting();
251e98cb 60 virtual bool IsOk() const { return m_fd != INVALID_SOCKET; }
8c029a5b 61
f0fbbe23 62private:
51fe4b60
VZ
63 virtual void DoClose()
64 {
7d66cdcc 65 DisableEvents();
51fe4b60
VZ
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
22185a1f 78 // enable or disable notifications for socket input/output events
df21920b
VZ
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); }
f0fbbe23 83
22185a1f 84 // really enable or disable socket input/output events
df21920b 85 void DoEnableEvents(int flags, bool enable);
f0fbbe23 86
51fe4b60 87protected:
51fe4b60
VZ
88 // descriptors for input and output event notification channels associated
89 // with the socket
90 int m_fds[2];
53a161e1
VZ
91
92private:
93 // notify the associated wxSocket about a change in socket state and shut
51fe4b60
VZ
94 // down the socket if the event is wxSOCKET_LOST
95 void OnStateChange(wxSocketNotify event);
96
df21920b
VZ
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
51fe4b60
VZ
102 // give it access to our m_fds
103 friend class wxSocketFDBasedManager;
a324a7bc
GL
104};
105
6bcc1145
VZ
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
51fe4b60 109class wxSocketFDBasedManager : public wxSocketManager
2804f77d
VZ
110{
111public:
6bcc1145
VZ
112 wxSocketFDBasedManager()
113 {
114 m_fdioManager = NULL;
115 }
116
117 virtual bool OnInit();
2804f77d
VZ
118 virtual void OnExit() { }
119
4f260c9c
VZ
120 virtual wxSocketImpl *CreateSocket(wxSocketBase& wxsocket)
121 {
122 return new wxSocketImplUnix(wxsocket);
123 }
124
6bcc1145
VZ
125 virtual void Install_Callback(wxSocketImpl *socket_, wxSocketNotify event);
126 virtual void Uninstall_Callback(wxSocketImpl *socket_, wxSocketNotify event);
2804f77d 127
6bcc1145 128protected:
51fe4b60 129 // get the FD index corresponding to the given wxSocketNotify
6bcc1145
VZ
130 wxFDIOManager::Direction
131 GetDirForEvent(wxSocketImpl *socket, wxSocketNotify event);
2804f77d
VZ
132
133 // access the FDs we store
6bcc1145 134 int& FD(wxSocketImplUnix *socket, wxFDIOManager::Direction d)
2804f77d 135 {
a9d859df 136 return socket->m_fds[d];
2804f77d 137 }
a9d859df 138
6bcc1145 139 wxFDIOManager *m_fdioManager;
2804f77d 140
6bcc1145 141 wxDECLARE_NO_COPY_CLASS(wxSocketFDBasedManager);
2804f77d 142};
d422d01e 143
2804f77d 144#endif /* _WX_UNIX_GSOCKUNX_H_ */