NamedPipe class splitted into NAmedPipe and NamedPipeListener; nxapush switched to...
[public/netxms.git] / src / libnetxms / nxproc.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** NetXMS Foundation Library
4 ** Copyright (C) 2003-2017 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU Lesser General Public License as published
8 ** by the Free Software Foundation; either version 3 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU Lesser General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: nxproc.cpp
21 **
22 **/
23
24 #include "libnetxms.h"
25 #include <nxproc.h>
26
27 /**
28 * Named pipe listener constructor
29 */
30 NamedPipeListener::NamedPipeListener(const TCHAR *name, HPIPE handle, NamedPipeRequestHandler reqHandler, void *userArg)
31 {
32 nx_strncpy(m_name, name, MAX_PIPE_NAME_LEN);
33 m_handle = handle;
34 m_reqHandler = reqHandler;
35 m_userArg = userArg;
36 m_serverThread = INVALID_THREAD_HANDLE;
37 m_stop = false;
38 }
39
40 /**
41 * Start named pipe server
42 */
43 void NamedPipeListener::start()
44 {
45 if (m_serverThread != INVALID_THREAD_HANDLE)
46 return; // already started
47
48 m_stop = false;
49 m_serverThread = ThreadCreateEx(NamedPipeListener::serverThreadStarter, 0, this);
50 }
51
52 /**
53 * Stop named pipe server
54 */
55 void NamedPipeListener::stop()
56 {
57 m_stop = true;
58 ThreadJoin(m_serverThread);
59 m_serverThread = INVALID_THREAD_HANDLE;
60 }
61
62 /**
63 * Named pipe server thread starter
64 */
65 THREAD_RESULT THREAD_CALL NamedPipeListener::serverThreadStarter(void *arg)
66 {
67 static_cast<NamedPipeListener*>(arg)->serverThread();
68 return THREAD_OK;
69 }
70
71 /**
72 * Named pipe constructor
73 */
74 NamedPipe::NamedPipe(const TCHAR *name, HPIPE handle)
75 {
76 nx_strncpy(m_name, name, MAX_PIPE_NAME_LEN);
77 m_handle = handle;
78 m_writeLock = MutexCreate();
79 }