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
);
22 virtual void Shutdown();
23 virtual GSocket
*WaitConnection(wxSocketBase
& wxsocket
);
25 GSocketError
SetServer();
29 GSocketError
Connect(GSocketStream stream
);
30 GSocketError
SetNonOriented();
31 int Read(char *buffer
, int size
);
32 int Write(const char *buffer
, int size
);
33 void SetNonBlocking(bool non_block
);
34 GSocketError WXDLLIMPEXP_NET
GetError();
35 GSocketError
GetSockOpt(int level
, int optname
, void *optval
, int *optlen
);
36 GSocketError
SetSockOpt(int level
, int optname
,
37 const void *optval
, int optlen
);
38 //attach or detach from main loop
39 void Notify(bool flag
);
41 void Detected_Write();
44 // enable or disable notifications for socket input/output events but only
45 // if m_use_events is true; do nothing otherwise
55 DoEnableEvents(false);
58 // really enable or disable socket input/output events, regardless of
60 void DoEnableEvents(bool enable
);
63 // enable or disable events for the given event if m_use_events; do nothing
66 // notice that these functions also update m_detected: EnableEvent() clears
67 // the corresponding bit in it and DisableEvent() sets it
68 void EnableEvent(GSocketEvent event
);
69 void DisableEvent(GSocketEvent event
);
72 GSocketError
Input_Timeout();
73 GSocketError
Output_Timeout();
74 int Recv_Stream(char *buffer
, int size
);
75 int Recv_Dgram(char *buffer
, int size
);
76 int Send_Stream(const char *buffer
, int size
);
77 int Send_Dgram(const char *buffer
, int size
);
79 /* DFE: We can't protect these data member until the GUI code is updated */
81 wxGSocketIOHandler
*m_handler
;
83 // true if socket should fire events
86 // pointer for storing extra (usually GUI-specific) data
87 void *m_gui_dependent
;
90 // notify the associated wxSocket about a change in socket state and shut
91 // down the socket if the event is GSOCK_LOST
92 void OnStateChange(GSocketEvent event
);
95 // A version of GSocketManager which uses FDs for socket IO
97 // This class uses GSocket::m_gui_dependent field to store the 2 (for input and
98 // output) FDs associated with the socket.
99 class GSocketFDBasedManager
: public GSocketManager
102 // no special initialization/cleanup needed when using FDs
103 virtual bool OnInit() { return true; }
104 virtual void OnExit() { }
106 // allocate/free the storage we need
107 virtual bool Init_Socket(GSocket
*socket
)
109 socket
->m_gui_dependent
= malloc(sizeof(int)*2);
110 int * const fds
= static_cast<int *>(socket
->m_gui_dependent
);
118 virtual void Close_Socket(GSocket
*socket
)
120 Uninstall_Callback(socket
, GSOCK_INPUT
);
121 Uninstall_Callback(socket
, GSOCK_OUTPUT
);
126 virtual void Destroy_Socket(GSocket
*socket
)
128 free(socket
->m_gui_dependent
);
132 // identifies either input or output direction
134 // NB: the values of this enum shouldn't change
141 // get the FD index corresponding to the given GSocketEvent
142 SocketDir
GetDirForEvent(GSocket
*socket
, GSocketEvent event
)
147 wxFAIL_MSG( "unexpected socket event" );
159 case GSOCK_CONNECTION
:
160 // FIXME: explain this?
161 return socket
->m_server
? FD_INPUT
: FD_OUTPUT
;
165 // access the FDs we store
166 int& FD(GSocket
*socket
, SocketDir d
)
168 return static_cast<int *>(socket
->m_gui_dependent
)[d
];
172 // Common base class for all ports using X11-like (and hence implemented in
173 // X11, Motif and GTK) AddInput() and RemoveInput() functions
174 class GSocketInputBasedManager
: public GSocketFDBasedManager
177 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
)
179 wxCHECK_RET( socket
->m_fd
!= -1,
180 "shouldn't be called on invalid socket" );
182 const SocketDir d
= GetDirForEvent(socket
, event
);
184 int& fd
= FD(socket
, d
);
188 fd
= AddInput(socket
, d
);
191 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
)
193 const SocketDir d
= GetDirForEvent(socket
, event
);
195 int& fd
= FD(socket
, d
);
204 // these functions map directly to XtAdd/RemoveInput() or
205 // gdk_input_add/remove()
206 virtual int AddInput(GSocket
*socket
, SocketDir d
) = 0;
207 virtual void RemoveInput(int fd
) = 0;
210 #endif /* _WX_UNIX_GSOCKUNX_H_ */