The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

UV::UDP - UDP socket handles in libuv

SYNOPSIS

    #!/usr/bin/perl
    use strict;
    use warnings;

    use Socket;

    # A new socket handle will be initialised against the default loop
    my $udp = UV::UDP->new;

    $udp->connect(pack_sockaddr_in(1234, inet_aton("127.0.0.1"));
    $udp->send("Hello, server!\n");

    # set up the data recv callback
    $udp->on(recv => sub {
        my ($self, $err, $buf) = @_;
        say "More data: $buf";
    });
    $udp->recv_start();

DESCRIPTION

This module provides an interface to libuv's UDP handle.

EVENTS

UV::UDP makes the following extra events available.

recv

    $udp->on("recv", sub {
        my ($self, $status, $buf, $addr) = @_;
        say "Received more data: <$buf>";
    });

The recv callback fires whenever a datagram is received on the socket to be passed to the application.

METHODS

UV::UDP inherits all methods from UV::Handle and also makes the following extra methods available.

open

    $udp->open($fh);

The open method associates the UDP handle with an existing filehandle already opened by the process.

bind

    $udp->bind($addr);

The bind method associates the UDP socket with the given local address.

connect

    $udp->connect($addr);

The connect method associates the UDP socket with the given remote address.

getpeername

    my $addr = $udp->getpeername;

The getpeername method returns a packed sockaddr string containing the remote address with which this UDP handle is associated.

getsockname

    my $addr = $udp->getsockname;

The getsockname method returns a packed sockaddr string containing the local address with which this UDP handle is associated.

recv_start

    $udp->recv_start;

The recv_start method starts the receiving side of the UDP socket handle. The recv event callback will be invoked whenever there is new data to be given to the application.

recv_stop

    $udp->recv_stop;

The recv_stop method stops the receiving side of the stream handle.

send

    $udp->send($s, sub {
        say "Data has now been sent";
    });

The send method sends another datagram to the peer. The callback argument will be invoked when it has been flushed to the filehandle.

    $udp->send($s, $addr, sub { ... });

Optionally additionally a destination address can be provided, for use with unconnected UDP sockets.

try_send

    $udp->try_send($s);
    $udp->try_send($s, $addr);

The try_send method behaves similarly to "send" but will fail with UV_EAGAIN if it cannot send the data immediately, rather than enqueing for later.

set_broadcast

    $udp->set_broadcast($on);

The set_broadcast method turns broadcast on or off.

set_ttl

    $udp->set_ttl($ttl);

The set_ttl method sets the time-to-live of transmitted packets.

set_multicast_loop

    $udp->set_multicast_loop($on);

The set_multicast_loop method turns the multicast loopback flag on or off.

set_multicast_ttl

    $udp->set_multicast_ttl($ttl);

The set_multicast_ttl method sets the time-to-live of transmitted multicast packets.

set_multicast_interface

    $udp->set_multicast_interface($ifaddr);

The set_multicast_interface method sets the interface address to send or receive data on. The interface address is specified in a plain byte string.

set_membership

    $udp->set_membership($mcaddr, $ifaddr, $membership);

The set_membership method joins or leaves a multicast group. $membership should be one of the exported constants UV_JOIN_GROUP or UV_LEAVE_GROUP. The group and interface addresses are specified in plain byte strings.

set_source_membership

    $udp->set_source_membership($mcaddr, $ifaddr, $srcaddr, $membership);

The set_source_membership method joins or leaves a source-specific multicast group. $membership should be one of the exported constants UV_JOIN_GROUP or UV_LEAVE_GROUP. The group, interface, and source addresses are specified in plain byte strings.

get_send_queue_size

get_send_queue_count

    $size  = $udp->get_send_queue_size;
    $count = $udp->get_send_queue_count;

Returns the total size and current count of items in the send queue.

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.