Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #==============================================================================
- # ** Socket
- #------------------------------------------------------------------------------
- # Esta classe lida com o soquete de rede. Ela estabelece
- # uma conexão entre o cliente e o servidor.
- #------------------------------------------------------------------------------
- # Autor: Cidiomar
- #==============================================================================
- class SocketError < StandardError
- ENOASSOCHOST = 'getaddrinfo: nenhum endereço associado ao nome do host.'
- lib = Fiddle.dlopen('System/VXAOS.dll')
- SocketLib__GetLastError = Fiddle::Function.new(
- lib['SocketLib__GetLastError'],
- [],
- Fiddle::TYPE_INT
- )
- def self.raise(errno = SocketLib__GetLastError.call)
- Kernel.raise(
- Errno.const_get(
- Errno.constants.detect do |c|
- Errno.const_get(c).new.errno == errno
- end
- ),
- '',
- caller(2)
- )
- end
- def self.raise_no_assoc_host
- Kernel.raise(SocketError, ENOASSOCHOST, caller(2))
- end
- end
- #==============================================================================
- # ** SocketLib
- #==============================================================================
- class SocketLib
- lib = Fiddle.dlopen('System/VXAOS.dll')
- SocketLib__setup = Fiddle::Function.new(
- lib['SocketLib__setup'],
- [],
- Fiddle::TYPE_INT
- )
- SocketLib__connect = Fiddle::Function.new(
- lib['SocketLib__connect'],
- [
- Fiddle::TYPE_VOIDP,
- Fiddle::TYPE_VOIDP,
- Fiddle::TYPE_VOIDP
- ],
- Fiddle::TYPE_INT
- )
- SocketLib__close = Fiddle::Function.new(
- lib['SocketLib__close'],
- [Fiddle::TYPE_UINT],
- Fiddle::TYPE_INT
- )
- SocketLib__send = Fiddle::Function.new(
- lib['SocketLib__send'],
- [
- Fiddle::TYPE_UINT,
- Fiddle::TYPE_VOIDP,
- Fiddle::TYPE_INT
- ],
- Fiddle::TYPE_INT
- )
- SocketLib__recv = Fiddle::Function.new(
- lib['SocketLib__recv'],
- [
- Fiddle::TYPE_UINT,
- Fiddle::TYPE_VOIDP,
- Fiddle::TYPE_INT
- ],
- Fiddle::TYPE_INT
- )
- SocketLib__recv_non_block = Fiddle::Function.new(
- lib['SocketLib__recv_non_block'],
- [
- Fiddle::TYPE_UINT,
- Fiddle::TYPE_VOIDP,
- Fiddle::TYPE_INT
- ],
- Fiddle::TYPE_INT
- )
- SocketLib__TestHost = Fiddle::Function.new(
- lib['SocketLib__TestHost'],
- [
- Fiddle::TYPE_VOIDP,
- Fiddle::TYPE_VOIDP
- ],
- Fiddle::TYPE_INT
- )
- SocketLib__eof = Fiddle::Function.new(
- lib['SocketLib__eof'],
- [Fiddle::TYPE_UINT],
- Fiddle::TYPE_INT
- )
- def initialize(ip, port)
- _port = port.to_s
- @socket_id = [0, 0, 0, 0].pack('L')
- err = SocketLib__connect.call(ip, _port, @socket_id)
- unless err == 0
- if err == -1
- SocketError.raise_no_assoc_host
- else
- SocketError.raise
- end
- end
- @socket_id = @socket_id.unpack1('L')
- end
- def send(data)
- data = data.to_s
- return 0 if data.empty?
- if (ss = SocketLib__send.call(@socket_id, data, data.bytesize)) < 0
- SocketError.raise
- end
- ss
- end
- def recv(maxlen)
- buff = "\0" * (maxlen.to_i + 4)
- r_len = SocketLib__recv.call(@socket_id, buff, maxlen)
- SocketError.raise if r_len < 0
- if r_len == maxlen
- buff
- else
- buff[0...r_len]
- end
- end
- def recv_non_block(maxlen)
- buff = "\0" * (maxlen.to_i + 4)
- r_len = SocketLib__recv_non_block.call(@socket_id, buff, maxlen)
- return -1 if r_len <= 0
- if r_len == maxlen
- buff
- else
- buff[0...r_len]
- end
- end
- def eof?
- SocketLib__eof.call(@socket_id) == 0
- end
- def close
- SocketLib__close.call(@socket_id)
- end
- class << self
- def test_host(ip, port)
- if (SocketLib__TestHost.call(ip, port) == 1)
- return true
- else
- SocketError::SocketLib__GetLastError.call()
- return false
- end
- end
- end
- # Consulta o ping de forma bloqueante. Deve ser
- # utilizado apenas em situações realmente necessárias.
- def self.ping(server)
- IO.popen("ping -n 1 #{server}").close
- $?.exitstatus == 0
- end
- if SocketLib__setup.call != 0
- SocketError.raise
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement