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