1999-07-23 13:24:25 -04:00
|
|
|
/* -------------------------------------------------------------------------
|
2005-05-04 14:57:50 -04:00
|
|
|
* Project: GSocket (Generic Socket) for WX
|
|
|
|
* Name: gsockunx.h
|
|
|
|
* Copyright: (c) Guilhem Lavaux
|
|
|
|
* Licence: wxWindows Licence
|
|
|
|
* Purpose: GSocket Unix header
|
|
|
|
* CVSID: $Id$
|
1999-07-23 13:24:25 -04:00
|
|
|
* -------------------------------------------------------------------------
|
|
|
|
*/
|
1999-09-30 19:53:10 -04:00
|
|
|
|
2007-12-19 11:45:08 -05:00
|
|
|
#ifndef _WX_UNIX_GSOCKUNX_H_
|
|
|
|
#define _WX_UNIX_GSOCKUNX_H_
|
1999-08-06 06:10:10 -04:00
|
|
|
|
2007-07-14 16:18:38 -04:00
|
|
|
class wxGSocketIOHandler;
|
|
|
|
|
2008-11-22 19:11:09 -05:00
|
|
|
class GSocket : public GSocketBase
|
2004-08-03 15:18:39 -04:00
|
|
|
{
|
|
|
|
public:
|
2008-11-23 08:12:46 -05:00
|
|
|
GSocket(wxSocketBase& wxsocket);
|
|
|
|
virtual ~GSocket();
|
|
|
|
|
2008-11-22 20:44:50 -05:00
|
|
|
virtual void Shutdown();
|
2008-11-23 08:12:46 -05:00
|
|
|
virtual GSocket *WaitConnection(wxSocketBase& wxsocket);
|
|
|
|
|
2004-01-18 02:46:18 -05:00
|
|
|
GSocketError SetServer();
|
2004-08-08 01:30:36 -04:00
|
|
|
bool SetReusable();
|
2007-07-04 17:33:11 -04:00
|
|
|
bool SetBroadcast();
|
|
|
|
bool DontDoBind();
|
2004-01-18 02:46:18 -05:00
|
|
|
GSocketError Connect(GSocketStream stream);
|
|
|
|
GSocketError SetNonOriented();
|
|
|
|
int Read(char *buffer, int size);
|
|
|
|
int Write(const char *buffer, int size);
|
2004-08-08 01:30:36 -04:00
|
|
|
void SetNonBlocking(bool non_block);
|
2004-08-03 15:18:39 -04:00
|
|
|
GSocketError WXDLLIMPEXP_NET GetError();
|
2004-07-26 22:15:32 -04:00
|
|
|
GSocketError GetSockOpt(int level, int optname, void *optval, int *optlen);
|
|
|
|
GSocketError SetSockOpt(int level, int optname,
|
|
|
|
const void *optval, int optlen);
|
2007-12-19 11:45:08 -05:00
|
|
|
//attach or detach from main loop
|
|
|
|
void Notify(bool flag);
|
2008-11-22 20:44:50 -05:00
|
|
|
void Detected_Read();
|
|
|
|
void Detected_Write();
|
2007-11-17 10:21:26 -05:00
|
|
|
|
2008-11-25 08:33:07 -05:00
|
|
|
private:
|
|
|
|
// enable or disable notifications for socket input/output events but only
|
|
|
|
// if m_use_events is true; do nothing otherwise
|
|
|
|
void EnableEvents()
|
|
|
|
{
|
|
|
|
if ( m_use_events )
|
|
|
|
DoEnableEvents(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DisableEvents()
|
|
|
|
{
|
|
|
|
if ( m_use_events )
|
|
|
|
DoEnableEvents(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// really enable or disable socket input/output events, regardless of
|
|
|
|
// m_use_events value
|
|
|
|
void DoEnableEvents(bool enable);
|
|
|
|
|
|
|
|
|
|
|
|
// enable or disable events for the given event if m_use_events; do nothing
|
|
|
|
// otherwise
|
|
|
|
//
|
|
|
|
// notice that these functions also update m_detected: EnableEvent() clears
|
|
|
|
// the corresponding bit in it and DisableEvent() sets it
|
|
|
|
void EnableEvent(GSocketEvent event);
|
|
|
|
void DisableEvent(GSocketEvent event);
|
|
|
|
|
|
|
|
|
2004-01-18 02:46:18 -05:00
|
|
|
GSocketError Input_Timeout();
|
|
|
|
GSocketError Output_Timeout();
|
|
|
|
int Recv_Stream(char *buffer, int size);
|
|
|
|
int Recv_Dgram(char *buffer, int size);
|
|
|
|
int Send_Stream(const char *buffer, int size);
|
|
|
|
int Send_Dgram(const char *buffer, int size);
|
|
|
|
public:
|
2004-03-09 17:48:53 -05:00
|
|
|
/* DFE: We can't protect these data member until the GUI code is updated */
|
|
|
|
/* protected: */
|
2007-07-14 16:18:38 -04:00
|
|
|
wxGSocketIOHandler *m_handler;
|
1999-07-22 13:51:54 -04:00
|
|
|
|
2007-12-19 11:45:08 -05:00
|
|
|
// true if socket should fire events
|
|
|
|
bool m_use_events;
|
|
|
|
|
|
|
|
// pointer for storing extra (usually GUI-specific) data
|
|
|
|
void *m_gui_dependent;
|
2008-11-23 08:12:46 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
// notify the associated wxSocket about a change in socket state and shut
|
|
|
|
// down the socket if the event is GSOCK_LOST
|
|
|
|
void OnStateChange(GSocketEvent event);
|
1999-07-22 13:51:54 -04:00
|
|
|
};
|
|
|
|
|
2007-12-19 11:45:08 -05:00
|
|
|
// A version of GSocketManager which uses FDs for socket IO
|
|
|
|
//
|
|
|
|
// This class uses GSocket::m_gui_dependent field to store the 2 (for input and
|
|
|
|
// output) FDs associated with the socket.
|
|
|
|
class GSocketFDBasedManager : public GSocketManager
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// no special initialization/cleanup needed when using FDs
|
|
|
|
virtual bool OnInit() { return true; }
|
|
|
|
virtual void OnExit() { }
|
|
|
|
|
|
|
|
// allocate/free the storage we need
|
|
|
|
virtual bool Init_Socket(GSocket *socket)
|
|
|
|
{
|
|
|
|
socket->m_gui_dependent = malloc(sizeof(int)*2);
|
2008-11-01 22:39:52 -04:00
|
|
|
int * const fds = static_cast<int *>(socket->m_gui_dependent);
|
2007-12-19 11:45:08 -05:00
|
|
|
|
|
|
|
fds[0] = -1;
|
|
|
|
fds[1] = -1;
|
2000-01-20 22:10:42 -05:00
|
|
|
|
2007-12-19 11:45:08 -05:00
|
|
|
return true;
|
|
|
|
}
|
1999-08-06 06:10:10 -04:00
|
|
|
|
2008-11-25 08:33:07 -05:00
|
|
|
virtual void Close_Socket(GSocket *socket)
|
2007-12-19 11:45:08 -05:00
|
|
|
{
|
|
|
|
Uninstall_Callback(socket, GSOCK_INPUT);
|
|
|
|
Uninstall_Callback(socket, GSOCK_OUTPUT);
|
2008-11-25 08:33:07 -05:00
|
|
|
|
|
|
|
close(socket->m_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Destroy_Socket(GSocket *socket)
|
|
|
|
{
|
|
|
|
free(socket->m_gui_dependent);
|
2007-12-19 11:45:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// identifies either input or output direction
|
|
|
|
//
|
|
|
|
// NB: the values of this enum shouldn't change
|
|
|
|
enum SocketDir
|
|
|
|
{
|
|
|
|
FD_INPUT,
|
|
|
|
FD_OUTPUT
|
|
|
|
};
|
|
|
|
|
|
|
|
// get the FD index corresponding to the given GSocketEvent
|
|
|
|
SocketDir GetDirForEvent(GSocket *socket, GSocketEvent event)
|
|
|
|
{
|
|
|
|
switch ( event )
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
wxFAIL_MSG( "unexpected socket event" );
|
|
|
|
// fall through
|
|
|
|
|
|
|
|
case GSOCK_LOST:
|
|
|
|
// fall through
|
|
|
|
|
|
|
|
case GSOCK_INPUT:
|
|
|
|
return FD_INPUT;
|
|
|
|
|
|
|
|
case GSOCK_OUTPUT:
|
|
|
|
return FD_OUTPUT;
|
|
|
|
|
|
|
|
case GSOCK_CONNECTION:
|
|
|
|
// FIXME: explain this?
|
|
|
|
return socket->m_server ? FD_INPUT : FD_OUTPUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// access the FDs we store
|
|
|
|
int& FD(GSocket *socket, SocketDir d)
|
|
|
|
{
|
2008-11-01 22:39:52 -04:00
|
|
|
return static_cast<int *>(socket->m_gui_dependent)[d];
|
2007-12-19 11:45:08 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Common base class for all ports using X11-like (and hence implemented in
|
|
|
|
// X11, Motif and GTK) AddInput() and RemoveInput() functions
|
|
|
|
class GSocketInputBasedManager : public GSocketFDBasedManager
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void Install_Callback(GSocket *socket, GSocketEvent event)
|
|
|
|
{
|
|
|
|
wxCHECK_RET( socket->m_fd != -1,
|
|
|
|
"shouldn't be called on invalid socket" );
|
|
|
|
|
|
|
|
const SocketDir d = GetDirForEvent(socket, event);
|
|
|
|
|
|
|
|
int& fd = FD(socket, d);
|
|
|
|
if ( fd != -1 )
|
|
|
|
RemoveInput(fd);
|
|
|
|
|
|
|
|
fd = AddInput(socket, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Uninstall_Callback(GSocket *socket, GSocketEvent event)
|
|
|
|
{
|
|
|
|
const SocketDir d = GetDirForEvent(socket, event);
|
|
|
|
|
|
|
|
int& fd = FD(socket, d);
|
|
|
|
if ( fd != -1 )
|
|
|
|
{
|
|
|
|
RemoveInput(fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// these functions map directly to XtAdd/RemoveInput() or
|
|
|
|
// gdk_input_add/remove()
|
|
|
|
virtual int AddInput(GSocket *socket, SocketDir d) = 0;
|
|
|
|
virtual void RemoveInput(int fd) = 0;
|
|
|
|
};
|
1999-08-06 06:10:10 -04:00
|
|
|
|
2007-12-19 11:45:08 -05:00
|
|
|
#endif /* _WX_UNIX_GSOCKUNX_H_ */
|