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