/* * 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); }
/* * 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); }
UDPSocket UDPSocket::Create(std::int32_t domain) { UDPSocket socket; socket.Init(domain); return socket; }
int main(int argc, char*argv[]){ if(argc < 4){ printf("use it right u dummie\n"); exit(1); } int srcPort = atoi(argv[1]); char* destIp = argv[2]; int destPort = atoi(argv[3]); UDPSocket* Socket = new UDPSocket(); Socket->Init(); Socket->Bind(srcPort); Socket->SetBlocking(false); Socket->On(0, function1); auto last = std::chrono::high_resolution_clock::now(); while(1){ auto now = std::chrono::high_resolution_clock::now(); auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now - last); if(ms.count() > 2000){ Socket->Send(0, "hello", sizeof("hello"), destIp, destPort); last = now; } Socket->PollEvents(); } }
/** * Create the UDP Socket and bind to the port. We do this outside the server so * we can't bind to ports < 1024. */ UDPSocket *SetupUDPSocket(uint16_t port) { UDPSocket *socket = new UDPSocket(); // setup the UDP socket if (!socket->Init()) { OLA_WARN << "Failed to Init UDP Socket"; return NULL; } if (!socket->Bind(IPV4SocketAddress(IPV4Address::WildCard(), port))) { OLA_WARN << "UDP Port failed to find"; return NULL; } return socket; }
void OSCNodeTest::setUp() { // Init logging ola::InitLogging(ola::OLA_LOG_INFO, ola::OLA_LOG_STDERR); // Setup and register the Timeout. m_timeout_id = m_ss.RegisterSingleTimeout( ABORT_TIMEOUT_IN_MS, ola::NewSingleCallback(this, &OSCNodeTest::Timeout)); OLA_ASSERT_TRUE(m_timeout_id); // Init our UDP socket. OLA_ASSERT_TRUE(m_udp_socket.Init()); // Put some data into the DMXBuffer m_dmx_data.SetFromString("0,1,2,3,4,5,6,7,8,9,10"); // Initialize the OSCNode OLA_ASSERT_TRUE(m_osc_node->Init()); }
/** * Start up the controller */ bool SimpleE133Controller::Init() { if (!m_udp_socket.Init()) return false; if (!m_udp_socket.Bind(IPV4SocketAddress(m_controller_ip, 0))) { OLA_INFO << "Failed to bind to UDP port"; return false; } m_ss.AddReadDescriptor(&m_udp_socket); if (m_slp_thread.get()) { if (!m_slp_thread->Init()) { OLA_WARN << "SLPThread Init() failed"; return false; } m_slp_thread->Start(); } return true; }
/* * 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; }