OpenBarnyard
 
Loading...
Searching...
No Matches
T2NamedPipeServer_Win.cpp
Go to the documentation of this file.
1#include "ToshiPCH.h"
3
4#include "Toshi/T2String.h"
5
6//-----------------------------------------------------------------------------
7// Enables memory debugging.
8// Note: Should be the last include!
9//-----------------------------------------------------------------------------
10#include <Core/TMemoryDebugOn.h>
11
13
15 : m_pServer( a_pServer )
16{
17}
18
20{
21 while ( TTRUE )
22 {
23 TBOOL bHasClient = TTRUE;
24
25 if ( ConnectNamedPipe( m_pServer->m_hPipe, NULL ) == FALSE )
26 {
27 auto iErr = GetLastError();
28
29 if ( iErr == ERROR_NO_DATA )
30 {
31 DisconnectNamedPipe( m_pServer->m_hPipe );
32 TTRACE( "A client has disconnected from the '%s' named pipe\n", m_pServer->m_szName );
33 m_pServer->m_bHasClient = TFALSE;
34 bHasClient = TFALSE;
35 }
36 else if ( iErr != ERROR_PIPE_CONNECTED )
37 {
38 TASSERT( TFALSE, "Failed ConnectNamedPipe" );
39 bHasClient = TFALSE;
40 }
41 }
42
43 if ( bHasClient )
44 {
45 if ( m_pServer->m_bHasClient == TFALSE )
46 {
47 TTRACE( "A client has connected to the '%s' named pipe\n", m_pServer->m_szName );
48 m_pServer->m_bHasClient = TTRUE;
49 }
50
51 if ( m_pServer->m_fnUpdateStream )
52 {
53 m_pServer->m_fnUpdateStream( m_pServer->m_pMemory, m_pServer->m_uiMemorySize, m_pServer->m_pUserData );
54 }
55
56 if ( m_pServer->m_pMemory && m_pServer->m_uiMemorySize > 0 )
57 {
58 WriteFile(
59 m_pServer->m_hPipe,
60 m_pServer->m_pMemory,
61 m_pServer->m_uiMemorySize,
62 NULL,
63 NULL
64 );
65 }
66 }
67
68 ThreadSleep( m_pServer->m_uiSendInterval );
69 }
70}
71
73 : m_pThread( TNULL ), m_pMemory( TNULL ), m_fnUpdateStream( TNULL ), m_uiMemorySize( 0 ), m_hPipe( INVALID_HANDLE_VALUE ), m_bHasClient( TFALSE ), m_bStarted( TFALSE ), m_pUserData( TNULL )
74{
75}
76
81
82TBOOL T2NamedPipeServer::Start( const TCHAR* a_szName, TUINT a_uiSendInterval, TUINT a_uiOutBufferSize, TUINT a_uiInBufferSize )
83{
84 TASSERT( TNULL != a_szName );
85 TASSERT( TFALSE == m_bStarted );
86
87 m_uiSendInterval = a_uiSendInterval;
88 m_szName = a_szName;
89
90 T2FormatString256 pipeName;
91 pipeName.Format( "\\\\.\\pipe\\%s", a_szName );
92
93 m_hPipe = CreateNamedPipeA(
94 pipeName.Get(),
95 PIPE_ACCESS_OUTBOUND,
96 PIPE_TYPE_MESSAGE,
97 1,
98 a_uiOutBufferSize,
99 a_uiInBufferSize,
100 0,
101 NULL
102 );
103
104 TASSERT( INVALID_HANDLE_VALUE != m_hPipe );
105
106 m_pThread = new T2NamedPipeServerThread( this );
107 m_bStarted = m_pThread->Create( 0, TThread::THREAD_PRIORITY_LOWEST, 0 );
108
109 TTRACE( "Started named pipe '%s'\n", m_szName );
110
111 TASSERT( TTRUE == m_bStarted );
112 return m_bStarted;
113}
114
116{
117 if ( m_bStarted && m_pThread )
118 {
119 m_pThread->Destroy();
120
121 delete m_pThread;
122 m_pThread = TNULL;
123 m_bStarted = TFALSE;
124 }
125}
126
128{
129 m_fnUpdateStream = a_fnUpdateCallback;
130}
131
132void T2NamedPipeServer::SetMemoryStream( void* a_pMemory, TUINT a_uiSize )
133{
134 m_pMemory = a_pMemory;
135 m_uiMemorySize = a_uiSize;
136}
137
#define TASSERT(X,...)
Definition Defines.h:138
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TTRACE(...)
Definition Defines.h:155
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
T2FormatString< 256, T2StringTraits< TCHAR > > T2FormatString256
Definition T2String.h:243
unsigned int TUINT
Definition Typedefs.h:8
char TCHAR
Definition Typedefs.h:20
#define TNULL
Definition Typedefs.h:23
#define TFALSE
Definition Typedefs.h:24
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
void ThreadSleep(TUINT dwMilliseconds)
T2NamedPipeServerThread(T2NamedPipeServer *a_pServer)
void SetMemoryStream(void *a_pMemory, TUINT a_uiSize)
void SetMemoryStreamUpdateCallback(UpdateStreamCallback_t a_fnUpdateCallback)
void(*)(void *&a_rMemoryStream, TUINT &a_rDataSize, void *a_pUserData) UpdateStreamCallback_t
TBOOL Start(const TCHAR *a_szName, TUINT a_uiSendInterval, TUINT a_uiOutBufferSize=1, TUINT a_uiInBufferSize=64 *1024)
@ THREAD_PRIORITY_LOWEST
Definition TThread_Win.h:31
void Format(const CharType *a_szFormat,...)
Definition T2String.h:172
constexpr CharType * Get()
Definition T2String.h:204