-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinitializer.cpp
220 lines (204 loc) · 4.92 KB
/
initializer.cpp
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
209
210
211
212
213
214
215
216
217
218
219
220
/*
* Copyright(c) 2000 arton
*
* You may distribute under the terms of either the GNU General Public
* License
*
*/
#include "stdafx.h"
#include <process.h>
#include "RScript.h"
#include "Initializer.h"
#include "Win32Ole.h"
#include "RScriptCore.h"
#include "RubyWrapper.h"
CInitializer CInitializer::GlobalInitializer;
bool CInitializer::g_fInRuby(false);
//
// Create STA and run Ruby only in it.
//
class CRubyWrapperFilter : public IMessageFilter
{
public:
CRubyWrapperFilter() : m_pPrevious(NULL)
{
}
~CRubyWrapperFilter()
{
ATLTRACE(_T("Destroy MessageFilter\n"));
}
HRESULT STDMETHODCALLTYPE QueryInterface(
const IID & riid,
void **ppvObj)
{
if (InlineIsEqualGUID(riid, IID_IUnknown) || InlineIsEqualGUID(riid, IID_IMessageFilter))
{
*ppvObj = this;
return S_OK;
}
return E_NOINTERFACE;
}
ULONG STDMETHODCALLTYPE AddRef() {
return 1;
}
ULONG STDMETHODCALLTYPE Release() {
return 1;
}
//
DWORD STDMETHODCALLTYPE HandleInComingCall(
DWORD dwCallType, //Type of incoming call
HTASK threadIDCaller, //Task handle calling this task
DWORD dwTickCount, //Elapsed tick count
LPINTERFACEINFO lpInterfaceInfo //Pointer to INTERFACEINFO structure
)
{
switch (dwCallType)
{
case CALLTYPE_TOPLEVEL_CALLPENDING:
if (lpInterfaceInfo)
{
#ifdef _DEBUG
WCHAR sz[64];
StringFromGUID2(lpInterfaceInfo->iid, sz, 64);
ATLTRACE(_T("HandleInComingCall for %08X LPINFO method=%d, unk=%08X, IID=%ls\n"), threadIDCaller, lpInterfaceInfo->wMethod, lpInterfaceInfo->pUnk, sz);
#endif
if (InlineIsEqualGUID(IID_IRubyWrapper, lpInterfaceInfo->iid)
&& lpInterfaceInfo->wMethod > 4 && lpInterfaceInfo->wMethod < 10)
return SERVERCALL_REJECTED;
}
ATLTRACE(_T("HandleInComingCall for %08X result = %d\n"), threadIDCaller, SERVERCALL_RETRYLATER);
return SERVERCALL_ISHANDLED;
default:
break;
}
return SERVERCALL_ISHANDLED;
}
DWORD STDMETHODCALLTYPE RetryRejectedCall(
HTASK threadIDCallee, //Server task handle
DWORD dwTickCount, //Elapsed tick count
DWORD dwRejectType //Returned rejection message
)
{
if (m_pPrevious)
return m_pPrevious->RetryRejectedCall(threadIDCallee, dwTickCount, dwRejectType);
return 1000;
}
DWORD STDMETHODCALLTYPE MessagePending(
HTASK threadIDCallee, //Called applications task handle
DWORD dwTickCount, //Elapsed tick count
DWORD dwPendingType //Call type
)
{
if (m_pPrevious)
return m_pPrevious->MessagePending(threadIDCallee, dwTickCount, dwPendingType);
Sleep(100);
return PENDINGMSG_WAITNOPROCESS;
}
void Initialize()
{
HRESULT hr = CoRegisterMessageFilter(this, &m_pPrevious);
if (hr != S_OK)
{
m_pPrevious = NULL;
}
}
private:
IMessageFilter* m_pPrevious;
} g_Filter;
unsigned __stdcall RubyWrapper(void * pParam)
{
MSG msg;
// Create Thread Queue
PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE);
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
g_Filter.Initialize();
{
CComObject<CRubyWrapper>* p = NULL;
HRESULT hr = CComObject<CRubyWrapper>::CreateInstance(&p);
ATLTRACE(_T("Create Wrapper = %08X in thread %08X\n"), hr, GetCurrentThreadId());
if (hr == S_OK)
{
p->Globalize();
}
HANDLE h = *reinterpret_cast<HANDLE*>(pParam);
SetEvent(h);
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
if (msg.message == WM_TIMER)
{
if (msg.wParam && p->GetTimerID() == msg.wParam) break;
}
CInitializer::SetInRuby(true);
DispatchMessage(&msg);
CInitializer::SetInRuby(false);
}
ruby_finalize();
if (p)
{
p->Unglobalize();
while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
DispatchMessage(&msg);
}
}
}
try
{
CoUninitialize();
}
catch (...)
{
TerminateThread(GetCurrentThread(), -1);
}
_endthreadex(0);
return 0;
}
CInitializer::CInitializer()
: m_fInit(false), m_dwRubyThread(0)
{
}
#if defined(_DEBUG)
#define THREAD_INIT_TIMER 100000
#else
#define THREAD_INIT_TIMER 10000
#endif
void CInitializer::InitNew()
{
if (m_fInit) return;
Lock();
if (m_fInit)
{
Unlock();
return;
}
m_fInit = true;
HANDLE hEvt = CreateEvent(NULL, FALSE, FALSE, NULL);
long h = (long)_beginthreadex(NULL, 0, RubyWrapper, &hEvt, 0, reinterpret_cast<UINT*>(&m_dwRubyThread));
if (h != -1)
{
DWORD dw = WaitForSingleObject(hEvt, THREAD_INIT_TIMER);
ATLTRACE(_T("Wait = %08X\n"), dw);
CRubyWrapper::GetCWrapper()->SetHandle((HANDLE)h);
}
CloseHandle(hEvt);
Unlock();
}
void CInitializer::Destroy()
{
if (m_fInit)
{
m_fInit = false;
}
if (m_dwRubyThread)
{
CRubyWrapper* p = CRubyWrapper::GetCWrapper();
CloseHandle(p->GetHandle());
PostThreadMessage(m_dwRubyThread, WM_QUIT, 0, 0);
m_dwRubyThread = 0;
}
}
CInitializer::~CInitializer()
{
Destroy();
ATLTRACE(_("RScript Initalizer was Destroyed\n"));
}