Switch to unified view

a/src/utils/netcon.h b/src/utils/netcon.h
...
...
21
#else
21
#else
22
#include "config.h"
22
#include "config.h"
23
#endif
23
#endif
24
24
25
#include <sys/time.h>
25
#include <sys/time.h>
26
#include <map>
26
27
#include <string>
27
#include <string>
28
29
#include <memory>
28
#include <memory>
30
29
31
/// A set of classes to manage client-server communication over a
30
/// A set of classes to manage client-server communication over a
32
/// connection-oriented network, or a pipe.
31
/// connection-oriented network, or a pipe.
33
///
32
///
...
...
83
    /// connection timeout issues.
82
    /// connection timeout issues.
84
    virtual int set_nonblock(int onoff);
83
    virtual int set_nonblock(int onoff);
85
84
86
    /// Decide what events the connection will be looking for
85
    /// Decide what events the connection will be looking for
87
    /// (NETCONPOLL_READ, NETCONPOLL_WRITE)
86
    /// (NETCONPOLL_READ, NETCONPOLL_WRITE)
88
    int setselevents(int evs) {
87
    int setselevents(int evs);
89
        return m_wantedEvents = evs;
90
    }
91
    /// Retrieve the connection's currently monitored set of events
88
    /// Retrieve the connection's currently monitored set of events
92
    int getselevents() {
89
    int getselevents() {
93
        return m_wantedEvents;
90
        return m_wantedEvents;
94
    }
95
    /// Add events to current set
96
    int addselevents(int evs) {
97
        return m_wantedEvents |= evs;
98
    }
99
    /// Clear events from current set
100
    int clearselevents(int evs) {
101
        return m_wantedEvents &= ~evs;
102
    }
91
    }
103
92
104
    friend class SelectLoop;
93
    friend class SelectLoop;
