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_
16 class wxGSocketIOHandler
;
18 class GSocket
: public GSocketBase
21 GSocket(wxSocketBase
& wxsocket
);
24 virtual void Shutdown();
25 virtual GSocket
*WaitConnection(wxSocketBase
& wxsocket
);
27 GSocketError
SetServer();
31 GSocketError
Connect(GSocketStream stream
);
32 GSocketError
SetNonOriented();
33 int Read(char *buffer
, int size
);
34 int Write(const char *buffer
, int size
);
35 void SetNonBlocking(bool non_block
);
36 GSocketError WXDLLIMPEXP_NET
GetError();
37 GSocketError
GetSockOpt(int level
, int optname
, void *optval
, int *optlen
);
38 GSocketError
SetSockOpt(int level
, int optname
,
39 const void *optval
, int optlen
);
40 //attach or detach from main loop
41 void Notify(bool flag
);
43 void Detected_Write();
46 // enable or disable notifications for socket input/output events but only
47 // if m_use_events is true; do nothing otherwise
57 DoEnableEvents(false);
60 // really enable or disable socket input/output events, regardless of
62 void DoEnableEvents(bool enable
);
65 // enable or disable events for the given event if m_use_events; do nothing
68 // notice that these functions also update m_detected: EnableEvent() clears
69 // the corresponding bit in it and DisableEvent() sets it
70 void EnableEvent(GSocketEvent event
);
71 void DisableEvent(GSocketEvent event
);
74 GSocketError
Input_Timeout();
75 GSocketError
Output_Timeout();
76 int Recv_Stream(char *buffer
, int size
);
77 int Recv_Dgram(char *buffer
, int size
);
78 int Send_Stream(const char *buffer
, int size
);
79 int Send_Dgram(const char *buffer
, int size
);
81 /* DFE: We can't protect these data member until the GUI code is updated */
83 wxGSocketIOHandler
*m_handler
;
85 // true if socket should fire events
88 // pointer for storing extra (usually GUI-specific) data
89 void *m_gui_dependent
;
92 // notify the associated wxSocket about a change in socket state and shut
93 // down the socket if the event is GSOCK_LOST
94 void OnStateChange(GSocketEvent event
);
97 // A version of GSocketManager which uses FDs for socket IO
99 // This class uses GSocket::m_gui_dependent field to store the 2 (for input and
100 // output) FDs associated with the socket.
101 class GSocketFDBasedManager
: public GSocketManager
104 // no special initialization/cleanup needed when using FDs
105 virtual bool OnInit() { return true; }
106 virtual void OnExit() { }
108 // allocate/free the storage we need
109 virtual bool Init_Socket(GSocket
*socket
)
111 socket
->m_gui_dependent
= malloc(sizeof(int)*2);
112 int * const fds
= static_cast<int *>(socket
->m_gui_dependent
);
120 virtual void Close_Socket(GSocket
*socket
)
122 Uninstall_Callback(socket
, GSOCK_INPUT
);
123 Uninstall_Callback(socket
, GSOCK_OUTPUT
);
128 virtual void Destroy_Socket(GSocket
*socket
)
130 free(socket
->m_gui_dependent
);
134 // identifies either input or output direction
136 // NB: the values of this enum shouldn't change
143 // get the FD index corresponding to the given GSocketEvent
144 SocketDir
GetDirForEvent(GSocket
*socket
, GSocketEvent event
)
149 wxFAIL_MSG( "unexpected socket event" );
161 case GSOCK_CONNECTION
:
162 // FIXME: explain this?
163 return socket
->m_server
? FD_INPUT
: FD_OUTPUT
;
167 // access the FDs we store
168 int& FD(GSocket
*socket
, SocketDir d
)
170 return static_cast<int *>(socket
->m_gui_dependent
)[d
];
174 // Common base class for all ports using X11-like (and hence implemented in
175 // X11, Motif and GTK) AddInput() and RemoveInput() functions
176 class GSocketInputBasedManager
: public GSocketFDBasedManager
179 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
)
181 wxCHECK_RET( socket
->m_fd
!= -1,
182 "shouldn't be called on invalid socket" );
184 const SocketDir d
= GetDirForEvent(socket
, event
);
186 int& fd
= FD(socket
, d
);
190 fd
= AddInput(socket
, d
);
193 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
)
195 const SocketDir d
= GetDirForEvent(socket
, event
);
197 int& fd
= FD(socket
, d
);
206 // these functions map directly to XtAdd/RemoveInput() or
207 // gdk_input_add/remove()
208 virtual int AddInput(GSocket
*socket
, SocketDir d
) = 0;
209 virtual void RemoveInput(int fd
) = 0;
212 #endif /* _WX_UNIX_GSOCKUNX_H_ */