| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||
| 3 | # | ||||||||||
| 4 | # (C) Paul Evans, 2011-2024 -- leonerd@leonerd.org.uk | ||||||||||
| 5 | |||||||||||
| 6 | package IO::Async::Protocol 0.805; | ||||||||||
| 7 | |||||||||||
| 8 | 4 | ▶ | use v5.14; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 4 | ▶ | use base qw( IO::Async::Notifier ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 4 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | =head1 NAME | ||||||||||
| 16 | |||||||||||
| 17 | C<IO::Async::Protocol> - base class for transport-based protocols | ||||||||||
| 18 | |||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||
| 20 | |||||||||||
| 21 | =for highlighter language=perl | ||||||||||
| 22 | |||||||||||
| 23 | This subclass of L<IO::Async:Notifier> provides storage for a | ||||||||||
| 24 | L<IO::Async::Handle> object, to act as a transport for some protocol. It | ||||||||||
| 25 | contains an instance of the transport object, which it adds as a child | ||||||||||
| 26 | notifier, allowing a level of independence from the actual transport being | ||||||||||
| 27 | used. For example, a stream may actually be an L<IO::Async::SSLStream> to | ||||||||||
| 28 | allow the protocol to be used over SSL. | ||||||||||
| 29 | |||||||||||
| 30 | This class is not intended to be used directly, instead, see one of the | ||||||||||
| 31 | subclasses | ||||||||||
| 32 | |||||||||||
| 33 | =over 4 | ||||||||||
| 34 | |||||||||||
| 35 | =item L<IO::Async::Protocol::Stream> - base class for stream-based protocols | ||||||||||
| 36 | |||||||||||
| 37 | =back | ||||||||||
| 38 | |||||||||||
| 39 | =cut | ||||||||||
| 40 | |||||||||||
| 41 | =head1 EVENTS | ||||||||||
| 42 | |||||||||||
| 43 | The following events are invoked, either using subclass methods or CODE | ||||||||||
| 44 | references in parameters: | ||||||||||
| 45 | |||||||||||
| 46 | =head2 on_closed | ||||||||||
| 47 | |||||||||||
| 48 | Optional. Invoked when the transport handle becomes closed. | ||||||||||
| 49 | |||||||||||
| 50 | =cut | ||||||||||
| 51 | |||||||||||
| 52 | =head1 PARAMETERS | ||||||||||
| 53 | |||||||||||
| 54 | The following named parameters may be passed to C<new> or C<configure>: | ||||||||||
| 55 | |||||||||||
| 56 | =head2 transport => IO::Async::Handle | ||||||||||
| 57 | |||||||||||
| 58 | The L<IO::Async::Handle> to delegate communications to. | ||||||||||
| 59 | |||||||||||
| 60 | =head2 on_closed => CODE | ||||||||||
| 61 | |||||||||||
| 62 | CODE reference for the C<on_closed> event. | ||||||||||
| 63 | |||||||||||
| 64 | When a new C<transport> object is given, it will be configured by calling the | ||||||||||
| 65 | C<setup_transport> method, then added as a child notifier. If a different | ||||||||||
| 66 | transport object was already configured, this will first be removed and | ||||||||||
| 67 | deconfigured using the C<teardown_transport>. | ||||||||||
| 68 | |||||||||||
| 69 | =cut | ||||||||||
| 70 | |||||||||||
| 71 | sub configure | ||||||||||
| 72 | { | ||||||||||
| 73 | 17 | ▶ | my $self = shift; | ||||||||
|
Subroutine
configure: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||
| 74 | 17 | my %params = @_; | |||||||||
| 75 | |||||||||||
| 76 | 17 | for (qw( on_closed )) { | |||||||||
| 77 | 17 | ▶ | $self->{$_} = delete $params{$_} if exists $params{$_}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | 17 | ▶ | if( exists $params{transport} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 10 | my $transport = delete $params{transport}; | |||||||||
| 82 | |||||||||||
| 83 | 10 | ▶ | if( $self->{transport} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 3 | $self->remove_child( $self->transport ); | |||||||||
| 85 | |||||||||||
| 86 | 3 | $self->teardown_transport( $self->transport ); | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 10 | $self->{transport} = $transport; | |||||||||
| 90 | |||||||||||
| 91 | 10 | ▶ | if( $transport ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 8 | $self->setup_transport( $self->transport ); | |||||||||
| 93 | |||||||||||
| 94 | 8 | $self->add_child( $self->transport ); | |||||||||
| 95 | } | ||||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | 17 | $self->SUPER::configure( %params ); | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | =head1 METHODS | ||||||||||
| 102 | |||||||||||
| 103 | =cut | ||||||||||
| 104 | |||||||||||
| 105 | =head2 transport | ||||||||||
| 106 | |||||||||||
| 107 | $transport = $protocol->transport; | ||||||||||
| 108 | |||||||||||
| 109 | Returns the stored transport object | ||||||||||
| 110 | |||||||||||
| 111 | =cut | ||||||||||
| 112 | |||||||||||
| 113 | sub transport | ||||||||||
| 114 | { | ||||||||||
| 115 | 30 | ▶ | my $self = shift; | ||||||||
|
Subroutine
transport: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 116 | 30 | return $self->{transport}; | |||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | =head2 connect | ||||||||||
| 120 | |||||||||||
| 121 | $protocol->connect( %args ); | ||||||||||
| 122 | |||||||||||
| 123 | Sets up a connection to a peer, and configures the underlying C<transport> for | ||||||||||
| 124 | the Protocol. | ||||||||||
| 125 | |||||||||||
| 126 | Takes the following named arguments: | ||||||||||
| 127 | |||||||||||
| 128 | =over 8 | ||||||||||
| 129 | |||||||||||
| 130 | =item socktype => STRING or INT | ||||||||||
| 131 | |||||||||||
| 132 | Required. Identifies the socket type, and the type of continuation that will | ||||||||||
| 133 | be used. If this value is C<"stream"> or C<SOCK_STREAM> then C<on_stream> | ||||||||||
| 134 | continuation will be used; otherwise C<on_socket> will be used. | ||||||||||
| 135 | |||||||||||
| 136 | =item on_connected => CODE | ||||||||||
| 137 | |||||||||||
| 138 | Optional. If supplied, will be invoked once the connection has been | ||||||||||
| 139 | established. | ||||||||||
| 140 | |||||||||||
| 141 | $on_connected->( $protocol ); | ||||||||||
| 142 | |||||||||||
| 143 | =item transport => IO::Async::Handle | ||||||||||
| 144 | |||||||||||
| 145 | Optional. If this is provided, it will immediately be configured as the | ||||||||||
| 146 | transport (by calling C<configure>), and the C<on_connected> callback will be | ||||||||||
| 147 | invoked. This is provided as a convenient shortcut. | ||||||||||
| 148 | |||||||||||
| 149 | =back | ||||||||||
| 150 | |||||||||||
| 151 | Other arguments will be passed to the underlying L<IO::Async::Loop> C<connect> | ||||||||||
| 152 | call. | ||||||||||
| 153 | |||||||||||
| 154 | =cut | ||||||||||
| 155 | |||||||||||
| 156 | sub connect | ||||||||||
| 157 | { | ||||||||||
| 158 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
connect: called CC=7 SCAR=22.0
Pod
documented
| |||||||||||
| 159 | 1 | my %args = @_; | |||||||||
| 160 | |||||||||||
| 161 | 1 | my $on_connected = delete $args{on_connected}; | |||||||||
| 162 | |||||||||||
| 163 | 1 | ▶ | if( my $transport = $args{transport} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 164 | 0 | $self->configure( transport => $transport ); | |||||||||
| 165 | |||||||||||
| 166 | 0 | ▶ | $on_connected->( $self ) if $on_connected; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 167 | |||||||||||
| 168 | 0 | return; | |||||||||
| 169 | } | ||||||||||
| 170 | |||||||||||
| 171 | 1 | ▶ | my $socktype = $args{socktype} or croak "Expected socktype"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 172 | |||||||||||
| 173 | 1 | ▶ | my $on_transport = do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 174 | 4 | ▶ | no warnings 'numeric'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 175 | 1 | ▶ | $socktype eq "stream" || $socktype == Socket::SOCK_STREAM() | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | } ? "on_stream" : "on_socket"; | ||||||||||
| 177 | |||||||||||
| 178 | 1 | ▶ | my $loop = $self->loop or croak "Cannot ->connect a ".ref($self)." that is not in a Loop"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 179 | |||||||||||
| 180 | $loop->connect( | ||||||||||
| 181 | %args, | ||||||||||
| 182 | socktype => "stream", | ||||||||||
| 183 | |||||||||||
| 184 | $on_transport => sub { | ||||||||||
| 185 | 1 | ▶ | my ( $transport ) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||
| 186 | |||||||||||
| 187 | 1 | $self->configure( transport => $transport ); | |||||||||
| 188 | |||||||||||
| 189 | 1 | ▶ | $on_connected->( $self ) if $on_connected; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 190 | }, | ||||||||||
| 191 | 1 | ); | |||||||||
| 192 | } | ||||||||||
| 193 | |||||||||||
| 194 | =head1 TRANSPORT DELEGATION | ||||||||||
| 195 | |||||||||||
| 196 | The following methods are delegated to the transport object | ||||||||||
| 197 | |||||||||||
| 198 | close | ||||||||||
| 199 | |||||||||||
| 200 | =cut | ||||||||||
| 201 | |||||||||||
| 202 | 0 | ▶ | sub close { shift->transport->close } | ||||||||
|
Subroutine
close: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 203 | |||||||||||
| 204 | =head1 SUBCLASS METHODS | ||||||||||
| 205 | |||||||||||
| 206 | C<IO::Async::Protocol> is a base class provided so that specific subclasses of | ||||||||||
| 207 | it provide more specific behaviour. The base class provides a number of | ||||||||||
| 208 | methods that subclasses may wish to override. | ||||||||||
| 209 | |||||||||||
| 210 | If a subclass implements any of these, be sure to invoke the superclass method | ||||||||||
| 211 | at some point within the code. | ||||||||||
| 212 | |||||||||||
| 213 | =cut | ||||||||||
| 214 | |||||||||||
| 215 | =head2 setup_transport | ||||||||||
| 216 | |||||||||||
| 217 | $protocol->setup_transport( $transport ); | ||||||||||
| 218 | |||||||||||
| 219 | Called by C<configure> when a new C<transport> object is given, this method | ||||||||||
| 220 | should perform whatever setup is required to wire the new transport object | ||||||||||
| 221 | into the protocol object; typically by setting up event handlers. | ||||||||||
| 222 | |||||||||||
| 223 | =cut | ||||||||||
| 224 | |||||||||||
| 225 | sub setup_transport | ||||||||||
| 226 | { | ||||||||||
| 227 | 8 | ▶ | my $self = shift; | ||||||||
|
Subroutine
setup_transport: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 228 | 8 | my ( $transport ) = @_; | |||||||||
| 229 | |||||||||||
| 230 | $transport->configure( | ||||||||||
| 231 | on_closed => $self->_capture_weakself( sub { | ||||||||||
| 232 | 2 | ▶ | my $self = shift or return; | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
Branch: 1 branch
| |||||||||||
| 233 | 2 | my ( $transport ) = @_; | |||||||||
| 234 | |||||||||||
| 235 | 2 | $self->maybe_invoke_event( on_closed => ); | |||||||||
| 236 | |||||||||||
| 237 | 2 | $self->configure( transport => undef ); | |||||||||
| 238 | 8 | } ), | |||||||||
| 239 | ); | ||||||||||
| 240 | } | ||||||||||
| 241 | |||||||||||
| 242 | =head2 teardown_transport | ||||||||||
| 243 | |||||||||||
| 244 | $protocol->teardown_transport( $transport ); | ||||||||||
| 245 | |||||||||||
| 246 | The reverse of C<setup_transport>; called by C<configure> when a previously | ||||||||||
| 247 | set-up transport object is about to be replaced. | ||||||||||
| 248 | |||||||||||
| 249 | =cut | ||||||||||
| 250 | |||||||||||
| 251 | sub teardown_transport | ||||||||||
| 252 | { | ||||||||||
| 253 | 3 | ▶ | my $self = shift; | ||||||||
|
Subroutine
teardown_transport: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 254 | 3 | my ( $transport ) = @_; | |||||||||
| 255 | |||||||||||
| 256 | 3 | $transport->configure( | |||||||||
| 257 | on_closed => undef, | ||||||||||
| 258 | ); | ||||||||||
| 259 | } | ||||||||||
| 260 | |||||||||||
| 261 | =head1 AUTHOR | ||||||||||
| 262 | |||||||||||
| 263 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||
| 264 | |||||||||||
| 265 | =cut | ||||||||||
| 266 | |||||||||||
| 267 | 4 | 0x55AA; | |||||||||