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
This commit is contained in:
@@ -21,9 +21,7 @@
|
|||||||
#include "gsocket.h"
|
#include "gsocket.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(__BORLANDC__)
|
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#endif
|
|
||||||
#include <winsock.h>
|
#include <winsock.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
@@ -69,6 +67,7 @@ struct _GAddress
|
|||||||
|
|
||||||
GSocketError _GSocket_Input_Timeout(GSocket *socket);
|
GSocketError _GSocket_Input_Timeout(GSocket *socket);
|
||||||
GSocketError _GSocket_Output_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_Stream(GSocket *socket, char *buffer, int size);
|
||||||
int _GSocket_Recv_Dgram(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);
|
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,
|
GSocketError _GAddress_translate_from(GAddress *address,
|
||||||
struct sockaddr *addr, int len);
|
struct sockaddr *addr, int len);
|
||||||
|
GSocketError _GAddress_translate_to (GAddress *address,
|
||||||
GSocketError _GAddress_translate_to(GAddress *address,
|
struct sockaddr **addr, int *len);
|
||||||
struct sockaddr **addr, int *len);
|
|
||||||
|
|
||||||
GSocketError _GAddress_Init_INET(GAddress *address);
|
GSocketError _GAddress_Init_INET(GAddress *address);
|
||||||
GSocketError _GAddress_Init_UNIX(GAddress *address);
|
GSocketError _GAddress_Init_UNIX(GAddress *address);
|
||||||
|
|
||||||
|
@@ -131,6 +131,11 @@ bool wxIPV4address::LocalHost()
|
|||||||
return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
|
return (GAddress_INET_SetHostName(m_address, "localhost") == GSOCK_NOERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool wxIPV4address::AnyAddress()
|
||||||
|
{
|
||||||
|
return (GAddress_INET_SetAnyAddress(m_address) == GSOCK_NOERROR);
|
||||||
|
}
|
||||||
|
|
||||||
wxString wxIPV4address::Hostname()
|
wxString wxIPV4address::Hostname()
|
||||||
{
|
{
|
||||||
char hostname[1024];
|
char hostname[1024];
|
||||||
|
@@ -69,6 +69,11 @@ void Client_OnRequest(wxSocketBase& sock,
|
|||||||
wxSocketNotify evt,
|
wxSocketNotify evt,
|
||||||
char *cdata);
|
char *cdata);
|
||||||
|
|
||||||
|
|
||||||
|
// All sockets will be created with the following flags
|
||||||
|
|
||||||
|
#define SCKIPC_FLAGS wxSOCKET_NONE
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// wxTCPClient
|
// wxTCPClient
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -93,7 +98,7 @@ wxConnectionBase *wxTCPClient::MakeConnection (const wxString& host,
|
|||||||
const wxString& server_name,
|
const wxString& server_name,
|
||||||
const wxString& topic)
|
const wxString& topic)
|
||||||
{
|
{
|
||||||
wxSocketClient *client = new wxSocketClient();
|
wxSocketClient *client = new wxSocketClient(SCKIPC_FLAGS);
|
||||||
wxSocketStream *stream = new wxSocketStream(*client);
|
wxSocketStream *stream = new wxSocketStream(*client);
|
||||||
wxDataInputStream *data_is = new wxDataInputStream(*stream);
|
wxDataInputStream *data_is = new wxDataInputStream(*stream);
|
||||||
wxDataOutputStream *data_os = new wxDataOutputStream(*stream);
|
wxDataOutputStream *data_os = new wxDataOutputStream(*stream);
|
||||||
@@ -165,18 +170,18 @@ wxTCPServer::wxTCPServer ()
|
|||||||
|
|
||||||
bool wxTCPServer::Create(const wxString& server_name)
|
bool wxTCPServer::Create(const wxString& server_name)
|
||||||
{
|
{
|
||||||
wxIPV4address addr;
|
|
||||||
wxSocketServer *server;
|
wxSocketServer *server;
|
||||||
|
|
||||||
addr.LocalHost(); // GRG
|
// wxIPV4address defaults to INADDR_ANY:0
|
||||||
|
wxIPV4address addr;
|
||||||
addr.Service(server_name);
|
addr.Service(server_name);
|
||||||
|
|
||||||
// Create a socket listening on specified port
|
// Create a socket listening on specified port
|
||||||
server = new wxSocketServer(addr);
|
server = new wxSocketServer(addr, SCKIPC_FLAGS);
|
||||||
server->Callback((wxSocketBase::wxSockCbk)Server_OnRequest);
|
server->Callback((wxSocketBase::wxSockCbk)Server_OnRequest);
|
||||||
server->CallbackData((char *)this);
|
server->CallbackData((char *)this);
|
||||||
server->SetNotify(wxSOCKET_CONNECTION_FLAG);
|
server->SetNotify(wxSOCKET_CONNECTION_FLAG);
|
||||||
server->Notify(TRUE); // GRG
|
server->Notify(TRUE);
|
||||||
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
@@ -3,9 +3,8 @@
|
|||||||
// Purpose: Socket handler classes
|
// Purpose: Socket handler classes
|
||||||
// Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia
|
// Authors: Guilhem Lavaux, Guillermo Rodriguez Garcia
|
||||||
// Created: April 1997
|
// Created: April 1997
|
||||||
// Updated: September 1999
|
// Copyright: (C) 1999-1997, Guilhem Lavaux
|
||||||
// Copyright: (C) 1999, 1998, 1997, Guilhem Lavaux
|
// (C) 2000-1999, Guillermo Rodriguez Garcia
|
||||||
// (C) 1999, Guillermo Rodriguez Garcia
|
|
||||||
// RCS_ID: $Id$
|
// RCS_ID: $Id$
|
||||||
// License: see wxWindows license
|
// License: see wxWindows license
|
||||||
/////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////
|
||||||
@@ -53,7 +52,6 @@
|
|||||||
// use wxPostEvent or not
|
// use wxPostEvent or not
|
||||||
#define EXPERIMENTAL_USE_POST 1
|
#define EXPERIMENTAL_USE_POST 1
|
||||||
|
|
||||||
|
|
||||||
// --------------------------------------------------------------
|
// --------------------------------------------------------------
|
||||||
// ClassInfos
|
// ClassInfos
|
||||||
// --------------------------------------------------------------
|
// --------------------------------------------------------------
|
||||||
@@ -168,8 +166,8 @@ wxSocketBase& wxSocketBase::Read(char* buffer, wxUint32 nbytes)
|
|||||||
|
|
||||||
m_lcount = _Read(buffer, nbytes);
|
m_lcount = _Read(buffer, nbytes);
|
||||||
|
|
||||||
// If in WAITALL mode, all bytes should have been read.
|
// If in wxSOCKET_WAITALL mode, all bytes should have been read.
|
||||||
if (m_flags & WAITALL)
|
if (m_flags & wxSOCKET_WAITALL)
|
||||||
m_error = (m_lcount != nbytes);
|
m_error = (m_lcount != nbytes);
|
||||||
else
|
else
|
||||||
m_error = (m_lcount == 0);
|
m_error = (m_lcount == 0);
|
||||||
@@ -215,7 +213,7 @@ wxUint32 wxSocketBase::_Read(char* buffer, wxUint32 nbytes)
|
|||||||
{
|
{
|
||||||
while (ret > 0 && nbytes > 0)
|
while (ret > 0 && nbytes > 0)
|
||||||
{
|
{
|
||||||
if (!(m_flags & wxSOCKET_BLOCK) && !(WaitForRead()))
|
if (!(m_flags & wxSOCKET_BLOCK) && !WaitForRead())
|
||||||
break;
|
break;
|
||||||
|
|
||||||
ret = GSocket_Read(m_socket, buffer, nbytes);
|
ret = GSocket_Read(m_socket, buffer, nbytes);
|
||||||
@@ -412,7 +410,7 @@ wxUint32 wxSocketBase::_Write(const char *buffer, wxUint32 nbytes)
|
|||||||
{
|
{
|
||||||
while (ret > 0 && nbytes > 0)
|
while (ret > 0 && nbytes > 0)
|
||||||
{
|
{
|
||||||
if (!(m_flags & wxSOCKET_BLOCK) && !(WaitForWrite()))
|
if (!(m_flags & wxSOCKET_BLOCK) && !WaitForWrite())
|
||||||
break;
|
break;
|
||||||
|
|
||||||
ret = GSocket_Write(m_socket, buffer, nbytes);
|
ret = GSocket_Write(m_socket, buffer, nbytes);
|
||||||
@@ -629,14 +627,16 @@ void wxSocketBase::RestoreState()
|
|||||||
// wxSocketBase Wait functions
|
// wxSocketBase Wait functions
|
||||||
// --------------------------------------------------------------
|
// --------------------------------------------------------------
|
||||||
|
|
||||||
// GRG: I have completely rewritten this family of functions
|
// These WaitXXX unctions do not depend on the event system any
|
||||||
// so that they don't depend on event notifications; instead,
|
// longer; instead, they poll the socket, using GSocket_Select()
|
||||||
// they poll the socket, using GSocket_Select(), to check for
|
// to check for the specified combination of event flags, until
|
||||||
// the specified combination of event flags, until an event
|
// an event occurs or until the timeout ellapses. The polling
|
||||||
// occurs or until the timeout ellapses. The polling loop
|
// loop calls PROCESS_EVENTS(), so this won't block the GUI.
|
||||||
// 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)
|
//
|
||||||
|
bool wxSocketBase::_Wait(long seconds, long milliseconds,
|
||||||
|
wxSocketEventFlags flags)
|
||||||
{
|
{
|
||||||
GSocketEventFlags result;
|
GSocketEventFlags result;
|
||||||
_wxSocketInternalTimer timer;
|
_wxSocketInternalTimer timer;
|
||||||
@@ -677,7 +677,7 @@ bool wxSocketBase::_Wait(long seconds, long milliseconds, wxSocketEventFlags fla
|
|||||||
// because maybe the WaitXXX functions are not being used.
|
// because maybe the WaitXXX functions are not being used.
|
||||||
//
|
//
|
||||||
// Do this at least once (important if timeout == 0, when
|
// 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)
|
while (state == -1)
|
||||||
{
|
{
|
||||||
@@ -709,8 +709,8 @@ bool wxSocketBase::_Wait(long seconds, long milliseconds, wxSocketEventFlags fla
|
|||||||
// Wait more?
|
// Wait more?
|
||||||
if ((timeout == 0) || (m_interrupt))
|
if ((timeout == 0) || (m_interrupt))
|
||||||
break;
|
break;
|
||||||
|
else
|
||||||
PROCESS_EVENTS();
|
PROCESS_EVENTS();
|
||||||
}
|
}
|
||||||
|
|
||||||
timer.Stop();
|
timer.Stop();
|
||||||
@@ -824,16 +824,15 @@ void wxSocketBase::OnRequest(wxSocketNotify req_evt)
|
|||||||
wxSocketEvent event(m_id);
|
wxSocketEvent event(m_id);
|
||||||
wxSocketEventFlags flag = EventToNotify(req_evt);
|
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)
|
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 :
|
case wxSOCKET_CONNECTION :
|
||||||
m_establishing = FALSE;
|
m_establishing = FALSE;
|
||||||
m_connected = TRUE;
|
m_connected = TRUE;
|
||||||
@@ -842,19 +841,26 @@ void wxSocketBase::OnRequest(wxSocketNotify req_evt)
|
|||||||
Close();
|
Close();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// If we are in the middle of a R/W operation, do not
|
// If we are in the middle of a R/W operation, do not
|
||||||
// propagate events to users.
|
// propagate events to users. Also, filter 'late' events
|
||||||
//
|
// which are no longer valid.
|
||||||
|
//
|
||||||
case wxSOCKET_INPUT:
|
case wxSOCKET_INPUT:
|
||||||
if (m_reading) return;
|
if (m_reading || !GSocket_Select(m_socket, GSOCK_INPUT_FLAG))
|
||||||
|
return;
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
|
||||||
case wxSOCKET_OUTPUT:
|
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)
|
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_socket = this;
|
||||||
event.m_skevt = req_evt;
|
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))
|
void wxSocketBase::OldOnNotify(wxSocketNotify WXUNUSED(evt))
|
||||||
|
Reference in New Issue
Block a user