Xboard - fire & forget
Posted:
25 May 2006, 14:33
by jarema
Hi all !
I'm trying to write simple XBoard client & I have known old problem with
CHESS ENGINE COMMUNICATION PROTOCOL.
I read (of course)
http://www.tim-mann.org/xboard/engine-intf.html and have tried many ways to put messages to XBoard and get the answers from it.
But all my ideas fails. (stdin, stdout - popen, dup, fork, close... ehhhh it's still doesn't' work).
So - I thing many beginner programmers have similar problem.
Does anybody have a simple program in C or C++ which allow take control over XBoard?
I prefer Fire && Forget solution, because I've spent many hours on thing - that was rather Reverse Engineering.
Re: Xboard - fire & forget
Posted:
26 May 2006, 19:51
by Dann Corbit
Most of the time, the problems with winboard/xboard communication stem from fogetting to turn off stdin and stdout (or cin and cout) buffering.
It's sort of like the Nagle problem with TCP/IP communications.
If you need to be really sure about transactions, you have to make sure that the buffers don't sit in memory.
There are probably 30 open source Xboard engines you can examine to see how communication works.
Here is an overkill method to turn off buffering in C:
// No buffering, please...
setbuf(stdout, NULL);
setbuf(stdin, NULL);
// and I *really* mean it, too!
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stdin, NULL, _IONBF, 0);
Probably, only the second set is needed. This is from the C99 ANSI/ISO standard:
7.19.5.5 The setbuf function
Synopsis
1 #include <stdio.h>
void setbuf(FILE * restrict stream,
char * restrict buf);
Description
2 Except that it returns no value, the setbuf function is equivalent to the setvbuf function invoked with the values _IOFBF for mode and BUFSIZ for size, or (if buf is a null pointer), with the value _IONBF for mode.
Returns
3 The setbuf function returns no value.
Forward references: the setvbuf function (7.19.5.6).
7.19.5.6 The setvbuf function
Synopsis
1 #include <stdio.h>
int setvbuf(FILE * restrict stream,
char * restrict buf,
int mode, size_t size);
Description
2 The setvbuf function may be used only after the stream pointed to by stream has been associated with an open file and before any other operation (other than an unsuccessful call to setvbuf) is performed on the stream. The argument mode determines how stream will be buffered, as follows: _IOFBF causes input/output to be fully buffered; _IOLBF causes input/output to be line buffered; _IONBF causes input/output to be unbuffered. If buf is not a null pointer, the array it points to may be used instead of a buffer allocated by the setvbuf function230) and the argument size specifies the size of the array; otherwise, size may determine the size of a buffer allocated by the setvbuf function. The contents of the array at any time are indeterminate.
Returns
3 The setvbuf function returns zero on success, or nonzero if an invalid value is given for mode or if the request cannot be honored.
For C++, use the pubsetbuf() method like this:
cin.rdbuf()->pubsetbuf(NULL,0);
cout.rdbuf()->pubsetbuf(NULL,0);
Here is the ANSI/ISO C++ standard on what pubsetbuf does.
27.5.2.2.2 Buffer management and positioning [lib.streambuf.buffer]
basic_streambuf<char_type,traits>* pubsetbuf(char_type* s, streamsize n);
1 Returns: setbuf(s,n).