Windows execmd: do check for cancellation

This commit is contained in:
Jean-Francois Dockes 2015-09-08 18:16:46 +02:00
parent 6590598765
commit fa13c25f6f
4 changed files with 29 additions and 11 deletions

View file

@ -152,6 +152,7 @@
<ClInclude Include="..\index\mimetype.h" /> <ClInclude Include="..\index\mimetype.h" />
<ClInclude Include="..\internfile\internfile.h" /> <ClInclude Include="..\internfile\internfile.h" />
<ClInclude Include="..\internfile\mh_symlink.h" /> <ClInclude Include="..\internfile\mh_symlink.h" />
<ClInclude Include="..\utils\cancelcheck.h" />
<ClInclude Include="..\utils\cpuconf.h" /> <ClInclude Include="..\utils\cpuconf.h" />
<ClInclude Include="..\utils\debuglog.h" /> <ClInclude Include="..\utils\debuglog.h" />
<ClInclude Include="..\utils\execmd.h" /> <ClInclude Include="..\utils\execmd.h" />

View file

@ -93,6 +93,9 @@
<ClInclude Include="..\utils\cpuconf.h"> <ClInclude Include="..\utils\cpuconf.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\utils\cancelcheck.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\utils\smallut.cpp"> <ClCompile Include="..\utils\smallut.cpp">

View file

@ -512,7 +512,7 @@ int ExecCmd::send(const string& data)
return -1; return -1;
} }
WaitResult waitRes = Wait(m->oInputWrite.hEvent, 5000); WaitResult waitRes = Wait(m->oInputWrite.hEvent, m->timeoutMs);
if (waitRes == Ok) { if (waitRes == Ok) {
if (!GetOverlappedResult(m->hInputWrite, if (!GetOverlappedResult(m->hInputWrite,
&m->oInputWrite, &dwWritten, TRUE)) { &m->oInputWrite, &dwWritten, TRUE)) {
@ -569,7 +569,9 @@ int ExecCmd::receive(string& data, int cnt)
} }
totread += dwRead; totread += dwRead;
data.append(chBuf, dwRead); data.append(chBuf, dwRead);
LOGDEB1(("ExecCmd::receive: got %d bytes\n", int(dwRead))); if (m->advise)
m->advise->newData(dwRead);
LOGDEB(("ExecCmd::receive: got %d bytes\n", int(dwRead)));
} else if (waitRes == Quit) { } else if (waitRes == Quit) {
if (!CancelIo(m->hOutputRead)) { if (!CancelIo(m->hOutputRead)) {
printError("CancelIo"); printError("CancelIo");
@ -577,13 +579,18 @@ int ExecCmd::receive(string& data, int cnt)
break; break;
} else if (waitRes == Timeout) { } else if (waitRes == Timeout) {
// We only want to cancel if m_advise says so here. Is the io still // We only want to cancel if m_advise says so here. Is the io still
// valid at this point ? // valid at this point ? Should we catch a possible exception to CancelIo?
if (m->advise)
m->advise->newData(0);
if (m->killRequest) {
LOGINFO(("ExecCmd::doexec: cancel request\n"));
if (!CancelIo(m->hOutputRead)) { if (!CancelIo(m->hOutputRead)) {
printError("CancelIo"); printError("CancelIo");
} }
break; break;
} }
} }
}
return totread; return totread;
} }

View file

@ -151,11 +151,12 @@ public:
void newData(int cnt) { void newData(int cnt) {
if (op_flags & OPT_c) { if (op_flags & OPT_c) {
static int callcnt; static int callcnt;
if (callcnt++ == 10) { if (callcnt++ == 5) {
// Just sets the cancellation flag // Just sets the cancellation flag
CancelCheck::instance().setCancel(); CancelCheck::instance().setCancel();
// Would be called from somewhere else and throws an // Would be called from somewhere else and throws an
// exception. We call it here for simplicity // exception. We call it here for simplicity
cerr << "newData: should throw !\n";
CancelCheck::instance().checkCancel(); CancelCheck::instance().checkCancel();
} }
} }
@ -223,7 +224,13 @@ int main(int argc, char *argv[])
case 'm': op_flags |= OPT_m; break; case 'm': op_flags |= OPT_m; break;
case 'i': op_flags |= OPT_i; break; case 'i': op_flags |= OPT_i; break;
case 'o': op_flags |= OPT_o; break; case 'o': op_flags |= OPT_o; break;
case'h': cout << "MESSAGE FROM TREXECMD\n"; return 0; case'h':
for (int i = 0; i < 10; i++) {
cout << "MESSAGE " << i << " FROM TREXECMD\n";
cout.flush();
sleep(1);
}
return 0;
default: Usage(); break; default: Usage(); break;
} }
b1: argc--; argv++; b1: argc--; argv++;
@ -265,10 +272,10 @@ int main(int argc, char *argv[])
// Set callback to be called whenever there is new data // Set callback to be called whenever there is new data
// available and at a periodic interval, to check for // available and at a periodic interval, to check for
// cancellation // cancellation
#ifdef LATER
MEAdv adv; MEAdv adv;
mexec.setAdvise(&adv); mexec.setAdvise(&adv);
mexec.setTimeout(5); //mexec.setTimeout(5);
#ifdef LATER
// Stderr output goes there // Stderr output goes there
mexec.setStderr("/tmp/trexecStderr"); mexec.setStderr("/tmp/trexecStderr");
#endif #endif