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
This commit is contained in:
Vadim Zeitlin
2013-04-22 10:45:30 +00:00
parent 98a177f45a
commit 0623b0f001
5 changed files with 56 additions and 9 deletions

View File

@@ -567,6 +567,7 @@ All:
- Add wxDateTime::DiffAsDateSpan() and wxDateSpan::GetTotalMonths() (jonasr). - Add wxDateTime::DiffAsDateSpan() and wxDateSpan::GetTotalMonths() (jonasr).
- Add wxVector::assign() (Jonas Rydberg). - Add wxVector::assign() (Jonas Rydberg).
- Add wx[F]File{Input,Output}Stream::GetFile() (troelsk). - Add wx[F]File{Input,Output}Stream::GetFile() (troelsk).
- Add wxSocketBase::GetSocket() (Laurent Poujoulat).
- Add Nepali translation (Him Prasad Gautam). - Add Nepali translation (Him Prasad Gautam).
All (GUI): All (GUI):

View File

@@ -79,10 +79,6 @@
// define some symbols which winsock.h defines but traditional BSD headers // define some symbols which winsock.h defines but traditional BSD headers
// don't // don't
#ifndef __WINDOWS__
#define SOCKET int
#endif
#ifndef INVALID_SOCKET #ifndef INVALID_SOCKET
#define INVALID_SOCKET (-1) #define INVALID_SOCKET (-1)
#endif #endif
@@ -293,7 +289,7 @@ public:
// TODO: make these fields protected and provide accessors for those of // TODO: make these fields protected and provide accessors for those of
// them that wxSocketBase really needs // them that wxSocketBase really needs
//protected: //protected:
SOCKET m_fd; wxSOCKET_T m_fd;
int m_initialRecvBufferSize; int m_initialRecvBufferSize;
int m_initialSendBufferSize; int m_initialSendBufferSize;

View File

@@ -30,6 +30,16 @@ class wxSocketImpl;
// Types and constants // 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. // Types of different socket notifications or events.
// //
// NB: the values here should be consecutive and start with 0 as they are // NB: the values here should be consecutive and start with 0 as they are
@@ -187,6 +197,9 @@ public:
void SetNotify(wxSocketEventFlags flags); void SetNotify(wxSocketEventFlags flags);
void Notify(bool notify); void Notify(bool notify);
// Get the underlying socket descriptor.
wxSOCKET_T GetSocket() const;
// initialize/shutdown the sockets (done automatically so there is no need // initialize/shutdown the sockets (done automatically so there is no need
// to call these functions usually) // to call these functions usually)
// //

View File

@@ -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 @class wxIPaddress
@@ -1387,6 +1397,25 @@ public:
*/ */
void SetNotify(wxSocketEventFlags flags); 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;
//@} //@}
}; };

View File

@@ -526,7 +526,7 @@ wxSocketImpl *wxSocketImpl::Accept(wxSocketBase& wxsocket)
{ {
wxSockAddressStorage from; wxSockAddressStorage from;
WX_SOCKLEN_T fromlen = sizeof(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 // accepting is similar to reading in the sense that it resets "ready for
// read" flag on the socket // read" flag on the socket
@@ -1826,9 +1826,9 @@ wxSocketServer::wxSocketServer(const wxSockAddress& addr,
return; return;
} }
// Notice that we need a cast as SOCKET is 64 bit under Win64 and that the // Notice that we need a cast as wxSOCKET_T is 64 bit under Win64 and that
// cast is safe because a SOCKET is a handle and so limited to 32 (or, // the cast is safe because a wxSOCKET_T is a handle and so limited to 32
// actually, even 24) bit values anyhow. // (or, actually, even 24) bit values anyhow.
wxLogTrace( wxTRACE_Socket, wxT("wxSocketServer on fd %u"), wxLogTrace( wxTRACE_Socket, wxT("wxSocketServer on fd %u"),
static_cast<unsigned>(m_impl->m_fd) ); static_cast<unsigned>(m_impl->m_fd) );
} }
@@ -1894,6 +1894,14 @@ bool wxSocketServer::WaitForAccept(long seconds, long milliseconds)
return DoWait(seconds, milliseconds, wxSOCKET_CONNECTION_FLAG) == 1; 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) bool wxSocketBase::GetOption(int level, int optname, void *optval, int *optlen)
{ {
wxASSERT_MSG( m_impl, wxT("Socket not initialised") ); wxASSERT_MSG( m_impl, wxT("Socket not initialised") );