-
Notifications
You must be signed in to change notification settings - Fork 0
/
MsgOpt.h
74 lines (66 loc) · 1.65 KB
/
MsgOpt.h
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#pragma once
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <sstream>
// #pragma comment(lib, "user32.lib")
using namespace std;
enum MsgType {
msg_client = 1,
msg_svr = 2,
win_handle = 3,
};
struct MsgStruct {
MsgType type;
int val;
};
class EvtOpt {
private:
HANDLE _hEvent;
public:
EvtOpt() {}
~EvtOpt() {
Uninit();
}
bool Create(string name); // for event svr
bool Connect(string name); // for event client
bool Wait(DWORD time = INFINITE);
bool Signal();
bool Uninit();
};
typedef bool(*PFRcvMsg)(MsgStruct& msg);
class MsgOpt {
private:
HANDLE _hMapFile;
LPVOID _pBuf;
int _bufSize;
bool _exited;
EvtOpt _evtOpt;
PFRcvMsg _onRcvMsg;
public:
MsgOpt(int bufSize) :_bufSize(bufSize) {}; // used by client
MsgOpt(int bufSize, PFRcvMsg onRcvMsg) : // used by svr
_bufSize(bufSize),
_onRcvMsg(onRcvMsg),
_exited(false) { }
~MsgOpt() { Uninit(); }
bool Listen(string name); // used by svr
bool WaitMsg(int time = INFINITE);
bool Connect(string name); // used by client
bool PostMsg(MsgStruct& msg);
bool Uninit();
};
class MsgMgr {
private:
MsgOpt _msgSvr;
MsgOpt _msgClient;
public:
MsgMgr(int bufSize, PFRcvMsg onRcvMsg) :
_msgSvr(bufSize, onRcvMsg),
_msgClient(bufSize) {};
~MsgMgr() { Destroy(); };
bool Create(string name, bool main);
bool WaitMsg(int time = INFINITE);
bool PostMsg(MsgStruct& msg);
bool Destroy();
};