]> git.saurik.com Git - wxWidgets.git/blob - include/wx/unix/gsockunx.h
move more socket functions common to Winsock and BSD implementations to common code
[wxWidgets.git] / include / wx / unix / gsockunx.h
1 /* -------------------------------------------------------------------------
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$
8 * -------------------------------------------------------------------------
9 */
10
11 #ifndef _WX_UNIX_GSOCKUNX_H_
12 #define _WX_UNIX_GSOCKUNX_H_
13
14 class wxGSocketIOHandler;
15
16 class GSocket : public GSocketBase
17 {
18 public:
19 GSocket(wxSocketBase& wxsocket);
20 virtual ~GSocket();
21
22 virtual void Close();
23 virtual void Shutdown();
24 virtual GSocket *WaitConnection(wxSocketBase& wxsocket);
25
26 GSocketError SetServer();
27 bool SetReusable();
28 bool SetBroadcast();
29 bool DontDoBind();
30 GSocketError Connect(GSocketStream stream);
31 GSocketError SetNonOriented();
32 int Read(char *buffer, int size);
33 int Write(const char *buffer, int size);
34 void SetNonBlocking(bool non_block);
35 GSocketError WXDLLIMPEXP_NET GetError();
36 GSocketError GetSockOpt(int level, int optname, void *optval, int *optlen);
37 GSocketError SetSockOpt(int level, int optname,
38 const void *optval, int optlen);
39 //attach or detach from main loop
40 void Notify(bool flag);
41 void Detected_Read();
42 void Detected_Write();
43
44 protected:
45 //enable or disable event callback using gsocket gui callback table
46 void EnableEvents(bool flag = true);
47 void DisableEvents() { EnableEvents(false); }
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);
56 public:
57 /* DFE: We can't protect these data member until the GUI code is updated */
58 /* protected: */
59 wxGSocketIOHandler *m_handler;
60
61 // true if socket should fire events
62 bool m_use_events;
63
64 // pointer for storing extra (usually GUI-specific) data
65 void *m_gui_dependent;
66
67 private:
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);
71 };
72
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.
77 class GSocketFDBasedManager : public GSocketManager
78 {
79 public:
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);
88 int * const fds = static_cast<int *>(socket->m_gui_dependent);
89
90 fds[0] = -1;
91 fds[1] = -1;
92
93 return true;
94 }
95 virtual void Destroy_Socket(GSocket *socket)
96 {
97 free(socket->m_gui_dependent);
98 }
99
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
111 protected:
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 {
148 return static_cast<int *>(socket->m_gui_dependent)[d];
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
154 class GSocketInputBasedManager : public GSocketFDBasedManager
155 {
156 public:
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
183 private:
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 };
189
190 #endif /* _WX_UNIX_GSOCKUNX_H_ */