File: selectdispatcher.h

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (126 lines) | stat: -rw-r--r-- 3,597 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/////////////////////////////////////////////////////////////////////////////
// Name:        wx/private/selectdispatcher.h
// Purpose:     wxSelectDispatcher class
// Authors:     Lukasz Michalski and Vadim Zeitlin
// Created:     December 2006
// Copyright:   (c) Lukasz Michalski
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

#ifndef _WX_PRIVATE_SELECTDISPATCHER_H_
#define _WX_PRIVATE_SELECTDISPATCHER_H_

#include "wx/defs.h"

#if wxUSE_SELECT_DISPATCHER

#if defined(HAVE_SYS_SELECT_H) || defined(__WATCOMC__)
    #include <sys/time.h>
    #include <sys/select.h>
#endif

#ifdef __WATCOMC__
    #include <types.h>
    #include <sys/ioctl.h>
    #include <tcpustd.h>
#else
    #include <sys/types.h>
#endif

#include "wx/private/fdiodispatcher.h"

// helper class storing all the select() fd sets
class WXDLLIMPEXP_BASE wxSelectSets
{
public:
    // ctor zeroes out all fd_sets
    wxSelectSets();

    // default copy ctor, assignment operator and dtor are ok


    // return true if fd appears in any of the sets
    bool HasFD(int fd) const;

    // add or remove FD to our sets depending on whether flags contains
    // wxFDIO_INPUT/OUTPUT/EXCEPTION bits
    bool SetFD(int fd, int flags);

    // same as SetFD() except it unsets the bits set in the flags for the given
    // fd
    bool ClearFD(int fd)
    {
        return SetFD(fd, 0);
    }


    // call select() with our sets: the other parameters are the same as for
    // select() itself
    int Select(int nfds, struct timeval *tv);

    // call the handler methods corresponding to the sets having this fd if it
    // is present in any set and return true if it is
    bool Handle(int fd, wxFDIOHandler& handler) const;

private:
    typedef void (wxFDIOHandler::*Callback)();

    // the FD sets indices
    enum
    {
        Read,
        Write,
        Except,
        Max
    };

    // the sets used with select()
    fd_set m_fds[Max];

    // the wxFDIO_XXX flags, functions and names (used for debug messages only)
    // corresponding to the FD sets above
    static int ms_flags[Max];
    static const char *ms_names[Max];
    static Callback ms_handlers[Max];
};

class WXDLLIMPEXP_BASE wxSelectDispatcher : public wxMappedFDIODispatcher
{
public:
    // default ctor
    wxSelectDispatcher() { m_maxFD = -1; }

    // implement pure virtual methods of the base class
    virtual bool RegisterFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL);
    virtual bool ModifyFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL);
    virtual bool UnregisterFD(int fd);
    virtual bool HasPending() const;
    virtual int Dispatch(int timeout = TIMEOUT_INFINITE);

private:
    // common part of RegisterFD() and ModifyFD()
    bool DoUpdateFDAndHandler(int fd, wxFDIOHandler *handler, int flags);

    // call the handlers for the fds present in the given sets, return the
    // number of handlers we called
    int ProcessSets(const wxSelectSets& sets);

    // helper of ProcessSets(): call the handler if its fd is in the set
    void DoProcessFD(int fd, const fd_set& fds, wxFDIOHandler *handler,
                     const char *name);

    // common part of HasPending() and Dispatch(): calls select() with the
    // specified timeout
    int DoSelect(wxSelectSets& sets, int timeout) const;


    // the select sets containing all the registered fds
    wxSelectSets m_sets;

    // the highest registered fd value or -1 if none
    int m_maxFD;
};

#endif // wxUSE_SELECT_DISPATCHER

#endif // _WX_PRIVATE_SOCKETEVTDISPATCH_H_