【发布时间】:2018-01-25 00:44:31
【问题描述】:
我正在编写一个 Vala 程序,它使用标准输出和标准输入作为子进程生成,用于与父级进行消息传输。在异步方法中,我调用 read_stdin(sb) 来检查传入消息。我尝试了各种标准输入方法,例如gets 和getc,但它们似乎都阻塞了'\n' 换行符。我需要读取所有可用的字符并立即返回(非阻塞)。
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;
}
stdin 可以用于非阻塞 IO 还是我需要创建类似...
DataInputStream stdin_reader = new DataInputStream(stdin);
【问题讨论】:
标签: vala