source: xplra/src/client/c/hu/varadiistvan/xplra/MultiGetter.cc@ 22:e29d792d1b5d

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

Moved the registration code into MultiBuffer

File size: 4.8 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
52MultiGetter::MultiGetter(XPlane& xplane) throw() :
53 MultiBuffer(xplane, Protocol::COMMAND_REGISTER_GET_MULTI,
54 Protocol::COMMAND_UNREGISTER_GET_MULTI)
55{
56}
57
58//------------------------------------------------------------------------------
59
60inline void MultiGetter::read(const DataRef& dataRef) throw(Exception)
61{
62 switch (dataRef.type) {
63 case Protocol::TYPE_INT:
64 *reinterpret_cast<int32_t*>(data + dataRef.dataOffset) =
65 xplane.stream->readS32();
66 break;
67 case Protocol::TYPE_FLOAT:
68 *reinterpret_cast<float*>(data + dataRef.dataOffset) =
69 xplane.stream->readFloat();
70 break;
71 case Protocol::TYPE_DOUBLE:
72 *reinterpret_cast<double*>(data + dataRef.dataOffset) =
73 xplane.stream->readDouble();
74 break;
75 case Protocol::TYPE_FLOAT_ARRAY:
76 case Protocol::TYPE_INT_ARRAY:
77 case Protocol::TYPE_BYTE_ARRAY:
78 {
79 size_t length = xplane.stream->readS32();
80 xplane.checkStream();
81 length = min(length, dataRef.length);
82 size_t elementSize =
83 (dataRef.type==Protocol::TYPE_FLOAT_ARRAY) ? sizeof(float) :
84 ((dataRef.type==Protocol::TYPE_INT_ARRAY) ?
85 sizeof(int32_t) : sizeof(uint8_t));
86 xplane.stream->read(data + dataRef.dataOffset, length * elementSize);
87 }
88 }
89}
90
91//------------------------------------------------------------------------------
92
93void MultiGetter::doExecute() throw(Exception)
94{
95 xplane.checkStream();
96 xplane.stream->writeU8(Protocol::COMMAND_EXECUTE_GET_MULTI);
97 xplane.stream->writeU32(registeredID);
98 xplane.stream->flush();
99 xplane.checkResult();
100
101 for(vector<DataRef>::const_iterator i = dataRefs.begin();
102 i!=dataRefs.end(); ++i)
103 {
104 xplane.checkStream();
105 const DataRef& dataRef = *i;
106 read(dataRef);
107 }
108}
109
110
111//------------------------------------------------------------------------------
112
113void MultiGetter::doExecuteUnregistered() throw(Exception)
114{
115 for(vector<DataRef>::const_iterator i = dataRefs.begin();
116 i!=dataRefs.end(); ++i)
117 {
118 const DataRef& dataRef = *i;
119 xplane.stream->writeU8(Protocol::COMMAND_GET_SINGLE);
120 xplane.stream->writeString(dataRef.name);
121 xplane.stream->writeU8(dataRef.type);
122 if (dataRef.isArray()) {
123 xplane.stream->writeS32(dataRef.length);
124 xplane.stream->writeS32(dataRef.offset);
125 }
126 xplane.stream->flush();
127 xplane.checkResult();
128 read(dataRef);
129 }
130}
131
132//------------------------------------------------------------------------------
133
134// Local Variables:
135// mode: C++
136// c-basic-offset: 4
137// indent-tabs-mode: nil
138// End:
Note: See TracBrowser for help on using the repository browser.