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:
Ron Lee
2002-07-29 04:13:25 +00:00
parent 7af68c666b
commit 007c77ab4d
5 changed files with 66 additions and 39 deletions

View File

@@ -76,6 +76,8 @@
#define PROCESS_EVENTS()
#endif // wxUSE_GUI/!wxUSE_GUI
#define wxTRACE_Socket _T("wxSocket")
// --------------------------------------------------------------------------
// wxWin macros
// --------------------------------------------------------------------------
@@ -777,6 +779,12 @@ bool wxSocketBase::GetPeer(wxSockAddress& addr_man) const
return FALSE;
peer = GSocket_GetPeer(m_socket);
// copying a null address would just trigger an assert anyway
if (!peer)
return FALSE;
addr_man.SetAddress(peer);
GAddress_destroy(peer);
@@ -1067,18 +1075,25 @@ wxSocketServer::wxSocketServer(wxSockAddress& addr_man,
wxSocketFlags flags)
: wxSocketBase(flags, wxSOCKET_SERVER)
{
// Create the socket
wxLogTrace( wxTRACE_Socket, _T("Opening wxSocketServer") );
m_socket = GSocket_new();
if (!m_socket)
{
wxLogTrace( wxTRACE_Socket, _T("*** GSocket_new failed") );
return;
}
// Setup the socket as server
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;
}
@@ -1086,7 +1101,6 @@ wxSocketServer::wxSocketServer(wxSockAddress& addr_man,
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
// wxUSE_SOCKETS
// vi:sts=4:sw=4:et

View File

@@ -9,6 +9,7 @@
#if wxUSE_SOCKETS
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
@@ -80,6 +81,8 @@ void _GSocket_Uninstall_Callback(GSocket *socket, GSocketEvent event)
gint *m_id = (gint *)(socket->m_gui_dependent);
int c;
assert( m_id != NULL );
switch (event)
{
case GSOCK_LOST: /* fall-through */

View File

@@ -9,6 +9,7 @@
#if wxUSE_SOCKETS
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
@@ -80,6 +81,8 @@ void _GSocket_Uninstall_Callback(GSocket *socket, GSocketEvent event)
gint *m_id = (gint *)(socket->m_gui_dependent);
int c;
assert( m_id != NULL );
switch (event)
{
case GSOCK_LOST: /* fall-through */

View File

@@ -103,6 +103,13 @@ GSocket *GSocket_new(void)
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)
{
assert(socket != NULL);
@@ -139,8 +146,7 @@ void GSocket_Shutdown(GSocket *socket)
if (socket->m_fd != INVALID_SOCKET)
{
shutdown(socket->m_fd, 2);
closesocket(socket->m_fd);
socket->m_fd = INVALID_SOCKET;
GSocket_close(socket);
}
/* Disable GUI callbacks */
@@ -148,7 +154,6 @@ void GSocket_Shutdown(GSocket *socket)
socket->m_cbacks[evt] = NULL;
socket->m_detected = GSOCK_LOST_FLAG;
_GSocket_Disable_Events(socket);
}
/* Address handling */
@@ -325,8 +330,7 @@ GSocketError GSocket_SetServer(GSocket *sck)
(SOCKLEN_T *)&sck->m_local->m_len) != 0) ||
(listen(sck->m_fd, 5) != 0))
{
closesocket(sck->m_fd);
sck->m_fd = INVALID_SOCKET;
GSocket_close(sck);
sck->m_error = GSOCK_IOERR;
return GSOCK_IOERR;
}
@@ -508,8 +512,7 @@ GSocketError GSocket_Connect(GSocket *sck, GSocketStream stream)
if (err != GSOCK_NOERROR)
{
closesocket(sck->m_fd);
sck->m_fd = INVALID_SOCKET;
GSocket_close(sck);
/* 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,
* then the call to GSocket_Connect() has failed.
*/
closesocket(sck->m_fd);
sck->m_fd = INVALID_SOCKET;
GSocket_close(sck);
sck->m_error = GSOCK_IOERR;
return GSOCK_IOERR;
}
@@ -597,8 +599,7 @@ GSocketError GSocket_SetNonOriented(GSocket *sck)
sck->m_local->m_addr,
(SOCKLEN_T *)&sck->m_local->m_len) != 0))
{
closesocket(sck->m_fd);
sck->m_fd = INVALID_SOCKET;
GSocket_close(sck);
sck->m_error = GSOCK_IOERR;
return GSOCK_IOERR;
}
@@ -1380,3 +1381,5 @@ GSocketError GAddress_UNIX_GetPath(GAddress *address, char *path, size_t sbuf)
typedef void (*wxDummy)();
#endif /* wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__) */
// vi:sts=4:sw=4:et

View File

@@ -172,6 +172,13 @@ GSocket *GSocket_new(void)
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)
{
assert(socket != NULL);
@@ -208,8 +215,7 @@ void GSocket_Shutdown(GSocket *socket)
if (socket->m_fd != INVALID_SOCKET)
{
shutdown(socket->m_fd, 2);
close(socket->m_fd);
socket->m_fd = INVALID_SOCKET;
GSocket_close(socket);
}
/* Disable GUI callbacks */
@@ -217,7 +223,6 @@ void GSocket_Shutdown(GSocket *socket)
socket->m_cbacks[evt] = NULL;
socket->m_detected = GSOCK_LOST_FLAG;
_GSocket_Disable_Events(socket);
}
/* Address handling */
@@ -396,8 +401,7 @@ GSocketError GSocket_SetServer(GSocket *sck)
(SOCKLEN_T *) &sck->m_local->m_len) != 0) ||
(listen(sck->m_fd, 5) != 0))
{
close(sck->m_fd);
sck->m_fd = INVALID_SOCKET;
GSocket_close(sck);
sck->m_error = GSOCK_IOERR;
return GSOCK_IOERR;
}
@@ -577,8 +581,7 @@ GSocketError GSocket_Connect(GSocket *sck, GSocketStream stream)
{
if (_GSocket_Output_Timeout(sck) == GSOCK_TIMEDOUT)
{
close(sck->m_fd);
sck->m_fd = INVALID_SOCKET;
GSocket_close(sck);
/* sck->m_error is set in _GSocket_Output_Timeout */
return GSOCK_TIMEDOUT;
}
@@ -610,8 +613,7 @@ GSocketError GSocket_Connect(GSocket *sck, GSocketStream stream)
/* If connect failed with an error other than EINPROGRESS,
* then the call to GSocket_Connect has failed.
*/
close(sck->m_fd);
sck->m_fd = INVALID_SOCKET;
GSocket_close(sck);
sck->m_error = GSOCK_IOERR;
return GSOCK_IOERR;
}
@@ -675,8 +677,7 @@ GSocketError GSocket_SetNonOriented(GSocket *sck)
sck->m_local->m_addr,
(SOCKLEN_T *) &sck->m_local->m_len) != 0))
{
close(sck->m_fd);
sck->m_fd = INVALID_SOCKET;
GSocket_close(sck);
sck->m_error = GSOCK_IOERR;
return GSOCK_IOERR;
}
@@ -1301,7 +1302,7 @@ GAddress *GAddress_copy(GAddress *address)
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);
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__) */
// vi:sts=4:sw=4:et