Changeset 527
- Timestamp:
- 05/02/07 20:46:56 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/client-xs-20070328/Cache-Memcached-GetParserXS/lib/Cache/Memcached/GetParserXS.pm
r304 r527 4 4 use strict; 5 5 use warnings; 6 # We don't want to inherit from this, because our constants may be different. 7 # use base 'Cache::Memcached::GetParser'; 6 8 use Carp; 7 8 require Exporter; 9 use Errno qw( EINPROGRESS EWOULDBLOCK EISCONN ); 9 10 use AutoLoader; 10 11 11 our @ISA = qw(Exporter Cache::Memcached::GetParser);12 our $VERSION = '0.01'; 12 13 13 # Items to export into callers namespace by default. Note: do not export 14 # names by default without a very good reason. Use EXPORT_OK instead. 15 # Do not simply export all your public functions/methods/constants. 14 require XSLoader; 15 XSLoader::load('Cache::Memcached::GetParserXS', $VERSION); 16 16 17 # This allows declaration use Cache::Memcached::GetParserXS ':all'; 18 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK 19 # will save memory. 20 our %EXPORT_TAGS = ( 'all' => [ qw( 17 sub DEST; 18 sub NSLEN; 19 sub ON_ITEM; 20 sub BUF; 21 sub STATE; 22 sub OFFSET; 23 sub FLAGS; 24 sub KEY; 25 sub FINISHED; 21 26 22 ) ] ); 27 sub new { 28 my ($class, $dest, $nslen, $on_item) = @_; 23 29 24 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} });30 my $self = bless [], (ref $class || $class); 25 31 26 our @EXPORT = qw( 32 $self->[DEST] = $dest; 33 $self->[NSLEN] = $nslen; 34 $self->[ON_ITEM] = $on_item; 35 $self->[BUF] = ''; 36 $self->[STATE] = 0; 37 $self->[OFFSET] = 0; 38 $self->[FLAGS] = undef; 39 $self->[KEY] = undef; 40 $self->[FINISHED] = {}; 27 41 28 ); 42 return $self 43 } 29 44 30 our $VERSION = '0.01'; 45 sub current_key { 46 return $_[0][KEY]; 47 } 48 49 # returns 1 on success, -1 on failure, and 0 if still working. 50 sub parse_from_sock { 51 my ($self, $sock) = @_; 52 my $res; 53 54 # where are we reading into? 55 if ($self->[STATE]) { # reading value into $ret 56 my $ret = $self->[DEST]; 57 $res = sysread($sock, $ret->{$self->[KEY]}, 58 $self->[STATE] - $self->[OFFSET], 59 $self->[OFFSET]); 60 61 return 0 62 if !defined($res) and $!==EWOULDBLOCK; 63 64 if ($res == 0) { # catches 0=conn closed or undef=error 65 $self->[ON_ITEM] = undef; 66 return -1; 67 } 68 69 $self->[OFFSET] += $res; 70 if ($self->[OFFSET] == $self->[STATE]) { # finished reading 71 $self->[OFFSET] = 0; 72 $self->[STATE] = 0; 73 # wait for another VALUE line or END... 74 } 75 return 0; # still working, haven't got to end yet 76 } 77 78 # we're reading a single line. 79 # first, read whatever's there, but be satisfied with 2048 bytes 80 $res = sysread($sock, $self->[BUF], 81 128*1024, $self->[OFFSET]); 82 return 0 83 if !defined($res) and $!==EWOULDBLOCK; 84 if ($res == 0) { 85 $self->[ON_ITEM] = undef; 86 return -1; 87 } 88 89 $self->[OFFSET] += $res; 90 91 my $answer = $self->parse_buffer; 92 93 if ($answer > 0) { 94 my $finished = $self->[FINISHED]; 95 my $finalize = $self->[ON_ITEM]; 96 97 while (my ($key, $flags) = each %$finished) { 98 $finalize->($key, $flags); 99 } 100 } 101 102 return $answer; 103 } 104 105 sub DESTROY {} # Empty definition, so AUTOLOAD doesn't catch it 106 107 # sub parse_buffer is defined in XS 31 108 32 109 sub AUTOLOAD { … … 52 129 goto &$AUTOLOAD; 53 130 } 54 55 require XSLoader;56 XSLoader::load('Cache::Memcached::GetParserXS', $VERSION);57 58 # Preloaded methods go here.59 60 # Autoload methods go after =cut, and are processed by the autosplit program.61 131 62 132 1;
