/// <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); } }
/// <summary> /// Callback that informs us when a Link is connected. /// </summary> /// <param name="connectingLink"></param> /// <returns></returns> virtual protected VscResult LinkConnectCallback(VscLink connectingLink) { VscResult retRes = VscResult.OK; Log($"VS Connect Link connecting: {connectingLink.ptr.ToInt64():x}.\n"); VsConnectNode node = VsConnectNode.GetInstanceFromLink(connectingLink); retRes = ShouldProcessCallback(node, connectingLink); return(retRes); }
static public VsConnectNode GetInstanceFromLink(VscLink link) { VsConnectNode retNode = null; var vscNode = vsc_Api_V3_t.Link_GetNode(link); if (vscNode != IntPtr.Zero) { var nodeAppData = vsc_Api_V3_t.Node_GetAppData(vscNode); retNode = VsUtility.ToObject(nodeAppData) as VsConnectNode; } return(retNode); }
virtual protected VscResult ShouldProcessCallback(VsConnectNode node, VscLink link) { VscResult retRes; if (node == null || link == IntPtr.Zero) { retRes = VscResult.ERROR_INVALID_PARAM; } else if (node.ShuttingDown) { retRes = VscResult.ERROR_UNAVAILABLE; } else { retRes = VscResult.OK; } return(retRes); }
public VsConnectNode Listen_IP(string ListenAddress , int port = 0 , int maxConnections = 10 , bool requireTimeSync = false) { ListenAddress.TrimAndUnquoteInPlace(); VscResult createRes = VscResult.UNDEFINED; var newNodePtr = vsc_Api_V3_t.Node_Create_UDPIP(ListenAddress , (ushort)port , maxConnections , requireTimeSync ? VscTimeSyncMode.ABSOLUTE : VscTimeSyncMode.NONE , linkConnectCallback , linkdisconnectCallback , contractRequestCallback , contractCanceledCallback , sendUpdateCallback , receiveUpdateCallback , null , ref createRes ); if (newNodePtr != IntPtr.Zero) { var newNode = new VsConnectNode(newNodePtr); vsc_Api_V3_t.Node_SetAppData(newNode.VscNode, ToPtr(newNode)); Log(("Successfully created VS Connect Server Node.")); mVscNodes.Add(newNode); return(newNode); } else { LogError($"Failed to create VS Connect Server Node: {ListenAddress}:{port}"); return(null); } }
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); }