/* * Test UDP sockets with an IOQueue work correctly. * The client connects and the server sends some data. The client checks the * data matches and then closes the connection. */ void SocketTest::testIOQueueUDPSend() { IPV4SocketAddress socket_address(IPV4Address::Loopback(), 9010); UDPSocket socket; OLA_ASSERT_TRUE(socket.Init()); OLA_ASSERT_FALSE(socket.Init()); OLA_ASSERT_TRUE(socket.Bind(socket_address)); OLA_ASSERT_FALSE(socket.Bind(socket_address)); socket.SetOnData( ola::NewCallback(this, &SocketTest::UDPReceiveAndSend, &socket)); OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&socket)); UDPSocket client_socket; OLA_ASSERT_TRUE(client_socket.Init()); OLA_ASSERT_FALSE(client_socket.Init()); client_socket.SetOnData( ola::NewCallback( this, &SocketTest::UDPReceiveAndTerminate, static_cast<UDPSocket*>(&client_socket))); OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&client_socket)); IOQueue output; output.Write(static_cast<const uint8_t*>(test_cstring), sizeof(test_cstring)); ssize_t bytes_sent = client_socket.SendTo(&output, socket_address); OLA_ASSERT_EQ(static_cast<ssize_t>(sizeof(test_cstring)), bytes_sent); m_ss->Run(); m_ss->RemoveReadDescriptor(&socket); m_ss->RemoveReadDescriptor(&client_socket); }
/** * Check that we send OSC messages correctly. */ void OSCNodeTest::testSendBlob() { // First up create a UDP socket to receive the messages on. // Port 0 means 'ANY' IPV4SocketAddress socket_address(IPV4Address::Loopback(), 0); // Bind the socket, set the callback, and register with the select server. OLA_ASSERT_TRUE(m_udp_socket.Bind(socket_address)); m_udp_socket.SetOnData(NewCallback(this, &OSCNodeTest::UDPSocketReady)); OLA_ASSERT_TRUE(m_ss.AddReadDescriptor(&m_udp_socket)); // Store the local address of the UDP socket so we know where to tell the // OSCNode to send to. OLA_ASSERT_TRUE(m_udp_socket.GetSocketAddress(&socket_address)); // Setup the OSCTarget pointing to the local socket address OSCTarget target(socket_address, TEST_OSC_ADDRESS); // Add the target to the node. m_osc_node->AddTarget(TEST_GROUP, target); // Send the data OLA_ASSERT_TRUE(m_osc_node->SendData(TEST_GROUP, OSCNode::FORMAT_BLOB, m_dmx_data)); // Run the SelectServer this will return either when UDPSocketReady // completes, or the abort timeout triggers. m_ss.Run(); // Remove target OLA_ASSERT_TRUE(m_osc_node->RemoveTarget(TEST_GROUP, target)); // Try to remove it a second time OLA_ASSERT_FALSE(m_osc_node->RemoveTarget(TEST_GROUP, target)); // Try to remove the target from a group that doesn't exist OLA_ASSERT_FALSE(m_osc_node->RemoveTarget(TEST_GROUP + 1, target)); }
/* * Test UDP sockets work correctly. * The client connects and the server sends some data. The client checks the * data matches and then closes the connection. */ void SocketTest::testUDPSocket() { IPV4SocketAddress socket_address(IPV4Address::Loopback(), 0); UDPSocket socket; OLA_ASSERT_TRUE(socket.Init()); OLA_ASSERT_FALSE(socket.Init()); OLA_ASSERT_TRUE(socket.Bind(socket_address)); OLA_ASSERT_FALSE(socket.Bind(socket_address)); IPV4SocketAddress local_address; OLA_ASSERT_TRUE(socket.GetSocketAddress(&local_address)); OLA_ASSERT_EQ(static_cast<uint16_t>(AF_INET), local_address.Family()); socket.SetOnData( ola::NewCallback(this, &SocketTest::UDPReceiveAndSend, &socket)); OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&socket)); UDPSocket client_socket; OLA_ASSERT_TRUE(client_socket.Init()); OLA_ASSERT_FALSE(client_socket.Init()); client_socket.SetOnData( ola::NewCallback( this, &SocketTest::UDPReceiveAndTerminate, static_cast<UDPSocket*>(&client_socket))); OLA_ASSERT_TRUE(m_ss->AddReadDescriptor(&client_socket)); ssize_t bytes_sent = client_socket.SendTo( static_cast<const uint8_t*>(test_cstring), sizeof(test_cstring), local_address); OLA_ASSERT_EQ(static_cast<ssize_t>(sizeof(test_cstring)), bytes_sent); m_ss->Run(); m_ss->RemoveReadDescriptor(&socket); m_ss->RemoveReadDescriptor(&client_socket); }
/* * Main */ int main(int argc, char *argv[]) { ola::InitLogging(ola::OLA_LOG_INFO, ola::OLA_LOG_STDERR); udp_socket.SetOnData(ola::NewCallback(&SocketReady)); if (!udp_socket.Init()) { OLA_WARN << "Failed to init"; return 1; } if (!udp_socket.Bind(6038)) { OLA_WARN << "Failed to bind"; return 1; } if (!udp_socket.EnableBroadcast()) { OLA_WARN << "Failed to enabl bcast"; return 1; } ss.AddSocket(&udp_socket); ss.Run(); return 0; }