d127cf9e5cfb7128646d28a3a8e43a51c2b55f52
[public/netxms.git] / include / nxproc.h
1 #ifndef _nxproc_h_
2 #define _nxproc_h_
3
4 #include <nms_common.h>
5
6 /**
7 * Max pipe name length
8 */
9 #define MAX_PIPE_NAME_LEN 64
10
11 class NamedPipe;
12
13 /**
14 * Client request handler
15 */
16 typedef void (*NamedPipeRequestHandler)(NamedPipe *pipe, void *userArg);
17
18 /**
19 * Named pipe class
20 */
21 class LIBNETXMS_EXPORTABLE NamedPipe
22 {
23 private:
24 TCHAR m_name[MAX_PIPE_NAME_LEN];
25 HPIPE m_handle;
26 bool m_isListener;
27 NamedPipeRequestHandler m_reqHandler;
28 void *m_userArg;
29 THREAD m_serverThread;
30 bool m_stop;
31
32 NamedPipe(const TCHAR *name, HPIPE handle, bool listener, NamedPipeRequestHandler reqHandler = NULL, void *userArg = NULL)
33 {
34 nx_strncpy(m_name, name, MAX_PIPE_NAME_LEN);
35 m_handle = handle;
36 m_isListener = listener;
37 m_reqHandler = reqHandler;
38 m_userArg = userArg;
39 m_serverThread = INVALID_THREAD_HANDLE;
40 m_stop = false;
41 }
42
43 void serverThread();
44 static THREAD_RESULT THREAD_CALL serverThreadStarter(void *arg);
45
46 public:
47 ~NamedPipe();
48
49 const TCHAR *name() const { return m_name; }
50 HPIPE handle() { return m_handle; }
51 bool isListener() const { return m_isListener; }
52
53 void startServer();
54 void stopServer();
55
56 static NamedPipe *createListener(const TCHAR *name, NamedPipeRequestHandler reqHandler, void *userArg);
57 static NamedPipe *connectTo(const TCHAR *name);
58 };
59
60 #endif /* _nxproc_h_ */