From d3ea65274fc7a9c96cf71829030bfb324a75a377 Mon Sep 17 00:00:00 2001 From: Guillermo Rodriguez Garcia Date: Fri, 21 Jan 2000 03:12:01 +0000 Subject: [PATCH] GSocket: - Added lots of comments to both code and headers. - Restructured some parts of Unix version. Will try to merge with MSW soon and have a gsockcmn.c - Fixed a potential bug in the MSW version of GSocket_Connect when in blocking mode. - Fixed a potential (different) bug in the corresponding Unix version. - Now WaitConnection correctly fills out the peer address field. - Added GAddress_INET_SetAnyAddress (sets address to INADDR_ANY) - Default address is INADDR_ANY again, not INADDR_NONE; failed hostname lookups sets the address to INADDR_NONE. wxSocket: - Added some safety checks to the wxPostEvent stuff. wxIPV4address: - Added a wxIPV4address::AnyAddress method. wxIPCConnection and co.: - Servers binds to INADDR_ANY, instead of binding to localhost. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@5561 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/msw/gsockmsw.h | 9 ++--- src/common/sckaddr.cpp | 5 +++ src/common/sckipc.cpp | 15 +++++--- src/common/socket.cpp | 72 +++++++++++++++++++++------------------ 4 files changed, 57 insertions(+), 44 deletions(-) diff --git a/include/wx/msw/gsockmsw.h b/include/wx/msw/gsockmsw.h index ba6a2848d5..e7aa1147cd 100644 --- a/include/wx/msw/gsockmsw.h +++ b/include/wx/msw/gsockmsw.h @@ -21,9 +21,7 @@ #include "gsocket.h" #endif -#if defined(__BORLANDC__) #include -#endif #include #ifdef __cplusplus @@ -69,6 +67,7 @@ struct _GAddress GSocketError _GSocket_Input_Timeout(GSocket *socket); GSocketError _GSocket_Output_Timeout(GSocket *socket); +GSocketError _GSocket_Connect_Timeout(GSocket *socket); int _GSocket_Recv_Stream(GSocket *socket, char *buffer, int size); int _GSocket_Recv_Dgram(GSocket *socket, char *buffer, int size); int _GSocket_Send_Stream(GSocket *socket, const char *buffer, int size); @@ -84,10 +83,8 @@ LRESULT CALLBACK _GSocket_Internal_WinProc(HWND, UINT, WPARAM, LPARAM); GSocketError _GAddress_translate_from(GAddress *address, struct sockaddr *addr, int len); - -GSocketError _GAddress_translate_to(GAddress *address, - struct sockaddr **addr, int *len); - +GSocketError _GAddress_translate_to (GAddress *address, + struct sockaddr **addr, int *len); GSocketError _GAddress_Init_INET(GAddress *address); GSocketError _GAddress_Init_UNIX(GAddress *address); diff --git a/src/common/sckaddr.cpp b/src/common/sckaddr.cpp index 75a6079af4..8131c0427a 100644 --- a/src/common/sckaddr.cpp +++ b/src/common/sckaddr.cpp @@ -131,6 +131,11 @@ bool wxIPV4address::LocalHost() return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR); } +bool wxIPV4address::AnyAddress() +{ + return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR); +} + wxString wxIPV4address::Hostname() { char hostname[1024]; diff --git a/src/common/sckipc.cpp b/src/common/sckipc.cpp index 80fe44212d..c532acbbee 100644 --- a/src/common/sckipc.cpp +++ b/src/common/sckipc.cpp @@ -69,6 +69,11 @@ void Client_OnRequest(wxSocketBase& sock, wxSocketNotify evt, char *cdata); + +// All sockets will be created with the following flags + +#define SCKIPC_FLAGS wxSOCKET_NONE + // --------------------------------------------------------------------------- // wxTCPClient // --------------------------------------------------------------------------- @@ -93,7 +98,7 @@ wxConnectionBase *wxTCPClient::MakeConnection (const wxString& host, const wxString& server_name, const wxString& topic) { - wxSocketClient *client = new wxSocketClient(); + wxSocketClient *client = new wxSocketClient(SCKIPC_FLAGS); wxSocketStream *stream = new wxSocketStream(*client); wxDataInputStream *data_is = new wxDataInputStream(*stream); wxDataOutputStream *data_os = new wxDataOutputStream(*stream); @@ -165,18 +170,18 @@ wxTCPServer::wxTCPServer () bool wxTCPServer::Create(const wxString& server_name) { - wxIPV4address addr; wxSocketServer *server; - addr.LocalHost(); // GRG + // wxIPV4address defaults to INADDR_ANY:0 + wxIPV4address addr; addr.Service(server_name); // Create a socket listening on specified port - server = new wxSocketServer(addr); + server = new wxSocketServer(addr, SCKIPC_FLAGS); server->Callback((wxSocketBase::wxSockCbk)Server_OnRequest); server->CallbackData((char *)this); server->SetNotify(wxSOCKET_CONNECTION_FLAG); - server->Notify(TRUE); // GRG + server->Notify(TRUE); return TRUE; } diff --git a/src/common/socket.cpp b/src/common/socket.cpp index c83644159e..0058d50318 100644 --- a/src/common/socket.cpp +++ b/src/common/socket.cpp @@ -3,9 +3,8 @@ // Purpose: Socket handler classes // Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia // Created: April 1997 -// Updated: September 1999 -// Copyright: (C) 1999, 1998, 1997, Guilhem Lavaux -// (C) 1999, Guillermo Rodriguez Garcia +// Copyright: (C) 1999-1997, Guilhem Lavaux +// (C) 2000-1999, Guillermo Rodriguez Garcia // RCS_ID: $Id$ // License: see wxWindows license ///////////////////////////////////////////////////////////////////////////// @@ -53,7 +52,6 @@ // use wxPostEvent or not #define EXPERIMENTAL_USE_POST 1 - // -------------------------------------------------------------- // ClassInfos // -------------------------------------------------------------- @@ -168,8 +166,8 @@ wxSocketBase& wxSocketBase::Read(char* buffer, wxUint32 nbytes) m_lcount = _Read(buffer, nbytes); - // If in WAITALL mode, all bytes should have been read. - if (m_flags & WAITALL) + // If in wxSOCKET_WAITALL mode, all bytes should have been read. + if (m_flags & wxSOCKET_WAITALL) m_error = (m_lcount != nbytes); else m_error = (m_lcount == 0); @@ -215,7 +213,7 @@ wxUint32 wxSocketBase::_Read(char* buffer, wxUint32 nbytes) { while (ret > 0 && nbytes > 0) { - if (!(m_flags & wxSOCKET_BLOCK) && !(WaitForRead())) + if (!(m_flags & wxSOCKET_BLOCK) && !WaitForRead()) break; ret = GSocket_Read(m_socket, buffer, nbytes); @@ -412,7 +410,7 @@ wxUint32 wxSocketBase::_Write(const char *buffer, wxUint32 nbytes) { while (ret > 0 && nbytes > 0) { - if (!(m_flags & wxSOCKET_BLOCK) && !(WaitForWrite())) + if (!(m_flags & wxSOCKET_BLOCK) && !WaitForWrite()) break; ret = GSocket_Write(m_socket, buffer, nbytes); @@ -629,14 +627,16 @@ void wxSocketBase::RestoreState() // wxSocketBase Wait functions // -------------------------------------------------------------- -// GRG: I have completely rewritten this family of functions -// so that they don't depend on event notifications; instead, -// they poll the socket, using GSocket_Select(), to check for -// the specified combination of event flags, until an event -// occurs or until the timeout ellapses. The polling loop -// calls PROCESS_EVENTS(), so this won't block the GUI. - -bool wxSocketBase::_Wait(long seconds, long milliseconds, wxSocketEventFlags flags) +// These WaitXXX unctions do not depend on the event system any +// longer; instead, they poll the socket, using GSocket_Select() +// to check for the specified combination of event flags, until +// an event occurs or until the timeout ellapses. The polling +// loop calls PROCESS_EVENTS(), so this won't block the GUI. +// +// XXX: Should it honour the wxSOCKET_BLOCK flag ? +// +bool wxSocketBase::_Wait(long seconds, long milliseconds, + wxSocketEventFlags flags) { GSocketEventFlags result; _wxSocketInternalTimer timer; @@ -677,7 +677,7 @@ bool wxSocketBase::_Wait(long seconds, long milliseconds, wxSocketEventFlags fla // because maybe the WaitXXX functions are not being used. // // Do this at least once (important if timeout == 0, when - // we are just polling) + // we are just polling). Also, if just polling, do not yield. // while (state == -1) { @@ -709,8 +709,8 @@ bool wxSocketBase::_Wait(long seconds, long milliseconds, wxSocketEventFlags fla // Wait more? if ((timeout == 0) || (m_interrupt)) break; - - PROCESS_EVENTS(); + else + PROCESS_EVENTS(); } timer.Stop(); @@ -824,16 +824,15 @@ void wxSocketBase::OnRequest(wxSocketNotify req_evt) wxSocketEvent event(m_id); wxSocketEventFlags flag = EventToNotify(req_evt); - // fprintf(stderr, "%s: Entering OnRequest (evt %d)\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt); + // dbg("Entering OnRequest (evt %d)\n", req_evt); - // NOTE: this duplicates some of the code in _Wait, (lost - // connections and delayed connection establishment) but - // this doesn't hurt. It has to be here because maybe the - // WaitXXX are not being used, and it has to be in _Wait - // as well because the event might be a bit delayed. - // switch (req_evt) { + // This duplicates some code in _Wait(), but this doesn't + // hurt. It has to be here because we don't know whether + // WaitXXX will be used, and it has to be in _Wait as well + // because the event might be a bit delayed. + // case wxSOCKET_CONNECTION : m_establishing = FALSE; m_connected = TRUE; @@ -842,19 +841,26 @@ void wxSocketBase::OnRequest(wxSocketNotify req_evt) Close(); break; - // If we are in the middle of a R/W operation, do not - // propagate events to users. - // + // If we are in the middle of a R/W operation, do not + // propagate events to users. Also, filter 'late' events + // which are no longer valid. + // case wxSOCKET_INPUT: - if (m_reading) return; + if (m_reading || !GSocket_Select(m_socket, GSOCK_INPUT_FLAG)) + return; + else + break; case wxSOCKET_OUTPUT: - if (m_writing) return; + if (m_writing || !GSocket_Select(m_socket, GSOCK_OUTPUT_FLAG)) + return; + else + break; } if (((m_neededreq & flag) == flag) && m_notify_state) { - // fprintf(stderr, "%s: Evt %d delivered\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt); + // dbg("Evt %d delivered\n", req_evt); event.m_socket = this; event.m_skevt = req_evt; @@ -871,7 +877,7 @@ void wxSocketBase::OnRequest(wxSocketNotify req_evt) } - // fprintf(stderr, "%s: Exiting OnRequest (evt %d)\n", (m_type == SOCK_CLIENT)? "client" : "server", req_evt); + // dbg("Exiting OnRequest (evt %d)\n", req_evt); } void wxSocketBase::OldOnNotify(wxSocketNotify WXUNUSED(evt)) -- 2.45.2