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
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 void SetNonBlocking(bool non_block
);
38 void SetTimeout(unsigned long millisec
);
39 GSocketError WXDLLIMPEXP_NET
GetError();
40 void SetCallback(GSocketEventFlags flags
,
41 GSocketCallback callback
, char *cdata
);
42 void UnsetCallback(GSocketEventFlags flags
);
43 GSocketError
GetSockOpt(int level
, int optname
, void *optval
, int *optlen
);
44 GSocketError
SetSockOpt(int level
, int optname
,
45 const void *optval
, int optlen
);
46 //attach or detach from main loop
47 void Notify(bool flag
);
48 virtual void Detected_Read();
49 virtual void Detected_Write();
50 void SetInitialSocketBuffers(int recv
, int send
)
52 m_initialRecvBufferSize
= recv
;
53 m_initialSendBufferSize
= send
;
57 //enable or disable event callback using gsocket gui callback table
58 void EnableEvents(bool flag
= true);
59 void DisableEvents() { EnableEvents(false); }
60 void Enable(GSocketEvent event
);
61 void Disable(GSocketEvent event
);
62 GSocketError
Input_Timeout();
63 GSocketError
Output_Timeout();
64 int Recv_Stream(char *buffer
, int size
);
65 int Recv_Dgram(char *buffer
, int size
);
66 int Send_Stream(const char *buffer
, int size
);
67 int Send_Dgram(const char *buffer
, int size
);
69 /* DFE: We can't protect these data member until the GUI code is updated */
71 wxGSocketIOHandler
*m_handler
;
73 // true if socket should fire events
76 // pointer for storing extra (usually GUI-specific) data
77 void *m_gui_dependent
;
80 // A version of GSocketManager which uses FDs for socket IO
82 // This class uses GSocket::m_gui_dependent field to store the 2 (for input and
83 // output) FDs associated with the socket.
84 class GSocketFDBasedManager
: public GSocketManager
87 // no special initialization/cleanup needed when using FDs
88 virtual bool OnInit() { return true; }
89 virtual void OnExit() { }
91 // allocate/free the storage we need
92 virtual bool Init_Socket(GSocket
*socket
)
94 socket
->m_gui_dependent
= malloc(sizeof(int)*2);
95 int * const fds
= static_cast<int *>(socket
->m_gui_dependent
);
102 virtual void Destroy_Socket(GSocket
*socket
)
104 free(socket
->m_gui_dependent
);
107 virtual void Enable_Events(GSocket
*socket
)
109 Install_Callback(socket
, GSOCK_INPUT
);
110 Install_Callback(socket
, GSOCK_OUTPUT
);
112 virtual void Disable_Events(GSocket
*socket
)
114 Uninstall_Callback(socket
, GSOCK_INPUT
);
115 Uninstall_Callback(socket
, GSOCK_OUTPUT
);
119 // identifies either input or output direction
121 // NB: the values of this enum shouldn't change
128 // get the FD index corresponding to the given GSocketEvent
129 SocketDir
GetDirForEvent(GSocket
*socket
, GSocketEvent event
)
134 wxFAIL_MSG( "unexpected socket event" );
146 case GSOCK_CONNECTION
:
147 // FIXME: explain this?
148 return socket
->m_server
? FD_INPUT
: FD_OUTPUT
;
152 // access the FDs we store
153 int& FD(GSocket
*socket
, SocketDir d
)
155 return static_cast<int *>(socket
->m_gui_dependent
)[d
];
159 // Common base class for all ports using X11-like (and hence implemented in
160 // X11, Motif and GTK) AddInput() and RemoveInput() functions
161 class GSocketInputBasedManager
: public GSocketFDBasedManager
164 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
)
166 wxCHECK_RET( socket
->m_fd
!= -1,
167 "shouldn't be called on invalid socket" );
169 const SocketDir d
= GetDirForEvent(socket
, event
);
171 int& fd
= FD(socket
, d
);
175 fd
= AddInput(socket
, d
);
178 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
)
180 const SocketDir d
= GetDirForEvent(socket
, event
);
182 int& fd
= FD(socket
, d
);
191 // these functions map directly to XtAdd/RemoveInput() or
192 // gdk_input_add/remove()
193 virtual int AddInput(GSocket
*socket
, SocketDir d
) = 0;
194 virtual void RemoveInput(int fd
) = 0;
197 #endif /* _WX_UNIX_GSOCKUNX_H_ */