Add set_net_interface function and use the when setting up the multicast socket

This commit is contained in:
Isaac Connor 2020-02-19 14:27:36 -05:00
parent 258ae23fb9
commit 2315bed56d
1 changed files with 66 additions and 51 deletions

View File

@ -43,7 +43,7 @@ my %message_of :ATTR(:name<message> :default<()>);
my %is_success_of :ATTR(:name<is_success> :default<()>);
my %local_addr_of :ATTR(:name<local_addr> :init_arg<local_addr> :default<()>);
my $net_interface;
# create methods normally inherited from SOAP::Client
SUBFACTORY: {
@ -60,14 +60,22 @@ sub _notify_response
}
sub set_net_interface {
my $self = shift;
$net_interface = shift;
}
sub send_multi() {
my ($self, $address, $port, $utf8_string) = @_;
my $destination = $address . ':' . $port;
my $socket = IO::Socket::Multicast->new(PROTO => 'udp',
LocalPort=>$port, PeerAddr=>$destination, ReuseAddr=>1)
or die 'Cannot open multicast socket to ' . ${address} . ':' . ${port};
my $socket = IO::Socket::Multicast->new(
PROTO => 'udp',
LocalPort=>$port,
PeerAddr=>$destination,
ReuseAddr=>1
) or die 'Cannot open multicast socket to ' . ${address} . ':' . ${port};
$_ = $socket->mcast_if($net_interface) if $net_interface;
my $bytes = $utf8_string;
utf8::encode($bytes);
@ -80,9 +88,11 @@ sub receive_multi() {
my ($self, $address, $port) = @_;
my $data = undef;
my $socket = IO::Socket::Multicast->new(PROTO => 'udp',
LocalPort=>$port, ReuseAddr=>1);
$socket->mcast_add($address);
my $socket = IO::Socket::Multicast->new(
PROTO => 'udp',
LocalPort=>$port,
ReuseAddr=>1);
$socket->mcast_add($address, $net_interface);
my $readbits = '';
vec($readbits, $socket->fileno, 1) = 1;
@ -98,10 +108,14 @@ sub receive_uni() {
my ($self, $address, $port, $localaddr) = @_;
my $data = undef;
my $socket = IO::Socket::Multicast->new(PROTO => 'udp',
LocalAddr => $localaddr, LocalPort=>$port, ReuseAddr=>1);
my $socket = IO::Socket::Multicast->new(
PROTO => 'udp',
LocalAddr => $localaddr,
LocalPort=>$port,
ReuseAddr=>1
);
$socket->mcast_add($address);
$socket->mcast_add($address, $net_interface);
my $readbits = '';
vec($readbits, $socket->fileno, 1) = 1;
@ -126,6 +140,7 @@ sub send_receive {
$self->send_multi($address, $port, $envelope);
my $localaddr = $self->get_local_addr();
#warn "localddr $localaddr";
my ($response, $last_response);
my $wait = WAIT_COUNT;
@ -146,13 +161,12 @@ sub send_receive {
if ( $last_response ) {
$self->set_code();
$self->set_message("");
$self->set_message('');
$self->set_is_success(1);
$self->set_status('OK');
}
else{
} else {
$self->set_code();
$self->set_message("Timed out waiting for response");
$self->set_message('Timed out waiting for response');
$self->set_is_success(0);
$self->set_status('TIMEOUT');
}
@ -161,3 +175,4 @@ sub send_receive {
}
1;
__END__