| 1 | package Net::NATS::Connection; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | use Class::XSAccessor { | ||
| 5 | constructors => [ '_new' ], | ||
| 6 | accessors => [ | ||
| 7 | 'socket_args', | ||
| 8 | '_socket', | ||
| 9 | ], | ||
| 10 | lvalue_accessors => [ | ||
| 11 | 'buffer', | ||
| 12 | 'eobuf', | ||
| 13 | 'eof', | ||
| 14 | 'error', | ||
| 15 | ], | ||
| 16 | }; | ||
| 17 | |||
| 18 | use IO::Socket::INET; | ||
| 19 | use Errno qw(EAGAIN EINTR EWOULDBLOCK); | ||
| 20 | |||
| 21 | sub new { | ||
| 22 | my $class = shift; | ||
| 23 | my $self = $class->_new(@_); | ||
| 24 | |||
| 25 | $self->socket_args->{Proto} = 'tcp'; | ||
| 26 | |||
| 27 | my $socket = IO::Socket::INET->new(%{$self->socket_args}) | ||
| 28 | or return; | ||
| 29 | $self->_socket($socket); | ||
| 30 | $socket->blocking(0); | ||
| 31 | $self->flush(); | ||
| 32 | |||
| 33 | return $self; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub upgrade { | ||
| 37 | my $self = shift; | ||
| 38 | |||
| 39 | unless ($IO::Socket::SSL::VERSION) { | ||
| 40 | eval { require IO::Socket::SSL }; | ||
| 41 | die $@ if $@; | ||
| 42 | } | ||
| 43 | |||
| 44 | my $socket = IO::Socket::SSL->start_SSL($self->_socket, %{$self->socket_args}) | ||
| 45 | or die "failed to start_SSL: error=$!, ssl_error=$IO::Socket::SSL_ERROR"; | ||
| 46 | |||
| 47 | $self->_socket($socket); | ||
| 48 | $socket->blocking(0); | ||
| 49 | $self->flush(); | ||
| 50 | } | ||
| 51 | |||
| 52 | # clear out the buffered data | ||
| 53 | sub flush { | ||
| 54 | my $self = shift; | ||
| 55 | |||
| 56 | $self->buffer = ''; | ||
| 57 | $self->eobuf = 0; | ||
| 58 | $self->eof = 0; | ||
| 59 | $self->error = ''; | ||
| 60 | return 1; | ||
| 61 | } | ||
| 62 | |||
| 63 | # test if we have data on the handle, with optional timeout. | ||
| 64 | sub can_read { | ||
| 65 | my $self = shift; | ||
| 66 | |||
| 67 | return IO::Select->new($self->_socket)->can_read(@_); | ||
| 68 | } | ||
| 69 | |||
| 70 | # block until the handle is ready to write, with optional timeout. | ||
| 71 | sub can_write { | ||
| 72 | my $self = shift; | ||
| 73 | |||
| 74 | return IO::Select->new($self->_socket)->can_write(@_); | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | # implement non-blocking getline() function by managing our own data buffer | ||
| 79 | # based on sample code from "Network Programming with Perl" by L.D. Stein. | ||
| 80 | |||
| 81 | # $bytes = $self->nb_getline($data); | ||
| 82 | # data is stored in $data, returns number of bytes on success | ||
| 83 | # returns undef on error and sets $self->error, $data has any partial read | ||
| 84 | # returns 0 on EOF, $data has partial read | ||
| 85 | # returns 0E0 if would block (ie, not a full line read), data is unchanged. | ||
| 86 | |||
| 87 | sub nb_getline { | ||
| 88 | my $self = shift; | ||
| 89 | |||
| 90 | return 0 if $self->eof; # previous read reached EOF | ||
| 91 | return undef if $self->error; # previous read encountered error | ||
| 92 | |||
| 93 | # look up position of EOL in the buffer | ||
| 94 | my $idx = index($self->buffer, $/); | ||
| 95 | if ($idx < 0) { | ||
| 96 | # EOL was not found, so suck in more data if we can | ||
| 97 | $self->eobuf = length $self->buffer; | ||
| 98 | # append to our buffer from the file handle if any data is there. | ||
| 99 | my $count = sysread($self->_socket,$self->buffer,1024,$self->eobuf); | ||
| 100 | |||
| 101 | if (!defined $count) { | ||
| 102 | return '0E0' if $! == EWOULDBLOCK; # we handle this error | ||
| 103 | |||
| 104 | $self->error = $!; # remember the error for later | ||
| 105 | $_[0] = $self->buffer; # return whatever we read | ||
| 106 | return length($_[0]); | ||
| 107 | } | ||
| 108 | elsif ($count == 0) { # EOF | ||
| 109 | $self->eof = 1; # remember for later | ||
| 110 | $_[0] = $self->buffer; # return whatever we read | ||
| 111 | return length($_[0]); | ||
| 112 | } | ||
| 113 | else { | ||
| 114 | # look for EOL again in the newly read data | ||
| 115 | $idx = index($self->buffer, $/, $self->eobuf); | ||
| 116 | # if not found, pretend this was EWOULDBLOCK | ||
| 117 | if ($idx < 0) { | ||
| 118 | $self->eobuf = length $self->buffer; | ||
| 119 | return '0E0'; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | } | ||
| 123 | |||
| 124 | # we successfully read what we needed up to a new line | ||
| 125 | $_[0] = substr($self->buffer,0,$idx + length($/)); | ||
| 126 | substr($self->buffer,0,$idx + length($/)) = ''; | ||
| 127 | $self->eobuf = length $self->buffer; | ||
| 128 | return length($_[0]); | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | # implement read() function upon our data buffer. | ||
| 133 | sub nb_read { | ||
| 134 | my $self = shift; | ||
| 135 | my $length = $_[1]; | ||
| 136 | |||
| 137 | return 0 if $self->eof; # previous read reached EOF | ||
| 138 | return undef if $self->error; # previous read encountered error | ||
| 139 | |||
| 140 | # do we have enough data? | ||
| 141 | if (length $self->buffer < $length) { | ||
| 142 | # not enough, so suck in more data if we can | ||
| 143 | $self->eobuf = length $self->buffer; | ||
| 144 | # append to our buffer from the file handle if any data is there. | ||
| 145 | my $count = sysread($self->_socket,$self->buffer,1024,$self->eobuf); | ||
| 146 | |||
| 147 | if (!defined $count) { | ||
| 148 | return '0E0' if $! == EWOULDBLOCK; # we handle this error | ||
| 149 | |||
| 150 | $self->error = $!; # remember the error for later | ||
| 151 | $_[0] = $self->buffer; # return whatever we read | ||
| 152 | return length($_[0]); | ||
| 153 | } | ||
| 154 | elsif ($count == 0) { # EOF | ||
| 155 | $self->eof = 1; # remember for later | ||
| 156 | $_[0] = $self->buffer; # return whatever we read | ||
| 157 | return length($_[0]); | ||
| 158 | } | ||
| 159 | else { | ||
| 160 | # check length again | ||
| 161 | $self->eobuf = length $self->buffer; | ||
| 162 | # if not, pretend this was EWOULDBLOCK | ||
| 163 | if ($self->eobuf < $length) { | ||
| 164 | return '0E0'; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | } | ||
| 168 | |||
| 169 | # we successfully read what we needed | ||
| 170 | $_[0] = substr($self->buffer,0,$length); | ||
| 171 | substr($self->buffer,0,$length) = ''; | ||
| 172 | $self->eobuf = length $self->buffer; | ||
| 173 | return length($_[0]); | ||
| 174 | } | ||
| 175 | |||
| 176 | # blocking send | ||
| 177 | # return 1 on success, undef on failure | ||
| 178 | sub send { | ||
| 179 | my $self = shift; | ||
| 180 | my $msg = "$_[0]\r\n"; | ||
| 181 | |||
| 182 | my $len = length $msg; | ||
| 183 | my $offset = 0; | ||
| 184 | while ($len) { | ||
| 185 | my $written = syswrite $self->_socket, $msg, $len, $offset; | ||
| 186 | if (defined $written) { | ||
| 187 | $len -= $written; | ||
| 188 | $offset += $written; | ||
| 189 | } else { | ||
| 190 | if ($! == EAGAIN || $! == EINTR) { # retry sending | ||
| 191 | #warn "waiting for socket ready after $!\n"; | ||
| 192 | $self->can_write(); # block until ready to write | ||
| 193 | } | ||
| 194 | else { | ||
| 195 | $self->error = $!; | ||
| 196 | return; # can't do anything with failed write. socket likely closed. | ||
| 197 | } | ||
| 198 | } | ||
| 199 | } | ||
| 200 | return 1; | ||
| 201 | } | ||
| 202 | |||
| 203 | 1; |