将现有的FlatBuffers对象添加到FlatBufferBuilder C#

时间:2016-09-30 21:00:13

标签: c# flatbuffers

我有两个表都包含一个环境表

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;
}

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

我的问题是,因为我在代码中已经有一个正确填充的Environment对象,我正在创建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>获得Environment

1 个答案:

答案 0 :(得分:0)

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

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

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