ovr_sdk

diff LibOVR/Src/Service/Service_NetClient.cpp @ 0:1b39a1b46319

initial 0.4.4
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 14 Jan 2015 06:51:16 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/LibOVR/Src/Service/Service_NetClient.cpp	Wed Jan 14 06:51:16 2015 +0200
     1.3 @@ -0,0 +1,882 @@
     1.4 +/************************************************************************************
     1.5 +
     1.6 +Filename    :   Service_NetClient.cpp
     1.7 +Content     :   Client for service interface
     1.8 +Created     :   June 12, 2014
     1.9 +Authors     :   Michael Antonov, Kevin Jenkins, Chris Taylor
    1.10 +
    1.11 +Copyright   :   Copyright 2014 Oculus VR, LLC All Rights reserved.
    1.12 +
    1.13 +Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); 
    1.14 +you may not use the Oculus VR Rift SDK except in compliance with the License, 
    1.15 +which is provided at the time of installation or download, or which 
    1.16 +otherwise accompanies this software in either electronic or hard copy form.
    1.17 +
    1.18 +You may obtain a copy of the License at
    1.19 +
    1.20 +http://www.oculusvr.com/licenses/LICENSE-3.2 
    1.21 +
    1.22 +Unless required by applicable law or agreed to in writing, the Oculus VR SDK 
    1.23 +distributed under the License is distributed on an "AS IS" BASIS,
    1.24 +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.25 +See the License for the specific language governing permissions and
    1.26 +limitations under the License.
    1.27 +
    1.28 +************************************************************************************/
    1.29 +
    1.30 +#include "Service_NetClient.h"
    1.31 +#include "../Net/OVR_MessageIDTypes.h"
    1.32 +
    1.33 +#if defined (OVR_OS_MAC) || defined(OVR_OS_LINUX)
    1.34 +#define GetCurrentProcessId getpid
    1.35 +#endif
    1.36 +OVR_DEFINE_SINGLETON(OVR::Service::NetClient);
    1.37 +
    1.38 +namespace OVR { namespace Service {
    1.39 +
    1.40 +using namespace OVR::Net;
    1.41 +
    1.42 +
    1.43 +//// NetClient
    1.44 +
    1.45 +NetClient::NetClient() :
    1.46 +    LatencyTesterAvailable(false),
    1.47 +    HMDCount(0),
    1.48 +    EdgeTriggeredHMDCount(false)
    1.49 +{
    1.50 +    GetSession()->AddSessionListener(this);
    1.51 +
    1.52 +    // Register RPC functions
    1.53 +    registerRPC();
    1.54 +
    1.55 +    Start();
    1.56 +
    1.57 +	// Must be at end of function
    1.58 +    PushDestroyCallbacks();
    1.59 +}
    1.60 +
    1.61 +NetClient::~NetClient()
    1.62 +{
    1.63 +}
    1.64 +
    1.65 +void NetClient::OnSystemDestroy()
    1.66 +{
    1.67 +    onSystemDestroy();
    1.68 +}
    1.69 +
    1.70 +void NetClient::OnThreadDestroy()
    1.71 +{
    1.72 +    onThreadDestroy();
    1.73 +}
    1.74 +
    1.75 +int NetClient::Run()
    1.76 +{
    1.77 +    SetThreadName("NetClient");
    1.78 +
    1.79 +    while (!Terminated)
    1.80 +    {
    1.81 +        // Note: There is no watchdog here because the watchdog is part of the private code
    1.82 +
    1.83 +        GetSession()->Poll(false);
    1.84 +
    1.85 +        if (GetSession()->GetActiveSocketsCount() == 0)
    1.86 +        {
    1.87 +            Thread::MSleep(10);
    1.88 +        }
    1.89 +    }
    1.90 +
    1.91 +    return 0;
    1.92 +}
    1.93 +
    1.94 +void NetClient::OnReceive(ReceivePayload* pPayload, ListenerReceiveResult* lrrOut)
    1.95 +{
    1.96 +    OVR_UNUSED(lrrOut);
    1.97 +    OVR_UNUSED(pPayload);
    1.98 +}
    1.99 +
   1.100 +void NetClient::OnDisconnected(Connection* conn)
   1.101 +{
   1.102 +    OVR_UNUSED(conn);
   1.103 +
   1.104 +    OVR_DEBUG_LOG(("[NetClient] Disconnected"));
   1.105 +
   1.106 +    EdgeTriggeredHMDCount = false;
   1.107 +}
   1.108 +
   1.109 +void NetClient::OnConnected(Connection* conn)
   1.110 +{
   1.111 +    OVR_UNUSED(conn);
   1.112 +
   1.113 +    OVR_DEBUG_LOG(("[NetClient] Connected to a server running version %d.%d.%d (my version=%d.%d.%d)",
   1.114 +        conn->RemoteMajorVersion, conn->RemoteMinorVersion, conn->RemotePatchVersion,
   1.115 +        RPCVersion_Major, RPCVersion_Minor, RPCVersion_Patch));
   1.116 +
   1.117 +    EdgeTriggeredHMDCount = false;
   1.118 +}
   1.119 +
   1.120 +bool NetClient::Connect(bool blocking)
   1.121 +{
   1.122 +    // Set up bind parameters
   1.123 +	OVR::Net::BerkleyBindParameters bbp;
   1.124 +	bbp.Address = "::1"; // Bind to localhost only!
   1.125 +    bbp.blockingTimeout = 5000;
   1.126 +	OVR::Net::SockAddr sa;
   1.127 +	sa.Set("::1", VRServicePort, SOCK_STREAM);
   1.128 +
   1.129 +    // Attempt to connect
   1.130 +    OVR::Net::SessionResult result = GetSession()->ConnectPTCP(&bbp, &sa, blocking);
   1.131 +
   1.132 +    // Already connected counts as success too
   1.133 +    return result == Net::SessionResult_OK ||
   1.134 +           result == Net::SessionResult_AlreadyConnected ||
   1.135 +           result == Net::SessionResult_ConnectInProgress;
   1.136 +}
   1.137 +
   1.138 +void NetClient::Disconnect()
   1.139 +{
   1.140 +    GetSession()->Shutdown();
   1.141 +}
   1.142 +
   1.143 +bool NetClient::IsConnected(bool attemptReconnect, bool blockOnReconnect)
   1.144 +{
   1.145 +    // If it was able to connect,
   1.146 +    if (GetSession()->GetConnectionCount() > 0)
   1.147 +    {
   1.148 +        return true;
   1.149 +    }
   1.150 +    else if (attemptReconnect)
   1.151 +    {
   1.152 +        // Attempt to connect here
   1.153 +        Connect(blockOnReconnect);
   1.154 +
   1.155 +        // If it connected,
   1.156 +        if (GetSession()->GetConnectionCount() > 0)
   1.157 +        {
   1.158 +            return true;
   1.159 +        }
   1.160 +    }
   1.161 +
   1.162 +    // No connections
   1.163 +    return false;
   1.164 +}
   1.165 +
   1.166 +void NetClient::GetLocalProtocolVersion(int& major, int& minor, int& patch)
   1.167 +{
   1.168 +    major = RPCVersion_Major;
   1.169 +    minor = RPCVersion_Minor;
   1.170 +    patch = RPCVersion_Patch;
   1.171 +}
   1.172 +
   1.173 +bool NetClient::GetRemoteProtocolVersion(int& major, int& minor, int& patch)
   1.174 +{
   1.175 +    Ptr<Connection> conn = GetSession()->GetConnectionAtIndex(0);
   1.176 +
   1.177 +    if (conn)
   1.178 +    {
   1.179 +        major = conn->RemoteMajorVersion;
   1.180 +        minor = conn->RemoteMinorVersion;
   1.181 +        patch = conn->RemotePatchVersion;
   1.182 +        return true;
   1.183 +    }
   1.184 +
   1.185 +    return false;
   1.186 +}
   1.187 +
   1.188 +
   1.189 +//// NetClient API
   1.190 +
   1.191 +const char* NetClient::GetStringValue(VirtualHmdId hmd, const char* key, const char* default_val)
   1.192 +{
   1.193 +    if (!IsConnected(true, true))
   1.194 +    {
   1.195 +        return "";
   1.196 +    }
   1.197 +
   1.198 +	// If a null value is provided,
   1.199 +	if (!default_val)
   1.200 +	{
   1.201 +		default_val = "";
   1.202 +	}
   1.203 +
   1.204 +    ProfileGetValue1_Str = default_val;
   1.205 +
   1.206 +    OVR::Net::BitStream bsOut, returnData;
   1.207 +    bsOut.Write(hmd);
   1.208 +    bsOut.Write(key);
   1.209 +    bsOut.Write(default_val);
   1.210 +    if (!GetRPC1()->CallBlocking("GetStringValue_1", &bsOut, GetSession()->GetConnectionAtIndex(0), &returnData))
   1.211 +    {
   1.212 +		return "";
   1.213 +    }
   1.214 +    if (!returnData.Read(ProfileGetValue1_Str))
   1.215 +    {
   1.216 +        OVR_ASSERT(false);
   1.217 +    }
   1.218 +    return ProfileGetValue1_Str.ToCStr();
   1.219 +}
   1.220 +bool NetClient::GetBoolValue(VirtualHmdId hmd, const char* key, bool default_val)
   1.221 +{
   1.222 +    if (!IsConnected(true, true))
   1.223 +    {
   1.224 +        return default_val;
   1.225 +    }
   1.226 +
   1.227 +    OVR::Net::BitStream bsOut, returnData;
   1.228 +    bsOut.Write(hmd);
   1.229 +    bsOut.Write(key);
   1.230 +    bsOut.Write(default_val);
   1.231 +    if (!GetRPC1()->CallBlocking("GetBoolValue_1", &bsOut, GetSession()->GetConnectionAtIndex(0), &returnData))
   1.232 +    {
   1.233 +		return default_val;
   1.234 +    }
   1.235 +    uint8_t out = 0;
   1.236 +    if (!returnData.Read(out))
   1.237 +    {
   1.238 +        OVR_ASSERT(false);
   1.239 +    }
   1.240 +    return out != 0;
   1.241 +}
   1.242 +int NetClient::GetIntValue(VirtualHmdId hmd, const char* key, int default_val)
   1.243 +{
   1.244 +    if (!IsConnected(true, true))
   1.245 +    {
   1.246 +        return default_val;
   1.247 +    }
   1.248 +
   1.249 +    OVR::Net::BitStream bsOut, returnData;
   1.250 +    bsOut.Write(hmd);
   1.251 +    bsOut.Write(key);
   1.252 +    bsOut.Write(default_val);
   1.253 +    if (!GetRPC1()->CallBlocking("GetIntValue_1", &bsOut, GetSession()->GetConnectionAtIndex(0), &returnData))
   1.254 +    {
   1.255 +		return default_val;
   1.256 +    }
   1.257 +    int32_t out = (int32_t)default_val;
   1.258 +    if (!returnData.Read(out))
   1.259 +    {
   1.260 +        OVR_ASSERT(false);
   1.261 +    }
   1.262 +    return out;
   1.263 +}
   1.264 +double NetClient::GetNumberValue(VirtualHmdId hmd, const char* key, double default_val)
   1.265 +{
   1.266 +    if (!IsConnected(true, true))
   1.267 +    {
   1.268 +        return default_val;
   1.269 +    }
   1.270 +
   1.271 +    OVR::Net::BitStream bsOut, returnData;
   1.272 +    bsOut.Write(hmd);
   1.273 +    bsOut.Write(key);
   1.274 +    bsOut.Write(default_val);
   1.275 +    if (!GetRPC1()->CallBlocking("GetNumberValue_1", &bsOut, GetSession()->GetConnectionAtIndex(0), &returnData))
   1.276 +    {
   1.277 +		return default_val;
   1.278 +    }
   1.279 +    double out = 0.;
   1.280 +    returnData.Read(out);
   1.281 +    return out;
   1.282 +}
   1.283 +int NetClient::GetNumberValues(VirtualHmdId hmd, const char* key, double* values, int num_vals)
   1.284 +{
   1.285 +    if (!IsConnected(true, true))
   1.286 +    {
   1.287 +        return 0;
   1.288 +    }
   1.289 +
   1.290 +    OVR::Net::BitStream bsOut, returnData;
   1.291 +    bsOut.Write(hmd);
   1.292 +    bsOut.Write(key);
   1.293 +
   1.294 +    int32_t w = (int32_t)num_vals;
   1.295 +    bsOut.Write(w);
   1.296 +
   1.297 +    if (!GetRPC1()->CallBlocking("GetNumberValues_1", &bsOut, GetSession()->GetConnectionAtIndex(0), &returnData))
   1.298 +    {
   1.299 +		return 0;
   1.300 +    }
   1.301 +
   1.302 +    int32_t out = 0;
   1.303 +    if (!returnData.Read(out))
   1.304 +    {
   1.305 +        OVR_ASSERT(false);
   1.306 +    }
   1.307 +    OVR_ASSERT(out >= 0 && out <= num_vals);
   1.308 +    if (out < 0)
   1.309 +    {
   1.310 +        out = 0;
   1.311 +    }
   1.312 +    else if (out > num_vals)
   1.313 +    {
   1.314 +        out = num_vals;
   1.315 +    }
   1.316 +
   1.317 +    for (int i = 0; i < out && i < num_vals; i++)
   1.318 +    {
   1.319 +        if (!returnData.Read(values[i]))
   1.320 +        {
   1.321 +            return i;
   1.322 +        }
   1.323 +    }
   1.324 +
   1.325 +    return out;
   1.326 +}
   1.327 +
   1.328 +bool NetClient::SetStringValue(VirtualHmdId hmd, const char* key, const char* val)
   1.329 +{
   1.330 +    if (!IsConnected(true, true))
   1.331 +    {
   1.332 +        return false;
   1.333 +    }
   1.334 +
   1.335 +    OVR::Net::BitStream bsOut;
   1.336 +    bsOut.Write(hmd);
   1.337 +    bsOut.Write(key);
   1.338 +
   1.339 +    bsOut.Write(val);
   1.340 +
   1.341 +    if (!GetRPC1()->Signal("SetStringValue_1", &bsOut, GetSession()->GetConnectionAtIndex(0)))
   1.342 +    {
   1.343 +        return false;
   1.344 +    }
   1.345 +
   1.346 +    return true;
   1.347 +}
   1.348 +
   1.349 +bool NetClient::SetBoolValue(VirtualHmdId hmd, const char* key, bool val)
   1.350 +{
   1.351 +    if (!IsConnected(true, true))
   1.352 +    {
   1.353 +        return false;
   1.354 +    }
   1.355 +
   1.356 +    OVR::Net::BitStream bsOut;
   1.357 +    bsOut.Write(hmd);
   1.358 +    bsOut.Write(key);
   1.359 +
   1.360 +    uint8_t b = val ? 1 : 0;
   1.361 +    bsOut.Write(b);
   1.362 +
   1.363 +    if (!GetRPC1()->Signal("SetBoolValue_1", &bsOut, GetSession()->GetConnectionAtIndex(0)))
   1.364 +    {
   1.365 +        return false;
   1.366 +    }
   1.367 +
   1.368 +    return true;
   1.369 +}
   1.370 +
   1.371 +bool NetClient::SetIntValue(VirtualHmdId hmd, const char* key, int val)
   1.372 +{
   1.373 +    if (!IsConnected(true, true))
   1.374 +    {
   1.375 +        return false;
   1.376 +    }
   1.377 +
   1.378 +    OVR::Net::BitStream bsOut;
   1.379 +    bsOut.Write(hmd);
   1.380 +    bsOut.Write(key);
   1.381 +
   1.382 +    int32_t w = (int32_t)val;
   1.383 +    bsOut.Write(w);
   1.384 +
   1.385 +    if (!GetRPC1()->Signal("SetIntValue_1", &bsOut, GetSession()->GetConnectionAtIndex(0)))
   1.386 +    {
   1.387 +        return false;
   1.388 +    }
   1.389 +
   1.390 +    return true;
   1.391 +}
   1.392 +
   1.393 +bool NetClient::SetNumberValue(VirtualHmdId hmd, const char* key, double val)
   1.394 +{
   1.395 +    if (!IsConnected(true, true))
   1.396 +    {
   1.397 +        return false;
   1.398 +    }
   1.399 +
   1.400 +    OVR::Net::BitStream bsOut;
   1.401 +    bsOut.Write(hmd);
   1.402 +    bsOut.Write(key);
   1.403 +
   1.404 +    bsOut.Write(val);
   1.405 +
   1.406 +    if (!GetRPC1()->Signal("SetNumberValue_1", &bsOut, GetSession()->GetConnectionAtIndex(0)))
   1.407 +    {
   1.408 +        return false;
   1.409 +    }
   1.410 +
   1.411 +    return true;
   1.412 +}
   1.413 +
   1.414 +bool NetClient::SetNumberValues(VirtualHmdId hmd, const char* key, const double* vals, int num_vals)
   1.415 +{
   1.416 +    if (!IsConnected(true, true))
   1.417 +    {
   1.418 +        return false;
   1.419 +    }
   1.420 +
   1.421 +    OVR::Net::BitStream bsOut;
   1.422 +    bsOut.Write(hmd);
   1.423 +    bsOut.Write(key);
   1.424 +
   1.425 +    int32_t w_count = (int32_t)num_vals;
   1.426 +    bsOut.Write(w_count);
   1.427 +
   1.428 +    for (int i = 0; i < num_vals; i++)
   1.429 +    {
   1.430 +        bsOut.Write(vals[i]);
   1.431 +    }
   1.432 +
   1.433 +    if (!GetRPC1()->Signal("SetNumberValues_1", &bsOut, GetSession()->GetConnectionAtIndex(0)))
   1.434 +    {
   1.435 +        return false;
   1.436 +    }
   1.437 +
   1.438 +    return true;
   1.439 +}
   1.440 +
   1.441 +int NetClient::Hmd_Detect()
   1.442 +{
   1.443 +    if (!IsConnected(true, false))
   1.444 +    {
   1.445 +        return 0;
   1.446 +    }
   1.447 +
   1.448 +    // If using edge-triggered HMD counting,
   1.449 +    if (EdgeTriggeredHMDCount)
   1.450 +    {
   1.451 +        // Return the last update from the server
   1.452 +        return HMDCount;
   1.453 +    }
   1.454 +
   1.455 +    // Otherwise: We need to ask the first time
   1.456 +
   1.457 +	OVR::Net::BitStream bsOut, returnData;
   1.458 +
   1.459 +	if (!GetRPC1()->CallBlocking("Hmd_Detect_1", &bsOut, GetSession()->GetConnectionAtIndex(0), &returnData))
   1.460 +	{
   1.461 +		return 0;
   1.462 +	}
   1.463 +
   1.464 +    int32_t out = 0;
   1.465 +    if (!returnData.Read(out))
   1.466 +    {
   1.467 +        OVR_ASSERT(false);
   1.468 +    }
   1.469 +    HMDCount = out;
   1.470 +    EdgeTriggeredHMDCount = true;
   1.471 +	return out;
   1.472 +}
   1.473 +
   1.474 +bool NetClient::Hmd_Create(int index, HMDNetworkInfo* netInfo)
   1.475 +{
   1.476 +    if (!IsConnected(true, true))
   1.477 +    {
   1.478 +        return false;
   1.479 +    }
   1.480 +
   1.481 +	OVR::Net::BitStream bsOut, returnData;
   1.482 +
   1.483 +    int32_t w = (int32_t)index;
   1.484 +	bsOut.Write(w);
   1.485 +
   1.486 +    // Need the Pid for driver mode
   1.487 +    pid_t pid = GetCurrentProcessId();
   1.488 +    bsOut.Write(pid);
   1.489 +
   1.490 +	if (!GetRPC1()->CallBlocking("Hmd_Create_1", &bsOut, GetSession()->GetConnectionAtIndex(0), &returnData))
   1.491 +	{
   1.492 +		return false;
   1.493 +	}
   1.494 +
   1.495 +	return netInfo->Deserialize(&returnData);
   1.496 +}
   1.497 +
   1.498 +bool NetClient::GetDriverMode(bool& driverInstalled, bool& compatMode, bool& hideDK1Mode)
   1.499 +{
   1.500 +    if (!IsConnected(true, true))
   1.501 +    {
   1.502 +        return false;
   1.503 +    }
   1.504 +
   1.505 +    OVR::Net::BitStream bsOut, returnData;
   1.506 +
   1.507 +    bsOut.Write(InvalidVirtualHmdId);
   1.508 +
   1.509 +    if (!GetRPC1()->CallBlocking("GetDriverMode_1", &bsOut, GetSession()->GetConnectionAtIndex(0), &returnData))
   1.510 +    {
   1.511 +        return false;
   1.512 +    }
   1.513 +
   1.514 +    int32_t w_driverInstalled = 0;
   1.515 +    int32_t w_compatMode = 0;
   1.516 +    int32_t w_hideDK1Mode = 0;
   1.517 +    returnData.Read(w_driverInstalled);
   1.518 +    returnData.Read(w_compatMode);
   1.519 +    if (!returnData.Read(w_hideDK1Mode))
   1.520 +    {
   1.521 +        return false;
   1.522 +    }
   1.523 +
   1.524 +    driverInstalled = w_driverInstalled != 0;
   1.525 +    compatMode = w_compatMode != 0;
   1.526 +    hideDK1Mode = w_hideDK1Mode != 0;
   1.527 +    return true;
   1.528 +}
   1.529 +
   1.530 +bool NetClient::SetDriverMode(bool compatMode, bool hideDK1Mode)
   1.531 +{
   1.532 +    if (!IsConnected(true, true))
   1.533 +    {
   1.534 +        return false;
   1.535 +    }
   1.536 +
   1.537 +    OVR::Net::BitStream bsOut, returnData;
   1.538 +
   1.539 +    bsOut.Write(InvalidVirtualHmdId);
   1.540 +
   1.541 +    int32_t w_compatMode, w_hideDK1Mode;
   1.542 +    w_compatMode = compatMode ? 1 : 0;
   1.543 +    w_hideDK1Mode = hideDK1Mode ? 1 : 0;
   1.544 +    bsOut.Write(w_compatMode);
   1.545 +    bsOut.Write(w_hideDK1Mode);
   1.546 +
   1.547 +    if (!GetRPC1()->CallBlocking("SetDriverMode_1", &bsOut, GetSession()->GetConnectionAtIndex(0), &returnData))
   1.548 +    {
   1.549 +        return false;
   1.550 +    }
   1.551 +
   1.552 +    int32_t out = 0;
   1.553 +    if (!returnData.Read(out))
   1.554 +    {
   1.555 +        OVR_ASSERT(false);
   1.556 +        return false;
   1.557 +    }
   1.558 +
   1.559 +    return out != 0;
   1.560 +}
   1.561 +
   1.562 +bool NetClient::Hmd_AttachToWindow(VirtualHmdId hmd, void* hWindow)
   1.563 +{
   1.564 +    if (!IsConnected(false, false))
   1.565 +    {
   1.566 +        return false;
   1.567 +    }
   1.568 +
   1.569 +    OVR::Net::BitStream bsOut;
   1.570 +    bsOut.Write(hmd);
   1.571 +
   1.572 +    #ifdef OVR_OS_LINUX
   1.573 +    if (hWindow == NULL)
   1.574 +    {
   1.575 +        return false;
   1.576 +    }
   1.577 +    unsigned long hWinWord = *(unsigned long *)hWindow;
   1.578 +    #else
   1.579 +    UInt64 hWinWord = (UPInt)hWindow;
   1.580 +    #endif
   1.581 +    bsOut.Write(hWinWord);
   1.582 +
   1.583 +    if (!GetRPC1()->CallBlocking("Hmd_AttachToWindow_1", &bsOut, GetSession()->GetConnectionAtIndex(0)))
   1.584 +    {
   1.585 +        return false;
   1.586 +    }
   1.587 +
   1.588 +    return true;
   1.589 +}
   1.590 +
   1.591 +void NetClient::Hmd_Release(VirtualHmdId hmd)
   1.592 +{
   1.593 +    if (!IsConnected(false, false))
   1.594 +    {
   1.595 +        return;
   1.596 +    }
   1.597 +
   1.598 +	OVR::Net::BitStream bsOut;
   1.599 +	bsOut.Write(hmd);
   1.600 +	bool result = GetRPC1()->CallBlocking("Hmd_Release_1", &bsOut, GetSession()->GetConnectionAtIndex(0));
   1.601 +    OVR_ASSERT_AND_UNUSED(result, result);
   1.602 +}
   1.603 +
   1.604 +void NetClient::SetLastError(String str)
   1.605 +{
   1.606 +    Hmd_GetLastError_Str = str;
   1.607 +}
   1.608 +
   1.609 +// Last string is cached locally.
   1.610 +const char* NetClient::Hmd_GetLastError(VirtualHmdId hmd)
   1.611 +{
   1.612 +    if (hmd == InvalidVirtualHmdId || !IsConnected(false, false))
   1.613 +    {
   1.614 +        return Hmd_GetLastError_Str.ToCStr();
   1.615 +    }
   1.616 +
   1.617 +    OVR::Net::BitStream bsOut, returnData;
   1.618 +	bsOut.Write(hmd);
   1.619 +    if (!GetRPC1()->CallBlocking("Hmd_GetLastError_1", &bsOut, GetSession()->GetConnectionAtIndex(0), &returnData))
   1.620 +	{
   1.621 +		return Hmd_GetLastError_Str.ToCStr();
   1.622 +	}
   1.623 +    if (!returnData.Read(Hmd_GetLastError_Str))
   1.624 +    {
   1.625 +        OVR_ASSERT(false);
   1.626 +    }
   1.627 +	return Hmd_GetLastError_Str.ToCStr();
   1.628 +}
   1.629 +
   1.630 +
   1.631 +// Fills in description about HMD; this is the same as filled in by ovrHmd_Create.
   1.632 +// The actual descriptor is a par
   1.633 +bool NetClient::Hmd_GetHmdInfo(VirtualHmdId hmd, HMDInfo* hmdInfo)
   1.634 +{
   1.635 +    if (!IsConnected(false, false))
   1.636 +    {
   1.637 +        return false;
   1.638 +    }
   1.639 +
   1.640 +	OVR::Net::BitStream bsOut, returnData;
   1.641 +	bsOut.Write(hmd);
   1.642 +	if (!GetRPC1()->CallBlocking("Hmd_GetHmdInfo_1", &bsOut, GetSession()->GetConnectionAtIndex(0), &returnData))
   1.643 +	{
   1.644 +		return false;
   1.645 +	}
   1.646 +
   1.647 +    return NetSessionCommon::DeserializeHMDInfo(&returnData, hmdInfo);
   1.648 +}
   1.649 +
   1.650 +
   1.651 +//-------------------------------------------------------------------------------------
   1.652 +unsigned int NetClient::Hmd_GetEnabledCaps(VirtualHmdId hmd)
   1.653 +{
   1.654 +    if (!IsConnected(false, false))
   1.655 +    {
   1.656 +        return 0;
   1.657 +    }
   1.658 +
   1.659 +	OVR::Net::BitStream bsOut, returnData;
   1.660 +	bsOut.Write(hmd);
   1.661 +	if (!GetRPC1()->CallBlocking("Hmd_GetEnabledCaps_1", &bsOut, GetSession()->GetConnectionAtIndex(0), &returnData))
   1.662 +	{
   1.663 +		return 0;
   1.664 +	}
   1.665 +
   1.666 +    uint32_t c = 0;
   1.667 +    if (!returnData.Read(c))
   1.668 +    {
   1.669 +        OVR_ASSERT(false);
   1.670 +    }
   1.671 +	return c;
   1.672 +}
   1.673 +
   1.674 +// Returns new caps after modification
   1.675 +unsigned int NetClient::Hmd_SetEnabledCaps(VirtualHmdId hmd, unsigned int hmdCaps)
   1.676 +{
   1.677 +    if (!IsConnected(false, false))
   1.678 +    {
   1.679 +        return 0;
   1.680 +    }
   1.681 +
   1.682 +	OVR::Net::BitStream bsOut, returnData;
   1.683 +	bsOut.Write(hmd);
   1.684 +
   1.685 +    uint32_t c = (uint32_t)hmdCaps;
   1.686 +	bsOut.Write(c);
   1.687 +
   1.688 +	if (!GetRPC1()->CallBlocking("Hmd_SetEnabledCaps_1", &bsOut, GetSession()->GetConnectionAtIndex(0), &returnData))
   1.689 +	{
   1.690 +		return 0;
   1.691 +	}
   1.692 +
   1.693 +    c = 0;
   1.694 +    if (!returnData.Read(c))
   1.695 +    {
   1.696 +        OVR_ASSERT(false);
   1.697 +    }
   1.698 +    return c;
   1.699 +}
   1.700 +
   1.701 +
   1.702 +//-------------------------------------------------------------------------------------
   1.703 +// *** Tracking Setup
   1.704 +
   1.705 +bool NetClient::Hmd_ConfigureTracking(VirtualHmdId hmd, unsigned supportedCaps, unsigned requiredCaps)
   1.706 +{
   1.707 +    if (!IsConnected(false, false))
   1.708 +    {
   1.709 +        return false;
   1.710 +    }
   1.711 +
   1.712 +	OVR::Net::BitStream bsOut, returnData;
   1.713 +	bsOut.Write(hmd);
   1.714 +
   1.715 +    uint32_t w_sc = supportedCaps;
   1.716 +    bsOut.Write(w_sc);
   1.717 +    uint32_t w_rc = requiredCaps;
   1.718 +    bsOut.Write(w_rc);
   1.719 +
   1.720 +	if (!GetRPC1()->CallBlocking("Hmd_ConfigureTracking_1", &bsOut, GetSession()->GetConnectionAtIndex(0), &returnData))
   1.721 +	{
   1.722 +		return false;
   1.723 +	}
   1.724 +
   1.725 +    uint8_t b;
   1.726 +    if (!returnData.Read(b))
   1.727 +    {
   1.728 +        OVR_ASSERT(false);
   1.729 +    }
   1.730 +
   1.731 +	return b != 0;
   1.732 +}
   1.733 +
   1.734 +
   1.735 +void NetClient::Hmd_ResetTracking(VirtualHmdId hmd)
   1.736 +{
   1.737 +    if (!IsConnected(false, false))
   1.738 +    {
   1.739 +        return;
   1.740 +    }
   1.741 +
   1.742 +	OVR::Net::BitStream bsOut;
   1.743 +	bsOut.Write(hmd);
   1.744 +	if (!GetRPC1()->CallBlocking("Hmd_ResetTracking_1", &bsOut, GetSession()->GetConnectionAtIndex(0)))
   1.745 +	{
   1.746 +		return;
   1.747 +	}
   1.748 +}
   1.749 +
   1.750 +bool NetClient::LatencyUtil_ProcessInputs(double startTestSeconds, unsigned char rgbColorOut[3])
   1.751 +{
   1.752 +    if (!IsConnected(false, false))
   1.753 +    {
   1.754 +        return false;
   1.755 +    }
   1.756 +
   1.757 +    if (!LatencyTesterAvailable)
   1.758 +    {
   1.759 +        return false;
   1.760 +    }
   1.761 +
   1.762 +    OVR::Net::BitStream bsOut, returnData;
   1.763 +    bsOut.Write(startTestSeconds);
   1.764 +    if (!GetRPC1()->CallBlocking("LatencyUtil_ProcessInputs_1", &bsOut, GetSession()->GetConnectionAtIndex(0), &returnData))
   1.765 +    {
   1.766 +        return false;
   1.767 +    }
   1.768 +
   1.769 +    uint8_t u;
   1.770 +    returnData.Read(u);
   1.771 +    rgbColorOut[0] = u;
   1.772 +    returnData.Read(u);
   1.773 +    rgbColorOut[1] = u;
   1.774 +    if (!returnData.Read(u))
   1.775 +    {
   1.776 +        return false;
   1.777 +    }
   1.778 +    rgbColorOut[2] = u;
   1.779 +
   1.780 +    return true;
   1.781 +}
   1.782 +
   1.783 +const char* NetClient::LatencyUtil_GetResultsString()
   1.784 +{
   1.785 +    if (!IsConnected(false, false))
   1.786 +    {
   1.787 +        return NULL;
   1.788 +    }
   1.789 +
   1.790 +    OVR::Net::BitStream bsOut, returnData;
   1.791 +    if (!GetRPC1()->CallBlocking("LatencyUtil_GetResultsString_1", &bsOut, GetSession()->GetConnectionAtIndex(0), &returnData))
   1.792 +    {
   1.793 +        return NULL;
   1.794 +    }
   1.795 +
   1.796 +    if (!returnData.Read(LatencyUtil_GetResultsString_Str))
   1.797 +    {
   1.798 +        OVR_ASSERT(false);
   1.799 +    }
   1.800 +
   1.801 +    return LatencyUtil_GetResultsString_Str.ToCStr();
   1.802 +}
   1.803 +
   1.804 +bool NetClient::ShutdownServer()
   1.805 +{
   1.806 +    if (!IsConnected(false, false))
   1.807 +    {
   1.808 +        return false;
   1.809 +    }
   1.810 +
   1.811 +    OVR::Net::BitStream bsOut;
   1.812 +    GetRPC1()->BroadcastSignal("Shutdown_1", &bsOut);
   1.813 +
   1.814 +    return true;
   1.815 +}
   1.816 +
   1.817 +
   1.818 +//// Push Notifications:
   1.819 +
   1.820 +void NetClient::registerRPC()
   1.821 +{
   1.822 +#define RPC_REGISTER_SLOT(observerScope, functionName) \
   1.823 +    observerScope.SetHandler(OVR::Net::Plugins::RPCSlot::FromMember<NetClient, &NetClient::functionName>(this)); pRPC->RegisterSlot(OVR_STRINGIZE(functionName), observerScope);
   1.824 +
   1.825 +    // Register RPC functions:
   1.826 +    RPC_REGISTER_SLOT(InitialServerStateScope, InitialServerState_1);
   1.827 +    RPC_REGISTER_SLOT(LatencyTesterAvailableScope, LatencyTesterAvailable_1);
   1.828 +    RPC_REGISTER_SLOT(DefaultLogOutputScope, DefaultLogOutput_1);
   1.829 +    RPC_REGISTER_SLOT(HMDCountUpdateScope, HMDCountUpdate_1);
   1.830 +}
   1.831 +
   1.832 +void NetClient::InitialServerState_1(BitStream* userData, ReceivePayload* pPayload)
   1.833 +{
   1.834 +    LatencyTesterAvailable_1(userData, pPayload);
   1.835 +}
   1.836 +
   1.837 +void NetClient::LatencyTesterAvailable_1(BitStream* userData, ReceivePayload* pPayload)
   1.838 +{
   1.839 +    OVR_UNUSED(pPayload);
   1.840 +
   1.841 +    uint8_t b = 0;
   1.842 +    if (!userData->Read(b))
   1.843 +    {
   1.844 +        OVR_ASSERT(false);
   1.845 +        return;
   1.846 +    }
   1.847 +
   1.848 +    LatencyTesterAvailable = (b != 0);
   1.849 +}
   1.850 +
   1.851 +void NetClient::DefaultLogOutput_1(BitStream* userData, ReceivePayload* pPayload)
   1.852 +{
   1.853 +    OVR_UNUSED(pPayload);
   1.854 +
   1.855 +    String formattedText;
   1.856 +    LogMessageType messageType = Log_Text; // Will normally be overwritten below.
   1.857 +    userData->Read(messageType);
   1.858 +    if (userData->Read(formattedText))
   1.859 +    {
   1.860 +        if (OVR::Log::GetGlobalLog())
   1.861 +        {
   1.862 +            OVR::String logStr = "[From Service] ";
   1.863 +            logStr.AppendString(formattedText);
   1.864 +            OVR::Log::GetGlobalLog()->LogMessage(messageType, "%s", logStr.ToCStr());
   1.865 +        }
   1.866 +    }
   1.867 +}
   1.868 +
   1.869 +void NetClient::HMDCountUpdate_1(BitStream* userData, ReceivePayload* pPayload)
   1.870 +{
   1.871 +    OVR_UNUSED(pPayload);
   1.872 +
   1.873 +    int32_t hmdCount = 0;
   1.874 +    if (!userData->Read(hmdCount))
   1.875 +    {
   1.876 +        OVR_ASSERT(false);
   1.877 +        return;
   1.878 +    }
   1.879 +
   1.880 +    HMDCount = hmdCount;
   1.881 +    EdgeTriggeredHMDCount = true;
   1.882 +}
   1.883 +
   1.884 +
   1.885 +}} // namespace OVR::Service