Class: Lich::Common::SynchronizedSocket
- Inherits:
-
Object
- Object
- Lich::Common::SynchronizedSocket
- Defined in:
- documented/common/class_exts/synchronizedsocket.rb
Overview
Represents a thread-safe wrapper around a socket. This class ensures that socket operations are synchronized using a mutex.
Instance Method Summary collapse
-
#initialize(o) ⇒ SynchronizedSocket
constructor
Initializes a new SynchronizedSocket instance.
-
#method_missing(method, *args, &block) ⇒ Object
Handles calls to methods that are not defined in this class.
-
#puts(*args, &block) ⇒ nil
Outputs a string to the socket.
-
#puts_if(*args) ⇒ Boolean
Conditionally outputs a string to the socket based on a block’s return value.
-
#write(*args, &block) ⇒ nil
Writes data to the socket.
Constructor Details
#initialize(o) ⇒ SynchronizedSocket
Initializes a new SynchronizedSocket instance.
14 15 16 17 18 |
# File 'documented/common/class_exts/synchronizedsocket.rb', line 14 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
Note:
This method delegates calls to the underlying socket object.
Handles calls to methods that are not defined in this class.
66 67 68 |
# File 'documented/common/class_exts/synchronizedsocket.rb', line 66 def method_missing(method, *args, &block) @delegate.__send__ method, *args, &block end |
Instance Method Details
#puts(*args, &block) ⇒ nil
Outputs a string to the socket.
26 27 28 29 30 |
# File 'documented/common/class_exts/synchronizedsocket.rb', line 26 def puts(*args, &block) @mutex.synchronize { @delegate.puts(*args, &block) } end |
#puts_if(*args) ⇒ Boolean
Conditionally outputs a string to the socket based on a block’s return value.
37 38 39 40 41 42 43 44 45 46 |
# File 'documented/common/class_exts/synchronizedsocket.rb', line 37 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.
54 55 56 57 58 |
# File 'documented/common/class_exts/synchronizedsocket.rb', line 54 def write(*args, &block) @mutex.synchronize { @delegate.write(*args, &block) } end |