File: event_handler.hpp

package info (click to toggle)
actor-framework 0.17.6-3.2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 9,008 kB
  • sloc: cpp: 77,684; sh: 674; python: 309; makefile: 13
file content (123 lines) | stat: -rw-r--r-- 4,152 bytes parent folder | download | duplicates (4)
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
/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    C++                           *
 *                     | |     / _ \ | |_       Actor                         *
 *                     | |___ / ___ \|  _|      Framework                     *
 *                      \____/_/   \_|_|                                      *
 *                                                                            *
 * Copyright 2011-2018 Dominik Charousset                                     *
 *                                                                            *
 * Distributed under the terms and conditions of the BSD 3-Clause License or  *
 * (at your option) under the terms and conditions of the Boost Software      *
 * License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE.       *
 *                                                                            *
 * If you did not receive a copy of the license files, see                    *
 * http://opensource.org/licenses/BSD-3-Clause and                            *
 * http://www.boost.org/LICENSE_1_0.txt.                                      *
 ******************************************************************************/

#pragma once

#include "caf/io/fwd.hpp"
#include "caf/io/network/native_socket.hpp"
#include "caf/io/network/operation.hpp"
#include "caf/io/receive_policy.hpp"

namespace caf {
namespace io {
namespace network {

/// A socket I/O event handler.
class event_handler {
public:
  /// Stores various status flags and user-defined config parameters.
  struct state {
    /// Stores whether the socket is currently registered for reading.
    bool reading : 1;

    /// Stores whether the socket is currently registered for writing.
    bool writing : 1;

    /// Stores whether the parent actor demanded write receipts.
    bool ack_writes : 1;

    /// Stores whether graceful_shutdown() was called.
    bool shutting_down : 1;

    /// Stores what receive policy is currently active.
    unsigned rd_flag : 2;
  };

  event_handler(default_multiplexer& dm, native_socket sockfd);

  virtual ~event_handler();

  /// Returns true once the requested operation is done, i.e.,
  /// to signalize the multiplexer to remove this handler.
  /// The handler remains in the event loop as long as it returns false.
  virtual void handle_event(operation op) = 0;

  /// Callback to signalize that this handler has been removed
  /// from the event loop for operations of type `op`.
  virtual void removed_from_loop(operation op) = 0;

  /// Shuts down communication on the managed socket, eventually removing
  /// this event handler from the I/O loop.
  virtual void graceful_shutdown() = 0;

  /// Returns the native socket handle for this handler.
  native_socket fd() const {
    return fd_;
  }

  /// Returns the `multiplexer` this acceptor belongs to.
  default_multiplexer& backend() {
    return backend_;
  }

  /// Returns the bit field storing the subscribed events.
  int eventbf() const {
    return eventbf_;
  }

  /// Sets the bit field storing the subscribed events.
  void eventbf(int value) {
    eventbf_ = value;
  }

  /// Checks whether `close_read_channel` has been called.
  bool read_channel_closed() const {
    return !state_.reading;
  }

  /// Removes the file descriptor from the event loop of the parent.
  void passivate();

  /// Returns whether this event handlers signals successful writes to its
  /// parent actor.
  bool ack_writes() {
    return state_.ack_writes;
  }

  /// Sets whether this event handlers signals successful writes to its parent
  /// actor.
  void ack_writes(bool x) {
    state_.ack_writes = x;
  }

protected:
  /// Adds the file descriptor to the event loop of the parent.
  void activate();

  /// Sets flags for asynchronous event handling on the socket handle.
  void set_fd_flags();

  native_socket fd_;
  state state_;
  int eventbf_;
  default_multiplexer& backend_;
};

} // namespace network
} // namespace io
} // namespace caf