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
;
21 bool IsOk() { return m_ok
; }
24 GSocketError
SetLocal(GAddress
*address
);
25 GSocketError
SetPeer(GAddress
*address
);
28 GSocketError
SetServer();
29 GSocket
*WaitConnection();
33 GSocketError
Connect(GSocketStream stream
);
34 GSocketError
SetNonOriented();
35 int Read(char *buffer
, int size
);
36 int Write(const char *buffer
, int size
);
37 GSocketEventFlags
Select(GSocketEventFlags flags
);
38 void SetNonBlocking(bool non_block
);
39 void SetTimeout(unsigned long millisec
);
40 GSocketError WXDLLIMPEXP_NET
GetError();
41 void SetCallback(GSocketEventFlags flags
,
42 GSocketCallback callback
, char *cdata
);
43 void UnsetCallback(GSocketEventFlags flags
);
44 GSocketError
GetSockOpt(int level
, int optname
, void *optval
, int *optlen
);
45 GSocketError
SetSockOpt(int level
, int optname
,
46 const void *optval
, int optlen
);
47 //attach or detach from main loop
48 void Notify(bool flag
);
49 virtual void Detected_Read();
50 virtual void Detected_Write();
51 void SetInitialSocketBuffers(int recv
, int send
)
53 m_initialRecvBufferSize
= recv
;
54 m_initialSendBufferSize
= send
;
58 //enable or disable event callback using gsocket gui callback table
59 void EnableEvents(bool flag
= true);
60 void DisableEvents() { EnableEvents(false); }
61 void Enable(GSocketEvent event
);
62 void Disable(GSocketEvent event
);
63 GSocketError
Input_Timeout();
64 GSocketError
Output_Timeout();
65 int Recv_Stream(char *buffer
, int size
);
66 int Recv_Dgram(char *buffer
, int size
);
67 int Send_Stream(const char *buffer
, int size
);
68 int Send_Dgram(const char *buffer
, int size
);
70 int m_initialRecvBufferSize
;
71 int m_initialSendBufferSize
;
73 /* DFE: We can't protect these data member until the GUI code is updated */
76 wxGSocketIOHandler
*m_handler
;
88 unsigned long m_timeout
;
90 // true if socket should fire events
94 GSocketEventFlags m_detected
;
95 GSocketCallback m_cbacks
[GSOCK_MAX_EVENT
];
96 char *m_data
[GSOCK_MAX_EVENT
];
98 // pointer for storing extra (usually GUI-specific) data
99 void *m_gui_dependent
;
102 /* Definition of GAddress */
105 struct sockaddr
*m_addr
;
108 GAddressType m_family
;
111 GSocketError m_error
;
116 GSocketError
_GAddress_translate_from(GAddress
*address
,
117 struct sockaddr
*addr
, int len
);
118 GSocketError
_GAddress_translate_to (GAddress
*address
,
119 struct sockaddr
**addr
, int *len
);
120 GSocketError
_GAddress_Init_INET(GAddress
*address
);
121 GSocketError
_GAddress_Init_UNIX(GAddress
*address
);
123 // A version of GSocketManager which uses FDs for socket IO
125 // This class uses GSocket::m_gui_dependent field to store the 2 (for input and
126 // output) FDs associated with the socket.
127 class GSocketFDBasedManager
: public GSocketManager
130 // no special initialization/cleanup needed when using FDs
131 virtual bool OnInit() { return true; }
132 virtual void OnExit() { }
134 // allocate/free the storage we need
135 virtual bool Init_Socket(GSocket
*socket
)
137 socket
->m_gui_dependent
= malloc(sizeof(int)*2);
138 int * const fds
= wx_static_cast(int *, socket
->m_gui_dependent
);
145 virtual void Destroy_Socket(GSocket
*socket
)
147 free(socket
->m_gui_dependent
);
150 virtual void Enable_Events(GSocket
*socket
)
152 Install_Callback(socket
, GSOCK_INPUT
);
153 Install_Callback(socket
, GSOCK_OUTPUT
);
155 virtual void Disable_Events(GSocket
*socket
)
157 Uninstall_Callback(socket
, GSOCK_INPUT
);
158 Uninstall_Callback(socket
, GSOCK_OUTPUT
);
162 // identifies either input or output direction
164 // NB: the values of this enum shouldn't change
171 // get the FD index corresponding to the given GSocketEvent
172 SocketDir
GetDirForEvent(GSocket
*socket
, GSocketEvent event
)
177 wxFAIL_MSG( "unexpected socket event" );
189 case GSOCK_CONNECTION
:
190 // FIXME: explain this?
191 return socket
->m_server
? FD_INPUT
: FD_OUTPUT
;
195 // access the FDs we store
196 int& FD(GSocket
*socket
, SocketDir d
)
198 return wx_static_cast(int *, socket
->m_gui_dependent
)[d
];
202 // Common base class for all ports using X11-like (and hence implemented in
203 // X11, Motif and GTK) AddInput() and RemoveInput() functions
204 class GSocketInputBasedManager
: public GSocketFDBasedManager
207 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
)
209 wxCHECK_RET( socket
->m_fd
!= -1,
210 "shouldn't be called on invalid socket" );
212 const SocketDir d
= GetDirForEvent(socket
, event
);
214 int& fd
= FD(socket
, d
);
218 fd
= AddInput(socket
, d
);
221 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
)
223 const SocketDir d
= GetDirForEvent(socket
, event
);
225 int& fd
= FD(socket
, d
);
234 // these functions map directly to XtAdd/RemoveInput() or
235 // gdk_input_add/remove()
236 virtual int AddInput(GSocket
*socket
, SocketDir d
) = 0;
237 virtual void RemoveInput(int fd
) = 0;
240 #endif /* _WX_UNIX_GSOCKUNX_H_ */