]> git.saurik.com Git - wxWidgets.git/blob - include/wx/unix/gsockunx.h
compilation fixes for Unix after moving wxFD_XXX macros from wx/unix/private.h
[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();
20 ~GSocket();
21 virtual void Close();
22 virtual void Shutdown();
23 GSocketError SetLocal(GAddress *address);
24 GSocketError SetPeer(GAddress *address);
25 GAddress *GetLocal();
26 GAddress *GetPeer();
27 GSocketError SetServer();
28 GSocket *WaitConnection();
29 bool SetReusable();
30 bool SetBroadcast();
31 bool DontDoBind();
32 GSocketError Connect(GSocketStream stream);
33 GSocketError SetNonOriented();
34 int Read(char *buffer, int size);
35 int Write(const char *buffer, int size);
36 void SetNonBlocking(bool non_block);
37 void SetTimeout(unsigned long millisec);
38 GSocketError WXDLLIMPEXP_NET GetError();
39 void SetCallback(GSocketEventFlags flags,
40 GSocketCallback callback, char *cdata);
41 void UnsetCallback(GSocketEventFlags flags);
42 GSocketError GetSockOpt(int level, int optname, void *optval, int *optlen);
43 GSocketError SetSockOpt(int level, int optname,
44 const void *optval, int optlen);
45 //attach or detach from main loop
46 void Notify(bool flag);
47 void Detected_Read();
48 void Detected_Write();
49 void SetInitialSocketBuffers(int recv, int send)
50 {
51 m_initialRecvBufferSize = recv;
52 m_initialSendBufferSize = send;
53 }
54
55 protected:
56 //enable or disable event callback using gsocket gui callback table
57 void EnableEvents(bool flag = true);
58 void DisableEvents() { EnableEvents(false); }
59 void Enable(GSocketEvent event);
60 void Disable(GSocketEvent event);
61 GSocketError Input_Timeout();
62 GSocketError Output_Timeout();
63 int Recv_Stream(char *buffer, int size);
64 int Recv_Dgram(char *buffer, int size);
65 int Send_Stream(const char *buffer, int size);
66 int Send_Dgram(const char *buffer, int size);
67 public:
68 /* DFE: We can't protect these data member until the GUI code is updated */
69 /* protected: */
70 wxGSocketIOHandler *m_handler;
71
72 // true if socket should fire events
73 bool m_use_events;
74
75 // pointer for storing extra (usually GUI-specific) data
76 void *m_gui_dependent;
77 };
78
79 // A version of GSocketManager which uses FDs for socket IO
80 //
81 // This class uses GSocket::m_gui_dependent field to store the 2 (for input and
82 // output) FDs associated with the socket.
83 class GSocketFDBasedManager : public GSocketManager
84 {
85 public:
86 // no special initialization/cleanup needed when using FDs
87 virtual bool OnInit() { return true; }
88 virtual void OnExit() { }
89
90 // allocate/free the storage we need
91 virtual bool Init_Socket(GSocket *socket)
92 {
93 socket->m_gui_dependent = malloc(sizeof(int)*2);
94 int * const fds = static_cast<int *>(socket->m_gui_dependent);
95
96 fds[0] = -1;
97 fds[1] = -1;
98
99 return true;
100 }
101 virtual void Destroy_Socket(GSocket *socket)
102 {
103 free(socket->m_gui_dependent);
104 }
105
106 virtual void Enable_Events(GSocket *socket)
107 {
108 Install_Callback(socket, GSOCK_INPUT);
109 Install_Callback(socket, GSOCK_OUTPUT);
110 }
111 virtual void Disable_Events(GSocket *socket)
112 {
113 Uninstall_Callback(socket, GSOCK_INPUT);
114 Uninstall_Callback(socket, GSOCK_OUTPUT);
115 }
116
117 protected:
118 // identifies either input or output direction
119 //
120 // NB: the values of this enum shouldn't change
121 enum SocketDir
122 {
123 FD_INPUT,
124 FD_OUTPUT
125 };
126
127 // get the FD index corresponding to the given GSocketEvent
128 SocketDir GetDirForEvent(GSocket *socket, GSocketEvent event)
129 {
130 switch ( event )
131 {
132 default:
133 wxFAIL_MSG( "unexpected socket event" );
134 // fall through
135
136 case GSOCK_LOST:
137 // fall through
138
139 case GSOCK_INPUT:
140 return FD_INPUT;
141
142 case GSOCK_OUTPUT:
143 return FD_OUTPUT;
144
145 case GSOCK_CONNECTION:
146 // FIXME: explain this?
147 return socket->m_server ? FD_INPUT : FD_OUTPUT;
148 }
149 }
150
151 // access the FDs we store
152 int& FD(GSocket *socket, SocketDir d)
153 {
154 return static_cast<int *>(socket->m_gui_dependent)[d];
155 }
156 };
157
158 // Common base class for all ports using X11-like (and hence implemented in
159 // X11, Motif and GTK) AddInput() and RemoveInput() functions
160 class GSocketInputBasedManager : public GSocketFDBasedManager
161 {
162 public:
163 virtual void Install_Callback(GSocket *socket, GSocketEvent event)
164 {
165 wxCHECK_RET( socket->m_fd != -1,
166 "shouldn't be called on invalid socket" );
167
168 const SocketDir d = GetDirForEvent(socket, event);
169
170 int& fd = FD(socket, d);
171 if ( fd != -1 )
172 RemoveInput(fd);
173
174 fd = AddInput(socket, d);
175 }
176
177 virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event)
178 {
179 const SocketDir d = GetDirForEvent(socket, event);
180
181 int& fd = FD(socket, d);
182 if ( fd != -1 )
183 {
184 RemoveInput(fd);
185 fd = -1;
186 }
187 }
188
189 private:
190 // these functions map directly to XtAdd/RemoveInput() or
191 // gdk_input_add/remove()
192 virtual int AddInput(GSocket *socket, SocketDir d) = 0;
193 virtual void RemoveInput(int fd) = 0;
194 };
195
196 #endif /* _WX_UNIX_GSOCKUNX_H_ */