forked from apple/swift-nio-extras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DebugOutboundEventsHandler.swift
177 lines (161 loc) · 7.83 KB
/
DebugOutboundEventsHandler.swift
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if os(macOS) || os(tvOS) || os(iOS) || os(watchOS)
import Darwin
#else
import Glibc
#endif
import NIOCore
/// ChannelOutboundHandler that prints all outbound events that pass through the pipeline by default,
/// overridable by providing your own closure for custom logging. See ``DebugInboundEventsHandler`` for inbound events.
public class DebugOutboundEventsHandler: ChannelOutboundHandler {
/// The type of the outbound data which is wrapped in `NIOAny`.
public typealias OutboundIn = Any
/// The type of the outbound data which will be forwarded to the next `ChannelOutboundHandler` in the `ChannelPipeline`.
public typealias OutboundOut = Any
/// All possible outbound events which could occur.
public enum Event {
/// `Channel` registered for I/O events.
case register
/// Bound to a `SocketAddress`
case bind(address: SocketAddress)
/// Connected to an address.
case connect(address: SocketAddress)
/// Write operation.
case write(data: NIOAny)
/// Pending writes flushed.
case flush
/// Ready to read more data.
case read
/// Close the channel.
case close(mode: CloseMode)
/// User outbound event triggered.
case triggerUserOutboundEvent(event: Any)
}
var logger: (Event, ChannelHandlerContext) -> ()
/// Initialiser.
/// - parameters:
/// - logger: Method for logging events which happen.
public init(logger: @escaping (Event, ChannelHandlerContext) -> () = DebugOutboundEventsHandler.defaultPrint) {
self.logger = logger
}
/// Logs ``Event.register`` to ``logger``
/// Called to request that the `Channel` register itself for I/O events with its `EventLoop`.
/// - parameters:
/// - context: The `ChannelHandlerContext` which this `ChannelHandler` belongs to.
/// - promise: The `EventLoopPromise` which should be notified once the operation completes, or nil if no notification should take place.
public func register(context: ChannelHandlerContext, promise: EventLoopPromise<Void>?) {
logger(.register, context)
context.register(promise: promise)
}
/// Logs ``Event.bind`` to ``logger``
/// Called to request that the `Channel` bind to a specific `SocketAddress`.
/// - parameters:
/// - context: The `ChannelHandlerContext` which this `ChannelHandler` belongs to.
/// - to: The `SocketAddress` to which this `Channel` should bind.
/// - promise: The `EventLoopPromise` which should be notified once the operation completes, or nil if no notification should take place.
public func bind(context: ChannelHandlerContext, to address: SocketAddress, promise: EventLoopPromise<Void>?) {
logger(.bind(address: address), context)
context.bind(to: address, promise: promise)
}
/// Logs ``Event.connect`` to ``logger``
/// Called to request that the `Channel` connect to a given `SocketAddress`.
/// - parameters:
/// - context: The `ChannelHandlerContext` which this `ChannelHandler` belongs to.
/// - to: The `SocketAddress` to which the the `Channel` should connect.
/// - promise: The `EventLoopPromise` which should be notified once the operation completes, or nil if no notification should take place.
public func connect(context: ChannelHandlerContext, to address: SocketAddress, promise: EventLoopPromise<Void>?) {
logger(.connect(address: address), context)
context.connect(to: address, promise: promise)
}
/// Logs ``Event.data`` to ``logger``
/// Called to request a write operation.
/// - parameters:
/// - context: The `ChannelHandlerContext` which this `ChannelHandler` belongs to.
/// - data: The data to write through the `Channel`, wrapped in a `NIOAny`.
/// - promise: The `EventLoopPromise` which should be notified once the operation completes, or nil if no notification should take place.
public func write(context: ChannelHandlerContext, data: NIOAny, promise: EventLoopPromise<Void>?) {
logger(.write(data: data), context)
context.write(data, promise: promise)
}
/// Logs ``Event.flush`` to ``logger``
/// Called to request that the `Channel` flush all pending writes. The flush operation will try to flush out all previous written messages
/// that are pending.
/// - parameters:
/// - context: The `ChannelHandlerContext` which this `ChannelHandler` belongs to.
public func flush(context: ChannelHandlerContext) {
logger(.flush, context)
context.flush()
}
/// Logs ``Event.read`` to ``logger``
/// Called to request that the `Channel` perform a read when data is ready. The read operation will signal that we are ready to read more data.
/// - parameters:
/// - context: The `ChannelHandlerContext` which this `ChannelHandler` belongs to.
public func read(context: ChannelHandlerContext) {
logger(.read, context)
context.read()
}
/// Logs ``Event.close`` to ``logger``
/// Called to request that the `Channel` close itself down`.
/// - parameters:
/// - context: The `ChannelHandlerContext` which this `ChannelHandler` belongs to.
/// - mode: The `CloseMode` to apply
/// - promise: The `EventLoopPromise` which should be notified once the operation completes, or nil if no notification should take place.
public func close(context: ChannelHandlerContext, mode: CloseMode, promise: EventLoopPromise<Void>?) {
logger(.close(mode: mode), context)
context.close(mode: mode, promise: promise)
}
/// Logs ``Event.triggerUserOutboundEvent`` to ``logger``
/// Called when an user outbound event is triggered.
/// - parameters:
/// - context: The `ChannelHandlerContext` which this `ChannelHandler` belongs to.
/// - event: The triggered event.
/// - promise: The `EventLoopPromise` which should be notified once the operation completes, or nil if no notification should take place.
public func triggerUserOutboundEvent(context: ChannelHandlerContext, event: Any, promise: EventLoopPromise<Void>?) {
logger(.triggerUserOutboundEvent(event: event), context)
context.triggerUserOutboundEvent(event, promise: promise)
}
/// Print textual event description to stdout.
/// - parameters:
/// - event: The ``Event`` to print.
/// - in: The context the event occured in.
public static func defaultPrint(event: Event, in context: ChannelHandlerContext) {
let message: String
switch event {
case .register:
message = "Registering channel"
case .bind(let address):
message = "Binding to \(address)"
case .connect(let address):
message = "Connecting to \(address)"
case .write(let data):
message = "Writing \(data)"
case .flush:
message = "Flushing"
case .read:
message = "Reading"
case .close(let mode):
message = "Closing with mode \(mode)"
print()
case .triggerUserOutboundEvent(let event):
message = "Triggering user outbound event: { \(event) }"
}
print(message + " in \(context.name)")
fflush(stdout)
}
}
#if swift(>=5.6)
@available(*, unavailable)
extension DebugOutboundEventsHandler: Sendable {}
#endif