I'm writing a Vala program which is spawned as a child_process using stdout & stdin for message transfer with parent. In an async method I call read_stdin(sb) to check for incoming messages. I tried various stdin methods like gets and getc, but they all appear to block on '\n' newline. I need to read all chars available and return immediately (non-blocking).
const int STDIN_BUF_SIZE=1024;
char[] stdin_buffer;
bool read_stdin (StringBuilder sb) {
int iPos=0;
int c;
int buf_size=STDIN_BUF_SIZE-1;
// Char by char:
while (true) {
c = stdin.getc ();
if(c == GLib.FileStream.EOF) break;
stdin_buffer[iPos]=(char)c;
iPos++;
if ((char)c=='\n'||iPos==buf_size){
break;
}
}
if(iPos>0){
stdin_buffer[iPos]=0;
sb.append((string)stdin_buffer);
return true;
}
return false;
}
Can stdin be used for non-blocking IO or do I need to create something like...
DataInputStream stdin_reader = new DataInputStream(stdin);