source: xplra/src/client/c/hu/varadiistvan/xplra/XPlane.cc@ 36:29e3b676c0c2

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

Added a new command to query the versions

File size: 14.3 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 "XPlane.h"
32
33#include "MultiGetter.h"
34#include "MultiSetter.h"
35
36#include <hu/varadiistvan/scpl/io/LocalClientSocket.h>
37#include <hu/varadiistvan/scpl/io/DataStream.h>
38
39#include <xplra/Protocol.h>
40
41#include <memory>
42
43#ifndef _WIN32
44#include <signal.h>
45#endif
46
47//------------------------------------------------------------------------------
48
49using hu::varadiistvan::xplra::XPlane;
50using hu::varadiistvan::xplra::MultiGetter;
51using hu::varadiistvan::xplra::MultiSetter;
52
53using hu::varadiistvan::scpl::io::LocalClientSocket;
54using hu::varadiistvan::scpl::io::LocalConnector;
55using hu::varadiistvan::scpl::io::DataStream;
56
57using xplra::Protocol;
58
59using std::auto_ptr;
60using std::string;
61
62//------------------------------------------------------------------------------
63
64void XPlane::checkStream() throw(NotConnectedException, IOException)
65{
66 if (stream==0) {
67 throw NotConnectedException();
68 } else if (stream->failed()) {
69 throw IOException(stream->getErrorCode());
70 }
71}
72
73//------------------------------------------------------------------------------
74
75void XPlane::checkResult(uint8_t result) throw(ProtocolException)
76{
77 switch(result) {
78 case Protocol::RESULT_OK:
79 return;
80 case Protocol::RESULT_INVALID_COMMAND:
81 throw ProtocolException(ProtocolException::INVALID_COMMAND);
82 case Protocol::RESULT_UNKNOWN_DATAREF:
83 throw ProtocolException(ProtocolException::UNKNOWN_DATAREF);
84 case Protocol::RESULT_INVALID_TYPE:
85 throw ProtocolException(ProtocolException::INVALID_TYPE);
86 case Protocol::RESULT_INVALID_LENGTH:
87 throw ProtocolException(ProtocolException::INVALID_LENGTH);
88 case Protocol::RESULT_INVALID_OFFSET:
89 throw ProtocolException(ProtocolException::INVALID_OFFSET);
90 case Protocol::RESULT_INVALID_COUNT:
91 throw ProtocolException(ProtocolException::INVALID_COUNT);
92 case Protocol::RESULT_INVALID_ID:
93 throw ProtocolException(ProtocolException::INVALID_ID);
94 case Protocol::RESULT_OTHER_ERROR:
95 default:
96 throw ProtocolException(ProtocolException::OTHER);
97 }
98}
99
100//------------------------------------------------------------------------------
101
102void XPlane::checkResult() throw(ProtocolException, IOException)
103{
104 uint8_t result = stream->readU8();
105 checkStream();
106 checkResult(result);
107}
108
109//------------------------------------------------------------------------------
110//------------------------------------------------------------------------------
111
112XPlane::~XPlane() throw()
113{
114 disconnect();
115 for(multiBuffers_t::iterator i = multiBuffers.begin();
116 i!=multiBuffers.end(); ++i)
117 {
118 MultiBuffer* buffer = *i;
119 buffer->forgetRegistration();
120 delete buffer;
121 }
122}
123
124//------------------------------------------------------------------------------
125
126void XPlane::connect() throw(IOException)
127{
128 if (socket!=0) return;
129
130 auto_ptr<LocalClientSocket> clientSocket(new LocalClientSocket("xplra",
131 &waiter));
132 LocalConnector& connector = clientSocket->getConnector();
133
134 while (!connector.connect()) {
135 if (connector.failed()) {
136 throw IOException(connector.getErrorCode());
137 }
138 waiter.wait();
139 if (waiter.failed()) {
140 throw IOException(waiter.getErrorCode());
141 }
142 }
143
144#ifndef _WIN32
145 signal(SIGPIPE, SIG_IGN);
146#endif
147
148 socket = clientSocket.release();
149 stream = new DataStream(*socket);
150}
151
152//------------------------------------------------------------------------------
153
154void XPlane::disconnect() throw()
155{
156 if (socket==0) return;
157
158 delete stream; stream = 0;
159 delete socket; socket = 0;
160}
161
162//------------------------------------------------------------------------------
163
164MultiGetter& XPlane::createMultiGetter() throw()
165{
166 MultiGetter* getter = new MultiGetter(*this);
167 multiBuffers.insert(getter);
168 return *getter;
169}
170
171//------------------------------------------------------------------------------
172
173MultiSetter& XPlane::createMultiSetter() throw()
174{
175 MultiSetter* setter = new MultiSetter(*this);
176 multiBuffers.insert(setter);
177 return *setter;
178}
179
180//------------------------------------------------------------------------------
181
182bool XPlane::destroyMultiBuffer(MultiBuffer& buffer) throw(Exception)
183{
184 multiBuffers_t::iterator i = multiBuffers.find(&buffer);
185 if (i==multiBuffers.end()) return false;
186
187 multiBuffers.erase(i);
188 delete &buffer;
189
190 return true;
191}
192
193//------------------------------------------------------------------------------
194
195void XPlane::getVersions(int& xplaneVersion, int& xplmVersion, int& xplraVersion)
196 throw(Exception)
197{
198 stream->writeU8(Protocol::COMMAND_GET_VERSIONS);
199 stream->flush();
200 checkResult();
201
202 xplaneVersion = stream->readS32();
203 xplmVersion = stream->readS32();
204 xplraVersion = stream->readS32();
205
206 checkStream();
207}
208
209//------------------------------------------------------------------------------
210
211void XPlane::getScalar(const char* name, uint8_t type) throw(Exception)
212{
213 checkStream();
214
215 stream->writeU8(Protocol::COMMAND_GET_SINGLE);
216 stream->writeString(name, strlen(name));
217 stream->writeU8(type);
218 if (!stream->flush()) checkStream();
219
220 checkResult();
221}
222
223//------------------------------------------------------------------------------
224
225int XPlane::getInt(const char* name) throw(Exception)
226{
227 getScalar(name, Protocol::TYPE_INT);
228
229 int value = stream->readS32();
230 checkStream();
231 return value;
232}
233
234//------------------------------------------------------------------------------
235
236float XPlane::getFloat(const char* name) throw(Exception)
237{
238 getScalar(name, Protocol::TYPE_FLOAT);
239
240 float value = stream->readFloat();
241 checkStream();
242 return value;
243}
244
245//------------------------------------------------------------------------------
246
247double XPlane::getDouble(const char* name) throw(Exception)
248{
249 getScalar(name, Protocol::TYPE_DOUBLE);
250
251 double value = stream->readDouble();
252 checkStream();
253 return value;
254}
255
256//------------------------------------------------------------------------------
257
258size_t XPlane::getArray(const char* name, uint8_t type,
259 ssize_t length, size_t offset) throw(Exception)
260{
261 checkStream();
262
263 stream->writeU8(Protocol::COMMAND_GET_SINGLE);
264 stream->writeString(name, strlen(name));
265 stream->writeU8(type);
266 stream->writeS32(static_cast<int32_t>(length));
267 stream->writeS32(static_cast<int32_t>(offset));
268 if (!stream->flush()) checkStream();
269
270 uint8_t result = stream->readU8();
271 length = stream->readS32();
272 checkStream();
273 checkResult(result);
274
275 return length;
276}
277
278//------------------------------------------------------------------------------
279
280size_t XPlane::getFloatArray(const char* name, float* dest,
281 size_t length, size_t offset) throw(Exception)
282{
283 length = getArray(name, Protocol::TYPE_FLOAT_ARRAY, length, offset);
284
285 if (!stream->read(dest, length*sizeof(float))) checkStream();
286
287 return length;
288}
289
290//------------------------------------------------------------------------------
291
292float* XPlane::getFloatArray(const char* name, size_t& length,
293 size_t offset) throw(Exception)
294{
295 length = getArray(name, Protocol::TYPE_FLOAT_ARRAY, -1, offset);
296
297 auto_ptr<float> data(new float[length]);
298 if (!stream->read(data.get(), length*sizeof(float))) checkStream();
299 return data.release();
300}
301
302//------------------------------------------------------------------------------
303
304size_t XPlane::getIntArray(const char* name, int32_t* dest,
305 size_t length, size_t offset) throw(Exception)
306{
307 length = getArray(name, Protocol::TYPE_INT_ARRAY, length, offset);
308
309 if (!stream->read(dest, length*sizeof(int32_t))) checkStream();
310
311 return length;
312}
313
314//------------------------------------------------------------------------------
315
316int32_t* XPlane::getIntArray(const char* name, size_t& length,
317 size_t offset) throw(Exception)
318{
319 length = getArray(name, Protocol::TYPE_INT_ARRAY, -1, offset);
320
321 auto_ptr<int32_t> data(new int32_t[length]);
322 if (!stream->read(data.get(), length*sizeof(int32_t))) checkStream();
323 return data.release();
324}
325
326//------------------------------------------------------------------------------
327
328size_t XPlane::getByteArray(const char* name, uint8_t* dest,
329 size_t length, size_t offset) throw(Exception)
330{
331 length = getArray(name, Protocol::TYPE_BYTE_ARRAY, length, offset);
332
333 if (!stream->read(dest, length*sizeof(uint8_t))) checkStream();
334
335 return length;
336}
337
338//------------------------------------------------------------------------------
339
340uint8_t* XPlane::getByteArray(const char* name, size_t& length,
341 size_t offset) throw(Exception)
342{
343 length = getArray(name, Protocol::TYPE_BYTE_ARRAY, -1, offset);
344
345 auto_ptr<uint8_t> data(new uint8_t[length]);
346 if (!stream->read(data.get(), length*sizeof(uint8_t))) checkStream();
347 return data.release();
348}
349
350//------------------------------------------------------------------------------
351
352string XPlane::getString(const char* name, size_t offset) throw(Exception)
353{
354 size_t length = 0;
355 auto_ptr<uint8_t> data(getByteArray(name, length, offset));
356 return string(reinterpret_cast<char*>(data.get()));
357}
358
359//------------------------------------------------------------------------------
360
361void XPlane::setScalar(const char* name, uint8_t type) throw(Exception)
362{
363 stream->writeU8(Protocol::COMMAND_SET_SINGLE);
364 stream->writeString(name, strlen(name));
365 stream->writeU8(type);
366}
367
368//------------------------------------------------------------------------------
369
370void XPlane::setInt(const char* name, int value) throw(Exception)
371{
372 setScalar(name, Protocol::TYPE_INT);
373 stream->writeS32(value);
374 stream->flush();
375 checkResult();
376}
377
378//------------------------------------------------------------------------------
379
380void XPlane::setFloat(const char* name, float value) throw(Exception)
381{
382 setScalar(name, Protocol::TYPE_FLOAT);
383 stream->writeFloat(value);
384 stream->flush();
385 checkResult();
386}
387
388//------------------------------------------------------------------------------
389
390void XPlane::setDouble(const char* name, double value) throw(Exception)
391{
392 setScalar(name, Protocol::TYPE_DOUBLE);
393 stream->writeDouble(value);
394 stream->flush();
395 checkResult();
396}
397
398//------------------------------------------------------------------------------
399
400void XPlane::setArray(const char* name, uint8_t type, size_t length,
401 size_t offset) throw(Exception)
402{
403 stream->writeU8(Protocol::COMMAND_SET_SINGLE);
404 stream->writeString(name, strlen(name));
405 stream->writeU8(type);
406 stream->writeS32(static_cast<int32_t>(length));
407 stream->writeS32(static_cast<int32_t>(offset));
408}
409
410//------------------------------------------------------------------------------
411
412void XPlane::setFloatArray(const char* name, const float* values, size_t length,
413 size_t offset) throw(Exception)
414{
415 setArray(name, Protocol::TYPE_FLOAT_ARRAY, length, offset);
416 stream->write(values, length*sizeof(*values));
417 stream->flush();
418 checkResult();
419}
420
421//------------------------------------------------------------------------------
422
423void XPlane::setIntArray(const char* name, const int32_t* values, size_t length,
424 size_t offset) throw(Exception)
425{
426 setArray(name, Protocol::TYPE_INT_ARRAY, length, offset);
427 stream->write(values, length*sizeof(*values));
428 stream->flush();
429 checkResult();
430}
431
432//------------------------------------------------------------------------------
433
434void XPlane::setByteArray(const char* name, const uint8_t* values, size_t length,
435 size_t offset) throw(Exception)
436{
437 setArray(name, Protocol::TYPE_BYTE_ARRAY, length, offset);
438 stream->write(values, length*sizeof(*values));
439 stream->flush();
440 checkResult();
441}
442
443//------------------------------------------------------------------------------
444
445void XPlane::setString(const char* name, const char* value, size_t length,
446 size_t offset) throw(Exception)
447{
448 size_t valueLength = strlen(value);
449 if ((valueLength+1)>=length) {
450 setByteArray(name, reinterpret_cast<const uint8_t*>(value),
451 length, offset);
452 } else {
453 auto_ptr<uint8_t> buffer(new uint8_t[length]);
454 memcpy(buffer.get(), value, valueLength);
455 memset(buffer.get() + valueLength, 0, length - valueLength);
456 setByteArray(name, buffer.get(), length, offset);
457 }
458}
459//------------------------------------------------------------------------------
460
461// Local Variables:
462// mode: C++
463// c-basic-offset: 4
464// indent-tabs-mode: nil
465// End:
Note: See TracBrowser for help on using the repository browser.