| 1 | package MySQL::Replication::Command; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use version; | ||
| 7 | our $VERSION = qv( '0.0.1' ); | ||
| 8 | |||
| 9 | use base qw{ Class::Accessor }; | ||
| 10 | |||
| 11 | use Errno qw{ EAGAIN }; | ||
| 12 | use IO::Socket qw{ :crlf }; | ||
| 13 | |||
| 14 | our ( $Errstr, $SOCKET_READ_MAX ); | ||
| 15 | |||
| 16 | BEGIN { | ||
| 17 | $SOCKET_READ_MAX = 1024; | ||
| 18 | |||
| 19 | __PACKAGE__->mk_accessors( qw{ | ||
| 20 | Command | ||
| 21 | Headers | ||
| 22 | Body | ||
| 23 | OutputBuffer | ||
| 24 | }); | ||
| 25 | } | ||
| 26 | |||
| 27 | sub new { | ||
| 28 | my ( $Class, %Args ) = @_; | ||
| 29 | |||
| 30 | $Errstr = undef; | ||
| 31 | |||
| 32 | if ( not $Args{Command} ) { | ||
| 33 | $Errstr = "Required parameter 'Command' missing"; | ||
| 34 | return; | ||
| 35 | } | ||
| 36 | |||
| 37 | my $Self = $Class->SUPER::new( \%Args ); | ||
| 38 | |||
| 39 | if ( not $Args{Headers} ) { | ||
| 40 | $Self->Headers( {} ); | ||
| 41 | } | ||
| 42 | |||
| 43 | if ( $Args{Body} and not $Self->Headers()->{Length} ) { | ||
| 44 | $Self->Headers()->{Length} = length $Args{Body}; | ||
| 45 | } | ||
| 46 | |||
| 47 | return $Self; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub NewFromSocket { | ||
| 51 | my ( $Class, %Args ) = @_; | ||
| 52 | |||
| 53 | $Errstr = undef; | ||
| 54 | |||
| 55 | foreach my $Required ( qw{ Socket Buffer } ) { | ||
| 56 | next if exists $Args{$Required}; | ||
| 57 | |||
| 58 | $Errstr = "Required parameter '$Required' missing"; | ||
| 59 | return; | ||
| 60 | } | ||
| 61 | |||
| 62 | # | ||
| 63 | # Read the command type and any available headers | ||
| 64 | # | ||
| 65 | |||
| 66 | while ( ( ${ $Args{Buffer} } || '' ) !~ /^$CR?$LF/m ) { | ||
| 67 | my $Bytes = $Args{Socket}->sysread( | ||
| 68 | ${ $Args{Buffer} }, | ||
| 69 | $SOCKET_READ_MAX, | ||
| 70 | length ( ${ $Args{Buffer} } || '' ), | ||
| 71 | ); | ||
| 72 | |||
| 73 | if ( not defined $Bytes ) { | ||
| 74 | if ( $! == EAGAIN ) { | ||
| 75 | return if $Args{NonBlocking}; | ||
| 76 | sleep 1; | ||
| 77 | next; | ||
| 78 | } | ||
| 79 | |||
| 80 | $Errstr = "Error reading command from socket ($!)"; | ||
| 81 | return; | ||
| 82 | } | ||
| 83 | |||
| 84 | if ( not $Bytes ) { | ||
| 85 | $Errstr = 'Lost connection while reading command'; | ||
| 86 | return; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | # | ||
| 91 | # Read the entire body (if any) | ||
| 92 | # | ||
| 93 | |||
| 94 | pos( ${ $Args{Buffer} } ) = 0; | ||
| 95 | ${ $Args{Buffer} } =~ /\A(\w+)$CR?$LF(.*?)^$CR?$LF/smg; | ||
| 96 | |||
| 97 | my $Command = $1; | ||
| 98 | my $HeadersBuffer = $2; | ||
| 99 | my $CommandHeaderLength = pos( ${ $Args{Buffer} } ); | ||
| 100 | |||
| 101 | if ( not $Command ) { | ||
| 102 | $Errstr = 'Invalid command format'; | ||
| 103 | return; | ||
| 104 | } | ||
| 105 | |||
| 106 | if ( $HeadersBuffer and $HeadersBuffer =~ /^Length\s*:\s*(\d+)\s*$/m ) { | ||
| 107 | my $BodyLength = $1; | ||
| 108 | |||
| 109 | while ( length( ${ $Args{Buffer} } ) - $CommandHeaderLength < $BodyLength + 2 ) { | ||
| 110 | my $BytesToRead | ||
| 111 | = ( $BodyLength + 2 - length( ${ $Args{Buffer} } ) - $CommandHeaderLength ) > $SOCKET_READ_MAX | ||
| 112 | ? $BodyLength + 2 - length( ${ $Args{Buffer} } ) - $CommandHeaderLength | ||
| 113 | : $SOCKET_READ_MAX; | ||
| 114 | |||
| 115 | my $Bytes = $Args{Socket}->sysread( | ||
| 116 | ${ $Args{Buffer} }, | ||
| 117 | $BytesToRead, | ||
| 118 | length( ${ $Args{Buffer} } ), | ||
| 119 | ); | ||
| 120 | |||
| 121 | if ( not defined $Bytes ) { | ||
| 122 | if ( $! == EAGAIN ) { | ||
| 123 | return if $Args{NonBlocking}; | ||
| 124 | sleep 1; | ||
| 125 | next; | ||
| 126 | } | ||
| 127 | |||
| 128 | $Errstr = "Error reading body from socket ($!)"; | ||
| 129 | return; | ||
| 130 | } | ||
| 131 | |||
| 132 | if ( not $Bytes ) { | ||
| 133 | $Errstr = 'Lost connection while reading body'; | ||
| 134 | return; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | # | ||
| 140 | # Now that we have a full command, strip then extract headers and body (if any) | ||
| 141 | # | ||
| 142 | |||
| 143 | ${ $Args{Buffer} } =~ s/\A.*?^$CR?$LF//sm; | ||
| 144 | |||
| 145 | my %Headers; | ||
| 146 | |||
| 147 | if ( $HeadersBuffer ) { | ||
| 148 | HEADERS: foreach my $HeaderLine ( split /$CR?$LF/, $HeadersBuffer ) { | ||
| 149 | if ( $HeaderLine =~ /^([^:\s]+)\s*:\s*(.+?)\s*$/ ) { | ||
| 150 | $Headers{$1} = $2; | ||
| 151 | } | ||
| 152 | else { | ||
| 153 | $Errstr = 'Invalid header format'; | ||
| 154 | return; | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | my $Body; | ||
| 160 | |||
| 161 | if ( $Headers{Length} ) { | ||
| 162 | $Body = substr ${ $Args{Buffer} }, 0, $Headers{Length}, ''; | ||
| 163 | ${$Args{Buffer}} =~ s/\A$CR?$LF//m; | ||
| 164 | } | ||
| 165 | |||
| 166 | # | ||
| 167 | # return a new command object | ||
| 168 | # | ||
| 169 | |||
| 170 | return MySQL::Replication::Command->new( | ||
| 171 | Command => $Command, | ||
| 172 | ( | ||
| 173 | %Headers | ||
| 174 | ? ( Headers => \%Headers ) | ||
| 175 | : () | ||
| 176 | ), | ||
| 177 | ( | ||
| 178 | $Body | ||
| 179 | ? ( Body => $Body ) | ||
| 180 | : () | ||
| 181 | ), | ||
| 182 | ); | ||
| 183 | } | ||
| 184 | |||
| 185 | sub SendToSocket{ | ||
| 186 | my ( $Self, $Socket, %Args ) = @_; | ||
| 187 | |||
| 188 | $Errstr = undef; | ||
| 189 | |||
| 190 | if ( not length( $Self->OutputBuffer() || '' ) ) { | ||
| 191 | $Self->OutputBuffer( $Self->Stringify() ); | ||
| 192 | } | ||
| 193 | |||
| 194 | while ( length( $Self->OutputBuffer() || '' ) ) { | ||
| 195 | my $Bytes = $Socket->syswrite( $Self->OutputBuffer() ); | ||
| 196 | |||
| 197 | if ( not defined $Bytes ) { | ||
| 198 | if ( $! == EAGAIN ) { | ||
| 199 | return if $Args{NonBlocking}; | ||
| 200 | sleep 1; | ||
| 201 | next; | ||
| 202 | } | ||
| 203 | |||
| 204 | $Errstr = "Error writing command to socket ($!)"; | ||
| 205 | return; | ||
| 206 | } | ||
| 207 | |||
| 208 | substr $Self->{OutputBuffer}, 0, $Bytes, ''; | ||
| 209 | |||
| 210 | last if not length( $Self->{OutputBuffer} || '' ); | ||
| 211 | return if $Args{NonBlocking}; | ||
| 212 | sleep 1; | ||
| 213 | } | ||
| 214 | |||
| 215 | return 1; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub Stringify { | ||
| 219 | my ( $Self ) = @_; | ||
| 220 | |||
| 221 | return join( '', | ||
| 222 | $Self->Command() . $CRLF, | ||
| 223 | ( | ||
| 224 | map { $_ . ': ' . $Self->Headers()->{$_} . $CRLF } | ||
| 225 | sort keys %{ $Self->Headers() || {} } | ||
| 226 | ), | ||
| 227 | $CRLF, | ||
| 228 | ( | ||
| 229 | $Self->Body() | ||
| 230 | ? $Self->Body() . $CRLF | ||
| 231 | : () | ||
| 232 | ), | ||
| 233 | ); | ||
| 234 | } | ||
| 235 | |||
| 236 | 1; | ||
| 237 | |||
| 238 | __END__ |