0

我有两个表,它们都包含一个环境表

table Environment {
windDirection:int = 0;
windMagnitude:int = 0;
windVariation:int = 0;
rain:ushort = 0;
snow:ushort = 0;
dust:ushort = 0;
fog:ushort = 0;
}

table REQUEST_SCENARIO_CHANGE_ENVIRONMENT_M {
   scenarioId:string;
   environment:Environment;
}

enum SCENARIO_CHANGE_ENVIRONMENT_RESULT_E : byte {
  SUCCESS,
  CLIENT_DOESNT_CONTROL_SCENARIO,
  SCENARIO_NOT_IN_FREE_MODE, // scenario was not setup-free'd
  SCENARIO_NOT_FOUND,
  UNKNOWN_FAILURE
}

table RESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M {
   result:SCENARIO_CHANGE_ENVIRONMENT_RESULT_E;
   environment:Environment;
}

顾名思义,第一个表是由许多控制客户端中的一个发出的请求并发送到服务器。服务器以错误代码(结果)响应。如果结果是成功,则将 RESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M 发送给所有客户端。这样,如果环境成功地改变了一个,每个人都会注意到这一事实。

我的问题是,由于我在创建 RESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M 的代码中已经有一个正确填充的环境对象,是否可以采取

     var message = Message.GetRootAsMessage(Utilities.ByteBuffer(data.Data));
     var request = REQUEST_SCENARIO_CHANGE_ENVIRONMENT_M.GetRootAsREQUEST_SCENARIO_CHANGE_ENVIRONMENT_M(Utilities.ByteBuffer(message.GetPayloadBytes()));
     SCENARIO_CHANGE_ENVIRONMENT_RESULT_E result = SCENARIO_CHANGE_ENVIRONMENT_RESULT_E.UNKNOWN_FAILURE;
     Scenario scenario = GetScenarioFromId(request.ScenarioId);
     if (scenario == null)
     {
        result = SCENARIO_CHANGE_ENVIRONMENT_RESULT_E.SCENARIO_NOT_FOUND;
     }
     else
     {
        if (!scenario.IsController(message.SessionToken))
        {
           result = SCENARIO_CHANGE_ENVIRONMENT_RESULT_E.CLIENT_DOESNT_CONTROL_SCENARIO;
        }
        else if (scenario.EndScenario() == STS_ERRORS.ERR_NO_ERROR)
        {
           // TODO: Unit Test
           result = SCENARIO_CHANGE_ENVIRONMENT_RESULT_E.SUCCESS;
        }
        else
        {
           // TODO: Unit Test
           result = SCENARIO_CHANGE_ENVIRONMENT_RESULT_E.UNKNOWN_FAILURE;
        }
     }
     FlatBuffers.FlatBufferBuilder fbb = new FlatBuffers.FlatBufferBuilder(1024);
     IOSServer.IOSCore.Messages.Environment.StartEnvironment(fbb);

     RESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M.StartRESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M(fbb);
     RESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M.AddResult(fbb, result);
     RESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M.AddEnvironment(fbb, request.Environment);
        var response = RESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M.EndRESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M(fbb);
     fbb.Finish(response.Value);
     // If the scenario is not able to be ended. Only respond to the originator
     if (result != SCENARIO_CHANGE_ENVIRONMENT_RESULT_E.SUCCESS)
     {
        EventData responseData = new EventData(Utilities.GetMessageBytes(fbb, DataType.RESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M, Utilities.UniqueId(), 0, message.RequestId));// TODO: Timestamp
        Mediator.GetInstance().RaiseEvent(responseData);
     }
     else // Otherwise, send to all scenario members
     {
        EventData responseData = new EventData(Utilities.GetMessageBytes(fbb, DataType.RESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M, Utilities.UniqueId()));// TODO: Timestamp
        Mediator.GetInstance().RaiseEvent(responseData, sendToAllExternalClients: true);
        _scenarios.Remove(request.ScenarioId);
     }

问题是

RESPONSE_SCENARIO_CHANGE_ENVIRONMENT_M.AddEnvironment(fbb, request.Environment);

是否有可能得到一个FlatBuffers.Offset<Environment>from Environment

4

1 回答 1

0

将表从一个 FlatBuffer 复制到另一个通常是不可能的,除非reflection::CopyTable在 C++ 中(或使用对象 API)。

它需要被复制(不能简单地引用),因为所有表及其依赖项都需要驻留在同一个缓冲区中,并且通过偏移量进行访问。

目前在 C# 中,您唯一的解决方案是编写自己的CopyEnvironment函数。

于 2016-10-01T15:34:05.023 回答