X-Plane Remote Access Plugin and Client Library
GetMultiDataRefRequest.cc
1 // Copyright (c) 2013 by István Váradi
2 
3 // This file is part of XPLRA, a remote-access plugin for X-Plane
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 
8 // 1. Redistributions of source code must retain the above copyright notice, this
9 // list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18 // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 
25 // The views and conclusions contained in the software and documentation are those
26 // of the authors and should not be interpreted as representing official policies,
27 // either expressed or implied, of the FreeBSD Project.
28 
29 //------------------------------------------------------------------------------
30 
31 #include "GetMultiDataRefRequest.h"
32 
33 #include "Protocol.h"
34 
35 #include <hu/varadiistvan/xplcommon/Util.h>
36 
37 //------------------------------------------------------------------------------
38 
40 
41 using hu::varadiistvan::scpl::io::DataStream;
42 using hu::varadiistvan::xplcommon::Util;
43 
44 //------------------------------------------------------------------------------
45 
46 GetMultiDataRefRequest::GetMultiDataRefRequest(uint8_t& result,
47  DataStream& stream)
48 {
49  result = Protocol::RESULT_OK;
50 
51  uint32_t numTasks = stream.readU32();
52  if (!stream) return;
53  if (numTasks==0 || numTasks>Protocol::MAX_MULTI_COUNT) {
55  return;
56  }
57 
58  for(size_t i = 0;
59  i<numTasks && result==Protocol::RESULT_OK && stream; ++i)
60  {
61  GetDataRefTask* task = GetDataRefTask::create(result, stream);
62  if (task!=0) addTask(task);
63  }
64 }
65 
66 //------------------------------------------------------------------------------
67 
68 void GetMultiDataRefRequest::writeResult(DataStream& stream) const
69 {
70  // Util::debug("hu.varadiistvan.xplra.GetMultiDataRefRequest[%p]::writeResult\n", this);
71  size_t numTasks = getNumTasks();
72  for(size_t i = 0; i<numTasks; ++i) {
73  GetDataRefTask* task = getTask(i);
74  // Util::debug("hu.varadiistvan.xplra.GetMultiDataRefRequest[%p]::writeResult: task[%zu]: %d @ %p\n",
75  // this, i, task->isValid(), task);
76  if (!task->isValid()) {
77  stream.writeU8(Protocol::RESULT_UNKNOWN_DATAREF);
78  stream.writeU32(i);
79  return;
80  }
81  }
82 
83  stream.writeU8(Protocol::RESULT_OK);
84  for(size_t i = 0; i<numTasks; ++i) {
85  GetDataRefTask* task = getTask(i);
86  task->writeValue(stream);
87  }
88 }
89 
90 //------------------------------------------------------------------------------
91 
92 // Local Variables:
93 // mode: C++
94 // c-basic-offset: 4
95 // indent-tabs-mode: nil
96 // End:
bool isValid() const
Definition: DataRefTask.h:125
virtual void writeValue(hu::varadiistvan::scpl::io::DataStream &stream)=0
static GetDataRefTask * create(uint8_t &result, hu::varadiistvan::scpl::io::DataStream &stream)
void writeResult(hu::varadiistvan::scpl::io::DataStream &stream) const
GetDataRefTask * getTask(size_t index) const
static const size_t MAX_MULTI_COUNT
Definition: Protocol.h:261
static const uint8_t RESULT_OK
Definition: Protocol.h:195
static const uint8_t RESULT_UNKNOWN_DATAREF
Definition: Protocol.h:205
static const uint8_t RESULT_INVALID_COUNT
Definition: Protocol.h:225