The world should not end if a server has no peer. Let's return FALSE instead.
GAddress_copy should copy truthfully, not mangle the result if things are going badly. An earlier incarnation of the above. Disable events before closing the socket, though just disabling them at all will do. This was the cause of the nasty 'crash on fail to bind' behaviour that people have been seeing. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@16309 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -76,6 +76,8 @@
|
|||||||
#define PROCESS_EVENTS()
|
#define PROCESS_EVENTS()
|
||||||
#endif // wxUSE_GUI/!wxUSE_GUI
|
#endif // wxUSE_GUI/!wxUSE_GUI
|
||||||
|
|
||||||
|
#define wxTRACE_Socket _T("wxSocket")
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
// wxWin macros
|
// wxWin macros
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
@@ -777,6 +779,12 @@ bool wxSocketBase::GetPeer(wxSockAddress& addr_man) const
|
|||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
peer = GSocket_GetPeer(m_socket);
|
peer = GSocket_GetPeer(m_socket);
|
||||||
|
|
||||||
|
// copying a null address would just trigger an assert anyway
|
||||||
|
|
||||||
|
if (!peer)
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
addr_man.SetAddress(peer);
|
addr_man.SetAddress(peer);
|
||||||
GAddress_destroy(peer);
|
GAddress_destroy(peer);
|
||||||
|
|
||||||
@@ -1067,26 +1075,32 @@ wxSocketServer::wxSocketServer(wxSockAddress& addr_man,
|
|||||||
wxSocketFlags flags)
|
wxSocketFlags flags)
|
||||||
: wxSocketBase(flags, wxSOCKET_SERVER)
|
: wxSocketBase(flags, wxSOCKET_SERVER)
|
||||||
{
|
{
|
||||||
// Create the socket
|
wxLogTrace( wxTRACE_Socket, _T("Opening wxSocketServer") );
|
||||||
m_socket = GSocket_new();
|
|
||||||
|
|
||||||
if (!m_socket)
|
m_socket = GSocket_new();
|
||||||
return;
|
|
||||||
|
|
||||||
// Setup the socket as server
|
if (!m_socket)
|
||||||
GSocket_SetLocal(m_socket, addr_man.GetAddress());
|
{
|
||||||
if (GSocket_SetServer(m_socket) != GSOCK_NOERROR)
|
wxLogTrace( wxTRACE_Socket, _T("*** GSocket_new failed") );
|
||||||
{
|
return;
|
||||||
GSocket_destroy(m_socket);
|
}
|
||||||
m_socket = NULL;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
GSocket_SetTimeout(m_socket, m_timeout * 1000);
|
// Setup the socket as server
|
||||||
GSocket_SetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG |
|
|
||||||
GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG,
|
|
||||||
wx_socket_callback, (char *)this);
|
|
||||||
|
|
||||||
|
GSocket_SetLocal(m_socket, addr_man.GetAddress());
|
||||||
|
if (GSocket_SetServer(m_socket) != GSOCK_NOERROR)
|
||||||
|
{
|
||||||
|
GSocket_destroy(m_socket);
|
||||||
|
m_socket = NULL;
|
||||||
|
|
||||||
|
wxLogTrace( wxTRACE_Socket, _T("*** GSocket_SetServer failed") );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
GSocket_SetTimeout(m_socket, m_timeout * 1000);
|
||||||
|
GSocket_SetCallback(m_socket, GSOCK_INPUT_FLAG | GSOCK_OUTPUT_FLAG |
|
||||||
|
GSOCK_LOST_FLAG | GSOCK_CONNECTION_FLAG,
|
||||||
|
wx_socket_callback, (char *)this);
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
@@ -1308,3 +1322,5 @@ IMPLEMENT_DYNAMIC_CLASS(wxSocketModule, wxModule)
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
// wxUSE_SOCKETS
|
// wxUSE_SOCKETS
|
||||||
|
|
||||||
|
// vi:sts=4:sw=4:et
|
||||||
|
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#if wxUSE_SOCKETS
|
#if wxUSE_SOCKETS
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
@@ -80,6 +81,8 @@ void _GSocket_Uninstall_Callback(GSocket *socket, GSocketEvent event)
|
|||||||
gint *m_id = (gint *)(socket->m_gui_dependent);
|
gint *m_id = (gint *)(socket->m_gui_dependent);
|
||||||
int c;
|
int c;
|
||||||
|
|
||||||
|
assert( m_id != NULL );
|
||||||
|
|
||||||
switch (event)
|
switch (event)
|
||||||
{
|
{
|
||||||
case GSOCK_LOST: /* fall-through */
|
case GSOCK_LOST: /* fall-through */
|
||||||
|
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#if wxUSE_SOCKETS
|
#if wxUSE_SOCKETS
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
@@ -80,6 +81,8 @@ void _GSocket_Uninstall_Callback(GSocket *socket, GSocketEvent event)
|
|||||||
gint *m_id = (gint *)(socket->m_gui_dependent);
|
gint *m_id = (gint *)(socket->m_gui_dependent);
|
||||||
int c;
|
int c;
|
||||||
|
|
||||||
|
assert( m_id != NULL );
|
||||||
|
|
||||||
switch (event)
|
switch (event)
|
||||||
{
|
{
|
||||||
case GSOCK_LOST: /* fall-through */
|
case GSOCK_LOST: /* fall-through */
|
||||||
|
@@ -103,6 +103,13 @@ GSocket *GSocket_new(void)
|
|||||||
return socket;
|
return socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GSocket_close(GSocket *socket)
|
||||||
|
{
|
||||||
|
_GSocket_Disable_Events(socket);
|
||||||
|
closesocket(socket->m_fd);
|
||||||
|
socket->m_fd = INVALID_SOCKET;
|
||||||
|
}
|
||||||
|
|
||||||
void GSocket_destroy(GSocket *socket)
|
void GSocket_destroy(GSocket *socket)
|
||||||
{
|
{
|
||||||
assert(socket != NULL);
|
assert(socket != NULL);
|
||||||
@@ -139,8 +146,7 @@ void GSocket_Shutdown(GSocket *socket)
|
|||||||
if (socket->m_fd != INVALID_SOCKET)
|
if (socket->m_fd != INVALID_SOCKET)
|
||||||
{
|
{
|
||||||
shutdown(socket->m_fd, 2);
|
shutdown(socket->m_fd, 2);
|
||||||
closesocket(socket->m_fd);
|
GSocket_close(socket);
|
||||||
socket->m_fd = INVALID_SOCKET;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Disable GUI callbacks */
|
/* Disable GUI callbacks */
|
||||||
@@ -148,7 +154,6 @@ void GSocket_Shutdown(GSocket *socket)
|
|||||||
socket->m_cbacks[evt] = NULL;
|
socket->m_cbacks[evt] = NULL;
|
||||||
|
|
||||||
socket->m_detected = GSOCK_LOST_FLAG;
|
socket->m_detected = GSOCK_LOST_FLAG;
|
||||||
_GSocket_Disable_Events(socket);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Address handling */
|
/* Address handling */
|
||||||
@@ -325,8 +330,7 @@ GSocketError GSocket_SetServer(GSocket *sck)
|
|||||||
(SOCKLEN_T *)&sck->m_local->m_len) != 0) ||
|
(SOCKLEN_T *)&sck->m_local->m_len) != 0) ||
|
||||||
(listen(sck->m_fd, 5) != 0))
|
(listen(sck->m_fd, 5) != 0))
|
||||||
{
|
{
|
||||||
closesocket(sck->m_fd);
|
GSocket_close(sck);
|
||||||
sck->m_fd = INVALID_SOCKET;
|
|
||||||
sck->m_error = GSOCK_IOERR;
|
sck->m_error = GSOCK_IOERR;
|
||||||
return GSOCK_IOERR;
|
return GSOCK_IOERR;
|
||||||
}
|
}
|
||||||
@@ -508,8 +512,7 @@ GSocketError GSocket_Connect(GSocket *sck, GSocketStream stream)
|
|||||||
|
|
||||||
if (err != GSOCK_NOERROR)
|
if (err != GSOCK_NOERROR)
|
||||||
{
|
{
|
||||||
closesocket(sck->m_fd);
|
GSocket_close(sck);
|
||||||
sck->m_fd = INVALID_SOCKET;
|
|
||||||
/* sck->m_error is set in _GSocket_Connect_Timeout */
|
/* sck->m_error is set in _GSocket_Connect_Timeout */
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -532,8 +535,7 @@ GSocketError GSocket_Connect(GSocket *sck, GSocketStream stream)
|
|||||||
/* If connect failed with an error other than EWOULDBLOCK,
|
/* If connect failed with an error other than EWOULDBLOCK,
|
||||||
* then the call to GSocket_Connect() has failed.
|
* then the call to GSocket_Connect() has failed.
|
||||||
*/
|
*/
|
||||||
closesocket(sck->m_fd);
|
GSocket_close(sck);
|
||||||
sck->m_fd = INVALID_SOCKET;
|
|
||||||
sck->m_error = GSOCK_IOERR;
|
sck->m_error = GSOCK_IOERR;
|
||||||
return GSOCK_IOERR;
|
return GSOCK_IOERR;
|
||||||
}
|
}
|
||||||
@@ -597,8 +599,7 @@ GSocketError GSocket_SetNonOriented(GSocket *sck)
|
|||||||
sck->m_local->m_addr,
|
sck->m_local->m_addr,
|
||||||
(SOCKLEN_T *)&sck->m_local->m_len) != 0))
|
(SOCKLEN_T *)&sck->m_local->m_len) != 0))
|
||||||
{
|
{
|
||||||
closesocket(sck->m_fd);
|
GSocket_close(sck);
|
||||||
sck->m_fd = INVALID_SOCKET;
|
|
||||||
sck->m_error = GSOCK_IOERR;
|
sck->m_error = GSOCK_IOERR;
|
||||||
return GSOCK_IOERR;
|
return GSOCK_IOERR;
|
||||||
}
|
}
|
||||||
@@ -1380,3 +1381,5 @@ GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf)
|
|||||||
typedef void (*wxDummy)();
|
typedef void (*wxDummy)();
|
||||||
|
|
||||||
#endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
|
#endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
|
||||||
|
|
||||||
|
// vi:sts=4:sw=4:et
|
||||||
|
@@ -172,6 +172,13 @@ GSocket *GSocket_new(void)
|
|||||||
return socket;
|
return socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GSocket_close(GSocket *socket)
|
||||||
|
{
|
||||||
|
_GSocket_Disable_Events(socket);
|
||||||
|
close(socket->m_fd);
|
||||||
|
socket->m_fd = INVALID_SOCKET;
|
||||||
|
}
|
||||||
|
|
||||||
void GSocket_destroy(GSocket *socket)
|
void GSocket_destroy(GSocket *socket)
|
||||||
{
|
{
|
||||||
assert(socket != NULL);
|
assert(socket != NULL);
|
||||||
@@ -208,8 +215,7 @@ void GSocket_Shutdown(GSocket *socket)
|
|||||||
if (socket->m_fd != INVALID_SOCKET)
|
if (socket->m_fd != INVALID_SOCKET)
|
||||||
{
|
{
|
||||||
shutdown(socket->m_fd, 2);
|
shutdown(socket->m_fd, 2);
|
||||||
close(socket->m_fd);
|
GSocket_close(socket);
|
||||||
socket->m_fd = INVALID_SOCKET;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Disable GUI callbacks */
|
/* Disable GUI callbacks */
|
||||||
@@ -217,7 +223,6 @@ void GSocket_Shutdown(GSocket *socket)
|
|||||||
socket->m_cbacks[evt] = NULL;
|
socket->m_cbacks[evt] = NULL;
|
||||||
|
|
||||||
socket->m_detected = GSOCK_LOST_FLAG;
|
socket->m_detected = GSOCK_LOST_FLAG;
|
||||||
_GSocket_Disable_Events(socket);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Address handling */
|
/* Address handling */
|
||||||
@@ -396,8 +401,7 @@ GSocketError GSocket_SetServer(GSocket *sck)
|
|||||||
(SOCKLEN_T *) &sck->m_local->m_len) != 0) ||
|
(SOCKLEN_T *) &sck->m_local->m_len) != 0) ||
|
||||||
(listen(sck->m_fd, 5) != 0))
|
(listen(sck->m_fd, 5) != 0))
|
||||||
{
|
{
|
||||||
close(sck->m_fd);
|
GSocket_close(sck);
|
||||||
sck->m_fd = INVALID_SOCKET;
|
|
||||||
sck->m_error = GSOCK_IOERR;
|
sck->m_error = GSOCK_IOERR;
|
||||||
return GSOCK_IOERR;
|
return GSOCK_IOERR;
|
||||||
}
|
}
|
||||||
@@ -577,8 +581,7 @@ GSocketError GSocket_Connect(GSocket *sck, GSocketStream stream)
|
|||||||
{
|
{
|
||||||
if (_GSocket_Output_Timeout(sck) == GSOCK_TIMEDOUT)
|
if (_GSocket_Output_Timeout(sck) == GSOCK_TIMEDOUT)
|
||||||
{
|
{
|
||||||
close(sck->m_fd);
|
GSocket_close(sck);
|
||||||
sck->m_fd = INVALID_SOCKET;
|
|
||||||
/* sck->m_error is set in _GSocket_Output_Timeout */
|
/* sck->m_error is set in _GSocket_Output_Timeout */
|
||||||
return GSOCK_TIMEDOUT;
|
return GSOCK_TIMEDOUT;
|
||||||
}
|
}
|
||||||
@@ -610,8 +613,7 @@ GSocketError GSocket_Connect(GSocket *sck, GSocketStream stream)
|
|||||||
/* If connect failed with an error other than EINPROGRESS,
|
/* If connect failed with an error other than EINPROGRESS,
|
||||||
* then the call to GSocket_Connect has failed.
|
* then the call to GSocket_Connect has failed.
|
||||||
*/
|
*/
|
||||||
close(sck->m_fd);
|
GSocket_close(sck);
|
||||||
sck->m_fd = INVALID_SOCKET;
|
|
||||||
sck->m_error = GSOCK_IOERR;
|
sck->m_error = GSOCK_IOERR;
|
||||||
return GSOCK_IOERR;
|
return GSOCK_IOERR;
|
||||||
}
|
}
|
||||||
@@ -675,8 +677,7 @@ GSocketError GSocket_SetNonOriented(GSocket *sck)
|
|||||||
sck->m_local->m_addr,
|
sck->m_local->m_addr,
|
||||||
(SOCKLEN_T *) &sck->m_local->m_len) != 0))
|
(SOCKLEN_T *) &sck->m_local->m_len) != 0))
|
||||||
{
|
{
|
||||||
close(sck->m_fd);
|
GSocket_close(sck);
|
||||||
sck->m_fd = INVALID_SOCKET;
|
|
||||||
sck->m_error = GSOCK_IOERR;
|
sck->m_error = GSOCK_IOERR;
|
||||||
return GSOCK_IOERR;
|
return GSOCK_IOERR;
|
||||||
}
|
}
|
||||||
@@ -1301,7 +1302,7 @@ GAddress *GAddress_copy(GAddress *address)
|
|||||||
|
|
||||||
memcpy(addr2, address, sizeof(GAddress));
|
memcpy(addr2, address, sizeof(GAddress));
|
||||||
|
|
||||||
if (address->m_addr)
|
if (address->m_addr && address->m_len > 0)
|
||||||
{
|
{
|
||||||
addr2->m_addr = (struct sockaddr *)malloc(addr2->m_len);
|
addr2->m_addr = (struct sockaddr *)malloc(addr2->m_len);
|
||||||
if (addr2->m_addr == NULL)
|
if (addr2->m_addr == NULL)
|
||||||
@@ -1640,3 +1641,4 @@ GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf)
|
|||||||
|
|
||||||
#endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
|
#endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
|
||||||
|
|
||||||
|
// vi:sts=4:sw=4:et
|
||||||
|
Reference in New Issue
Block a user