Class: Lich::Common::SynchronizedSocket

Inherits:
Object
  • Object
show all
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.

Examples:

Creating a synchronized socket

socket = SynchronizedSocket.new(original_socket)

Instance Method Summary collapse

Constructor Details

#initialize(o) ⇒ SynchronizedSocket

Initializes a new SynchronizedSocket instance.

Parameters:

  • o (Object)

    The socket object to be synchronized.



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.

Parameters:

  • method (Symbol)

    The name of the method being called.

  • args (*Object)

    The arguments passed to the method.

  • block (Proc)

    An optional block to be executed.

Returns:

  • (Object)

    Returns the result of the method call on the delegate.



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.

Examples:

Sending a message to the socket

socket.puts("Hello, World!")

Parameters:

  • args (*Object)

    The arguments to be sent to the socket.

  • block (Proc)

    An optional block to be executed.

Returns:

  • (nil)

    Returns nil after outputting.



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.

Examples:

Conditionally sending a message

socket.puts_if("Hello, World!") { true }

Parameters:

  • args (*Object)

    The arguments to be sent to the socket if the condition is true.

Returns:

  • (Boolean)

    Returns true if the message was sent, false otherwise.



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.

Examples:

Writing data to the socket

socket.write("Data to send")

Parameters:

  • args (*Object)

    The arguments to be sent to the socket.

  • block (Proc)

    An optional block to be executed.

Returns:

  • (nil)

    Returns nil after writing.



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