Threads for both TX&RX (e.g UDP)


#1

Hi,

I am having problems defining 2 threads for the same issue, as receiving and transmiting.

If a class inherits from thread, i have a run thread function, but it is not possible to have 2.
Therefore I have a DatagramSocket class which contains 2 classes which both inherit from thread (and from DatagramSocket so I can read and write from/into the udp port).
The thing is that I bind the port in the main datagramsocket class. Then I can’t read anything from the socket. I think is because the thread inherits from DatagramSocket and doensn’t see that is open.if I bind the port in the thread it works. But again, the tx thread doesn’t see the port open.

I don’t know how to face this. Any idea?

I ahve also tried to have both threads without inherinting from datagrasocket, and try to pass the read function from the main _udp class to the thread, but i get always errors, since compiler says that thread class doens’t see the udp class… as:
void(_udp::rx*()) not compatible with void(rx*())

I am thinking of using the thread for recepcion and a timer for transmission. But I would like to know how to make it with 2 threads

class udp_thread : public DatagramSocket,
public Thread
{
public:

udp_thread(const String name)
	:Thread(String(name))
{
	rxotx = 0;


}
udp_thread(const String name, void *pargs, int xrxotx)
	:Thread(String(name))
{
	rxotx = xrxotx;
	
}
~udp_thread()
{
	stopThread(2000); //returns false if stopped by force.
}

void set_interval(int xinterval) {
	interval = xinterval;
}
void set_priority(int xprior) {
	priority = interval;
}

void run() override
{
	// this is the code that runs this thread - we'll loop continuously,
	// updating the coordinates of our blob.

	// threadShouldExit() returns true when the stopThread() method has been
	// called, so we should check it often, and exit as soon as it gets flagged.
	while (!threadShouldExit())
	{
		// sleep a bit so the threads don't all grind the CPU to a halt..
		wait(1);// interval);

				// because this is a background thread, we mustn't do any UI work without
				// first grabbing a MessageManagerLock..
		const MessageManagerLock mml(Thread::getCurrentThread());

		if (!mml.lockWasGained())  // if something is trying to kill this job, the lock
			return;                 // will fail, in which case we'd better return..

									// now we've got the UI thread locked, we can mess about with the components
									//RX
		int ret = 0;
		if (rxotx) {
			ret = waitUntilReady(false, 1000);
			if (ret == 1) {
				String s = "255.255.255.255";
				unsigned char data = 'X';
				ret = write(s, 65535, &data, 1);
				//					wait(-1);
			}

		}
		//RX
		else {
			ret = waitUntilReady(false, 1000);
			if (ret == 1) {
				//read(void *destBuffer, int maxBytesToRead, bool blockUntilSpecifiedAmountHasArrived, String &senderIPAddress, int &senderPortNumber)
				unsigned char rx_buffer[32];
				String IP;// = "255.255.255.255";
				int port = 65535;
				//int h = bindToPort(65535); (reads if  bound here)
				ret = read(rx_buffer, 32, 0, IP, port);
				if (ret > 0) {
					int i = 0;
				}

			}
		}
	}
}
private:
int interval, priority;
int rxotx;

//void (*rx)(void);

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(udp_thread)

};

class udp_ :	public DatagramSocket, //also inherits from Component?
{
public:
udp_(const String xth_tx, const String xth_rx)
{
	int i = 0;

	//udp_tx = new udp_thread(xth_tx);
	udp_rx = new udp_thread(xth_rx);
	addChildAndSetID(udp_rx, "rx_thread");
		
	startTimer(100);
	udp_rx->startThread();
//	udp_tx->startThread();

	//bind, etc
	//i = bindToPort(65535);
	

}
~udp_()
{
	udp_tx = nullptr;
	udp_rx = nullptr;
}
}
private:
ScopedPointer<udp_thread>udp_tx;
ScopedPointer<udp_thread>udp_rx;




void timerCallback() override
{
	int i = 0;
	
}

JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(udp_)

};


#2

Hi carlosm,

Are you trying to read and write on the same UDP port? It’s probably best to open a separate port for reading and writing.

Ed


#3

Yes, I use the same port to read and write. It how I think most work (whether I use a different port is becasue of a different purpose).

It is like wheter the thread has nothing to do with the parent class which it belongs to. I have tried succesfully having a callback in the thread to the parent, but if I perform more threads i think they will collpase using the same function for reading or writing and I guess I will have to use mutex and semaphores?

Regards
Carlos