/// <summary> /// Called when new data has arrived from a peer via the specified Link. /// </summary> /// <param name="link"></param> /// <param name="contract"></param> /// <param name="data"></param> /// <returns></returns> unsafe virtual protected VscResult ReceiveCallback(VscLink link, VscContract contract, VscUpdateData data) { { VscResult retRes; var node = VsConnectNode.GetInstanceFromLink(link); retRes = ShouldProcessCallback(node, link); if (!retRes.IsError()) { VscSchema schema = vsc_Api_V3_t.Contract_GetSchema(contract); var numSchemaFields = vsc_Api_V3_t.Schema_GetNumFields(schema); var localRole = vsc_Api_V3_t.Contract_GetLocalRole(contract); Check(localRole == VscRole.SENDER); Check(localRole == VscRole.SENDER); Check(schema != IntPtr.Zero); Check((IntPtr)vsc_Api_V3_t.UpdateData_GetSchema(data) == (IntPtr)schema); for (int fieldIndex = 0; fieldIndex < numSchemaFields; ++fieldIndex) { var field = vsc_Api_V3_t.Schema_GetField(schema, fieldIndex); var arraySize = vsc_Api_V3_t.Field_GetNumElements(field); Check(arraySize > 0); var vsVar = ToObject(vsc_Api_V3_t.Field_GetAppData(field)) as VSVarBase; if (vsVar != null && vsVar.Direction == VsVarDirection.Input) { double *fieldValue = (double *)vsc_Api_V3_t.UpdateData_GetFieldValue(data, fieldIndex); Check(fieldValue != null); if (vsVar.VariableType == typeof(double)) { ((VsConnectVar <double>)vsVar).Value = *fieldValue; } if (vsVar.VariableType == typeof(double[])) { double[] values = new double[arraySize]; for (int index = 0; index < arraySize; index++) { values[index] = fieldValue[index]; } ((VsConnectVar <double[]>)vsVar).Value = values; } } } retRes = VscResult.OK; } return(retRes); } }
void ContractCanceledCallback(VscLink link, VscContract contract) { var tsMode = vsc_Api_V3_t.Contract_GetTsMode(contract); var localRole = vsc_Api_V3_t.Contract_GetLocalRole(contract); if (tsMode != VscTimeSyncMode.NONE && localRole == VscRole.RECEIVER) { // Our Incoming TS Contract is being canceled. Ensure // time dilation is set back to its original value: //auto unode = UVsConnectNode::GetInstanceFromLink(link); //auto uVsConnect = Cast<UVsConnect>(unode->GetOuter()); //auto worldSettings = uVsConnect->mWorld->GetWorldSettings(); //if ( worldSettings ) uVsConnect->RestoreOriginalTimeDilation(worldSettings); } }
/// <summary> /// Clean up references/etc. when a Contract is destroyed. /// </summary> /// <param name="contract"></param> void SPreDestroyContract_(VscContract contract) { VsConnectServer vsConnect = ToObject(vsc_Api_V3_t.Contract_GetAppData(contract)) as VsConnectServer; // Loop through all Fields and remove the corresponding UObject* from vsConnect->mVsVars. var schema = vsc_Api_V3_t.Contract_GetSchema(contract); var numFields = vsc_Api_V3_t.Schema_GetNumFields(schema); for (int i = 0; i < numFields; ++i) { var field = vsc_Api_V3_t.Schema_GetField(schema, i); var vsvar = ToObject(vsc_Api_V3_t.Field_GetAppData(field)) as VSVarBase; if (vsvar != null) { vsc_Api_V3_t.Field_SetAppData(field, IntPtr.Zero); } } }
/// <summary> /// Callback used by VS Connect to report log messages to this application. /// </summary> /// <param name="logLevel"></param> /// <param name="vscNode"></param> /// <param name="vscLink"></param> /// <param name="vscContract"></param> /// <param name="format"></param> /// <param name="argptr"></param> /// <returns></returns> protected unsafe int _VscLogCallback(VscLogLevel logLevel , VscNode vscNode , VscLink vscLink , VscContract vscContract , string format , IntPtr argptr) { string logMessage = $"VS Connect "; logMessage += logLevel == VscLogLevel.ERROR ? "ERROR" : (logLevel == VscLogLevel.WARNING ? "WARNING" : ""); logMessage += string.Format("N:{0:x} L:{1:x} C:{2:x}", vscNode.ptr.ToInt64(), vscLink.ptr.ToInt64(), vscContract.ptr.ToInt64()); int BUFF_SIZE = 1024; byte[] tempBuff = new byte[BUFF_SIZE]; int charsOutput2 = vsprintf(tempBuff, format, argptr); //check(charsOutput2 > 0 ); logMessage += System.Text.Encoding.UTF8.GetString(tempBuff, 0, charsOutput2); if (VscLogLevel.ERROR == logLevel) { LogError(logMessage); } else if (VscLogLevel.WARNING == logLevel) { LogWarning(logMessage); } else { Log(logMessage); } return(logMessage.Length); }
virtual protected VscResult ProcessContractRequest(VscLink link, VscContract contract) { VscResult retRes; var node = VsConnectNode.GetInstanceFromLink(link); retRes = ShouldProcessCallback(node, link); if (retRes.IsError()) { LogWarning("VS Connect - Ignoring Contract Request."); } else { var tsMode = vsc_Api_V3_t.Contract_GetTsMode(contract); if (vsc_Api_V3_t.Contract_GetLocalRole(contract) == VscRole.RECEIVER && VS_TS_REQUIRE_FIXED_FRAME_RATE == 1 && tsMode != VscTimeSyncMode.NONE //&& !GEngine->bUseFixedFrameRate ) { LogError("VS Connect - Rejecting Incoming Contract with Time Sync (TS) enabled. Unity must be operating in fixed framerate mode to use TS."); retRes = VscResult.ERROR_UNSUPPORTED; } else { VscSchema schema = vsc_Api_V3_t.Contract_GetSchema(contract); var numSchemaFields = vsc_Api_V3_t.Schema_GetNumFields(schema); var localRole = vsc_Api_V3_t.Contract_GetLocalRole(contract); Check(schema != IntPtr.Zero); if (localRole == VscRole.SENDER) { Log($"VS Connect outgoing Contract request received: {contract.ptr.ToInt64():x}.\n"); } else if (localRole == VscRole.RECEIVER) { Log($"VS Connect outgoing Contract request received: {contract.ptr.ToInt64():x}.\n"); } else { LogWarning($"VS Connect outgoing Contract request received: {contract.ptr.ToInt64():x}.\n"); } for (int fieldIndex = 0; fieldIndex < numSchemaFields; ++fieldIndex) { var field = vsc_Api_V3_t.Schema_GetField(schema, fieldIndex); string objectName = ""; objectName = (vsc_Api_V3_t.Field_GetObjectName(field)).ToString(); var solver = GetObject(objectName); if (solver == null) { LogWarning($"Unable to locate VS Connect Object {objectName} for Contract request {contract.ptr.ToInt64():x}."); } else { string fieldName = vsc_Api_V3_t.Field_GetPropertyName(field); string fieldParams = vsc_Api_V3_t.Field_GetParams(field); var arraySize = vsc_Api_V3_t.Field_GetNumElements(field); var vscDataType = vsc_Api_V3_t.Field_GetDataType(field); var dataSize = vsc_Api_V3_t.Field_GetElementSizeInBits(field); fieldName.TrimAndUnquoteInPlace(); fieldParams.TrimAndUnquoteInPlace(); VSVarBase vsVar = null; if (localRole == VscRole.SENDER) { if (vscDataType == VscDataType.FLOAT && dataSize == 64 && arraySize == 1) { vsVar = new VsConnectVar <double>(solver, fieldName, solver.GetDoubleFuncs[fieldName]); } if (vscDataType == VscDataType.FLOAT && dataSize == 64 && arraySize > 1) { vsVar = new VsConnectVar <double[]>(solver, fieldName, solver.GetDoubleArrayFuncs[fieldName]); } if (vsVar == null) { LogWarning($"VS Connect - Contract request %llx, Object {objectName}: No output Property {fieldName} with params {fieldParams}) for ."); } } else if (localRole == VscRole.RECEIVER) { if (vscDataType == VscDataType.FLOAT && dataSize == 64 && arraySize == 1) { vsVar = new VsConnectVar <double>(solver, fieldName, solver.SetDoubleActions[fieldName]); } if (vscDataType == VscDataType.FLOAT && dataSize == 64 && arraySize > 1) { vsVar = new VsConnectVar <double[]>(solver, fieldName, solver.SetDoubleArrayActions[fieldName]); } if (vsVar == null) { LogWarning($"VS Connect - Contract request {contract.ptr.ToInt64():x}, Object {objectName}: No input Property {fieldName} with params {fieldParams}) for ."); } } else { vsVar = null; LogError(("VS Connect - Unhandled VS Connect Role.\n")); } Check(vsc_Api_V3_t.Field_GetAppData(field) == IntPtr.Zero); if (vsVar != null) { // Store the handle in the schema: vsc_Api_V3_t.Field_SetAppData(field, ToPtr(vsVar)); } } } Check(vsc_Api_V3_t.Contract_GetAppData(contract) == IntPtr.Zero); vsc_Api_V3_t.Contract_SetAppData(contract, ToPtr(Singleton)); vsc_Api_V3_t.Contract_SetPreDestroyCallback(contract, SPreDestroyContract_); retRes = VscResult.OK; } } return(retRes); }
/// <summary> /// Called by VS Connect when it needs new data to send. /// </summary> /// <param name="link"></param> /// <param name="contract"></param> /// <param name="out_data"></param> /// <returns></returns> unsafe virtual protected VscResult SendCallback(VscLink link, VscContract contract, VscUpdateData out_data) { VscResult retRes; var node = VsConnectNode.GetInstanceFromLink(link); retRes = ShouldProcessCallback(node, link); if (!retRes.IsError()) { VscSchema schema = vsc_Api_V3_t.Contract_GetSchema(contract); var numSchemaFields = vsc_Api_V3_t.Schema_GetNumFields(schema); var localRole = vsc_Api_V3_t.Contract_GetLocalRole(contract); Check(localRole == VscRole.SENDER); Check(schema != IntPtr.Zero); Check((IntPtr)vsc_Api_V3_t.UpdateData_GetSchema(out_data) == (IntPtr)schema); retRes = VscResult.OK; for (int fieldIndex = 0; VscResult.OK == retRes && fieldIndex < numSchemaFields; ++fieldIndex) { var field = vsc_Api_V3_t.Schema_GetField(schema, fieldIndex); double *fieldValue = (double *)vsc_Api_V3_t.UpdateData_GetFieldValue(out_data, fieldIndex); var vsVar = ToObject(vsc_Api_V3_t.Field_GetAppData(field)) as VSVarBase; Check(fieldValue != null); if (vsVar == null) { vsc_Api_V3_t.InvalidateDouble(fieldValue); } else { var arraySize = vsc_Api_V3_t.Field_GetNumElements(field); var vscDataType = vsc_Api_V3_t.Field_GetDataType(field); var dataSize = vsc_Api_V3_t.Field_GetElementSizeInBits(field); Check(vscDataType == VscDataType.FLOAT); Check(64 == dataSize); Check(arraySize > 0); if (vsVar.VariableType == typeof(double)) { *fieldValue = ((VsConnectVar <double>)vsVar).Value; } if (vsVar.VariableType == typeof(double[])) { int index = 0; foreach (var value in ((VsConnectVar <double[]>)vsVar).Value) { fieldValue[index] = value; index++; } } } } } return(retRes); }