Added GSocket for Unix (only GTK for the moment)
[wxWidgets.git] / include / wx / socket.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: socket.h
3 // Purpose: Socket handling classes
4 // Author: Guilhem Lavaux
5 // Modified by:
6 // Created: April 1997
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_NETWORK_SOCKET_H
13 #define _WX_NETWORK_SOCKET_H
14
15 #ifdef __GNUG__
16 #pragma interface "socket.h"
17 #endif
18
19 #include "wx/defs.h"
20
21 #if wxUSE_SOCKETS
22
23 // ---------------------------------------------------------------------------
24 // wxSocket headers (generic)
25 // ---------------------------------------------------------------------------
26 #ifdef WXPREC
27 #include "wx/wxprec.h"
28 #else
29 #include "wx/event.h"
30 #include "wx/string.h"
31 #endif
32
33 #include "wx/sckaddr.h"
34 #include "gsocket.h"
35
36 class WXDLLEXPORT wxSocketEvent;
37 class WXDLLEXPORT wxSocketBase : public wxEvtHandler
38 {
39 DECLARE_CLASS(wxSocketBase)
40 public:
41
42 enum wxSockFlags { NONE=0, NOWAIT=1, WAITALL=2, SPEED=4 };
43 // Type of request
44
45 enum wxSockType { SOCK_CLIENT, SOCK_SERVER, SOCK_INTERNAL, SOCK_UNINIT };
46 typedef void (*wxSockCbk)(wxSocketBase& sock,GSocketEvent evt,char *cdata);
47
48 protected:
49 GSocket *m_socket; // wxSocket socket
50 wxSockFlags m_flags; // wxSocket flags
51 wxSockType m_type; // wxSocket type
52 GSocketEventFlags m_neededreq; // Specify which requet signals we need
53 size_t m_lcount; // Last IO request size
54 unsigned long m_timeout; // IO timeout value
55
56 char *m_unread; // Pushback buffer
57 size_t m_unrd_size; // Pushback buffer size
58 size_t m_unrd_cur; // Pushback pointer
59
60 wxSockCbk m_cbk; // C callback
61 char *m_cdata; // C callback data
62
63 bool m_connected; // Connected ?
64 bool m_notify_state; // Notify state
65 int m_id; // Socket id (for event handler)
66
67 enum {
68 DEFER_READ, DEFER_WRITE, NO_DEFER
69 } m_defering; // Defering state
70 char *m_defer_buffer; // Defering target buffer
71 size_t m_defer_nbytes; // Defering buffer size
72
73 wxList m_states; // Stack of states
74
75 public:
76 wxSocketBase();
77 virtual ~wxSocketBase();
78 virtual bool Close();
79
80 // Base IO
81 wxSocketBase& Peek(char* buffer, size_t nbytes);
82 wxSocketBase& Read(char* buffer, size_t nbytes);
83 wxSocketBase& Write(const char *buffer, size_t nbytes);
84 wxSocketBase& Unread(const char *buffer, size_t nbytes);
85 wxSocketBase& ReadMsg(char *buffer, size_t nbytes);
86 wxSocketBase& WriteMsg(const char *buffer, size_t nbytes);
87 void Discard();
88
89 // Try not to use this two methods (they sould be protected)
90 void CreatePushbackAfter(const char *buffer, size_t size);
91 void CreatePushbackBefore(const char *buffer, size_t size);
92
93 // Status
94 inline bool Ok() const { return (m_socket != NULL); };
95 inline bool Error() const
96 { return (GSocket_GetError(m_socket) != GSOCK_NOERROR); };
97 inline bool IsConnected() const { return m_connected; };
98 inline bool IsDisconnected() const { return !IsConnected(); };
99 inline bool IsNoWait() const { return ((m_flags & NOWAIT) != 0); };
100 bool IsData() const;
101 inline size_t LastCount() const { return m_lcount; }
102 inline GSocketError LastError() const { return GSocket_GetError(m_socket); }
103 inline wxSockType GetType() const { return m_type; }
104
105 void SetFlags(wxSockFlags _flags);
106 wxSockFlags GetFlags() const;
107 inline void SetTimeout(unsigned long sec) { m_timeout = sec; }
108
109 // seconds = -1 means infinite wait
110 // seconds = 0 means no wait
111 // seconds > 0 means specified wait
112 bool Wait(long seconds = -1, long microseconds = 0);
113 bool WaitForRead(long seconds = -1, long microseconds = 0);
114 bool WaitForWrite(long seconds = -1, long microseconds = 0);
115 bool WaitForLost(long seconds = -1, long microseconds = 0);
116
117 // Save the current state of Socket
118 void SaveState();
119 void RestoreState();
120
121 // Setup external callback
122 wxSockCbk Callback(wxSockCbk cbk_);
123 char *CallbackData(char *data);
124
125 // Setup event handler
126 void SetEventHandler(wxEvtHandler& evt_hdlr, int id = -1);
127
128 // Method called when it happens something on the socket
129 void SetNotify(GSocketEventFlags flags);
130 virtual void OnRequest(GSocketEvent req_evt);
131
132 // Public internal callback
133 virtual void OldOnNotify(GSocketEvent WXUNUSED(evt));
134
135 // Some info on the socket...
136 virtual bool GetPeer(wxSockAddress& addr_man) const;
137 virtual bool GetLocal(wxSockAddress& addr_man) const;
138
139 // Install or uninstall callbacks
140 void Notify(bool notify);
141
142 // So you can know what the socket driver is looking for ...
143 inline GSocketEventFlags NeededReq() const { return m_neededreq; }
144
145 static GSocketEventFlags EventToNotify(GSocketEvent evt);
146
147 protected:
148 friend class wxSocketServer;
149 friend class wxSocketHandler;
150 friend class wxSocketInternal;
151
152 #ifdef __SALFORDC__
153 public:
154 #endif
155
156 wxSocketBase(wxSockFlags flags, wxSockType type);
157
158 #ifdef __SALFORDC__
159 protected:
160 #endif
161
162 bool _Wait(long seconds, long microseconds, int type);
163
164 int DeferRead(char *buffer, size_t nbytes);
165 int DeferWrite(const char *buffer, size_t nbytes);
166 void DoDefer(GSocketEvent evt);
167
168 // Pushback library
169 size_t GetPushback(char *buffer, size_t size, bool peek);
170 };
171
172 ////////////////////////////////////////////////////////////////////////
173
174 class WXDLLEXPORT wxSocketServer : public wxSocketBase
175 {
176 DECLARE_CLASS(wxSocketServer)
177 public:
178
179 // 'service' can be a name or a port-number
180
181 wxSocketServer(wxSockAddress& addr_man, wxSockFlags flags = wxSocketBase::NONE);
182
183 wxSocketBase* Accept();
184 bool AcceptWith(wxSocketBase& sock);
185 };
186
187 ////////////////////////////////////////////////////////////////////////
188
189 class WXDLLEXPORT wxSocketClient : public wxSocketBase
190 {
191 DECLARE_CLASS(wxSocketClient)
192 public:
193
194 wxSocketClient(wxSockFlags flags = wxSocketBase::NONE);
195 virtual ~wxSocketClient();
196
197 virtual bool Connect(wxSockAddress& addr_man, bool wait = TRUE);
198
199 bool WaitOnConnect(long seconds = -1, long microseconds = 0);
200
201 virtual void OnRequest(GSocketEvent flags);
202 };
203
204 class WXDLLEXPORT wxSocketEvent : public wxEvent {
205 DECLARE_DYNAMIC_CLASS(wxSocketEvent)
206 public:
207 wxSocketEvent(int id = 0);
208
209 GSocketEvent SocketEvent() const { return m_skevt; }
210 wxSocketBase *Socket() const { return m_socket; }
211
212 void CopyObject(wxObject& obj_d) const;
213
214 public:
215 GSocketEvent m_skevt;
216 wxSocketBase *m_socket;
217 };
218
219 typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
220
221 #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \
222 (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \
223 (wxObject *) NULL },
224
225 #endif
226 // wxUSE_SOCKETS
227
228 #endif
229 // _WX_NETWORK_SOCKET_H