gsocket.cpp
来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C++ 代码 · 共 1,932 行 · 第 1/4 页
CPP
1,932 行
/* -------------------------------------------------------------------------
* Project: GSocket (Generic Socket) for WX
* Name: gsocket.c
* Copyright: (c) Guilhem Lavaux
* Licence: wxWindows Licence
* Authors: David Elliott (C++ conversion, maintainer)
* Guilhem Lavaux,
* Guillermo Rodriguez Garcia <guille@iies.es>
* Purpose: GSocket main Unix and OS/2 file
* Licence: The wxWindows licence
* CVSID: $Id: gsocket.cpp,v 1.51.2.3 2006/02/22 01:55:43 KH Exp $
* -------------------------------------------------------------------------
*/
/*
* PLEASE don't put C++ comments here - this is a C source file.
*/
#if defined(__WATCOMC__)
#include "wx/wxprec.h"
#include <errno.h>
#include <nerrno.h>
#endif
#ifndef __GSOCKET_STANDALONE__
#include "wx/defs.h"
#endif
#if defined(__VISAGECPP__)
#define BSD_SELECT /* use Berkley Sockets select */
#endif
#if wxUSE_SOCKETS || defined(__GSOCKET_STANDALONE__)
#include <assert.h>
#include <sys/types.h>
#ifdef __VISAGECPP__
#include <string.h>
#include <sys/time.h>
#include <types.h>
#include <netinet/in.h>
#endif
#include <netdb.h>
#include <sys/ioctl.h>
#ifdef __VMS__
#include <socket.h>
struct sockaddr_un
{
u_char sun_len; /* sockaddr len including null */
u_char sun_family; /* AF_UNIX */
char sun_path[108]; /* path name (gag) */
};
#else
#include <sys/socket.h>
#include <sys/un.h>
#endif
#ifndef __VISAGECPP__
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#else
#include <nerrno.h>
# if __IBMCPP__ < 400
#include <machine/endian.h>
#include <socket.h>
#include <ioctl.h>
#include <select.h>
#include <unistd.h>
#define EBADF SOCEBADF
# ifdef min
# undef min
# endif
# else
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#define close(a) soclose(a)
#define select(a,b,c,d,e) bsdselect(a,b,c,d,e)
int _System bsdselect(int,
struct fd_set *,
struct fd_set *,
struct fd_set *,
struct timeval *);
int _System soclose(int);
# endif
#endif
#ifdef __EMX__
#include <sys/select.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#ifdef sun
# include <sys/filio.h>
#endif
#ifdef sgi
# include <bstring.h>
#endif
#ifdef _AIX
# include <strings.h>
#endif
#include <signal.h>
#ifndef WX_SOCKLEN_T
#ifdef VMS
# define WX_SOCKLEN_T unsigned int
#else
# ifdef __GLIBC__
# if __GLIBC__ == 2
# define WX_SOCKLEN_T socklen_t
# endif
# elif defined(__WXMAC__)
# define WX_SOCKLEN_T socklen_t
# else
# define WX_SOCKLEN_T int
# endif
#endif
#endif /* SOCKLEN_T */
#ifndef SOCKOPTLEN_T
#define SOCKOPTLEN_T WX_SOCKLEN_T
#endif
/*
* MSW defines this, Unices don't.
*/
#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif
/* UnixWare reportedly needs this for FIONBIO definition */
#ifdef __UNIXWARE__
#include <sys/filio.h>
#endif
/*
* INADDR_BROADCAST is identical to INADDR_NONE which is not defined
* on all systems. INADDR_BROADCAST should be fine to indicate an error.
*/
#ifndef INADDR_NONE
#define INADDR_NONE INADDR_BROADCAST
#endif
#if defined(__VISAGECPP__) || defined(__WATCOMC__)
#define MASK_SIGNAL() {
#define UNMASK_SIGNAL() }
#else
extern "C" { typedef void (*wxSigHandler)(int); }
#define MASK_SIGNAL() \
{ \
wxSigHandler old_handler = signal(SIGPIPE, SIG_IGN);
#define UNMASK_SIGNAL() \
signal(SIGPIPE, old_handler); \
}
#endif
/* If a SIGPIPE is issued by a socket call on a remotely closed socket,
the program will "crash" unless it explicitly handles the SIGPIPE.
By using MSG_NOSIGNAL, the SIGPIPE is suppressed. Later, we will
use SO_NOSIGPIPE (if available), the BSD equivalent. */
#ifdef MSG_NOSIGNAL
# define GSOCKET_MSG_NOSIGNAL MSG_NOSIGNAL
#else /* MSG_NOSIGNAL not available (FreeBSD including OS X) */
# define GSOCKET_MSG_NOSIGNAL 0
#endif /* MSG_NOSIGNAL */
#ifndef __GSOCKET_STANDALONE__
# include "wx/unix/gsockunx.h"
# include "wx/gsocket.h"
#else
# include "gsockunx.h"
# include "gsocket.h"
# ifndef WXUNUSED
# define WXUNUSED(x)
# endif
#endif /* __GSOCKET_STANDALONE__ */
/* debugging helpers */
#ifdef __GSOCKET_DEBUG__
# define GSocket_Debug(args) printf args
#else
# define GSocket_Debug(args)
#endif /* __GSOCKET_DEBUG__ */
/* Table of GUI-related functions. We must call them indirectly because
* of wxBase and GUI separation: */
static GSocketGUIFunctionsTable *gs_gui_functions;
class GSocketGUIFunctionsTableNull: public GSocketGUIFunctionsTable
{
public:
virtual bool OnInit();
virtual void OnExit();
virtual bool CanUseEventLoop();
virtual bool Init_Socket(GSocket *socket);
virtual void Destroy_Socket(GSocket *socket);
virtual void Install_Callback(GSocket *socket, GSocketEvent event);
virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event);
virtual void Enable_Events(GSocket *socket);
virtual void Disable_Events(GSocket *socket);
};
bool GSocketGUIFunctionsTableNull::OnInit()
{ return true; }
void GSocketGUIFunctionsTableNull::OnExit()
{}
bool GSocketGUIFunctionsTableNull::CanUseEventLoop()
{ return false; }
bool GSocketGUIFunctionsTableNull::Init_Socket(GSocket *WXUNUSED(socket))
{ return true; }
void GSocketGUIFunctionsTableNull::Destroy_Socket(GSocket *WXUNUSED(socket))
{}
void GSocketGUIFunctionsTableNull::Install_Callback(GSocket *WXUNUSED(socket), GSocketEvent WXUNUSED(event))
{}
void GSocketGUIFunctionsTableNull::Uninstall_Callback(GSocket *WXUNUSED(socket), GSocketEvent WXUNUSED(event))
{}
void GSocketGUIFunctionsTableNull::Enable_Events(GSocket *WXUNUSED(socket))
{}
void GSocketGUIFunctionsTableNull::Disable_Events(GSocket *WXUNUSED(socket))
{}
/* Global initialisers */
void GSocket_SetGUIFunctions(GSocketGUIFunctionsTable *guifunc)
{
gs_gui_functions = guifunc;
}
int GSocket_Init(void)
{
if (!gs_gui_functions)
{
static GSocketGUIFunctionsTableNull table;
gs_gui_functions = &table;
}
if ( !gs_gui_functions->OnInit() )
return 0;
return 1;
}
void GSocket_Cleanup(void)
{
if (gs_gui_functions)
{
gs_gui_functions->OnExit();
}
}
/* Constructors / Destructors for GSocket */
GSocket::GSocket()
{
int i;
m_fd = INVALID_SOCKET;
for (i=0;i<GSOCK_MAX_EVENT;i++)
{
m_cbacks[i] = NULL;
}
m_detected = 0;
m_local = NULL;
m_peer = NULL;
m_error = GSOCK_NOERROR;
m_server = false;
m_stream = true;
m_gui_dependent = NULL;
m_non_blocking = false;
m_reusable = false;
m_timeout = 10*60*1000;
/* 10 minutes * 60 sec * 1000 millisec */
m_establishing = false;
assert(gs_gui_functions);
/* Per-socket GUI-specific initialization */
m_ok = gs_gui_functions->Init_Socket(this);
}
void GSocket::Close()
{
gs_gui_functions->Disable_Events(this);
/* gsockosx.c calls CFSocketInvalidate which closes the socket for us */
#if !(defined(__DARWIN__) && (defined(__WXMAC__) || defined(__WXCOCOA__)))
close(m_fd);
#endif
m_fd = INVALID_SOCKET;
}
GSocket::~GSocket()
{
assert(this);
/* Check that the socket is really shutdowned */
if (m_fd != INVALID_SOCKET)
Shutdown();
/* Per-socket GUI-specific cleanup */
gs_gui_functions->Destroy_Socket(this);
/* Destroy private addresses */
if (m_local)
GAddress_destroy(m_local);
if (m_peer)
GAddress_destroy(m_peer);
}
/* GSocket_Shutdown:
* Disallow further read/write operations on this socket, close
* the fd and disable all callbacks.
*/
void GSocket::Shutdown()
{
int evt;
assert(this);
/* Don't allow events to fire after socket has been closed */
gs_gui_functions->Disable_Events(this);
/* If socket has been created, shutdown it */
if (m_fd != INVALID_SOCKET)
{
shutdown(m_fd, 2);
Close();
}
/* Disable GUI callbacks */
for (evt = 0; evt < GSOCK_MAX_EVENT; evt++)
m_cbacks[evt] = NULL;
m_detected = GSOCK_LOST_FLAG;
}
/* Address handling */
/* GSocket_SetLocal:
* GSocket_GetLocal:
* GSocket_SetPeer:
* GSocket_GetPeer:
* Set or get the local or peer address for this socket. The 'set'
* functions return GSOCK_NOERROR on success, an error code otherwise.
* The 'get' functions return a pointer to a GAddress object on success,
* or NULL otherwise, in which case they set the error code of the
* corresponding GSocket.
*
* Error codes:
* GSOCK_INVSOCK - the socket is not valid.
* GSOCK_INVADDR - the address is not valid.
*/
GSocketError GSocket::SetLocal(GAddress *address)
{
assert(this);
/* the socket must be initialized, or it must be a server */
if ((m_fd != INVALID_SOCKET && !m_server))
{
m_error = GSOCK_INVSOCK;
return GSOCK_INVSOCK;
}
/* check address */
if (address == NULL || address->m_family == GSOCK_NOFAMILY)
{
m_error = GSOCK_INVADDR;
return GSOCK_INVADDR;
}
if (m_local)
GAddress_destroy(m_local);
m_local = GAddress_copy(address);
return GSOCK_NOERROR;
}
GSocketError GSocket::SetPeer(GAddress *address)
{
assert(this);
/* check address */
if (address == NULL || address->m_family == GSOCK_NOFAMILY)
{
m_error = GSOCK_INVADDR;
return GSOCK_INVADDR;
}
if (m_peer)
GAddress_destroy(m_peer);
m_peer = GAddress_copy(address);
return GSOCK_NOERROR;
}
GAddress *GSocket::GetLocal()
{
GAddress *address;
struct sockaddr addr;
WX_SOCKLEN_T size = sizeof(addr);
GSocketError err;
assert(this);
/* try to get it from the m_local var first */
if (m_local)
return GAddress_copy(m_local);
/* else, if the socket is initialized, try getsockname */
if (m_fd == INVALID_SOCKET)
{
m_error = GSOCK_INVSOCK;
return NULL;
}
if (getsockname(m_fd, &addr, (WX_SOCKLEN_T *) &size) < 0)
{
m_error = GSOCK_IOERR;
return NULL;
}
/* got a valid address from getsockname, create a GAddress object */
address = GAddress_new();
if (address == NULL)
{
m_error = GSOCK_MEMERR;
return NULL;
}
err = _GAddress_translate_from(address, &addr, size);
if (err != GSOCK_NOERROR)
{
GAddress_destroy(address);
m_error = err;
return NULL;
}
return address;
}
GAddress *GSocket::GetPeer()
{
assert(this);
/* try to get it from the m_peer var */
if (m_peer)
return GAddress_copy(m_peer);
return NULL;
}
/* Server specific parts */
/* GSocket_SetServer:
* Sets up this socket as a server. The local address must have been
* set with GSocket_SetLocal() before GSocket_SetServer() is called.
* Returns GSOCK_NOERROR on success, one of the following otherwise:
*
* Error codes:
* GSOCK_INVSOCK - the socket is in use.
* GSOCK_INVADDR - the local address has not been set.
* GSOCK_IOERR - low-level error.
*/
GSocketError GSocket::SetServer()
{
int arg = 1;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?