X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=UCI2WB.c;h=00501461cc033a50cec0417b501bf260c418931a;hb=5d50faa12b64983f87219b8b4a50ce8a6c7bcbee;hp=e709384f4d7d716d55d37fd5888dff9f92d3fcc3;hpb=64739477abdd86edbea682069de301b3affddddc;p=uci2wb.git diff --git a/UCI2WB.c b/UCI2WB.c index e709384..0050146 100644 --- a/UCI2WB.c +++ b/UCI2WB.c @@ -20,12 +20,6 @@ #include #include -#ifdef _MSC_VER -#define SLEEP() Sleep(1) -#else -#define SLEEP() usleep(10) -#endif - // Set VARIANTS for in WinBoard variant feature. (With -s option this will always be reset to use "shogi".) # define VARIANTS "normal,xiangqi" @@ -41,6 +35,21 @@ int statDepth, statScore, statNodes, statTime, currNr, size; char currMove[20]; FILE *toE, *fromE; int pid; +#ifdef WIN32 +WinPipe(HANDLE *hRd, HANDLE *hWr) +{ + SECURITY_ATTRIBUTES saAttr; + + /* Set the bInheritHandle flag so pipe handles are inherited. */ + saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); + saAttr.bInheritHandle = TRUE; + saAttr.lpSecurityDescriptor = NULL; + + /* Create a pipe */ + return CreatePipe(hRd, hWr, &saAttr, 0); +} +#endif + #define INIT 0 #define WAKEUP 1 #define PAUSE 2 @@ -49,7 +58,12 @@ void Sync (int action) { #ifdef WIN32 - if(action == PAUSE) while(pause) SLEEP(); // in Windows we still rely on polling. :-( + static HANDLE hWr, hRd; DWORD d; char c; + switch(action) { + case INIT: WinPipe(&hRd, &hWr); break; + case WAKEUP: WriteFile(hWr, "\n", 1, &d, NULL); break; + case PAUSE: ReadFile(hRd, &c, 1, &d, NULL); + } #else static int syncPipe[2]; char c; switch(action) { @@ -292,7 +306,7 @@ printf("# start search\n"); } else fprintf(toE, "setoption name %s\n", line+7), printf("# setoption name %s\n", line+7); } else if(!strcmp(command, "protover")) { - printf("feature variants=\"%s\" setboard=1 usermove=1 debug=1 reuse=0 done=0\n", sc=='s' ? "shogi,5x5+5_shogi" : VARIANTS); + printf("feature variants=\"%s\" setboard=1 usermove=1 debug=1 ping=1 reuse=0 done=0\n", sc=='s' ? "shogi,5x5+5_shogi" : VARIANTS); fprintf(toE, "u%ci\n", sc); // this prompts UCI engine for options } else if(!strcmp(command, "setboard")) { @@ -327,6 +341,7 @@ printf("# start search\n"); else if(!strcmp(command, "nopost")) post = 0; else if(!strcmp(command, "easy")) ponder = 0; else if(!strcmp(command, "hard")) ponder = 1; + else if(!strcmp(command, "ping")) pause = 1, fprintf(toE, "isready\n"), fflush(toE), Sync(PAUSE), printf("pong %s", line+5); else if(!strcmp(command, "memory")) sscanf(line, "memory %d", &memory); else if(!strcmp(command, "cores")) sscanf(line, "cores %d", &cores); else if(!strcmp(command, "sd")) sscanf(line, "sd %d", &depth); @@ -341,22 +356,16 @@ StartEngine(char *cmdLine, char *dir) #ifdef WIN32 HANDLE hChildStdinRd, hChildStdinWr, hChildStdoutRd, hChildStdoutWr; - SECURITY_ATTRIBUTES saAttr; BOOL fSuccess; PROCESS_INFORMATION piProcInfo; STARTUPINFO siStartInfo; DWORD err; - /* Set the bInheritHandle flag so pipe handles are inherited. */ - saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); - saAttr.bInheritHandle = TRUE; - saAttr.lpSecurityDescriptor = NULL; - /* Create a pipe for the child's STDOUT. */ - if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0)) return GetLastError(); + if (! WinPipe(&hChildStdoutRd, &hChildStdoutWr)) return GetLastError(); /* Create a pipe for the child's STDIN. */ - if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) return GetLastError(); + if (! WinPipe(&hChildStdinRd, &hChildStdinWr)) return GetLastError(); SetCurrentDirectory(dir); // go to engine directory