1
#pragma once
2

            
3
#include <cstdint>
4

            
5
#include "envoy/event/dispatcher.h"
6
#include "envoy/event/file_event.h"
7

            
8
#include "source/common/common/assert.h"
9
#include "source/extensions/io_socket/user_space/io_handle.h"
10

            
11
namespace Envoy {
12

            
13
namespace Extensions {
14
namespace IoSocket {
15
namespace UserSpace {
16

            
17
// A FileEvent implementation which is used to drive UserSpaceHandle.
18
// Declare the class final to safely call virtual function setEnabled in constructor.
19
class FileEventImpl final : public Event::FileEvent, Logger::Loggable<Logger::Id::io> {
20
public:
21
  FileEventImpl(Event::Dispatcher& dispatcher, Event::FileReadyCb cb, uint32_t events,
22
                IoHandle& io_source);
23

            
24
  // Event::FileEvent
25
  void activate(uint32_t events) override;
26
  void setEnabled(uint32_t events) override;
27

            
28
  // This event always acts as edge triggered regardless the underlying OS is level or
29
  // edge triggered. The event owner on windows platform should not emulate edge events.
30
1
  void unregisterEventIfEmulatedEdge(uint32_t) override {}
31
1
  void registerEventIfEmulatedEdge(uint32_t) override {}
32

            
33
  // Notify events. Unlike activate() method, this method activates the given events only if the
34
  // events are enabled.
35
  void activateIfEnabled(uint32_t events);
36

            
37
private:
38
  // This class maintains the ephemeral events and enabled events.
39
  class EventListener {
40
  public:
41
    ~EventListener() = default;
42

            
43
    // Reset the enabled events. The caller must refresh the triggered events.
44
151
    void setEnabledEvents(uint32_t enabled_events) { enabled_events_ = enabled_events; }
45

            
46
    // Return the enabled events.
47
120
    uint32_t getEnabledEvents() { return enabled_events_; }
48

            
49
151
    void clearEphemeralEvents() {
50
      // Clear ephemeral events to align with FileEventImpl::setEnabled().
51
151
      ephemeral_events_ = 0;
52
151
    }
53

            
54
375
    void onEventActivated(uint32_t activated_events) { ephemeral_events_ |= activated_events; }
55

            
56
291
    uint32_t getAndClearEphemeralEvents() { return std::exchange(ephemeral_events_, 0); }
57

            
58
  private:
59
    // The events set by activate() and will be cleared after the io callback.
60
    uint32_t ephemeral_events_{};
61
    // The events set by setEnabled(). The new value replaces the old value.
62
    uint32_t enabled_events_{};
63
  };
64

            
65
  // Used to populate the event operations of enable and activate.
66
  EventListener event_listener_;
67

            
68
  // The handle to registered async callback from dispatcher.
69
  Event::SchedulableCallbackPtr schedulable_;
70

            
71
  // Supplies readable and writable status.
72
  IoHandle& io_source_;
73
};
74
} // namespace UserSpace
75
} // namespace IoSocket
76
} // namespace Extensions
77
} // namespace Envoy