source: xplra/src/client/c/hu/varadiistvan/xplra/MultiGetter.cc@ 19:280541440d22

Last change on this file since 19:280541440d22 was 19:280541440d22, checked in by István Váradi <ivaradi@…>, 11 years ago

Getting multiple values works

File size: 5.2 KB
Line 
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 "MultiGetter.h"
32
33#include "XPlane.h"
34
35#include <xplra/Protocol.h>
36
37#include <hu/varadiistvan/scpl/io/DataStream.h>
38
39#include <cstdio>
40
41//------------------------------------------------------------------------------
42
43using hu::varadiistvan::xplra::MultiGetter;
44
45using xplra::Protocol;
46
47using std::vector;
48using std::min;
49
50//------------------------------------------------------------------------------
51
52inline void MultiGetter::read(const DataRef& dataRef) throw(Exception)
53{
54 switch (dataRef.type) {
55 case Protocol::TYPE_INT:
56 *reinterpret_cast<int32_t*>(data + dataRef.dataOffset) =
57 xplane.stream->readS32();
58 break;
59 case Protocol::TYPE_FLOAT:
60 *reinterpret_cast<float*>(data + dataRef.dataOffset) =
61 xplane.stream->readFloat();
62 break;
63 case Protocol::TYPE_DOUBLE:
64 *reinterpret_cast<double*>(data + dataRef.dataOffset) =
65 xplane.stream->readDouble();
66 break;
67 case Protocol::TYPE_FLOAT_ARRAY:
68 case Protocol::TYPE_INT_ARRAY:
69 case Protocol::TYPE_BYTE_ARRAY:
70 {
71 size_t length = xplane.stream->readS32();
72 xplane.checkStream();
73 length = min(length, dataRef.length);
74 size_t elementSize =
75 (dataRef.type==Protocol::TYPE_FLOAT_ARRAY) ? sizeof(float) :
76 ((dataRef.type==Protocol::TYPE_INT_ARRAY) ?
77 sizeof(int32_t) : sizeof(uint8_t));
78 xplane.stream->read(data + dataRef.dataOffset, length * elementSize);
79 }
80 }
81}
82
83//------------------------------------------------------------------------------
84
85uint8_t MultiGetter::doRegister() throw(Exception)
86{
87 xplane.stream->writeU8(Protocol::COMMAND_REGISTER_GET_MULTI);
88 xplane.stream->writeU32(dataRefs.size());
89
90 for(vector<DataRef>::const_iterator i = dataRefs.begin();
91 i!=dataRefs.end(); ++i)
92 {
93 const DataRef& dataRef = *i;
94 xplane.stream->writeString(dataRef.name);
95 xplane.stream->writeU8(dataRef.type);
96 if (dataRef.isArray()) {
97 xplane.stream->writeS32(dataRef.length);
98 xplane.stream->writeS32(dataRef.offset);
99 }
100 }
101
102 return Protocol::COMMAND_UNREGISTER_GET_MULTI;
103}
104
105//------------------------------------------------------------------------------
106
107void MultiGetter::doExecute() throw(Exception)
108{
109 xplane.checkStream();
110 xplane.stream->writeU8(Protocol::COMMAND_EXECUTE_GET_MULTI);
111 xplane.stream->writeU32(registeredID);
112 xplane.stream->flush();
113 xplane.checkResult();
114
115 for(vector<DataRef>::const_iterator i = dataRefs.begin();
116 i!=dataRefs.end(); ++i)
117 {
118 xplane.checkStream();
119 const DataRef& dataRef = *i;
120 read(dataRef);
121 }
122}
123
124
125//------------------------------------------------------------------------------
126
127void MultiGetter::doExecuteUnregistered() throw(Exception)
128{
129 for(vector<DataRef>::const_iterator i = dataRefs.begin();
130 i!=dataRefs.end(); ++i)
131 {
132 const DataRef& dataRef = *i;
133 xplane.stream->writeU8(Protocol::COMMAND_GET_SINGLE);
134 xplane.stream->writeString(dataRef.name);
135 xplane.stream->writeU8(dataRef.type);
136 if (dataRef.isArray()) {
137 xplane.stream->writeS32(dataRef.length);
138 xplane.stream->writeS32(dataRef.offset);
139 }
140 xplane.stream->flush();
141 xplane.checkResult();
142 read(dataRef);
143 }
144}
145
146//------------------------------------------------------------------------------
147
148// Local Variables:
149// mode: C++
150// c-basic-offset: 4
151// indent-tabs-mode: nil
152// End:
Note: See TracBrowser for help on using the repository browser.