refactoring: replace wxSocketDispatcher with more generic wxSelectDispatcher (patch...
[wxWidgets.git] / include / wx / private / selectdispatcher.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/private/selectdispatcher.h
3 // Purpose: wxSelectDispatcher class
4 // Authors: Lukasz Michalski
5 // Modified by:
6 // Created: December 2006
7 // Copyright: (c) Lukasz Michalski
8 // RCS-ID: $Id$
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_PRIVATE_SELECTDISPATCHER_H_
13 #define _WX_PRIVATE_SELECTDISPATCHER_H_
14
15 #include "wx/defs.h"
16
17 #include "wx/hashmap.h"
18
19 static const int wxSELECT_TIMEOUT_INFINITE = -1;
20
21 // handler used to process events on descriptors
22 class wxFDIOHandler
23 {
24 public:
25 // called when descriptor is available for non-blocking read
26 virtual void OnReadWaiting(int fd) = 0;
27
28 // called when descriptor is available for non-blocking write
29 virtual void OnWriteWaiting(int fd) = 0;
30
31 // called when there is exception on descriptor
32 virtual void OnExceptionWaiting(int fd) = 0;
33 };
34
35 // those flags describes sets where descriptor should be added
36 enum wxSelectDispatcherEntryFlags
37 {
38 wxSelectInput = 1,
39 wxSelectOutput = 2,
40 wxSelectException = 4,
41 wxSelectAll = wxSelectInput | wxSelectOutput | wxSelectException
42 };
43
44 WX_DECLARE_HASH_MAP(
45 int,
46 wxFDIOHandler*,
47 wxIntegerHash,
48 wxIntegerEqual,
49 wxFDIOHandlerMap
50 );
51
52 class WXDLLIMPEXP_CORE wxSelectDispatcher
53 {
54 public:
55 // returns instance of the table
56 static wxSelectDispatcher& Get();
57
58 virtual ~wxSelectDispatcher()
59 {
60 }
61
62 // register descriptor in sets.
63 void RegisterFD(int fd, wxFDIOHandler* handler, int flags = wxSelectAll);
64
65 // unregister descriptor from sets and return handler for cleanup
66 wxFDIOHandler* UnregisterFD(int fd, int flags = wxSelectAll);
67
68 // return handler for descriptor or null if fd is not registered
69 wxFDIOHandler* FindHandler(int fd);
70
71 // calls select on registered descriptors and
72 void RunLoop(int timeout = wxSELECT_TIMEOUT_INFINITE);
73
74 protected:
75 wxSelectDispatcher() { }
76
77 private:
78 void ProcessSets(fd_set* readset, fd_set* writeset, fd_set* exeptset, int max_fd);
79
80 fd_set m_readset;
81 fd_set m_writeset;
82 fd_set m_exeptset;
83
84 int m_maxFD;
85 wxFDIOHandlerMap m_handlers;
86
87 static wxSelectDispatcher *ms_instance;
88
89 friend class wxSelectDispatcherModule;
90 };
91
92
93 #endif // _WX_PRIVATE_SOCKETEVTDISPATCH_H_