Class: UDPSocket

Inherits:
IPSocket show all
Defined in:
lib/rubysl/socket.rb

Constant Summary collapse

FFI =
Rubinius::FFI

Instance Method Summary collapse

Methods inherited from IPSocket

#addr, getaddress, #peeraddr, #recvfrom, #recvfrom_nonblock

Methods inherited from BasicSocket

#close_read, #close_write, do_not_reverse_lookup, #do_not_reverse_lookup, do_not_reverse_lookup=, #do_not_reverse_lookup=, for_fd, from_descriptor, #from_descriptor, #getpeername, #getsockname, #getsockopt, #recv, #recv_nonblock, #recvfrom, #setsockopt, #shutdown

Constructor Details

#initialize(socktype = Socket::AF_INET) ⇒ UDPSocket

Returns a new instance of UDPSocket.



1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
# File 'lib/rubysl/socket.rb', line 1276

def initialize(socktype = Socket::AF_INET)
  @no_reverse_lookup = self.class.do_not_reverse_lookup
  @socktype = socktype
  status = Socket::Foreign.socket @socktype,
                                  Socket::SOCK_DGRAM,
                                  Socket::IPPROTO_UDP
  Errno.handle 'socket(2)' if status < 0

  IO.setup self, status, nil, true
end

Instance Method Details

#bind(host, port) ⇒ Object



1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
# File 'lib/rubysl/socket.rb', line 1287

def bind(host, port)
  @host = host.to_s if host
  @port = port.to_s if port

  addrinfos = Socket::Foreign.getaddrinfo(@host,
                                         @port,
                                         @socktype,
                                         Socket::SOCK_DGRAM, 0,
                                         Socket::AI_PASSIVE)

  status = -1

  addrinfos.each do |addrinfo|
    flags, family, socket_type, protocol, sockaddr, canonname = addrinfo

    status = Socket::Foreign.bind descriptor, sockaddr

    break if status >= 0
  end

  if status < 0
    Errno.handle 'bind(2)'
  end

  status
end

#connect(host, port) ⇒ Object



1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
# File 'lib/rubysl/socket.rb', line 1314

def connect(host, port)
  sockaddr = Socket::Foreign.pack_sockaddr_in host, port, @socktype, Socket::SOCK_DGRAM, 0

  syscall = 'connect(2)'
  status = Socket::Foreign.connect descriptor, sockaddr

  if status < 0
    Errno.handle syscall
  end

  0
end

#inspectObject



1342
1343
1344
# File 'lib/rubysl/socket.rb', line 1342

def inspect
  "#<#{self.class}:0x#{object_id.to_s(16)} #{@host}:#{@port}>"
end

#send(message, flags, *to) ⇒ Object



1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
# File 'lib/rubysl/socket.rb', line 1327

def send(message, flags, *to)
  connect *to unless to.empty?

  bytes = message.bytesize
  bytes_sent = 0

  FFI::MemoryPointer.new :char, bytes + 1 do |buffer|
    buffer.write_string message, bytes
    bytes_sent = Socket::Foreign.send(descriptor, buffer, bytes, flags)
    Errno.handle 'send(2)' if bytes_sent < 0
  end

  bytes_sent
end