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
22 virtual void Shutdown();
23 GSocketError
SetLocal(GAddress
*address
);
24 GSocketError
SetPeer(GAddress
*address
);
27 GSocketError
SetServer();
28 GSocket
*WaitConnection();
32 GSocketError
Connect(GSocketStream stream
);
33 GSocketError
SetNonOriented();
34 int Read(char *buffer
, int size
);
35 int Write(const char *buffer
, int size
);
36 void SetNonBlocking(bool non_block
);
37 void SetTimeout(unsigned long millisec
);
38 GSocketError WXDLLIMPEXP_NET
GetError();
39 void SetCallback(GSocketEventFlags flags
,
40 GSocketCallback callback
, char *cdata
);
41 void UnsetCallback(GSocketEventFlags flags
);
42 GSocketError
GetSockOpt(int level
, int optname
, void *optval
, int *optlen
);
43 GSocketError
SetSockOpt(int level
, int optname
,
44 const void *optval
, int optlen
);
45 //attach or detach from main loop
46 void Notify(bool flag
);
48 void Detected_Write();
49 void SetInitialSocketBuffers(int recv
, int send
)
51 m_initialRecvBufferSize
= recv
;
52 m_initialSendBufferSize
= send
;
56 //enable or disable event callback using gsocket gui callback table
57 void EnableEvents(bool flag
= true);
58 void DisableEvents() { EnableEvents(false); }
59 void Enable(GSocketEvent event
);
60 void Disable(GSocketEvent event
);
61 GSocketError
Input_Timeout();
62 GSocketError
Output_Timeout();
63 int Recv_Stream(char *buffer
, int size
);
64 int Recv_Dgram(char *buffer
, int size
);
65 int Send_Stream(const char *buffer
, int size
);
66 int Send_Dgram(const char *buffer
, int size
);
68 /* DFE: We can't protect these data member until the GUI code is updated */
70 wxGSocketIOHandler
*m_handler
;
72 // true if socket should fire events
75 // pointer for storing extra (usually GUI-specific) data
76 void *m_gui_dependent
;
79 // A version of GSocketManager which uses FDs for socket IO
81 // This class uses GSocket::m_gui_dependent field to store the 2 (for input and
82 // output) FDs associated with the socket.
83 class GSocketFDBasedManager
: public GSocketManager
86 // no special initialization/cleanup needed when using FDs
87 virtual bool OnInit() { return true; }
88 virtual void OnExit() { }
90 // allocate/free the storage we need
91 virtual bool Init_Socket(GSocket
*socket
)
93 socket
->m_gui_dependent
= malloc(sizeof(int)*2);
94 int * const fds
= static_cast<int *>(socket
->m_gui_dependent
);
101 virtual void Destroy_Socket(GSocket
*socket
)
103 free(socket
->m_gui_dependent
);
106 virtual void Enable_Events(GSocket
*socket
)
108 Install_Callback(socket
, GSOCK_INPUT
);
109 Install_Callback(socket
, GSOCK_OUTPUT
);
111 virtual void Disable_Events(GSocket
*socket
)
113 Uninstall_Callback(socket
, GSOCK_INPUT
);
114 Uninstall_Callback(socket
, GSOCK_OUTPUT
);
118 // identifies either input or output direction
120 // NB: the values of this enum shouldn't change
127 // get the FD index corresponding to the given GSocketEvent
128 SocketDir
GetDirForEvent(GSocket
*socket
, GSocketEvent event
)
133 wxFAIL_MSG( "unexpected socket event" );
145 case GSOCK_CONNECTION
:
146 // FIXME: explain this?
147 return socket
->m_server
? FD_INPUT
: FD_OUTPUT
;
151 // access the FDs we store
152 int& FD(GSocket
*socket
, SocketDir d
)
154 return static_cast<int *>(socket
->m_gui_dependent
)[d
];
158 // Common base class for all ports using X11-like (and hence implemented in
159 // X11, Motif and GTK) AddInput() and RemoveInput() functions
160 class GSocketInputBasedManager
: public GSocketFDBasedManager
163 virtual void Install_Callback(GSocket
*socket
, GSocketEvent event
)
165 wxCHECK_RET( socket
->m_fd
!= -1,
166 "shouldn't be called on invalid socket" );
168 const SocketDir d
= GetDirForEvent(socket
, event
);
170 int& fd
= FD(socket
, d
);
174 fd
= AddInput(socket
, d
);
177 virtual void Uninstall_Callback(GSocket
*socket
, GSocketEvent event
)
179 const SocketDir d
= GetDirForEvent(socket
, event
);
181 int& fd
= FD(socket
, d
);
190 // these functions map directly to XtAdd/RemoveInput() or
191 // gdk_input_add/remove()
192 virtual int AddInput(GSocket
*socket
, SocketDir d
) = 0;
193 virtual void RemoveInput(int fd
) = 0;
196 #endif /* _WX_UNIX_GSOCKUNX_H_ */