AnyEve...le::UDP(3pm)
π AnyEvent::Handle::UDP π
okay bestie, so this is AnyEvent::Handle::UDP β client/server UDP handles for AnyEvent, living its best life in the User Contributed Perl Documentation section (that's the 3pm part, iykyk).
β¨ VERSION β¨
version 0.050 β yes, we're on the 0.050 era, iconic behavior honestly.
πΈ SYNOPSIS πΈ
here's the vibe, a whole echo server in like six lines, no cap:
my $echo_server = AnyEvent::Handle::UDP->new(
bind => ['0.0.0.0', 4000],
on_recv => sub {
my ($data, $ae_handle, $client_addr) = @_;
$ae_handle->push_send($data, $client_addr);
},
);
it just⦠receives stuff and throws it right back. slay.
π¦ DESCRIPTION π¦
This module is an abstraction around UDP sockets for use with AnyEvent. that's the whole thing, that's the assignment π
π ATTRIBUTES π
π«Ά on_recv
The callback for when a package arrives. It takes three arguments: the datagram, the handle, and the address the datagram was received from. three lil guys, all important.
π on_error
The callback for when an error occurs. It takes three arguments: the handle, a boolean saying whether the error is fatal or not, and the error message. read that boolean, bestie, it matters.
β¨ on_bind
The callback for when the bind has been performed (this may be after object construction if address lookup is involved). so like⦠don't assume it already happened, okay?
π on_connect
The callback for when the connect has been performed (this may be after object construction if address lookup is involved). same energy as above, different verb.
πΈ on_drain
This sets the callback that is called when the send buffer becomes empty. The callback takes the handle as its only argument. one single argument, we love a low-maintenance callback.
π
autoflush
Always attempt to send data to the operating system immediately, without waiting for the loop to indicate the filehandle is write-ready. impatient? valid. this is the impatient button.
π receive_size
The buffer size for the receiving, in bytes. It defaults to 1500, which is slightly more than the MTA on ethernet. very on-the-nose default, honestly obsessed.
π§ family
Sets the socket family. The default is 0, which means either IPv4 or IPv6. The values 4 and 6 mean IPv4 and IPv6 respectively.
β fh
The underlying filehandle. Note that this doesn't cooperate with the connect and bind parameters. like, they do not get along. do not introduce them.
π reuse_addr
If true will enable quick reuse of the bound address.
π timeout, rtimeout, wtimeout
If non-zero, then these enables an "inactivity" timeout: whenever this many seconds pass without a successful read or write on the underlying file handle (or a call to timeout_reset), the on_timeout callback will be invoked (and if that one is missing, a non-fatal ETIMEDOUT error will be raised).
There are three variants of the timeouts that work independently of each other, for both read and write (triggered when nothing was read OR written), just read (triggered when nothing was read), and just write: timeout, rtimeout and wtimeout, with corresponding callbacks on_timeout, on_rtimeout and on_wtimeout, and reset functions timeout_reset, rtimeout_reset, and wtimeout_reset.
β οΈ note that timeout processing is active even when you do not have any outstanding read or write requests: If you plan to keep the connection idle then you should disable the timeout temporarily or ignore the timeout in the correspondingon_timeoutcallback, in which caseAnyEvent::Handlewill simply restart the timeout. yeah. it's watching. it's always watching. π
Calling clear_timeout (or setting it to zero, which does the same) disables the corresponding timeout.
π¦ on_timeout, on_rtimeout, on_wtimeout
The callback that's called whenever the inactivity timeout passes. If you return from this callback, then the timeout will be reset as if some activity had happened, so this condition is not fatal in any way. you literally just have to return and everything's fine, periodt.
π METHODS π
π new
Create a new UDP handle. As arguments it accepts any attribute, as well as these two:
connectβ Set the address to which datagrams are sent by default, and the only address from which datagrams are received. It must be either a packedsockaddrstruct or an arrayref containing a hostname and a portnumber.bindβ The address to bind the socket to. It must be either a packedsockaddrstruct or an arrayref containing a hostname and a portnumber.
All except on_recv are optional, though using either connect or bind (or both) is strongly recommended unless you give it a connected/bound fh. like please. do that. π
π bind_to($address)
Bind to the specified addres. Note that a bound socket may be rebound to another address. $address must be in the same form as the bind argument to new. (yes, "addres" β we're keeping the typo, it's part of the lore π)
π« connect_to($address)
Connect to the specified address. Note that a connected socket may be reconnected to another address. $address must be in the same form as the connect argument to new.
π₯ push_send($message, $to = undef, $cv = AnyEvent::CondVar->new)
Try to send a message. If a socket is not connected a receptient address must also be given. If it is connected giving a receptient may not work as expected, depending on your platform. It returns $cv, which will become true when $message is sent. the $cv is your little "did it go tho" receipt π
π§ timeout_reset, rtimeout_reset, wtimeout_reset
Reset the activity timeout, as if data was received or sent.
πΈ sockname
Get the local address, per getsockname.
π peername
Get the peer's address, per getpeername.
π destroy
Destroy the handle. bye bestie π«Ά
π« BACKWARDS COMPATIBILITY π«
This module is not backwards compatible in any way with the defunct previous module of the same name by Jan Henning Thorsen. not even a little. zero. nada. it's giving total rebrand.
βοΈ AUTHOR βοΈ
Leon Timmermans <leont@cpan.org>
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2011 by Leon Timmermans.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
and that's the whole page, bestie!! this was perl v5.36.0, dated 2022-11-27, and it lives at AnyEve...le::UDP(3pm) β go forth and UDP responsibly, mwah πβ¨π
π¦ where this page comes from
- debian bookworm: libanyevent-handle-udp-perl 0.050-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libanyevent-handle-udp-perl 0.050-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libanyevent-handle-udp-perl 0.050-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu questing: libanyevent-handle-udp-perl 0.050-3
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms