Module: Pixelflut::Sender

Defined in:
lib/pixelflut/sender.rb

Defined Under Namespace

Classes: Address

Class Method Summary collapse

Class Method Details

.as_address(host, port) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/pixelflut/sender.rb', line 5

def self.as_address(host, port)
  require('socket') unless defined?(Addrinfo)
  info = Addrinfo.tcp(host, port)
  Address.new(
    Socket.pack_sockaddr_in(info.ip_port, info.ip_address),
    info.ipv6? ? :INET6 : :INET
  )
end

.create_socket(address) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/pixelflut/sender.rb', line 20

def self.create_socket(address)
  socket = Socket.new(address.type, :STREAM)
  socket.connect(address.sockaddr_in)
  socket.setsockopt(:TCP, :NODELAY, 1)
  socket.setsockopt(:SOCKET, :KEEPALIVE, 0)
  socket.sync = socket.do_not_reverse_lookup = true
  socket
end

.send(address, data) {|data.bytesize| ... } ⇒ Object

Yields:



14
15
16
17
18
# File 'lib/pixelflut/sender.rb', line 14

def self.send(address, data)
  socket = create_socket(address)
  yield(data.bytesize) if block_given?
  socket.write(data) while true
end