1 /* -------------------------------------------------------------------------
2 * Project: GSocket (Generic Socket) for WX
4 * Copyright: (c) Guilhem Lavaux
5 * Licence: wxWindows Licence
6 * Purpose: GSocket Unix header
8 * -------------------------------------------------------------------------
11 #ifndef _WX_UNIX_GSOCKUNX_H_
12 #define _WX_UNIX_GSOCKUNX_H_
14 class wxGSocketIOHandler
;
16 class GSocket
: public GSocketBase
19 GSocket(wxSocketBase
& wxsocket
);
23 virtual void Shutdown();
24 virtual GSocket
*WaitConnection(wxSocketBase
& wxsocket
);
26 GSocketError
SetServer();
30 GSocketError
Connect(GSocketStream stream
);
31 GSocketError
SetNonOriented();
32 int Read(char *buffer
, int size
);
33 int Write(const char *buffer
, int size
);
34 void SetNonBlocking(bool non_block
);
35 GSocketError WXDLLIMPEXP_NET
GetError();
36 GSocketError
GetSockOpt(int level
, int optname
, void *optval
, int *optlen
);
37 GSocketError
SetSockOpt(int level
, int optname
,
38 const void *optval
, int optlen
);
39 //attach or detach from main loop
40 void Notify(bool flag
);
42 void Detected_Write();
45 //enable or disable event callback using gsocket gui callback table
46 void EnableEvents(bool flag
= true);
47 void DisableEvents() { EnableEvents(false); }
48 void Enable(GSocketEvent event
);
49 void Disable(GSocketEvent event
);
50 GSocketError
Input_Timeout();
51 GSocketError
Output_Timeout();
52 int Recv_Stream(char *buffer
, int size
);
53 int Recv_Dgram(char *buffer
, int size
);
54 int Send_Stream(const char *buffer
, int size
);
55 int Send_Dgram(const char *buffer
, int size
);
57 /* DFE: We can't protect these data member until the GUI code is updated */
59 wxGSocketIOHandler
*m_handler
;
61 // true if socket should fire events
64 // pointer for storing extra (usually GUI-specific) data
65 void *m_gui_dependent
;
68 // notify the associated wxSocket about a change in socket state and shut
69 // down the socket if the event is GSOCK_LOST
70 void OnStateChange(GSocketEvent event
);
73 // A version of GSocketManager which uses FDs for socket IO
75 // This class uses GSocket::m_gui_dependent field to store the 2 (for input and
76 // output) FDs associated with the socket.
77 class GSocketFDBasedManager
: public GSocketManager
80 // no special initialization/cleanup needed when using FDs
81 virtual bool OnInit() { return true; }
82 virtual void OnExit() { }
84 // allocate/free the storage we need
85 virtual bool Init_Socket(GSocket
*socket
)
87 socket
->m_gui_dependent
= malloc(sizeof(int)*2);
88 int * const fds
= static_cast<int *>(socket
->m_gui_dependent
);
95 virtual void Destroy_Socket(GSocket
*socket
)
97 free(socket
->m_gui_dependent
);
100 virtual void Enable_Events(GSocket
*socket
)
102 Install_Callback(socket
, GSOCK_INPUT
);
103 Install_Callback(socket
, GSOCK_OUTPUT
);
105 virtual void Disable_Events(GSocket
*socket
)
107 Uninstall_Callback(socket
, GSOCK_INPUT
);
108 Uninstall_Callback(socket
, GSOCK_OUTPUT
);
112 // identifies either input or output direction
114 // NB: the values of this enum shouldn't change
121 // get the FD index corresponding to the given GSocketEvent
122 SocketDir
GetDirForEvent(GSocket
*socket
, GSocketEvent event
)
127 wxFAIL_MSG( "unexpected socket event" );
139 case GSOCK_CONNECTION
:
140 // FIXME: explain this?
141 return socket
->m_server
? FD_INPUT
: FD_OUTPUT
;
145 // access the FDs we store
146 int& FD(GSocket
*socket
, SocketDir d
)
148 return static_cast<int *>(socket
->m_gui_dependent
)[d
];
152 // Common base class for all ports using X11-like (and hence implemented in
153 // X11, Motif and GTK) AddInput() and RemoveInput() functions
154 class GSocketInputBasedManager
: public GSocketFDBasedManager
157 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
)
159 wxCHECK_RET( socket
->m_fd
!= -1,
160 "shouldn't be called on invalid socket" );
162 const SocketDir d
= GetDirForEvent(socket
, event
);
164 int& fd
= FD(socket
, d
);
168 fd
= AddInput(socket
, d
);
171 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
)
173 const SocketDir d
= GetDirForEvent(socket
, event
);
175 int& fd
= FD(socket
, d
);
184 // these functions map directly to XtAdd/RemoveInput() or
185 // gdk_input_add/remove()
186 virtual int AddInput(GSocket
*socket
, SocketDir d
) = 0;
187 virtual void RemoveInput(int fd
) = 0;
190 #endif /* _WX_UNIX_GSOCKUNX_H_ */