Class: Chan::Counter
- Inherits:
-
Object
- Object
- Chan::Counter
- Defined in:
- lib/xchan/counter.rb
Overview
Chan::Counter provides a counter for the number of written and received bytes on a given channel.
Instance Method Summary collapse
-
#bytes_read ⇒ Integer
Returns the number of bytes read from a channel.
-
#bytes_written ⇒ Integer
Returns the number of bytes written to a channel.
-
#close ⇒ void
Close the counter.
- #increment!(bytes_read: 0, bytes_written: 0) ⇒ void
- #initialize(tmpdir) ⇒ Chan::Counter constructor
Constructor Details
#initialize(tmpdir) ⇒ Chan::Counter
12 13 14 15 16 17 |
# File 'lib/xchan/counter.rb', line 12 def initialize(tmpdir) @io = Chan.temporary_file(%w[counter .bin], tmpdir:) @io.binmode @io.sync = true write(@io, 0, 0) end |
Instance Method Details
#bytes_read ⇒ Integer
Returns the number of bytes read from a channel
30 31 32 33 |
# File 'lib/xchan/counter.rb', line 30 def bytes_read bytes, _ = read(@io) bytes end |
#bytes_written ⇒ Integer
Returns the number of bytes written to a channel
22 23 24 25 |
# File 'lib/xchan/counter.rb', line 22 def bytes_written _, bytes = read(@io) bytes end |
#close ⇒ void
This method returns an undefined value.
Close the counter
52 53 54 |
# File 'lib/xchan/counter.rb', line 52 def close @io.close end |
#increment!(bytes_read: 0, bytes_written: 0) ⇒ void
This method returns an undefined value.
42 43 44 45 46 47 |
# File 'lib/xchan/counter.rb', line 42 def increment!(bytes_read: 0, bytes_written: 0) bytes_in, bytes_out = read(@io) bytes_in += bytes_read bytes_out += bytes_written write(@io, bytes_in, bytes_out) end |