From: Vadim Zeitlin Date: Mon, 22 Apr 2013 10:45:30 +0000 (+0000) Subject: Add wxSocketBase::GetSocket(). X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/0623b0f0014f99d327d293c772260bfca0fd91e0 Add wxSocketBase::GetSocket(). Provide direct access to the underlying socket descriptor. Closes #8829. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@73837 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/changes.txt b/docs/changes.txt index 4b80f44e18..147ad8a039 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -567,6 +567,7 @@ All: - Add wxDateTime::DiffAsDateSpan() and wxDateSpan::GetTotalMonths() (jonasr). - Add wxVector::assign() (Jonas Rydberg). - Add wx[F]File{Input,Output}Stream::GetFile() (troelsk). +- Add wxSocketBase::GetSocket() (Laurent Poujoulat). - Add Nepali translation (Him Prasad Gautam). All (GUI): diff --git a/include/wx/private/socket.h b/include/wx/private/socket.h index 78e3e6351a..297c40d176 100644 --- a/include/wx/private/socket.h +++ b/include/wx/private/socket.h @@ -79,10 +79,6 @@ // define some symbols which winsock.h defines but traditional BSD headers // don't -#ifndef __WINDOWS__ - #define SOCKET int -#endif - #ifndef INVALID_SOCKET #define INVALID_SOCKET (-1) #endif @@ -293,7 +289,7 @@ public: // TODO: make these fields protected and provide accessors for those of // them that wxSocketBase really needs //protected: - SOCKET m_fd; + wxSOCKET_T m_fd; int m_initialRecvBufferSize; int m_initialSendBufferSize; diff --git a/include/wx/socket.h b/include/wx/socket.h index 93b39325da..0d0a0d7b5c 100644 --- a/include/wx/socket.h +++ b/include/wx/socket.h @@ -30,6 +30,16 @@ class wxSocketImpl; // Types and constants // ------------------------------------------------------------------------ +// Define the type of native sockets. +#if defined(__WINDOWS__) + // Although socket descriptors are still 32 bit values, even under Win64, + // the socket type is 64 bit there. + typedef wxUIntPtr wxSOCKET_T; +#else + typedef int wxSOCKET_T; +#endif + + // Types of different socket notifications or events. // // NB: the values here should be consecutive and start with 0 as they are @@ -187,6 +197,9 @@ public: void SetNotify(wxSocketEventFlags flags); void Notify(bool notify); + // Get the underlying socket descriptor. + wxSOCKET_T GetSocket() const; + // initialize/shutdown the sockets (done automatically so there is no need // to call these functions usually) // diff --git a/interface/wx/socket.h b/interface/wx/socket.h index d89f21944f..6c2f80060d 100644 --- a/interface/wx/socket.h +++ b/interface/wx/socket.h @@ -7,6 +7,16 @@ ///////////////////////////////////////////////////////////////////////////// +/** + The type of the native socket. + + Notice that the definition below is simplified and this type is not always + int, e.g. it is a 64 bit integer type under Win64. + + @since 2.9.5 + */ +typedef int wxSOCKET_T; + /** @class wxIPaddress @@ -1387,6 +1397,25 @@ public: */ void SetNotify(wxSocketEventFlags flags); + /** + Returns the native socket descriptor. + + This is intended to use with rarely used specific platform features + that can only be accessed via the actual socket descriptor. + + Do not use this for reading or writing data from or to the socket as + this would almost surely interfere with wxSocket code logic and result + in unexpected behaviour. + + The socket must be successfully initialized, e.g. connected for client + sockets, before this method can be called. + + @return Returns the native socket descriptor. + + @since 2.9.5 + */ + wxSOCKET_T GetSocket() const; + //@} }; diff --git a/src/common/socket.cpp b/src/common/socket.cpp index 699d2881a1..545a68d54f 100644 --- a/src/common/socket.cpp +++ b/src/common/socket.cpp @@ -526,7 +526,7 @@ wxSocketImpl *wxSocketImpl::Accept(wxSocketBase& wxsocket) { wxSockAddressStorage from; WX_SOCKLEN_T fromlen = sizeof(from); - const SOCKET fd = accept(m_fd, &from.addr, &fromlen); + const wxSOCKET_T fd = accept(m_fd, &from.addr, &fromlen); // accepting is similar to reading in the sense that it resets "ready for // read" flag on the socket @@ -1826,9 +1826,9 @@ wxSocketServer::wxSocketServer(const wxSockAddress& addr, return; } - // Notice that we need a cast as SOCKET is 64 bit under Win64 and that the - // cast is safe because a SOCKET is a handle and so limited to 32 (or, - // actually, even 24) bit values anyhow. + // Notice that we need a cast as wxSOCKET_T is 64 bit under Win64 and that + // the cast is safe because a wxSOCKET_T is a handle and so limited to 32 + // (or, actually, even 24) bit values anyhow. wxLogTrace( wxTRACE_Socket, wxT("wxSocketServer on fd %u"), static_cast(m_impl->m_fd) ); } @@ -1894,6 +1894,14 @@ bool wxSocketServer::WaitForAccept(long seconds, long milliseconds) return DoWait(seconds, milliseconds, wxSOCKET_CONNECTION_FLAG) == 1; } +wxSOCKET_T wxSocketBase::GetSocket() const +{ + wxASSERT_MSG( m_impl, wxS("Socket not initialised") ); + + return m_impl->m_fd; +} + + bool wxSocketBase::GetOption(int level, int optname, void *optval, int *optlen) { wxASSERT_MSG( m_impl, wxT("Socket not initialised") );