Class: Lich::Common::SynchronizedSocket
- Inherits:
-
Object
- Object
- Lich::Common::SynchronizedSocket
- Defined in:
- lib/common/class_exts/synchronizedsocket.rb
Overview
A thread-safe wrapper around a socket that synchronizes access to the underlying socket object using a mutex.
Instance Method Summary collapse
-
#initialize(o) ⇒ SynchronizedSocket
constructor
Initializes a new instance of SynchronizedSocket.
-
#method_missing(method, *args) {|Block| ... } ⇒ Object
Delegates method calls to the underlying socket object.
-
#puts(*args) {|Block| ... } ⇒ nil
Writes a line to the socket, ensuring thread safety.
-
#puts_if(*args) {|Block| ... } ⇒ Boolean
Conditionally writes to the socket based on the result of a block.
-
#write(*args) {|Block| ... } ⇒ nil
Writes data to the socket, ensuring thread safety.
Constructor Details
#initialize(o) ⇒ SynchronizedSocket
Initializes a new instance of SynchronizedSocket.
16 17 18 19 20 |
# File 'lib/common/class_exts/synchronizedsocket.rb', line 16 def initialize(o) @delegate = o @mutex = Mutex.new # self # removed by robocop, needs broad testing end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) {|Block| ... } ⇒ Object
Delegates method calls to the underlying socket object.
74 75 76 |
# File 'lib/common/class_exts/synchronizedsocket.rb', line 74 def method_missing(method, *args, &block) @delegate.__send__ method, *args, &block end |
Instance Method Details
#puts(*args) {|Block| ... } ⇒ nil
Writes a line to the socket, ensuring thread safety.
29 30 31 32 33 |
# File 'lib/common/class_exts/synchronizedsocket.rb', line 29 def puts(*args, &block) @mutex.synchronize { @delegate.puts(*args, &block) } end |
#puts_if(*args) {|Block| ... } ⇒ Boolean
Conditionally writes to the socket based on the result of a block.
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/common/class_exts/synchronizedsocket.rb', line 42 def puts_if(*args) @mutex.synchronize { if yield @delegate.puts(*args) return true else return false end } end |
#write(*args) {|Block| ... } ⇒ nil
Writes data to the socket, ensuring thread safety.
60 61 62 63 64 |
# File 'lib/common/class_exts/synchronizedsocket.rb', line 60 def write(*args, &block) @mutex.synchronize { @delegate.write(*args, &block) } end |