105
    SelectLoop *getloop() {
94
    SelectLoop *getloop() {
106
        return m_loop;
95
        return m_loop;
...
...
113
protected:
102
protected:
114
    char *m_peer;   // Name of the connected host
103
    char *m_peer;   // Name of the connected host
115
    int   m_fd;
104
    int   m_fd;
116
    bool  m_ownfd;
105
    bool  m_ownfd;
117
    int   m_didtimo;
106
    int   m_didtimo;
118
    // Used when part of the selectloop map.
107
    // Used when part of the selectloop.
119
    short m_wantedEvents;
108
    short m_wantedEvents;
120
    SelectLoop *m_loop;
109
    SelectLoop *m_loop;
121
    // Method called by the selectloop when something can be done with a netcon
110
    // Method called by the selectloop when something can be done with a netcon
122
    virtual int cando(Netcon::Event reason) = 0;
111
    virtual int cando(Netcon::Event reason) = 0;
123
    // Called when added to loop
112
    // Called when added to loop
...
...
128
117
129
118
130
/// The selectloop interface is used to implement parallel servers.
119
/// The selectloop interface is used to implement parallel servers.
131
// The select loop mechanism allows several netcons to be used for io
120
// The select loop mechanism allows several netcons to be used for io
132
// in a program without blocking as long as there is data to be read
121
// in a program without blocking as long as there is data to be read
133
// or written. In a multithread program which is also using select, it
122
// or written. In a multithread program, if each thread needs
134
// would typically make sense to have one SelectLoop active per
123
// non-blocking IO it may make sense to have one SelectLoop active per
135
// thread.
124
// thread.
136
class SelectLoop {
125
class SelectLoop {
137
public:
126
public:
138
    SelectLoop()
127
    SelectLoop();
139
        : m_selectloopDoReturn(false), m_selectloopReturnValue(0),
128
    SelectLoop(const SelectLoop&) = delete;
140
          m_placetostart(0),
129
    SelectLoop& operator=(const SelectLoop&) = delete;
141
          m_periodichandler(0), m_periodicparam(0), m_periodicmillis(0) {
130
    ~SelectLoop();
142
    }
131
    
132
    /// Add a connection to be monitored (this will usually be called
133
    /// from the server's listen connection's accept callback)
134
    int addselcon(NetconP con, int events);
135
136
    /// Remove a connection from the monitored set. This is
137
    /// automatically called when EOF is detected on a connection.
138
    int remselcon(NetconP con);
139
140
    /// Set a function to be called periodically, or a time before return.
141
    /// @param handler the function to be called.
142
    ///  - if it is 0, doLoop() will return after ms mS (and can be called
143
    ///    again)
144
    ///  - if it is not 0, it will be called at ms mS intervals. If its return
145
    ///    value is <= 0, selectloop will return.
146
    /// @param clp client data to be passed to handler at every call.
147
    /// @param ms milliseconds interval between handler calls or
148
    ///   before return. Set to 0 for no periodic handler.
149
    void setperiodichandler(int (*handler)(void *), void *clp, int ms);
143
150
144
    /// Loop waiting for events on the connections and call the
151
    /// Loop waiting for events on the connections and call the
145
    /// cando() method on the object when something happens (this will in
152
    /// cando() method on the object when something happens (this will in
146
    /// turn typically call the app callback set on the netcon). Possibly
153
    /// turn typically call the app callback set on the netcon). Possibly
147
    /// call the periodic handler (if set) at regular intervals.
154
    /// call the periodic handler (if set) at regular intervals.
148
    /// @return -1 for error. 0 if no descriptors left for i/o. 1 for periodic
155
    /// @return -1 for error. 0 if no descriptors left for i/o. 1 for periodic
149
    ///  timeout (should call back in after processing)
156
    ///  timeout (should call back in after processing)
150
    int doLoop();
157
    int doLoop();
151
158
152
    /// Call from data handler: make selectloop return the param value
159
    /// Call from data handler: make doLoop() return @param value
153
    void loopReturn(int value) {
160
    void loopReturn(int value);
154
        m_selectloopDoReturn = true;
155
        m_selectloopReturnValue = value;
156
    }
157
    /// Add a connection to be monitored (this will usually be called
158
    /// from the server's listen connection's accept callback)
159
    int addselcon(NetconP con, int events);
160
    /// Remove a connection from the monitored set. This is
161
    /// automatically called when EOF is detected on a connection.
162
    int remselcon(NetconP con);
163
161
164
    /// Set a function to be called periodically, or a time before return.
162
    friend class Netcon;
165
    /// @param handler the function to be called.
166
    ///  - if it is 0, selectloop() will return after ms mS (and can be called
167
    ///    again
168
    ///  - if it is not 0, it will be called at ms mS intervals. If its return
169
    ///    value is <= 0, selectloop will return.
170
    /// @param clp client data to be passed to handler at every call.
171
    /// @param ms milliseconds interval between handler calls or
172
    ///   before return. Set to 0 for no periodic handler.
173
    void setperiodichandler(int (*handler)(void *), void *clp, int ms);
174
175
private:
163
private:
176
    // Set by client callback to tell selectloop to return.
164
    class Internal;
177
    bool m_selectloopDoReturn;
165
    Internal *m;
178
    int  m_selectloopReturnValue;
179
    int  m_placetostart;
180
181
    // Map of NetconP indexed by fd
182
    std::map<int, NetconP> m_polldata;
183
184
    // The last time we did the periodic thing. Initialized by setperiodic()
185
    struct timeval m_lasthdlcall;
186
    // The call back function and its parameter
187
    int (*m_periodichandler)(void *);
188
    void *m_periodicparam;
189
    // The periodic interval
190
    int m_periodicmillis;
191
    void periodictimeout(struct timeval *tv);
192
    int maybecallperiodic();
193
};
166
};
194
167
195
///////////////////////
168
///////////////////////
196
class NetconData;
169
class NetconData;
197
170