Advertisement
LeonMMS

Untitled

May 16th, 2025
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.26 KB | None | 0 0
  1. #==============================================================================
  2. # ** Socket
  3. #------------------------------------------------------------------------------
  4. #  Esta classe lida com o soquete de rede. Ela estabelece
  5. # uma conexão entre o cliente e o servidor.
  6. #------------------------------------------------------------------------------
  7. #  Autor: Cidiomar
  8. #==============================================================================
  9.  
  10. class SocketError < StandardError
  11.  
  12.   ENOASSOCHOST = 'getaddrinfo: nenhum endereço associado ao nome do host.'
  13.   lib = Fiddle.dlopen('System/VXAOS.dll')
  14.   SocketLib__GetLastError = Fiddle::Function.new(
  15.     lib['SocketLib__GetLastError'],
  16.     [],
  17.     Fiddle::TYPE_INT
  18.   )
  19.  
  20.   def self.raise(errno = SocketLib__GetLastError.call)
  21.     Kernel.raise(
  22.       Errno.const_get(
  23.         Errno.constants.detect do |c|
  24.           Errno.const_get(c).new.errno == errno
  25.         end
  26.       ),
  27.       '',
  28.       caller(2)
  29.     )
  30.   end
  31.  
  32.   def self.raise_no_assoc_host
  33.     Kernel.raise(SocketError, ENOASSOCHOST, caller(2))
  34.   end
  35.  
  36. end
  37.  
  38. #==============================================================================
  39. # ** SocketLib
  40. #==============================================================================
  41. class SocketLib
  42.  
  43.   lib = Fiddle.dlopen('System/VXAOS.dll')
  44.   SocketLib__setup = Fiddle::Function.new(
  45.     lib['SocketLib__setup'],
  46.     [],
  47.     Fiddle::TYPE_INT
  48.   )
  49.   SocketLib__connect = Fiddle::Function.new(
  50.     lib['SocketLib__connect'],
  51.     [
  52.       Fiddle::TYPE_VOIDP,
  53.       Fiddle::TYPE_VOIDP,
  54.       Fiddle::TYPE_VOIDP
  55.     ],
  56.     Fiddle::TYPE_INT
  57.   )
  58.   SocketLib__close = Fiddle::Function.new(
  59.     lib['SocketLib__close'],
  60.     [Fiddle::TYPE_UINT],
  61.     Fiddle::TYPE_INT
  62.   )
  63.   SocketLib__send = Fiddle::Function.new(
  64.     lib['SocketLib__send'],
  65.     [
  66.       Fiddle::TYPE_UINT,
  67.       Fiddle::TYPE_VOIDP,
  68.       Fiddle::TYPE_INT
  69.     ],
  70.     Fiddle::TYPE_INT
  71.   )
  72.   SocketLib__recv = Fiddle::Function.new(
  73.     lib['SocketLib__recv'],
  74.     [
  75.       Fiddle::TYPE_UINT,
  76.       Fiddle::TYPE_VOIDP,
  77.       Fiddle::TYPE_INT
  78.     ],
  79.     Fiddle::TYPE_INT
  80.   )
  81.   SocketLib__recv_non_block = Fiddle::Function.new(
  82.     lib['SocketLib__recv_non_block'],
  83.     [
  84.       Fiddle::TYPE_UINT,
  85.       Fiddle::TYPE_VOIDP,
  86.       Fiddle::TYPE_INT
  87.     ],
  88.     Fiddle::TYPE_INT
  89.   )
  90.   SocketLib__TestHost = Fiddle::Function.new(
  91.     lib['SocketLib__TestHost'],
  92.     [
  93.       Fiddle::TYPE_VOIDP,
  94.       Fiddle::TYPE_VOIDP
  95.     ],
  96.     Fiddle::TYPE_INT
  97.   )
  98.   SocketLib__eof = Fiddle::Function.new(
  99.     lib['SocketLib__eof'],
  100.     [Fiddle::TYPE_UINT],
  101.     Fiddle::TYPE_INT
  102.   )
  103.  
  104.   def initialize(ip, port)
  105.     _port = port.to_s
  106.     @socket_id = [0, 0, 0, 0].pack('L')
  107.     err = SocketLib__connect.call(ip, _port, @socket_id)
  108.     unless err == 0
  109.       if err == -1
  110.         SocketError.raise_no_assoc_host
  111.       else
  112.         SocketError.raise
  113.       end
  114.     end
  115.     @socket_id = @socket_id.unpack1('L')
  116.   end
  117.  
  118.   def send(data)
  119.     data = data.to_s
  120.     return 0 if data.empty?
  121.     if (ss = SocketLib__send.call(@socket_id, data, data.bytesize)) < 0
  122.       SocketError.raise
  123.     end
  124.     ss
  125.   end
  126.  
  127.   def recv(maxlen)
  128.     buff = "\0" * (maxlen.to_i + 4)
  129.     r_len = SocketLib__recv.call(@socket_id, buff, maxlen)
  130.     SocketError.raise if r_len < 0
  131.     if r_len == maxlen
  132.       buff
  133.     else
  134.       buff[0...r_len]
  135.     end
  136.   end
  137.  
  138.   def recv_non_block(maxlen)
  139.     buff = "\0" * (maxlen.to_i + 4)
  140.     r_len = SocketLib__recv_non_block.call(@socket_id, buff, maxlen)
  141.     return -1 if r_len <= 0
  142.     if r_len == maxlen
  143.       buff
  144.     else
  145.       buff[0...r_len]
  146.     end
  147.   end
  148.  
  149.   def eof?
  150.     SocketLib__eof.call(@socket_id) == 0
  151.   end
  152.  
  153.   def close
  154.     SocketLib__close.call(@socket_id)
  155.   end
  156.  
  157.   class << self
  158.     def test_host(ip, port)
  159.       if (SocketLib__TestHost.call(ip, port) == 1)
  160.         return true
  161.       else
  162.         SocketError::SocketLib__GetLastError.call()
  163.         return false
  164.       end
  165.     end
  166.   end
  167.  
  168.   # Consulta o ping de forma bloqueante. Deve ser
  169.   # utilizado apenas em situações realmente necessárias.
  170.   def self.ping(server)
  171.     IO.popen("ping -n 1 #{server}").close
  172.     $?.exitstatus == 0
  173.   end
  174.  
  175.   if SocketLib__setup.call != 0
  176.     SocketError.raise
  177.   end
  178.  
  179. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement