X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b67397a779eef3fb22ee143c8bd4e027c8c5dfc8..c29c95fe24973b94fd724db767193171ca7c513d:/interface/wx/socket.h diff --git a/interface/wx/socket.h b/interface/wx/socket.h index d6cd5ccce9..43d3c753c2 100644 --- a/interface/wx/socket.h +++ b/interface/wx/socket.h @@ -3,9 +3,101 @@ // Purpose: interface of wxIP*address, wxSocket* classes // Author: wxWidgets team // RCS-ID: $Id$ -// Licence: wxWindows license +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// + +/** + @class wxIPaddress + + wxIPaddress is an abstract base class for all internet protocol address + objects. Currently, only wxIPV4address is implemented. An experimental + implementation for IPV6, wxIPV6address, is being developed. + + @library{wxbase} + @category{net} +*/ +class wxIPaddress : public wxSockAddress +{ +public: + /** + Internally, this is the same as setting the IP address to @b INADDR_ANY. + + On IPV4 implementations, 0.0.0.0 + + On IPV6 implementations, :: + + @return @true on success, @false if something went wrong. + */ + bool AnyAddress(); + + /** + Internally, this is the same as setting the IP address to @b INADDR_BROADCAST. + + On IPV4 implementations, 255.255.255.255 + + @return @true on success, @false if something went wrong. + */ + virtual bool BroadcastAddress() = 0; + + /** + Set the address to hostname, which can be a host name or an IP-style address + in a format dependent on implementation. + + @return @true on success, @false if something goes wrong (invalid + hostname or invalid IP address). + */ + bool Hostname(const wxString& hostname); + + /** + Returns the hostname which matches the IP address. + */ + wxString Hostname() const; + + /** + Returns a wxString containing the IP address. + */ + virtual wxString IPAddress() const = 0; + + /** + Determines if current address is set to localhost. + + @return @true if address is localhost, @false if internet address. + */ + virtual bool IsLocalHost() const = 0; + + /** + Set address to localhost. + + On IPV4 implementations, 127.0.0.1 + + On IPV6 implementations, ::1 + + @return @true on success, @false if something went wrong. + */ + bool LocalHost(); + + /** + Set the port to that corresponding to the specified service. + + @return @true on success, @false if something goes wrong (invalid @a service). + */ + bool Service(const wxString& service); + + /** + Set the port to that corresponding to the specified service. + + @return @true on success, @false if something goes wrong (invalid @a service). + */ + bool Service(unsigned short service); + + /** + Returns the current service. + */ + unsigned short Service() const; +}; + + /** @class wxIPV4address @@ -70,12 +162,12 @@ public: @return @true on success, @false if something goes wrong (invalid @a service). */ - bool Service(unsigned short service) = 0; + bool Service(unsigned short service); /** Returns the current service. */ - unsigned short Service() const = 0; + unsigned short Service() const; }; @@ -118,7 +210,7 @@ public: accepted, it will wait for the next incoming connection to arrive. - @warning: This method will block the GUI. + @warning This method will block the GUI. If @a wait is @false, it will try to accept a pending connection if there is one, but it will always return immediately without blocking @@ -154,7 +246,7 @@ public: bool AcceptWith(wxSocketBase& socket, bool wait = true); /** - This function waits for an incoming connection. + Wait for an incoming connection. Use it if you want to call Accept() or AcceptWith() with @e wait set to @false, to detect when an incoming connection is waiting to be accepted. @@ -174,99 +266,6 @@ public: }; - -/** - @class wxIPaddress - - wxIPaddress is an abstract base class for all internet protocol address - objects. Currently, only wxIPV4address is implemented. An experimental - implementation for IPV6, wxIPV6address, is being developed. - - @library{wxbase} - @category{net} -*/ -class wxIPaddress : public wxSockAddress -{ -public: - /** - Internally, this is the same as setting the IP address to @b INADDR_ANY. - - On IPV4 implementations, 0.0.0.0 - - On IPV6 implementations, :: - - @return @true on success, @false if something went wrong. - */ - virtual bool AnyAddress() = 0; - - /** - Internally, this is the same as setting the IP address to @b INADDR_BROADCAST. - - On IPV4 implementations, 255.255.255.255 - - @return @true on success, @false if something went wrong. - */ - virtual bool BroadcastAddress() = 0; - - /** - Set the address to hostname, which can be a host name or an IP-style address - in a format dependent on implementation. - - @return @true on success, @false if something goes wrong (invalid - hostname or invalid IP address). - */ - virtual bool Hostname(const wxString& hostname) = 0; - - /** - Returns the hostname which matches the IP address. - */ - virtual wxString Hostname() const = 0; - - /** - Returns a wxString containing the IP address. - */ - virtual wxString IPAddress() const = 0; - - /** - Determines if current address is set to localhost. - - @return @true if address is localhost, @false if internet address. - */ - virtual bool IsLocalHost() const = 0; - - /** - Set address to localhost. - - On IPV4 implementations, 127.0.0.1 - - On IPV6 implementations, ::1 - - @return @true on success, @false if something went wrong. - */ - virtual bool LocalHost() = 0; - - /** - Set the port to that corresponding to the specified service. - - @return @true on success, @false if something goes wrong (invalid @a service). - */ - virtual bool Service(const wxString& service) = 0; - - /** - Set the port to that corresponding to the specified service. - - @return @true on success, @false if something goes wrong (invalid @a service). - */ - virtual bool Service(unsigned short service) = 0; - - /** - Returns the current service. - */ - virtual unsigned short Service() const = 0; -}; - - - /** @class wxSocketClient @@ -297,7 +296,7 @@ public: If @a wait is @true, Connect() will wait until the connection completes. - @warning: This method will block the GUI. + @warning This method will block the GUI. If @a wait is @false, Connect() will try to establish the connection and return immediately, without blocking the GUI. When used this way, @@ -428,6 +427,26 @@ public: Returns the length of the socket address. */ int SockAddrLen(); + + /** + Returns the pointer to the low-level representation of the address. + + This can be used to pass socket address information to a 3rd party + library. + + @return + Pointer to a sockaddr-derived struct. + */ + const sockaddr *GetAddressData() const; + + /** + Returns the length of the buffer retrieved by GetAddressData(). + + @return + The size of the sockaddr-derived struct corresponding to this + address. + */ + int GetAddressDataLen() const; }; @@ -436,10 +455,12 @@ public: @class wxSocketEvent This event class contains information about socket events. + This kind of events are sent to the event handler specified with + wxSocketBase::SetEventHandler. @beginEventTable{wxSocketEvent} @event{EVT_SOCKET(id, func)} - Process a socket event, supplying the member function. + Process a socket event, supplying the member function. @endEventTable @library{wxnet} @@ -575,7 +596,7 @@ enum wxSocketEventFlags @b wxSOCKET_REUSEADDR can also be used with socket clients to (re)bind to a particular local port for an outgoing connection. - This option can have surprising platform dependent behavior, so check the + This option can have surprising platform dependent behaviour, so check the documentation for your platform's implementation of setsockopt(). Note that on BSD-based systems(e.g. Mac OS X), use of @@ -595,7 +616,7 @@ enum wxSocketEventFlags the data. - @b wxSOCKET_BLOCK has nothing to do with the previous flags and it controls whether the GUI blocks. - - @b wxSOCKET_REUSEADDR controls special platform-specific behavior for + - @b wxSOCKET_REUSEADDR controls special platform-specific behaviour for reusing local addresses/ports. */ enum @@ -604,7 +625,7 @@ enum wxSOCKET_NOWAIT = 1, ///< Read/write as much data as possible and return immediately. wxSOCKET_WAITALL = 2, ///< Wait for all required data to be read/written unless an error occurs. wxSOCKET_BLOCK = 4, ///< Block the GUI (do not yield) while reading/writing data. - wxSOCKET_REUSEADDR = 8, ///< Allows the use of an in-use port (wxServerSocket only) + wxSOCKET_REUSEADDR = 8, ///< Allows the use of an in-use port. wxSOCKET_BROADCAST = 16, ///< Switches the socket to broadcast mode wxSOCKET_NOBIND = 32 ///< Stops the socket from being bound to a specific ///< adapter (normally used in conjunction with @@ -619,14 +640,11 @@ enum defines all basic IO functionality. @note - (Workaround for implementation limitation for wxWidgets up to 2.5.x) - If you want to use sockets or derived classes such as wxFTP in a secondary - thread, call @b wxSocketBase::Initialize() (undocumented) from the main - thread before creating any sockets - in wxApp::OnInit() for example. - See http://wiki.wxwidgets.org/wiki.pl?WxSocket or - http://www.litwindow.com/knowhow/knowhow.html for more details. + When using wxSocket from multiple threads, even implicitly (e.g. by using + wxFTP or wxHTTP in another thread) you must initialize the sockets from the + main thread by calling Initialize() before creating the other ones. - @beginEventTable{wxSocketEvent} + @beginEventEmissionTable{wxSocketEvent} @event{EVT_SOCKET(id, func)} Process a @c wxEVT_SOCKET event. See @ref wxSocketEventFlags and @ref wxSocketFlags for more info. @@ -661,7 +679,7 @@ public: Do not destroy a socket using the delete operator directly; use Destroy() instead. Also, do not create socket objects in the stack. */ - ~wxSocketBase(); + virtual ~wxSocketBase(); /** Destroys the socket safely. @@ -678,6 +696,39 @@ public: */ bool Destroy(); + /** + Perform the initialization needed in order to use the sockets. + + This function is called from wxSocket constructor implicitly and so + normally doesn't need to be called explicitly. There is however one + important exception: as this function must be called from the main + (UI) thread, if you use wxSocket from multiple threads you must call + Initialize() from the main thread before creating wxSocket objects in + the other ones. + + It is safe to call this function multiple times (only the first call + does anything) but you must call Shutdown() exactly once for every call + to Initialize(). + + This function should only be called from the main thread. + + @return + @true if the sockets can be used, @false if the initialization + failed and sockets are not available at all. + */ + static bool Initialize(); + + /** + Shut down the sockets. + + This function undoes the call to Initialize() and must be called after + every successful call to Initialize(). + + This function should only be called from the main thread, just as + Initialize(). + */ + static void Shutdown(); + //@} @@ -695,22 +746,18 @@ public: bool Error() const; /** - This function returns the local address field of the socket. The local - address field contains the complete local address of the socket (local - address, local port, ...). + Return the local address of the socket. @return @true if no error happened, @false otherwise. */ - bool GetLocal(wxSockAddress& addr) const; + virtual bool GetLocal(wxSockAddress& addr) const; /** - This function returns the peer address field of the socket. The peer - address field contains the complete peer host address of the socket - (address, port, ...). + Return the peer address field of the socket. @return @true if no error happened, @false otherwise. */ - bool GetPeer(wxSockAddress& addr) const; + virtual bool GetPeer(wxSockAddress& addr) const; /** Return the socket timeout in seconds. @@ -725,14 +772,14 @@ public: bool IsConnected() const; /** - This function waits until the socket is readable. + Check if the socket can be currently read or written. This might mean that queued data is available for reading or, for streamed sockets, that the connection has been closed, so that a read operation will complete immediately without blocking (unless the @b wxSOCKET_WAITALL flag is set, in which case the operation might still block). */ - bool IsData() const; + bool IsData(); /** Returns @true if the socket is not connected. @@ -773,8 +820,7 @@ public: wxSocketError LastError() const; /** - This function restores the previous state of the socket, as saved - with SaveState(). + Restore the previous state of the socket, as saved with SaveState(). Calls to SaveState() and RestoreState() can be nested. @@ -783,7 +829,8 @@ public: void RestoreState(); /** - This function saves the current state of the socket in a stack. + Save the current state of the socket in a stack. + Socket state includes flags, as set with SetFlags(), event mask, as set with SetNotify() and Notify(), user data, as set with SetClientData(). Calls to SaveState and RestoreState can be nested. @@ -803,9 +850,9 @@ public: //@{ /** - This function shuts down the socket, disabling further transmission and - reception of data; it also disables events for the socket and frees the - associated system resources. + Shut down the socket, disabling further transmission and reception of + data and disable events for the socket and frees the associated system + resources. Upon socket destruction, Close() is automatically called, so in most cases you won't need to do it yourself, unless you explicitly want to shut down @@ -817,7 +864,7 @@ public: The application must therefore be prepared to handle socket event messages even after calling Close(). */ - void Close(); + virtual bool Close(); /** Shuts down the writing end of the socket. @@ -829,14 +876,16 @@ public: void ShutdownOutput(); /** - This function simply deletes all bytes in the incoming queue. This function - always returns immediately and its operation is not affected by IO flags. + Delete all bytes in the incoming queue. + + This function always returns immediately and its operation is not + affected by IO flags. Use LastCount() to verify the number of bytes actually discarded. If you use Error(), it will always return @false. */ - wxSocketBase Discard(); + wxSocketBase& Discard(); /** Returns current IO flags, as set with SetFlags() @@ -859,9 +908,11 @@ public: void InterruptWait(); /** - This function peeks a buffer of @a nbytes bytes from the socket. + Peek into the socket by copying the next bytes which would be read by + Read() into the provided buffer. - Peeking a buffer doesn't delete it from the socket input queue. + Peeking a buffer doesn't delete it from the socket input queue, i.e. + calling Read() will return the same data. Use LastCount() to verify the number of bytes actually peeked. @@ -880,10 +931,11 @@ public: @see Error(), LastError(), LastCount(), SetFlags() */ - wxSocketBase Peek(void* buffer, wxUint32 nbytes); + wxSocketBase& Peek(void* buffer, wxUint32 nbytes); /** - This function reads a buffer of @a nbytes bytes from the socket. + Read up to the given number of bytes from the socket. + Use LastCount() to verify the number of bytes actually read. Use Error() to determine if the operation succeeded. @@ -901,13 +953,14 @@ public: @see Error(), LastError(), LastCount(), SetFlags() */ - wxSocketBase Read(void* buffer, wxUint32 nbytes); + wxSocketBase& Read(void* buffer, wxUint32 nbytes); /** - This function reads a buffer sent by WriteMsg() - on a socket. If the buffer passed to the function isn't big enough, the - remaining bytes will be discarded. This function always waits for the - buffer to be entirely filled, unless an error occurs. + Receive a message sent by WriteMsg(). + + If the buffer passed to the function isn't big enough, the remaining + bytes will be discarded. This function always waits for the buffer to + be entirely filled, unless an error occurs. Use LastCount() to verify the number of bytes actually read. @@ -928,22 +981,34 @@ public: @see Error(), LastError(), LastCount(), SetFlags(), WriteMsg() */ - wxSocketBase ReadMsg(void* buffer, wxUint32 nbytes); + wxSocketBase& ReadMsg(void* buffer, wxUint32 nbytes); /** Use SetFlags to customize IO operation for this socket. + The @a flags parameter may be a combination of flags ORed together. - The following flags can be used: + Notice that not all combinations of flags affecting the IO calls + (Read() and Write()) make sense, e.g. @b wxSOCKET_NOWAIT can't be + combined with @b wxSOCKET_WAITALL nor with @b wxSOCKET_BLOCK. + The following flags can be used: @beginFlagTable @flag{wxSOCKET_NONE} - Normal functionality. + Default mode: the socket will read some data in the IO calls and + will process events to avoid blocking UI while waiting for the data + to become available. @flag{wxSOCKET_NOWAIT} - Read/write as much data as possible and return immediately. + Don't wait for the socket to become ready in IO calls, read as much + data as is available -- potentially 0 bytes -- and return + immediately. @flag{wxSOCKET_WAITALL} - Wait for all required data to be read/written unless an error occurs. + Don't return before the entire amount of data specified in IO calls + is read or written unless an error occurs. If this flag is not + specified, the IO calls return as soon as any amount of data, even + less than the total number of bytes, is processed. @flag{wxSOCKET_BLOCK} - Block the GUI (do not yield) while reading/writing data. + Don't process the UI events while waiting for the socket to become + ready. This means that UI will be unresponsive during socket IO. @flag{wxSOCKET_REUSEADDR} Allows the use of an in-use port (wxServerSocket only). @flag{wxSOCKET_BROADCAST} @@ -958,24 +1023,29 @@ public: void SetFlags(wxSocketFlags flags); /** - This function allows you to set the local address and port, - useful when an application needs to reuse a particular port. When - a local port is set for a wxSocketClient, - @b bind() will be called before @b connect(). + Set the local address and port to use. + + This function must always be called for the server sockets but may also + be called for client sockets, if it is, @b bind() is called before @b + connect(). */ - bool SetLocal(const wxIPV4address& local); + virtual bool SetLocal(const wxIPV4address& local); /** - This function sets the default socket timeout in seconds. This timeout - applies to all IO calls, and also to the Wait() family - of functions if you don't specify a wait interval. Initially, the default + Set the default socket timeout in seconds. + + This timeout applies to all IO calls, and also to the Wait() family of + functions if you don't specify a wait interval. Initially, the default timeout is 10 minutes. */ - void SetTimeout(int seconds); + void SetTimeout(long seconds); /** - This function unreads a buffer. That is, the data in the buffer is put back - in the incoming queue. This function is not affected by wxSocket flags. + Put the specified data into the input queue. + + The data in the buffer will be returned by the next call to Read(). + + This function is not affected by wxSocket flags. If you use LastCount(), it will always return @a nbytes. @@ -990,19 +1060,20 @@ public: @see Error(), LastCount(), LastError() */ - wxSocketBase Unread(const void* buffer, wxUint32 nbytes); + wxSocketBase& Unread(const void* buffer, wxUint32 nbytes); /** - This function waits until any of the following conditions is @true: + Wait for any socket event. + Possible socket events are: @li The socket becomes readable. @li The socket becomes writable. @li An ongoing connection request has completed (wxSocketClient only) @li An incoming connection request has arrived (wxSocketServer only) @li The connection has been closed. - Note that it is recommended to use the individual Wait functions - to wait for the required condition, instead of this one. + Note that it is recommended to use the individual @b WaitForXXX() + functions to wait for the required condition, instead of this one. @param seconds Number of seconds to wait. @@ -1011,8 +1082,9 @@ public: @param millisecond Number of milliseconds to wait. - @return Returns @true when any of the above conditions is satisfied, - @false if the timeout was reached. + @return + @true when any of the above conditions is satisfied or @false if the + timeout was reached. @see InterruptWait(), wxSocketServer::WaitForAccept(), WaitForLost(), WaitForRead(), @@ -1021,8 +1093,10 @@ public: bool Wait(long seconds = -1, long millisecond = 0); /** - This function waits until the connection is lost. This may happen if - the peer gracefully closes the connection or if the connection breaks. + Wait until the connection is lost. + + This may happen if the peer gracefully closes the connection or if the + connection breaks. @param seconds Number of seconds to wait. @@ -1039,13 +1113,16 @@ public: bool WaitForLost(long seconds = -1, long millisecond = 0); /** - This function waits until the socket is readable. + Wait until the socket is readable. This might mean that queued data is available for reading or, for streamed sockets, that the connection has been closed, so that a read operation will complete immediately without blocking (unless the @b wxSOCKET_WAITALL flag is set, in which case the operation might still block). + Notice that this function should not be called if there is already data + available for reading on the socket. + @param seconds Number of seconds to wait. If -1, it will wait for the default timeout, @@ -1060,13 +1137,16 @@ public: bool WaitForRead(long seconds = -1, long millisecond = 0); /** - This function waits until the socket becomes writable. + Wait until the socket becomes writable. This might mean that the socket is ready to send new data, or for streamed sockets, that the connection has been closed, so that a write operation is guaranteed to complete immediately (unless the @b wxSOCKET_WAITALL flag is set, in which case the operation might still block). + Notice that this function should not be called if the socket is already + writable. + @param seconds Number of seconds to wait. If -1, it will wait for the default timeout, @@ -1081,7 +1161,7 @@ public: bool WaitForWrite(long seconds = -1, long millisecond = 0); /** - This function writes a buffer of @a nbytes bytes to the socket. + Write up to the given number of bytes to the socket. Use LastCount() to verify the number of bytes actually written. @@ -1101,14 +1181,16 @@ public: @see Error(), LastError(), LastCount(), SetFlags() */ - wxSocketBase Write(const void* buffer, wxUint32 nbytes); + wxSocketBase& Write(const void* buffer, wxUint32 nbytes); /** - This function writes a buffer of @a nbytes bytes from the socket, but it - writes a short header before so that ReadMsg() knows how much data should - it actually read. So, a buffer sent with WriteMsg() MUST be read with ReadMsg(). + Sends a buffer which can be read using ReadMsg(). + + WriteMsg() sends a short header before the data so that ReadMsg() + knows how much data should be actually read. - This function always waits for the entire buffer to be sent, unless an error occurs. + This function always waits for the entire buffer to be sent, unless an + error occurs. Use LastCount() to verify the number of bytes actually written. @@ -1131,7 +1213,7 @@ public: @see Error(), LastError(), LastCount(), SetFlags(), ReadMsg() */ - wxSocketBase WriteMsg(const void* buffer, wxUint32 nbytes); + wxSocketBase& WriteMsg(const void* buffer, wxUint32 nbytes); //@} @@ -1237,7 +1319,8 @@ public: virtual ~wxDatagramSocket(); /** - This function writes a buffer of @a nbytes bytes to the socket. + Write a buffer of @a nbytes bytes to the socket. + Use wxSocketBase::LastCount() to verify the number of bytes actually wrote. Use wxSocketBase::Error() to determine if the operation succeeded.