]> git.saurik.com Git - wxWidgets.git/blame - include/wx/socket.h
unused parameter warnings suppressed
[wxWidgets.git] / include / wx / socket.h
CommitLineData
f4ada568
GL
1/////////////////////////////////////////////////////////////////////////////
2// Name: socket.h
3// Purpose: Socket handling classes
fade627a 4// Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia
f4ada568
GL
5// Modified by:
6// Created: April 1997
7// RCS-ID: $Id$
8// Copyright: (c) Guilhem Lavaux
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
0c32066b 11
f4ada568
GL
12#ifndef _WX_NETWORK_SOCKET_H
13#define _WX_NETWORK_SOCKET_H
14
15#ifdef __GNUG__
0c32066b 16#pragma interface "socket.h"
f4ada568
GL
17#endif
18
ce4169a4
RR
19#include "wx/defs.h"
20
21#if wxUSE_SOCKETS
22
f4ada568
GL
23// ---------------------------------------------------------------------------
24// wxSocket headers (generic)
25// ---------------------------------------------------------------------------
26#ifdef WXPREC
ed58dbea 27# include "wx/wxprec.h"
f4ada568 28#else
ed58dbea
RR
29# include "wx/event.h"
30# include "wx/string.h"
f4ada568 31#endif
57dde4bd 32
ed58dbea 33#include "wx/sckaddr.h"
65ccd2b8 34#include "wx/gsocket.h"
f4ada568 35
aa8fb7a0
GL
36// ------------------------------------------------------------------------
37// GSocket type alias
38// ------------------------------------------------------------------------
39
40typedef enum {
41 wxSOCKET_INPUT = GSOCK_INPUT,
42 wxSOCKET_OUTPUT = GSOCK_OUTPUT,
43 wxSOCKET_CONNECTION = GSOCK_CONNECTION,
44 wxSOCKET_LOST = GSOCK_LOST
45} wxSocketNotify;
46
47enum {
48 wxSOCKET_INPUT_FLAG = GSOCK_INPUT_FLAG,
49 wxSOCKET_OUTPUT_FLAG = GSOCK_OUTPUT_FLAG,
50 wxSOCKET_CONNECTION_FLAG = GSOCK_CONNECTION_FLAG,
feeb8165 51 wxSOCKET_LOST_FLAG = GSOCK_LOST_FLAG
aa8fb7a0
GL
52};
53
54typedef GSocketEventFlags wxSocketEventFlags;
55
56typedef enum {
57 wxSOCKET_NOERROR = GSOCK_NOERROR,
58 wxSOCKET_INPOP = GSOCK_INVOP,
59 wxSOCKET_IOERR = GSOCK_IOERR,
60 wxSOCKET_INVADDR = GSOCK_INVADDR,
61 wxSOCKET_INVSOCK = GSOCK_INVSOCK,
62 wxSOCKET_NOHOST = GSOCK_NOHOST,
63 wxSOCKET_INVPORT = GSOCK_INVPORT,
64 wxSOCKET_WOULDBLOCK = GSOCK_WOULDBLOCK,
aa6d9706 65 wxSOCKET_TIMEDOUT = GSOCK_TIMEDOUT,
aa8fb7a0
GL
66 wxSOCKET_MEMERR = GSOCK_MEMERR
67} wxSocketError;
68
69// ------------------------------------------------------------------------
70// wxSocket base
71// ------------------------------------------------------------------------
72
39b91eca 73class WXDLLEXPORT wxTimer;
f4ada568 74class WXDLLEXPORT wxSocketEvent;
f4ada568
GL
75class WXDLLEXPORT wxSocketBase : public wxEvtHandler
76{
77 DECLARE_CLASS(wxSocketBase)
78public:
79
dbd300df
GL
80 enum { NONE=0, NOWAIT=1, WAITALL=2, SPEED=4 };
81 typedef int wxSockFlags;
f4ada568 82 // Type of request
f4ada568 83
65ccd2b8 84 enum wxSockType { SOCK_CLIENT, SOCK_SERVER, SOCK_INTERNAL, SOCK_UNINIT };
fade627a 85 typedef void (*wxSockCbk)(wxSocketBase& sock, wxSocketNotify evt, char *cdata);
f4ada568
GL
86
87protected:
a324a7bc
GL
88 GSocket *m_socket; // wxSocket socket
89 wxSockFlags m_flags; // wxSocket flags
f4ada568 90 wxSockType m_type; // wxSocket type
fade627a 91 wxSocketEventFlags m_neededreq; // Which events we are interested in
aa6d9706 92 wxUint32 m_lcount; // Last IO request size
a324a7bc 93 unsigned long m_timeout; // IO timeout value
35809fe3
GRG
94
95 char *m_unread; // Pushback buffer
aa6d9706
GL
96 wxUint32 m_unrd_size; // Pushback buffer size
97 wxUint32 m_unrd_cur; // Pushback pointer
a324a7bc 98
35809fe3
GRG
99 wxSockCbk m_cbk; // C callback
100 char *m_cdata; // C callback data
a324a7bc 101
35809fe3 102 bool m_connected; // Connected ?
7c395bf3 103 bool m_establishing; // Pending connections ?
a324a7bc 104 bool m_notify_state; // Notify state
35809fe3 105 int m_id; // Socket id (for event handler)
a324a7bc 106
39b91eca 107 // Defering variables
a324a7bc
GL
108 enum {
109 DEFER_READ, DEFER_WRITE, NO_DEFER
7c395bf3
GRG
110 } m_defering; // Defering state
111 char *m_defer_buffer; // Defering target buffer
112 wxUint32 m_defer_nbytes; // Defering buffer size
113 wxTimer *m_defer_timer; // Timer for defering mode
a324a7bc 114
7c395bf3
GRG
115 bool m_error; // Did an error occur in last IO call ?
116 wxList m_states; // Stack of states
65ccd2b8 117
f4ada568
GL
118public:
119 wxSocketBase();
120 virtual ~wxSocketBase();
121 virtual bool Close();
122
123 // Base IO
aa6d9706
GL
124 wxSocketBase& Peek(char* buffer, wxUint32 nbytes);
125 wxSocketBase& Read(char* buffer, wxUint32 nbytes);
126 wxSocketBase& Write(const char *buffer, wxUint32 nbytes);
127 wxSocketBase& Unread(const char *buffer, wxUint32 nbytes);
128 wxSocketBase& ReadMsg(char *buffer, wxUint32 nbytes);
129 wxSocketBase& WriteMsg(const char *buffer, wxUint32 nbytes);
f4ada568
GL
130 void Discard();
131
f4ada568 132 // Status
a324a7bc 133 inline bool Ok() const { return (m_socket != NULL); };
7c395bf3 134 inline bool Error() const { return m_error; };
f4ada568
GL
135 inline bool IsConnected() const { return m_connected; };
136 inline bool IsDisconnected() const { return !IsConnected(); };
a324a7bc 137 inline bool IsNoWait() const { return ((m_flags & NOWAIT) != 0); };
f4ada568 138 bool IsData() const;
aa6d9706 139 inline wxUint32 LastCount() const { return m_lcount; }
aa8fb7a0 140 inline wxSocketError LastError() const { return (wxSocketError)GSocket_GetError(m_socket); }
a737331d 141 inline wxSockType GetType() const { return m_type; }
65ccd2b8 142
7c395bf3
GRG
143 // Some info on the socket...
144 virtual bool GetPeer(wxSockAddress& addr_man) const;
145 virtual bool GetLocal(wxSockAddress& addr_man) const;
146
147 // Set attributes and flags
20e85460 148 void SetFlags(wxSockFlags _flags);
a737331d 149 wxSockFlags GetFlags() const;
7c395bf3 150 void SetTimeout(long seconds);
f4ada568 151
7c395bf3
GRG
152 // Wait functions
153 // seconds = -1 means default timeout (change with SetTimeout)
154 // seconds, milliseconds = 0 means no wait
155 // seconds, milliseconds > 0 means specified wait
aa6d9706
GL
156 bool Wait(long seconds = -1, long milliseconds = 0);
157 bool WaitForRead(long seconds = -1, long milliseconds = 0);
158 bool WaitForWrite(long seconds = -1, long milliseconds = 0);
159 bool WaitForLost(long seconds = -1, long milliseconds = 0);
65ccd2b8 160
f4ada568
GL
161 // Save the current state of Socket
162 void SaveState();
163 void RestoreState();
164
f4ada568
GL
165 // Setup event handler
166 void SetEventHandler(wxEvtHandler& evt_hdlr, int id = -1);
65ccd2b8 167
7c395bf3 168 // Tell wxSocket which events to notify
aa8fb7a0 169 void SetNotify(wxSocketEventFlags flags);
7c395bf3
GRG
170 void Notify(bool notify);
171 static wxSocketEventFlags EventToNotify(wxSocketNotify evt);
172 inline wxSocketEventFlags NeededReq() const { return m_neededreq; }
173
174 // External callback
175 wxSockCbk Callback(wxSockCbk cbk_);
176 char *CallbackData(char *data);
f4ada568
GL
177
178 // Public internal callback
aa8fb7a0 179 virtual void OldOnNotify(wxSocketNotify WXUNUSED(evt));
f4ada568 180
7c395bf3
GRG
181 // Do NOT use these functions; they should be protected!
182 void CreatePushbackAfter(const char *buffer, wxUint32 size);
183 void CreatePushbackBefore(const char *buffer, wxUint32 size);
184 void OnRequest(wxSocketNotify req_evt);
f4ada568
GL
185
186protected:
187 friend class wxSocketServer;
7c395bf3 188 friend class wxSocketClient;
f4ada568
GL
189 friend class wxSocketHandler;
190
ce3ed50d
JS
191#ifdef __SALFORDC__
192public:
193#endif
194
f4ada568 195 wxSocketBase(wxSockFlags flags, wxSockType type);
ce3ed50d
JS
196
197#ifdef __SALFORDC__
198protected:
199#endif
65ccd2b8 200
fade627a 201 bool _Wait(long seconds, long milliseconds, wxSocketEventFlags flags);
f4ada568 202
aa6d9706
GL
203 int DeferRead(char *buffer, wxUint32 nbytes);
204 int DeferWrite(const char *buffer, wxUint32 nbytes);
fade627a 205 void DoDefer();
65ccd2b8 206
f4ada568 207 // Pushback library
aa6d9706 208 wxUint32 GetPushback(char *buffer, wxUint32 size, bool peek);
f4ada568
GL
209};
210
211////////////////////////////////////////////////////////////////////////
212
213class WXDLLEXPORT wxSocketServer : public wxSocketBase
214{
215 DECLARE_CLASS(wxSocketServer)
216public:
f4ada568
GL
217 // 'service' can be a name or a port-number
218
219 wxSocketServer(wxSockAddress& addr_man, wxSockFlags flags = wxSocketBase::NONE);
220
35809fe3
GRG
221 wxSocketBase* Accept(bool wait = TRUE);
222 bool AcceptWith(wxSocketBase& sock, bool wait = TRUE);
223
7c395bf3 224 bool WaitForAccept(long seconds = -1, long milliseconds = 0);
f4ada568
GL
225};
226
227////////////////////////////////////////////////////////////////////////
228
229class WXDLLEXPORT wxSocketClient : public wxSocketBase
230{
231 DECLARE_CLASS(wxSocketClient)
232public:
233
234 wxSocketClient(wxSockFlags flags = wxSocketBase::NONE);
235 virtual ~wxSocketClient();
236
237 virtual bool Connect(wxSockAddress& addr_man, bool wait = TRUE);
238
aa6d9706 239 bool WaitOnConnect(long seconds = -1, long milliseconds = 0);
f4ada568
GL
240};
241
242class WXDLLEXPORT wxSocketEvent : public wxEvent {
243 DECLARE_DYNAMIC_CLASS(wxSocketEvent)
244public:
245 wxSocketEvent(int id = 0);
246
de3131e7 247 wxSocketNotify SocketEvent() const { return m_skevt; }
a737331d
GL
248 wxSocketBase *Socket() const { return m_socket; }
249
aadbdf11
GL
250 void CopyObject(wxObject& obj_d) const;
251
f4ada568 252public:
de3131e7 253 wxSocketNotify m_skevt;
a737331d 254 wxSocketBase *m_socket;
f4ada568
GL
255};
256
257typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&);
258
a737331d 259#define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \
db131261
RR
260 (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \
261 (wxObject *) NULL },
f4ada568
GL
262
263#endif
ce4169a4
RR
264 // wxUSE_SOCKETS
265
266#endif
267 // _WX_NETWORK_SOCKET_H