File: SocketRole.pm

package info (click to toggle)
libzmq-ffi-perl 1.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 552 kB
  • sloc: perl: 4,708; sh: 92; ansic: 30; makefile: 27
file content (159 lines) | stat: -rw-r--r-- 2,696 bytes parent folder | download
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
package ZMQ::FFI::SocketRole;
$ZMQ::FFI::SocketRole::VERSION = '1.19';
use FFI::Platypus;
use FFI::Platypus::Memory qw(malloc);

use ZMQ::FFI::Constants qw(zmq_msg_t_size);
use ZMQ::FFI::Util qw(current_tid zmq_version);

use Moo::Role;

has soname => (
    is       => 'ro',
    required => 1,
);

# zmq constant socket type, e.g. ZMQ_REQ
has type => (
    is       => 'ro',
    required => 1,
);

# real underlying zmq socket pointer
has socket_ptr => (
    is      => 'rw',
    default => -1,
);

# a weak reference to the context object
has context => (
    is       => 'ro',
    required => 1,
    weak_ref => 1,
);

# message struct to reuse when sending/receiving
has _zmq_msg_t => (
    is        => 'ro',
    lazy      => 1,
    builder   => '_build_zmq_msg_t',
);

# used to make sure we handle fork situations correctly
has _pid => (
    is      => 'ro',
    default => sub { $$ },
);

# used to make sure we handle thread situations correctly
has _tid => (
    is      => 'ro',
    default => sub { current_tid() },
);

has sockopt_sizes => (
    is      => 'ro',
    lazy    => 1,
    builder => '_build_sockopt_sizes'
);

has event_size => (
    is      => 'ro',
    lazy    => 1,
    builder => '_build_event_size'
);

sub _build_zmq_msg_t {
    my ($self) = @_;

    my $msg_ptr;
    {
        no strict q/refs/;
        my $class = ref $self;
        $msg_ptr = malloc(zmq_msg_t_size);
        &{"$class\::zmq_msg_init"}($msg_ptr);
    }

    return $msg_ptr;
}

sub _build_sockopt_sizes {
    my $ffi = FFI::Platypus->new();

    return {
        int    => $ffi->sizeof('int'),
        sint64 => $ffi->sizeof('sint64'),
        uint64 => $ffi->sizeof('uint64'),
    };
}

sub _build_event_size {
    my $ffi = FFI::Platypus->new();

    my ($major, $minor, $patch) = zmq_version();

    my $size;

    if ($major == 3) {
        $size = $ffi->sizeof('int') * 2 + $ffi->sizeof('opaque');
    }
    elsif ($major > 3) {
        $size = $ffi->sizeof('uint16', 'sint32');
    }

    return $size;
}

requires qw(
    connect
    disconnect
    bind
    unbind
    send
    send_multipart
    recv
    recv_multipart
    get_fd
    get_linger
    set_linger
    get_identity
    set_identity
    subscribe
    unsubscribe
    has_pollin
    has_pollout
    get
    set
    close
    monitor
    recv_event
);

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

ZMQ::FFI::SocketRole

=head1 VERSION

version 1.19

=head1 AUTHOR

Dylan Cali <calid1984@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2023 by Dylan Cali.

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

=cut