File indexing completed on 2024-05-05 17:50:04

0001 /*
0002    Copyright (C) 2018 Andreas Hartmetz <ahartmetz@gmail.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LGPL.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 
0019    Alternatively, this file is available under the Mozilla Public License
0020    Version 1.1.  You may obtain a copy of the License at
0021    http://www.mozilla.org/MPL/
0022 */
0023 
0024 #ifndef IIOEVENTFORWARDER_H
0025 #define IIOEVENTFORWARDER_H
0026 
0027 #include "iioeventlistener.h"
0028 #include "iioeventsource.h"
0029 
0030 // How to use:
0031 // - construct with upstream source as constructor argument
0032 // - connect listener: addIoListener(listener)
0033 // - use a reimplementation of handleIoReady() to observe I/O
0034 // - listener will remove itself automatically when it closes the I/O channel, which also causes
0035 //   the forwarder to remove itself from the source (see removeIoListenerInternal() implementation)
0036 // - it is possible to start over at this point, if needed (connect listener, etc)
0037 // NOTE: This connects one source to ONE listener, not one to many like a generic IIoEventSource does.
0038 //       More is not needed for the use cases of this class (Connection and Server).
0039 class IIoEventForwarder : public IIoEventListener, public IIoEventSource
0040 {
0041 public:
0042     IIoEventForwarder(IIoEventSource *upstreamSource)
0043        : m_upstream(upstreamSource) {}
0044 
0045     // IO::Status IIOEventListener::handleIoReady(IO::RW rw) override;
0046     // not overridden - users of this class reimplement it to spy on and/or intercept I/O events!
0047 
0048     // IIOEventListener
0049     FileDescriptor fileDescriptor() const override;
0050 
0051     // This only works due to the one-to-one limitation explained above.
0052     IIoEventListener *downstreamListener();
0053 
0054 protected:
0055     // IIOEventSource
0056     void addIoListenerInternal(IIoEventListener *iol, uint32 ioRw) override;
0057     void removeIoListenerInternal(IIoEventListener *iol) override;
0058     void updateIoInterestInternal(IIoEventListener *iol, uint32 ioRw) override;
0059 
0060 private:
0061     IIoEventSource *m_upstream = nullptr;
0062     IIoEventListener *m_downstream = nullptr;
0063 };
0064 
0065 #endif // IIOEVENTFORWARDER_H