Example #1
0
/*
 * 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);
}
Example #2
0
/*
 * 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);
}
Example #3
0
/**
 * 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));
}
Example #4
0
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();
		
	}
}
Example #5
0
/**
 * 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 ExternalConnectThread::ThreadFunc()
{
	m_udpSocket = m_address.m_socketFactory->CreateSocket();
	UDPSocket* socket = dynamic_cast<UDPSocket*>(m_udpSocket);
	if(!socket || !socket->Bind(m_address.m_localIP, m_address.m_localPort))
	{
		SignalError();
		return;
	}

	socket->SetSendAddr(m_address.m_ip, m_address.m_port);
	std::string data = m_address.m_moduleName;
	int size = m_address.m_moduleName.size();
	while(!IsStop())
	{
		if(m_changeAddr.Wait(1000) == Semaphore::TIMEOUT)
		{
			if (!socket->Send((char*)&size, sizeof(size)) ||
				!socket->Send((char*)data.c_str(), data.size() * sizeof(char)))
			{
				SignalError();
				return;
			}
		}
		else
		{
			break;
		}
	}

	UDTSecureSocketFactory* udtSocketFactory = dynamic_cast<UDTSecureSocketFactory*>(m_address.m_socketFactory);
	if(!udtSocketFactory)
	{
		SignalError();
		return;
	}


	udtSocketFactory->SetUdpSocket(socket->GetSocket());
	m_csSocket.Enter();
	m_udpSocket = 0;
	m_csSocket.Leave();
	ConnectThread::ThreadFunc();
}
/**
 * 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;
}
Example #8
0
/*
 * 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;
}
Example #9
0
	void Run()
	{
		const int port = 30003;
		UDPSocket socket;
		socket.Bind(port);

		const char sendbuffer[] = "hello world!";
		socket.Send(Address(127,0,0,1,port), sendbuffer, sizeof(sendbuffer));

		sleep_ms(50);

		char rcvbuffer[256];
		memset(rcvbuffer, 0, sizeof(rcvbuffer));
		Address sender;
		for(;;)
		{
			int bytes_read = socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer));
			if(bytes_read < 0)
				break;
		}
		//FIXME: This fails on some systems
		assert(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer))==0);
		assert(sender.getAddress() == Address(127,0,0,1, 0).getAddress());
	}
Example #10
0
void Servidor(std::string direccion)
{
	SocketAddress sa;
	sa.SetAddress(direccion);
	UDPSocket udpSocket;
	udpSocket.Bind(sa);
	udpSocket.NonBlocking(true);
	while (true)
	{
		char datos[1300];
		SocketAddress from;
		
		int bytesReceived = udpSocket.ReceiveFrom(datos, 1300, from);
		if (bytesReceived > 0)
		{
			datos[bytesReceived] = '\0';
			std::cout << "Recibo de "<<from<<": " << datos << std::endl;
			if (strcmp(datos, "exit") == 0)
			{
				break;
			}
		}
	}
}