summaryrefslogtreecommitdiff
path: root/utils/vmpi/net_view_thread.cpp
blob: 19ab38db2d0fd1be19c695959bab86ec64df6a1e (plain) (blame)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================//

#include "stdafx.h"
#include "net_view_thread.h"


char* CopyAlloc( const char *pStr )
{
	char *pRet = new char[ strlen( pStr ) + 1];
	strcpy( pRet, pStr );
	return pRet;
}


CNetViewThread::CNetViewThread()
{
	m_hThread = NULL;
	m_hThreadExitEvent = NULL;
	InitializeCriticalSection( &m_ComputerNamesCS );
}


CNetViewThread::~CNetViewThread()
{
	Term();
	DeleteCriticalSection( &m_ComputerNamesCS );
}


void CNetViewThread::Init()
{
	Term();

	m_hThreadExitEvent = CreateEvent( NULL, false, false, NULL );

	DWORD dwThreadID = 0;
	m_hThread = CreateThread( 
		NULL,
		0,
		&CNetViewThread::StaticThreadFn,
		this,
		0,
		&dwThreadID );
}


void CNetViewThread::Term()
{
	if ( m_hThread )
	{
		SetEvent( m_hThreadExitEvent );
		WaitForSingleObject( m_hThread, INFINITE );
		CloseHandle( m_hThread );
		m_hThread = NULL;
	}

	if ( m_hThreadExitEvent )
	{
		CloseHandle( m_hThreadExitEvent );
		m_hThreadExitEvent = NULL;
	}
}


void CNetViewThread::GetComputerNames( CUtlVector<char*> &computerNames )
{
	EnterCriticalSection( &m_ComputerNamesCS );

	computerNames.Purge();
	for ( int i=0; i < m_ComputerNames.Count(); i++ )
	{
		computerNames.AddToTail( CopyAlloc( m_ComputerNames[i] ) );
	}

	LeaveCriticalSection( &m_ComputerNamesCS );
}


void CNetViewThread::UpdateServicesFromNetView()
{
    HANDLE hChildStdoutRd, hChildStdoutWr;
    
	// Set the bInheritHandle flag so pipe handles are inherited.
    SECURITY_ATTRIBUTES saAttr; 
	saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); 
    saAttr.bInheritHandle = TRUE; 
    saAttr.lpSecurityDescriptor = NULL; 

	if( CreatePipe( &hChildStdoutRd, &hChildStdoutWr, &saAttr, 0 ) )
	{
		STARTUPINFO si;
		memset(&si, 0, sizeof si);
		si.cb = sizeof(si);
		si.dwFlags = STARTF_USESTDHANDLES;
		si.hStdOutput = hChildStdoutWr;
		
		PROCESS_INFORMATION pi;

		if( CreateProcess( 
			NULL, 
			"net view", 
			NULL,	// lpProcessAttributes
			NULL,	// lpThreadAttributes
			TRUE,	// bInheritHandls
			DETACHED_PROCESS,	// dwCreationFlags
			NULL,				// lpEnvironment
			NULL,				// lpCurrentDirectory
			&si,				// lpStartupInfo
			&pi					// lpProcessInformation
			) )
		{
			// read from pipe..
			#define BUFFER_SIZE 8192
			char buffer[BUFFER_SIZE];
			BOOL bDone = FALSE;
			CUtlVector<char> totalBuffer;
			
			while(1)
			{
				DWORD dwCount = 0;
				DWORD dwRead = 0;
				
				// read from input handle
				PeekNamedPipe(hChildStdoutRd, NULL, NULL, NULL, &dwCount, NULL);
				if (dwCount)
				{
					dwCount = min (dwCount, (DWORD)BUFFER_SIZE - 1);
					ReadFile(hChildStdoutRd, buffer, dwCount, &dwRead, NULL);
				}
				if(dwRead)
				{
					buffer[dwRead] = 0;
					totalBuffer.AddMultipleToTail( dwRead, buffer );
				}
				// check process termination
				else if( WaitForSingleObject( pi.hProcess, 1000 ) != WAIT_TIMEOUT )
				{
					if ( bDone )
						break;
					
					bDone = TRUE;	// next time we get it
				}
			}

			// Now parse the output.
			totalBuffer.AddToTail( 0 );
			ParseComputerNames( totalBuffer.Base() );
		}

		CloseHandle( hChildStdoutRd );
		CloseHandle( hChildStdoutWr );
	}
}


void CNetViewThread::ParseComputerNames( const char *pNetViewOutput )
{
	EnterCriticalSection( &m_ComputerNamesCS );

	m_ComputerNames.PurgeAndDeleteElements();
	
	const char *pCur = pNetViewOutput;
	while ( *pCur != 0 )
	{
		// If we get a \\, then it's a computer name followed by whitespace.
		if ( pCur[0] == '\\' && pCur[1] == '\\' )
		{
			char curComputerName[512];
			char *pOutPos = curComputerName;

			pCur += 2;
			while ( *pCur && !V_isspace( *pCur ) && (pOutPos-curComputerName < 510) )
			{
				*pOutPos++ = *pCur++;
			}
			*pOutPos = 0;

			m_ComputerNames.AddToTail( CopyAlloc( curComputerName ) );
		}
		++pCur;
	}

	LeaveCriticalSection( &m_ComputerNamesCS );
}


DWORD CNetViewThread::ThreadFn()
{
	// Update the services list every 30 seconds.
	do
	{
		UpdateServicesFromNetView();
	} while ( WaitForSingleObject( m_hThreadExitEvent, 30000 ) != WAIT_OBJECT_0 );

	return 0;
}


DWORD CNetViewThread::StaticThreadFn( LPVOID lpParameter )
{
	return ((CNetViewThread*)lpParameter)->ThreadFn();
}