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
221
222
223
224
225
226
227
228
|
/************************************************************************************
Filename : Service_Win32_FastIPC_Client.cpp
Content : Client side of connectionless fast IPC
Created : Sept 16, 2014
Authors : Chris Taylor
Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
you may not use the Oculus VR Rift SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.
You may obtain a copy of the License at
http://www.oculusvr.com/licenses/LICENSE-3.2
Unless required by applicable law or agreed to in writing, the Oculus VR SDK
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************************/
#include "Service_Win32_FastIPC_Client.h"
namespace OVR { namespace Service { namespace Win32 {
using namespace OVR::Net;
//// FastIPCClient
FastIPCClient::FastIPCClient()
{
}
bool FastIPCClient::ReadInitialData(const char* buffer)
{
uint32_t magic = *(uint32_t*)(buffer);
uint32_t verMajor = *(uint32_t*)(buffer + 4);
uint32_t verMinor = *(uint32_t*)(buffer + 8);
if (magic != Magic)
{
LogError("Magic does not match");
return false;
}
if (verMajor != MajorVersion)
{
LogError("Major version mismatch");
return false;
}
if (verMinor < MinorVersion)
{
LogError("Remote minor version too old for our feature level");
return false;
}
HANDLE remoteDataEvent = (HANDLE)*(uint64_t*)(buffer + 12);
HANDLE remoteReturnEvent = (HANDLE)*(uint64_t*)(buffer + 20);
pid_t serverProcessId = (pid_t) *(uint64_t*)(buffer + 28);
OVR_UNUSED(serverProcessId);
if (!remoteDataEvent || !remoteReturnEvent)
{
LogError("Handshake was malformed. It seems like a version mismatch.");
return false;
}
DataEvent.Attach(remoteDataEvent);
ReturnEvent.Attach(remoteReturnEvent);
return true;
}
bool FastIPCClient::Initialize(const char* sharedMemoryName)
{
SharedMemory::OpenParameters params;
params.accessMode = SharedMemory::AccessMode_ReadWrite;
params.globalName = sharedMemoryName;
params.minSizeBytes = RegionSize;
params.openMode = SharedMemory::OpenMode_OpenOnly;
params.remoteMode = SharedMemory::RemoteMode_ReadWrite;
Scratch = SharedMemoryFactory::GetInstance()->Open(params);
IPCMessageIndex = 1;
if (!Scratch || Scratch->GetSizeI() < RegionSize)
{
OVR_ASSERT(false);
LogError("Unable to open shared memory region");
return false;
}
char* data = (char*)Scratch->GetData();
// If unable to read handshake,
if (!ReadInitialData(data))
{
return false;
}
return true;
}
FastIPCClient::~FastIPCClient()
{
}
bool FastIPCClient::Call(Net::BitStream* parameters, Net::BitStream* returnData, int timeoutMs)
{
// If not initialized,
if (!ReturnEvent.IsValid())
{
OVR_ASSERT(false);
return false;
}
volatile unsigned char* scratch = (unsigned char*)Scratch->GetData();
uint32_t bytesUsed = ((uint32_t)parameters->GetNumberOfBitsUsed() + 7) / 8;
// If data is too long,
if (bytesUsed > RegionSize - 4)
{
OVR_ASSERT(false);
return false;
}
// First 4 bytes will be the size of the parameters
// Note that this is for IPC so endian-ness is not important
*(uint32_t*)(scratch + 4) = bytesUsed;
// Copy data into place
memcpy((char*)scratch + 8, parameters->GetData(), bytesUsed);
// Don't allow read/write operations to move around this point
MemoryBarrier();
// Write message index
*(volatile uint32_t*)scratch = IPCMessageIndex;
// Wake the remote thread to service our request
if (!::SetEvent(DataEvent.Get()))
{
OVR_ASSERT(false);
return false;
}
// Wait for result of call:
++IPCMessageIndex;
// Use the GetTickCount() API for low-resolution timing
DWORD t0 = ::GetTickCount();
int remaining = timeoutMs;
// Forever,
for (;;)
{
// Wait on the return event
DWORD result = ::WaitForSingleObject(ReturnEvent.Get(), timeoutMs < 0 ? INFINITE : remaining);
if (result == WAIT_FAILED)
{
int err = GetLastError();
LogError("[FastIPC] Wait failed with error %d", err);
}
// If wait succeeded,
if (result == WAIT_OBJECT_0)
{
if (*(volatile uint32_t*)scratch != IPCMessageIndex)
{
double callTimeoutStart = Timer::GetSeconds();
while (*(volatile uint32_t*)scratch != IPCMessageIndex)
{
if (Timer::GetSeconds() - callTimeoutStart > 1.)
{
LogError("[FastIPC] Timed out waiting for remote IPC message to be written.");
OVR_ASSERT(false);
return false;
}
Thread::YieldCurrentThread();
}
}
_ReadBarrier();
// Wrap the scratch buffer
uint32_t len = *(uint32_t*)(scratch + 4);
returnData->WrapBuffer((unsigned char*)scratch + 8, len);
++IPCMessageIndex;
// Return true for success
return true;
}
// If not waiting forever,
if (timeoutMs > 0)
{
// If wait time has elapsed,
int elapsed = ::GetTickCount() - t0;
if (elapsed >= timeoutMs)
{
// Break out of loop returning false
break;
}
// Calculate remaining wait time
remaining = timeoutMs - elapsed;
}
// Continue waiting
}
return false;
}
}}} // namespace OVR::Service::Win32
|