[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnugo-devel] GTP sometimes fails
From: |
Darren Cook |
Subject: |
[gnugo-devel] GTP sometimes fails |
Date: |
Fri, 18 Mar 2005 13:30:25 +0900 |
User-agent: |
Mozilla Thunderbird 1.0 (X11/20041206) |
Hi,
I'm connecting to gnugo 3.6 using GTP: using pipes from C++. When
running my unit tests, they sometimes work fine, but sometimes they
fail. If it fails it seems to be on the first command I send after
successfully connecting.
I was originally using lower-level file descriptors, but after having
lots of problems, I looked at gnugo's oracle.c code and switched to
using two FILE*. So my code is now basically the same as oracle.c
(except I use fwrite instead of fprintf). Does anyone have similar
problems with that code?
My send_command() is shown below.
I'm wondering if when fgets() returns NULL if it means Gnugo is still
processing? Should I just do a continue to have it try again? Perhaps
combined with sleeping for a millisecond or something. Or should fgets()
wait forever until the remote program sends a carriage-return?
Darren
P.S. This is on linux, with gnugo running on the same single-CPU machine.
----------------------------------------
const char *send_command(const char *command)const{
size_t sz=strlen(command);
size_t bytes=fwrite(command,1,sz,fp_out);
if(sz!=bytes){
std::cerr<<"write to pipe failed: sent "<<sz<<" bytes, but only
"<<bytes<<" bytes written apparently\n"<<std::flush;;
return NULL;
}
fflush(fp_out);
const int MAX_BUFFER=2048;
static char readbuf[MAX_BUFFER];
size_t ix=0; //Where in readbuf we're up to
readbuf[0]=0;
while(ix<(MAX_BUFFER-256)){
char buf[256]; //Max line length assumed to be 256
char *p=fgets(buf,255,fp_in);
if(!p){
std::cerr<<"Failed to read from gtp stream\n"<<std::flush;;
return NULL;
}
size_t len=strlen(p);
if(len==1){
if(ix==0)continue; //gnugo always returns at least "=", so I think
//this means it has not replied yet.
break; //Blank line indicates end of reply
}
strncpy(&readbuf[ix],buf,255);
ix+=len;
}
if(readbuf[0]!='='){
std::cerr<<"Sending "<<command<<" returned this
string:"<<readbuf<<"\n"<<std::flush;
return NULL;
}
return readbuf;
}