Method: OpenC3::UdpReadWriteSocket#initialize
- Defined in:
- lib/openc3/io/udp_sockets.rb
#initialize(bind_port = 0, bind_address = "0.0.0.0", external_port = nil, external_address = nil, multicast_interface_address = nil, ttl = 1, read_multicast = true, write_multicast = true) ⇒ UdpReadWriteSocket
Returns a new instance of UdpReadWriteSocket.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/openc3/io/udp_sockets.rb', line 41 def initialize( bind_port = 0, bind_address = "0.0.0.0", external_port = nil, external_address = nil, multicast_interface_address = nil, ttl = 1, read_multicast = true, write_multicast = true ) @socket = UDPSocket.new # Basic setup to reuse address @socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, 1) # Bind to local address and port - This sets recv port, write_src port, recv_address, and write_src_address @socket.bind(bind_address, bind_port) if bind_address and bind_port # Default send to the specified address and port @socket.connect(external_address, external_port) if external_address and external_port # Handle multicast if UdpReadWriteSocket.multicast?(external_address, external_port) if write_multicast # Basic setup set time to live @socket.setsockopt(Socket::IPPROTO_IP, Socket::IP_MULTICAST_TTL, ttl.to_i) # Set outgoing interface @socket.setsockopt( Socket::IPPROTO_IP, Socket::IP_MULTICAST_IF, IPAddr.new(multicast_interface_address).hton ) if multicast_interface_address end # Receive messages sent to the multicast address if read_multicast multicast_interface_address = "0.0.0.0" unless multicast_interface_address membership = IPAddr.new(external_address).hton + IPAddr.new(multicast_interface_address).hton @socket.setsockopt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP, membership) end end end |