| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: socket.h |
| 3 | // Purpose: Socket handling classes |
| 4 | // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia |
| 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 | |
| 27 | #ifdef WXPREC |
| 28 | # include "wx/wxprec.h" |
| 29 | #else |
| 30 | # include "wx/event.h" |
| 31 | # include "wx/string.h" |
| 32 | #endif |
| 33 | |
| 34 | #include "wx/sckaddr.h" |
| 35 | #include "wx/gsocket.h" |
| 36 | |
| 37 | // ------------------------------------------------------------------------ |
| 38 | // constants |
| 39 | // ------------------------------------------------------------------------ |
| 40 | |
| 41 | enum wxSocketNotify |
| 42 | { |
| 43 | wxSOCKET_INPUT = GSOCK_INPUT, |
| 44 | wxSOCKET_OUTPUT = GSOCK_OUTPUT, |
| 45 | wxSOCKET_CONNECTION = GSOCK_CONNECTION, |
| 46 | wxSOCKET_LOST = GSOCK_LOST |
| 47 | }; |
| 48 | |
| 49 | enum |
| 50 | { |
| 51 | wxSOCKET_INPUT_FLAG = GSOCK_INPUT_FLAG, |
| 52 | wxSOCKET_OUTPUT_FLAG = GSOCK_OUTPUT_FLAG, |
| 53 | wxSOCKET_CONNECTION_FLAG = GSOCK_CONNECTION_FLAG, |
| 54 | wxSOCKET_LOST_FLAG = GSOCK_LOST_FLAG |
| 55 | }; |
| 56 | |
| 57 | typedef GSocketEventFlags wxSocketEventFlags; |
| 58 | |
| 59 | enum wxSocketError |
| 60 | { |
| 61 | wxSOCKET_NOERROR = GSOCK_NOERROR, |
| 62 | wxSOCKET_INPOP = GSOCK_INVOP, |
| 63 | wxSOCKET_IOERR = GSOCK_IOERR, |
| 64 | wxSOCKET_INVADDR = GSOCK_INVADDR, |
| 65 | wxSOCKET_INVSOCK = GSOCK_INVSOCK, |
| 66 | wxSOCKET_NOHOST = GSOCK_NOHOST, |
| 67 | wxSOCKET_INVPORT = GSOCK_INVPORT, |
| 68 | wxSOCKET_WOULDBLOCK = GSOCK_WOULDBLOCK, |
| 69 | wxSOCKET_TIMEDOUT = GSOCK_TIMEDOUT, |
| 70 | wxSOCKET_MEMERR = GSOCK_MEMERR, |
| 71 | wxSOCKET_BUSY |
| 72 | }; |
| 73 | |
| 74 | enum |
| 75 | { |
| 76 | wxSOCKET_NONE = 0, |
| 77 | wxSOCKET_NOWAIT = 1, |
| 78 | wxSOCKET_WAITALL = 2, |
| 79 | wxSOCKET_BLOCK = 4 |
| 80 | }; |
| 81 | |
| 82 | // Type of request |
| 83 | enum wxSockType |
| 84 | { |
| 85 | SOCK_CLIENT, |
| 86 | SOCK_SERVER, |
| 87 | /* SOCK_DGRAM, */ |
| 88 | SOCK_INTERNAL, |
| 89 | SOCK_UNINIT |
| 90 | }; |
| 91 | |
| 92 | typedef int wxSockFlags; |
| 93 | |
| 94 | // ------------------------------------------------------------------------ |
| 95 | // wxSocket base |
| 96 | // ------------------------------------------------------------------------ |
| 97 | |
| 98 | class WXDLLEXPORT wxTimer; |
| 99 | class WXDLLEXPORT wxSocketEvent; |
| 100 | class WXDLLEXPORT wxSocketBase : public wxEvtHandler |
| 101 | { |
| 102 | DECLARE_CLASS(wxSocketBase) |
| 103 | public: |
| 104 | |
| 105 | enum |
| 106 | { |
| 107 | NONE = wxSOCKET_NONE, |
| 108 | NOWAIT = wxSOCKET_NOWAIT, |
| 109 | WAITALL = wxSOCKET_WAITALL, |
| 110 | SPEED = wxSOCKET_BLOCK |
| 111 | }; |
| 112 | |
| 113 | typedef void (*wxSockCbk)(wxSocketBase& sock, wxSocketNotify evt, char *cdata); |
| 114 | |
| 115 | protected: |
| 116 | GSocket *m_socket; // GSocket |
| 117 | int m_id; // Socket id (for event handler) |
| 118 | |
| 119 | // Attributes |
| 120 | wxSockFlags m_flags; // wxSocket flags |
| 121 | wxSockType m_type; // wxSocket type |
| 122 | wxSocketEventFlags m_neededreq; // Event mask |
| 123 | bool m_notify_state; // Notify events to users? |
| 124 | |
| 125 | // State |
| 126 | bool m_connected; // Connected? |
| 127 | bool m_establishing; // Establishing connection? |
| 128 | bool m_reading; // Busy reading? |
| 129 | bool m_writing; // Busy writing? |
| 130 | bool m_error; // Did last IO call fail? |
| 131 | wxUint32 m_lcount; // Last IO transaction size |
| 132 | unsigned long m_timeout; // IO timeout value |
| 133 | wxList m_states; // Stack of states |
| 134 | bool m_interrupt; // Interrupt ongoing wait operations |
| 135 | |
| 136 | // Pushback buffer |
| 137 | char *m_unread; // Pushback buffer |
| 138 | wxUint32 m_unrd_size; // Pushback buffer size |
| 139 | wxUint32 m_unrd_cur; // Pushback pointer (index into buffer) |
| 140 | |
| 141 | // Callback |
| 142 | wxSockCbk m_cbk; // C callback |
| 143 | char *m_cdata; // C callback data |
| 144 | |
| 145 | public: |
| 146 | wxSocketBase(); |
| 147 | virtual ~wxSocketBase(); |
| 148 | virtual bool Close(); |
| 149 | |
| 150 | // Base IO |
| 151 | wxSocketBase& Peek(char* buffer, wxUint32 nbytes); |
| 152 | wxSocketBase& Read(char* buffer, wxUint32 nbytes); |
| 153 | wxSocketBase& Write(const char *buffer, wxUint32 nbytes); |
| 154 | wxSocketBase& Unread(const char *buffer, wxUint32 nbytes); |
| 155 | wxSocketBase& ReadMsg(char *buffer, wxUint32 nbytes); |
| 156 | wxSocketBase& WriteMsg(const char *buffer, wxUint32 nbytes); |
| 157 | wxSocketBase& Discard(); |
| 158 | |
| 159 | // Status |
| 160 | inline bool Ok() const { return (m_socket != NULL); }; |
| 161 | inline bool Error() const { return m_error; }; |
| 162 | inline bool IsConnected() const { return m_connected; }; |
| 163 | inline bool IsDisconnected() const { return !IsConnected(); }; |
| 164 | inline bool IsNoWait() const { return ((m_flags & NOWAIT) != 0); }; |
| 165 | inline bool IsData() { return WaitForRead(0, 0); }; |
| 166 | inline wxUint32 LastCount() const { return m_lcount; } |
| 167 | inline wxSocketError LastError() const { return (wxSocketError)GSocket_GetError(m_socket); } |
| 168 | inline wxSockType GetType() const { return m_type; } |
| 169 | |
| 170 | // Addresses |
| 171 | virtual bool GetPeer(wxSockAddress& addr_man) const; |
| 172 | virtual bool GetLocal(wxSockAddress& addr_man) const; |
| 173 | |
| 174 | // Set attributes and flags |
| 175 | void SetTimeout(long seconds); |
| 176 | void SetFlags(wxSockFlags flags); |
| 177 | inline wxSockFlags GetFlags() const { return m_flags; }; |
| 178 | |
| 179 | /* Wait functions |
| 180 | * seconds = -1 means default timeout (change with SetTimeout) |
| 181 | * seconds, milliseconds = 0 means no wait |
| 182 | * seconds, milliseconds > 0 means specified wait |
| 183 | */ |
| 184 | bool Wait(long seconds = -1, long milliseconds = 0); |
| 185 | bool WaitForRead(long seconds = -1, long milliseconds = 0); |
| 186 | bool WaitForWrite(long seconds = -1, long milliseconds = 0); |
| 187 | bool WaitForLost(long seconds = -1, long milliseconds = 0); |
| 188 | |
| 189 | /* This function interrupts all ongoing wait operations for this |
| 190 | * socket; use it only as an escape mechanism (for example to close |
| 191 | * an app or to abort an operation). Reception of LOST events and |
| 192 | * calls to Close() automatically call this. |
| 193 | */ |
| 194 | void InterruptAllWaits() { m_interrupt = TRUE; }; |
| 195 | |
| 196 | // Save the current state of Socket |
| 197 | void SaveState(); |
| 198 | void RestoreState(); |
| 199 | |
| 200 | // Setup event handler |
| 201 | void SetEventHandler(wxEvtHandler& evt_hdlr, int id = -1); |
| 202 | |
| 203 | // Tell wxSocket which events to notify |
| 204 | void SetNotify(wxSocketEventFlags flags); |
| 205 | void Notify(bool notify); |
| 206 | static wxSocketEventFlags EventToNotify(wxSocketNotify evt); |
| 207 | inline wxSocketEventFlags NeededReq() const { return m_neededreq; } |
| 208 | |
| 209 | // External callback |
| 210 | wxSockCbk Callback(wxSockCbk cbk_); |
| 211 | char *CallbackData(char *data); |
| 212 | |
| 213 | // Public internal callback |
| 214 | virtual void OldOnNotify(wxSocketNotify WXUNUSED(evt)); |
| 215 | |
| 216 | // Do NOT use this function; it should be protected! |
| 217 | void OnRequest(wxSocketNotify req_evt); |
| 218 | |
| 219 | protected: |
| 220 | friend class wxSocketServer; |
| 221 | friend class wxSocketClient; |
| 222 | friend class wxSocketHandler; |
| 223 | |
| 224 | #ifdef __SALFORDC__ |
| 225 | public: |
| 226 | #endif |
| 227 | |
| 228 | wxSocketBase(wxSockFlags flags, wxSockType type); |
| 229 | |
| 230 | #ifdef __SALFORDC__ |
| 231 | protected: |
| 232 | #endif |
| 233 | |
| 234 | // Low level IO |
| 235 | wxUint32 _Read(char* buffer, wxUint32 nbytes); |
| 236 | wxUint32 _Write(const char *buffer, wxUint32 nbytes); |
| 237 | bool _Wait(long seconds, long milliseconds, wxSocketEventFlags flags); |
| 238 | |
| 239 | // Pushbacks |
| 240 | void Pushback(const char *buffer, wxUint32 size); |
| 241 | wxUint32 GetPushback(char *buffer, wxUint32 size, bool peek); |
| 242 | }; |
| 243 | |
| 244 | //////////////////////////////////////////////////////////////////////// |
| 245 | |
| 246 | class WXDLLEXPORT wxSocketServer : public wxSocketBase |
| 247 | { |
| 248 | DECLARE_CLASS(wxSocketServer) |
| 249 | public: |
| 250 | // 'service' can be a name or a port-number |
| 251 | |
| 252 | wxSocketServer(wxSockAddress& addr_man, wxSockFlags flags = wxSOCKET_NONE); |
| 253 | |
| 254 | wxSocketBase* Accept(bool wait = TRUE); |
| 255 | bool AcceptWith(wxSocketBase& sock, bool wait = TRUE); |
| 256 | |
| 257 | bool WaitForAccept(long seconds = -1, long milliseconds = 0); |
| 258 | }; |
| 259 | |
| 260 | //////////////////////////////////////////////////////////////////////// |
| 261 | |
| 262 | class WXDLLEXPORT wxSocketClient : public wxSocketBase |
| 263 | { |
| 264 | DECLARE_CLASS(wxSocketClient) |
| 265 | public: |
| 266 | |
| 267 | wxSocketClient(wxSockFlags flags = wxSOCKET_NONE); |
| 268 | virtual ~wxSocketClient(); |
| 269 | |
| 270 | virtual bool Connect(wxSockAddress& addr_man, bool wait = TRUE); |
| 271 | |
| 272 | bool WaitOnConnect(long seconds = -1, long milliseconds = 0); |
| 273 | }; |
| 274 | |
| 275 | class WXDLLEXPORT wxSocketEvent : public wxEvent { |
| 276 | DECLARE_DYNAMIC_CLASS(wxSocketEvent) |
| 277 | public: |
| 278 | wxSocketEvent(int id = 0); |
| 279 | |
| 280 | wxSocketNotify SocketEvent() const { return m_skevt; } |
| 281 | wxSocketBase *Socket() const { return m_socket; } |
| 282 | |
| 283 | void CopyObject(wxObject& obj_d) const; |
| 284 | |
| 285 | public: |
| 286 | wxSocketNotify m_skevt; |
| 287 | wxSocketBase *m_socket; |
| 288 | }; |
| 289 | |
| 290 | typedef void (wxEvtHandler::*wxSocketEventFunction)(wxSocketEvent&); |
| 291 | |
| 292 | #define EVT_SOCKET(id, func) { wxEVT_SOCKET, id, -1, \ |
| 293 | (wxObjectEventFunction) (wxEventFunction) (wxSocketEventFunction) & func, \ |
| 294 | (wxObject *) NULL }, |
| 295 | |
| 296 | #endif |
| 297 | // wxUSE_SOCKETS |
| 298 | |
| 299 | #endif |
| 300 | // _WX_NETWORK_SOCKET_H |