sckipc.cpp
来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 760 行 · 第 1/2 页
CPP
760 行
/////////////////////////////////////////////////////////////////////////////
// Name: sckipc.cpp
// Purpose: Interprocess communication implementation (wxSocket version)
// Author: Julian Smart
// Modified by: Guilhem Lavaux (big rewrite) May 1997, 1998
// Guillermo Rodriguez (updated for wxSocket v2) Jan 2000
// (callbacks deprecated) Mar 2000
// Vadim Zeitlin (added support for Unix sockets) Apr 2002
// Created: 1993
// RCS-ID: $Id: sckipc.cpp,v 1.47.2.1 2006/01/17 18:10:17 JS Exp $
// Copyright: (c) Julian Smart 1993
// (c) Guilhem Lavaux 1997, 1998
// (c) 2000 Guillermo Rodriguez <guille@iies.es>
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
// ==========================================================================
// declarations
// ==========================================================================
// --------------------------------------------------------------------------
// headers
// --------------------------------------------------------------------------
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma implementation "sckipc.h"
#endif
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/log.h"
#endif
#if wxUSE_SOCKETS && wxUSE_IPC && wxUSE_STREAMS
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include "wx/socket.h"
#include "wx/sckipc.h"
#include "wx/module.h"
#include "wx/event.h"
// --------------------------------------------------------------------------
// macros and constants
// --------------------------------------------------------------------------
// It seems to be already defined somewhere in the Xt includes.
#ifndef __XT__
// Message codes
enum
{
IPC_EXECUTE = 1,
IPC_REQUEST,
IPC_POKE,
IPC_ADVISE_START,
IPC_ADVISE_REQUEST,
IPC_ADVISE,
IPC_ADVISE_STOP,
IPC_REQUEST_REPLY,
IPC_FAIL,
IPC_CONNECT,
IPC_DISCONNECT
};
#endif
// All sockets will be created with the following flags
#define SCKIPC_FLAGS (wxSOCKET_WAITALL)
// headers needed for umask()
#ifdef __UNIX_LIKE__
#include <sys/types.h>
#include <sys/stat.h>
#endif // __UNIX_LIKE__
// ----------------------------------------------------------------------------
// private functions
// ----------------------------------------------------------------------------
// get the address object for the given server name, the caller must delete it
static wxSockAddress *
GetAddressFromName(const wxString& serverName, const wxString& host = wxEmptyString)
{
// we always use INET sockets under non-Unix systems
#if defined(__UNIX__) && !defined(__WINDOWS__) && !defined(__WINE__) && (!defined(__WXMAC__) || defined(__DARWIN__))
// under Unix, if the server name looks like a path, create a AF_UNIX
// socket instead of AF_INET one
if ( serverName.Find(_T('/')) != wxNOT_FOUND )
{
wxUNIXaddress *addr = new wxUNIXaddress;
addr->Filename(serverName);
return addr;
}
#endif // Unix/!Unix
{
wxIPV4address *addr = new wxIPV4address;
addr->Service(serverName);
if ( !host.empty() )
{
addr->Hostname(host);
}
return addr;
}
}
// --------------------------------------------------------------------------
// wxTCPEventHandler stuff (private class)
// --------------------------------------------------------------------------
class wxTCPEventHandler : public wxEvtHandler
{
public:
wxTCPEventHandler() : wxEvtHandler() {}
void Client_OnRequest(wxSocketEvent& event);
void Server_OnRequest(wxSocketEvent& event);
DECLARE_EVENT_TABLE()
DECLARE_NO_COPY_CLASS(wxTCPEventHandler)
};
enum
{
_CLIENT_ONREQUEST_ID = 1000,
_SERVER_ONREQUEST_ID
};
static wxTCPEventHandler *gs_handler = NULL;
// ==========================================================================
// implementation
// ==========================================================================
IMPLEMENT_DYNAMIC_CLASS(wxTCPServer, wxServerBase)
IMPLEMENT_DYNAMIC_CLASS(wxTCPClient, wxClientBase)
IMPLEMENT_CLASS(wxTCPConnection, wxConnectionBase)
// --------------------------------------------------------------------------
// wxTCPClient
// --------------------------------------------------------------------------
wxTCPClient::wxTCPClient () : wxClientBase()
{
}
wxTCPClient::~wxTCPClient ()
{
}
bool wxTCPClient::ValidHost(const wxString& host)
{
wxIPV4address addr;
return addr.Hostname(host);
}
wxConnectionBase *wxTCPClient::MakeConnection (const wxString& host,
const wxString& serverName,
const wxString& topic)
{
wxSocketClient *client = new wxSocketClient(SCKIPC_FLAGS);
wxSocketStream *stream = new wxSocketStream(*client);
wxDataInputStream *data_is = new wxDataInputStream(*stream);
wxDataOutputStream *data_os = new wxDataOutputStream(*stream);
wxSockAddress *addr = GetAddressFromName(serverName, host);
if ( !addr )
return NULL;
bool ok = client->Connect(*addr);
delete addr;
if ( ok )
{
unsigned char msg;
// Send topic name, and enquire whether this has succeeded
data_os->Write8(IPC_CONNECT);
data_os->WriteString(topic);
msg = data_is->Read8();
// OK! Confirmation.
if (msg == IPC_CONNECT)
{
wxTCPConnection *connection = (wxTCPConnection *)OnMakeConnection ();
if (connection)
{
if (connection->IsKindOf(CLASSINFO(wxTCPConnection)))
{
connection->m_topic = topic;
connection->m_sock = client;
connection->m_sockstrm = stream;
connection->m_codeci = data_is;
connection->m_codeco = data_os;
client->SetEventHandler(*gs_handler, _CLIENT_ONREQUEST_ID);
client->SetClientData(connection);
client->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
client->Notify(true);
return connection;
}
else
{
delete connection;
// and fall through to delete everything else
}
}
}
}
// Something went wrong, delete everything
delete data_is;
delete data_os;
delete stream;
client->Destroy();
return NULL;
}
wxConnectionBase *wxTCPClient::OnMakeConnection()
{
return new wxTCPConnection();
}
// --------------------------------------------------------------------------
// wxTCPServer
// --------------------------------------------------------------------------
wxTCPServer::wxTCPServer () : wxServerBase()
{
m_server = NULL;
}
bool wxTCPServer::Create(const wxString& serverName)
{
// Destroy previous server, if any
if (m_server)
{
m_server->SetClientData(NULL);
m_server->Destroy();
m_server = NULL;
}
wxSockAddress *addr = GetAddressFromName(serverName);
if ( !addr )
return false;
#ifdef __UNIX_LIKE__
mode_t umaskOld;
if ( addr->Type() == wxSockAddress::UNIX )
{
// ensure that the file doesn't exist as otherwise calling socket() would
// fail
int rc = remove(serverName.fn_str());
if ( rc < 0 && errno != ENOENT )
{
delete addr;
return false;
}
// also set the umask to prevent the others from reading our file
umaskOld = umask(077);
}
else
{
// unused anyhow but shut down the compiler warnings
umaskOld = 0;
}
#endif // __UNIX_LIKE__
// Create a socket listening on the specified port
m_server = new wxSocketServer(*addr, SCKIPC_FLAGS);
#ifdef __UNIX_LIKE__
if ( addr->Type() == wxSockAddress::UNIX )
{
// restore the umask
umask(umaskOld);
// save the file name to remove it later
m_filename = serverName;
}
#endif // __UNIX_LIKE__
delete addr;
if (!m_server->Ok())
{
m_server->Destroy();
m_server = NULL;
return false;
}
m_server->SetEventHandler(*gs_handler, _SERVER_ONREQUEST_ID);
m_server->SetClientData(this);
m_server->SetNotify(wxSOCKET_CONNECTION_FLAG);
m_server->Notify(true);
return true;
}
wxTCPServer::~wxTCPServer()
{
if (m_server)
{
m_server->SetClientData(NULL);
m_server->Destroy();
}
#ifdef __UNIX_LIKE__
if ( !m_filename.empty() )
{
if ( remove(m_filename.fn_str()) != 0 )
{
wxLogDebug(_T("Stale AF_UNIX file '%s' left."), m_filename.c_str());
}
}
#endif // __UNIX_LIKE__
}
wxConnectionBase *wxTCPServer::OnAcceptConnection( const wxString& WXUNUSED(topic) )
{
return new wxTCPConnection();
}
// --------------------------------------------------------------------------
// wxTCPConnection
// --------------------------------------------------------------------------
wxTCPConnection::wxTCPConnection () : wxConnectionBase()
{
m_sock = NULL;
m_sockstrm = NULL;
m_codeci = NULL;
m_codeco = NULL;
}
wxTCPConnection::wxTCPConnection(wxChar *buffer, int size)
: wxConnectionBase(buffer, size)
{
m_sock = NULL;
m_sockstrm = NULL;
m_codeci = NULL;
m_codeco = NULL;
}
wxTCPConnection::~wxTCPConnection ()
{
Disconnect();
wxDELETE(m_codeci);
wxDELETE(m_codeco);
wxDELETE(m_sockstrm);
if (m_sock)
{
m_sock->SetClientData(NULL);
m_sock->Destroy();
}
}
void wxTCPConnection::Compress(bool WXUNUSED(on))
{
// Use wxLZWStream
}
// Calls that CLIENT can make.
bool wxTCPConnection::Disconnect ()
{
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?