Asynchronous STDIN - windows libraries

C

Caballito

Hi!
I'd like to write a program, which makes an infinite loop doing
{ perform_operations(); sleep(for_minute);}. The program should always
monitor the stardard console input, if user inserts a command, program
wakes up from his sleep and handles the command.

I wrote the code using Asynchronous I/O multiplexing, however there is
one problem: I would like the program to be more server-like, which
means that the command is handled (and the program is interrupted)
only if user inserts full command and presses enter. The code I wrote,
when I do only one keypress holds and waits for message, while I'd
rather want it to not stop his loop until enter is pressed.

Here goes the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53


#include <windows.h>
#include <winbase.h>
#include <string>
#include <iostream>
using namespace std;
int main(){

struct _OVERLAPPED over;

ULONG_PTR internal = 0;
ULONG_PTR internalHigh = 0;
PWORD pointer = 0;
over.Internal = internal;
over.InternalHigh = internalHigh;
over.Offset = 0;
over.OffsetHigh = 2000;


struct _SECURITY_ATTRIBUTES secat;
secat.nLength = sizeof (struct _SECURITY_ATTRIBUTES);
secat.lpSecurityDescriptor = NULL;
secat.bInheritHandle = FALSE;


HANDLE conin = CreateFile("CONIN$",//console input
GENERIC_READ,
FILE_SHARE_READ,
&secat,
OPEN_EXISTING,
0,
0);


char bufor[2000];
DWORD i;

HANDLE handle[1];
handle[0]=conin;

while (1){
cout <<"I start my loop...";
DWORD test = WaitForMultipleObjects( 1, handle, TRUE, 5000);

if (test ==WAIT_TIMEOUT) cout <<" ...Timeout!"<<endl;
else if (test ==WAIT_FAILED) cout <<"error!"<<endl;

else {
for (int ii = 0; ii<2000; ii++) bufor [ii] = '\0';
ReadFile (conin, bufor, 2000, &i, &over);
cout << "I got: " << bufor;
}

}


Could you help me with this one?
Big thanks in advance for any info!
 
I

Ian Collins

Hi!
I'd like to write a program, which makes an infinite loop doing
{ perform_operations(); sleep(for_minute);}. The program should always
monitor the stardard console input, if user inserts a command, program
wakes up from his sleep and handles the command.

Try posting to a windows programming group, this isn't a C++ language
question.
 
G

Goran

Hi!
I'd like to write a program, which makes an infinite loop doing
{ perform_operations(); sleep(for_minute);}. The program should always
monitor the stardard console input, if user inserts a command, program
wakes up from his sleep and handles the command.

I wrote the code using Asynchronous I/O multiplexing, however there is
one problem: I would like the program to be more server-like, which
means that the command is handled (and the program is interrupted)
only if user inserts full command and presses enter.

From your description, I have no idea why you went async IO route. If
user action is the only source of commands, then perhaps you can do
the following:

string getline(std::istream& stream)
{
const size_t MAX_CHARS=3; // exact size irrelevant.
char buffer[MAX_CHARS+1];
std::string s;
do
{
stream.clear();
stream.getline(buffer, MAX_CHARS);
if (stream.bad())
{ // Report error
}
s += std::string(buffer);
}
while(stream.fail());
return s;
}

and then:

void process_command(const std::string& s) {...}
....
while (come_condition)
process_command(getline(std::cin));

Goran.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top