Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Community » Newbie corner » Stopping ReadStdIn() function
Re: Stopping ReadStdIn() function [message #57043 is a reply to message #57030] Mon, 17 May 2021 11:50 Go to previous messageGo to previous message
Oblivion is currently offline  Oblivion
Messages: 1094
Registered: August 2007
Senior Contributor
You can also write a non-blocking version of ReadStdIn so that you can set a timeout value. (this is for posix, but it can be easily adapted to windows)

void SetNonBlocking()
{
	fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) | O_NONBLOCK);
}

void SetBlocking()
{
	fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) & ~O_NONBLOCK);
}

String ReadStdIn2(int timeout)
{
	String r;
	SetNonBlocking();
	int t = msecs();
	do {
		SocketWaitEvent we;
		we.Add(STDIN_FILENO, WAIT_READ);
		if(we.Wait(10) && (we[0] & WAIT_READ)) {
			int c = 0;
			int n = read(STDIN_FILENO, (char*) &c, 1);
			if(c == '\n')
				break;
			if(n > 0) {
				r.Cat(c);
				t = msecs();
			}
		}
	}
	while(msecs(t) < timeout && !done);
	SetBlocking();
	return r.GetCount() ? r : String::GetVoid();
}



Best regards,
Oblivion


[Updated on: Mon, 17 May 2021 13:01]

Report message to a moderator

 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: static
Next Topic: my TCP client/server don't work correctly
Goto Forum:
  


Current Time: Tue May 14 10:09:24 CEST 2024

Total time taken to generate the page: 0.02335 seconds