source: xplra/src/client/c/hu/varadiistvan/xplra/xplra.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: 37.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 "xplra.h"
32
33#include "XPlane.h"
34#include "MultiGetter.h"
35#include "MultiSetter.h"
36#include "Exception.h"
37
38#include <vector>
39#include <memory>
40
41#include <cstdio>
42#include <cassert>
43
44//------------------------------------------------------------------------------
45
46using hu::varadiistvan::xplra::XPlane;
47using hu::varadiistvan::xplra::MultiBuffer;
48using hu::varadiistvan::xplra::MultiGetter;
49using hu::varadiistvan::xplra::MultiSetter;
50using hu::varadiistvan::xplra::Exception;
51using hu::varadiistvan::xplra::IOException;
52using hu::varadiistvan::xplra::ProtocolException;
53using hu::varadiistvan::xplra::NotConnectedException;
54using hu::varadiistvan::xplra::TypeMismatchException;
55using hu::varadiistvan::xplra::InvalidIDException;
56
57using std::exception;
58using std::string;
59using std::vector;
60using std::set;
61using std::auto_ptr;
62
63//------------------------------------------------------------------------------
64
65namespace {
66
67//------------------------------------------------------------------------------
68
69/**
70 * Template for handling slots.
71 */
72template <class Value>
73struct Slot
74{
75private:
76 /**
77 * The slots.
78 */
79 static std::vector< Slot<Value> > slots;
80
81 /**
82 * The index of the first free slot.
83 */
84 static int firstFree;
85
86public:
87 /**
88 * Add a new value.
89 *
90 * @return the ID of the new value
91 */
92 static int addValue(Value value) throw();
93
94 /**
95 * Get the value with the given ID.
96 */
97 static Value getValue(int valueID) throw();
98
99 /**
100 * Clear the value with the given ID.
101 */
102 static void clearValue(int valueID) throw();
103
104private:
105 /**
106 * Indicate if the slot contains a valid value or not.
107 */
108 bool valid;
109
110 union {
111 // The value, if the slot contains a value
112 Value value;
113
114 // The index of the next free slot
115 int nextFreeIndex;
116 };
117
118 /**
119 * Construct a slot with the given value
120 */
121 Slot(Value value) throw();
122
123 /**
124 * Set the value and return the former next free index. It
125 * should be called for a slot with no value.
126 */
127 int setValue(Value value) throw();
128
129 /**
130 * Get the value if this slot contains a value. Otherwise 0 is
131 * returned (converted to value).
132 */
133 Value getValue() const throw();
134
135 /**
136 * Clear the value and set the given next free index.
137 */
138 void clear(int nextFreeIndex) throw();
139};
140
141//------------------------------------------------------------------------------
142
143template <class Value> vector< Slot<Value> > Slot<Value>::slots;
144
145//------------------------------------------------------------------------------
146
147template <class Value> int Slot<Value>::firstFree = -1;
148
149//------------------------------------------------------------------------------
150
151template <class Value>
152inline Slot<Value>::Slot(Value value) throw() :
153 valid(true)
154{
155 this->value = value;
156}
157
158//------------------------------------------------------------------------------
159
160template <class Value>
161inline int Slot<Value>::setValue(Value value) throw()
162{
163 assert(!valid);
164 int nextFreeIndex = this->nextFreeIndex;
165 this->value = value;
166 valid = true;
167 return nextFreeIndex;
168}
169
170//------------------------------------------------------------------------------
171
172template <class Value>
173inline Value Slot<Value>::getValue() const throw()
174{
175 return valid ? value : static_cast<Value>(0);
176}
177
178//------------------------------------------------------------------------------
179
180template <class Value>
181inline void Slot<Value>::clear(int nextFreeIndex) throw()
182{
183 assert(valid);
184 valid = false;
185 this->nextFreeIndex = nextFreeIndex;
186}
187
188//------------------------------------------------------------------------------
189
190template <class Value>
191int Slot<Value>::addValue(Value value) throw()
192{
193 int id = firstFree;
194 if (id<0) {
195 id = slots.size();
196 slots.push_back(Slot<Value>(value));
197 } else {
198 Slot& slot = slots[id];
199 firstFree = slot.setValue(value);
200 }
201
202 return id;
203}
204
205//------------------------------------------------------------------------------
206
207template <class Value>
208Value Slot<Value>::getValue(int valueID) throw()
209{
210 size_t index = static_cast<size_t>(valueID);
211 return (index<slots.size()) ?
212 slots[index].getValue() : static_cast<Value>(0);
213}
214
215//------------------------------------------------------------------------------
216
217template <class Value>
218void Slot<Value>::clearValue(int valueID) throw()
219{
220 size_t index = static_cast<size_t>(valueID);
221 if (index<slots.size()) {
222 slots[index].clear(firstFree);
223 firstFree = index;
224 }
225}
226
227//------------------------------------------------------------------------------
228//------------------------------------------------------------------------------
229
230typedef Slot<MultiBuffer*> MultiBufferSlot;
231
232//------------------------------------------------------------------------------
233//------------------------------------------------------------------------------
234
235/**
236 * Information about a connection.
237 */
238class Connection : public XPlane
239{
240private:
241 /**
242 * Type for the set of multi-dataref buffers belonging to this connection.
243 */
244 typedef std::set<int> multiBufferIDs_t;
245
246public:
247 /**
248 * Get the connection for the given multi-dataref buffer.
249 */
250 static Connection& get(const MultiBuffer& buffer) throw();
251
252private:
253 /**
254 * The last error code.
255 */
256 int lastErrorCode;
257
258 /**
259 * The last error subcode.
260 */
261 unsigned long lastErrorSubCode;
262
263 /**
264 * The string of the last error
265 */
266 std::string lastErrorString;
267
268 /**
269 * The set of multi-dataref buffers belonging to this connection.
270 */
271 multiBufferIDs_t multiBufferIDs;
272
273public:
274 /**
275 * Construct the connection
276 */
277 Connection() throw();
278
279 /**
280 * Destroy the connection.
281 */
282 ~Connection() throw();
283
284 /**
285 * Handle the currently pending exception.
286 */
287 void handleException() throw();
288
289 /**
290 * Get the last error code.
291 */
292 int getLastError(unsigned long* lastErrorSubCode) const throw();
293
294 /**
295 * Get the string representation of the last error code.
296 */
297 const char* getLastErrorString() const throw();
298
299 /**
300 * Clear the last error code.
301 */
302 void clearLastError() throw();
303
304 /**
305 * Create a multi-dataref getter object and register it in a slot.
306 *
307 * @return the ID of the new getter.
308 */
309 int createMultiGetter() throw();
310
311 /**
312 * Create a multi-dataref setter object and register it in a slot.
313 *
314 * @return the ID of the new setter.
315 */
316 int createMultiSetter() throw();
317
318 /**
319 * Destroy the multi-dataref buffer with the given ID.
320 *
321 * @param bufferID the ID of the buffer. It should be a valid ID,
322 * that belongs to this connection.
323 *
324 * @return whether the buffer was found and could be destroyed.
325 */
326 bool destroyMultiBuffer(int bufferID) throw(Exception);
327};
328
329//------------------------------------------------------------------------------
330
331inline Connection& Connection::get(const MultiBuffer& buffer) throw()
332{
333 return static_cast<Connection&>(buffer.getXPlane());
334}
335
336//------------------------------------------------------------------------------
337
338inline Connection::Connection() throw() :
339 lastErrorCode(0),
340 lastErrorSubCode(0)
341{
342}
343
344//------------------------------------------------------------------------------
345
346Connection::~Connection() throw()
347{
348 for(multiBufferIDs_t::iterator i = multiBufferIDs.begin();
349 i!=multiBufferIDs.end(); ++i)
350 {
351 MultiBufferSlot::clearValue(*i);
352 }
353}
354
355//------------------------------------------------------------------------------
356
357void Connection::handleException() throw()
358{
359 try {
360 throw;
361 } catch(const IOException& e) {
362 lastErrorCode = ERROR_IO;
363 lastErrorSubCode = e.getErrorCode();
364 lastErrorString = e.what();
365 } catch(const ProtocolException& e) {
366 lastErrorCode = ERROR_PROTOCOL;
367 lastErrorSubCode = static_cast<unsigned long>(e.getErrorCode());
368 lastErrorString = e.what();
369 } catch(const NotConnectedException& e) {
370 lastErrorCode = ERROR_NOT_CONNECTED;
371 lastErrorSubCode = 0;
372 lastErrorString = e.what();
373 } catch(const TypeMismatchException& e) {
374 lastErrorCode = ERROR_TYPE_MISMATCH;
375 lastErrorSubCode = 0;
376 lastErrorString = e.what();
377 } catch(const InvalidIDException& e) {
378 lastErrorCode = ERROR_INVALID_ID;
379 lastErrorSubCode = 0;
380 lastErrorString = e.what();
381 } catch(const exception& e) {
382 lastErrorCode = ERROR_OTHER;
383 lastErrorSubCode = 0;
384 lastErrorString = e.what();
385 } catch(...) {
386 lastErrorCode = ERROR_OTHER;
387 lastErrorSubCode = 0;
388 lastErrorString = "<Exception of an unknown type>";
389 }
390}
391
392//------------------------------------------------------------------------------
393
394inline int Connection::getLastError(unsigned long* lastErrorSubCode)
395 const throw()
396{
397 if (lastErrorSubCode!=0) *lastErrorSubCode = this->lastErrorSubCode;
398 return lastErrorCode;
399}
400
401//------------------------------------------------------------------------------
402
403inline const char* Connection::getLastErrorString() const throw()
404{
405 return (lastErrorCode==ERROR_NONE) ? 0 : lastErrorString.c_str();
406}
407
408//------------------------------------------------------------------------------
409
410inline void Connection::clearLastError() throw()
411{
412 lastErrorCode = ERROR_NONE;
413 lastErrorSubCode = 0;
414 lastErrorString.clear();
415}
416
417//------------------------------------------------------------------------------
418
419inline int Connection::createMultiGetter() throw()
420{
421 MultiGetter& getter = XPlane::createMultiGetter();
422 return MultiBufferSlot::addValue(&getter);
423}
424
425//------------------------------------------------------------------------------
426
427inline int Connection::createMultiSetter() throw()
428{
429 MultiSetter& setter = XPlane::createMultiSetter();
430 return MultiBufferSlot::addValue(&setter);
431}
432
433//------------------------------------------------------------------------------
434
435bool Connection::destroyMultiBuffer(int bufferID) throw(Exception)
436{
437 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
438 if (buffer==0) return false;
439
440 if (XPlane::destroyMultiBuffer(*buffer)) {
441 multiBufferIDs.erase(bufferID);
442 MultiBufferSlot::clearValue(bufferID);
443 return true;
444 } else {
445 return false;
446 }
447}
448
449//------------------------------------------------------------------------------
450//------------------------------------------------------------------------------
451
452/**
453 * Slot for connections.
454 */
455typedef Slot<Connection*> ConnectionSlot;
456
457//------------------------------------------------------------------------------
458
459} /* anonymous namespace */
460
461//------------------------------------------------------------------------------
462
463extern "C" int xplra_get_last_error(int connectionID, unsigned long* subCode)
464{
465 Connection* connection = ConnectionSlot::getValue(connectionID);
466 return (connection==0) ? -1 : connection->getLastError(subCode);
467}
468
469//------------------------------------------------------------------------------
470
471extern "C" const char* xplra_get_last_error_string(int connectionID)
472{
473 Connection* connection = ConnectionSlot::getValue(connectionID);
474 return (connection==0) ? 0 : connection->getLastErrorString();
475}
476
477//------------------------------------------------------------------------------
478
479extern "C" void xplra_clear_last_error(int connectionID)
480{
481 Connection* connection = ConnectionSlot::getValue(connectionID);
482 if (connection!=0) connection->clearLastError();
483}
484
485//------------------------------------------------------------------------------
486
487extern "C" int xplra_connect()
488{
489 try {
490 auto_ptr<Connection> connection(new Connection());
491 connection->connect();
492 return ConnectionSlot::addValue(connection.release());
493 } catch(...) {
494 return -1;
495 }
496}
497
498//------------------------------------------------------------------------------
499
500extern "C"int xplra_get_versions(int connectionID,
501 int* xplaneVersion, int* xplmVersion,
502 int* xplraVersion)
503{
504 Connection* connection = ConnectionSlot::getValue(connectionID);
505 if (connection==0) return -1;
506 try {
507 connection->getVersions(*xplaneVersion, *xplmVersion, *xplraVersion);
508 return 0;
509 } catch (...) {
510 connection->handleException();
511 return -1;
512 }
513}
514
515//------------------------------------------------------------------------------
516
517extern "C" int xplra_get_int(int* value, int connectionID, const char* name)
518{
519 Connection* connection = ConnectionSlot::getValue(connectionID);
520 if (connection==0) return -1;
521 try {
522 *value = connection->getInt(name);
523 return 0;
524 } catch (...) {
525 connection->handleException();
526 return -1;
527 }
528}
529
530//------------------------------------------------------------------------------
531
532extern "C" int xplra_get_float(float* value, int connectionID, const char* name)
533{
534 Connection* connection = ConnectionSlot::getValue(connectionID);
535 if (connection==0) return -1;
536 try {
537 *value = connection->getFloat(name);
538 return 0;
539 } catch (...) {
540 connection->handleException();
541 return -1;
542 }
543}
544
545//------------------------------------------------------------------------------
546
547extern "C" int xplra_get_double(double* value,
548 int connectionID, const char* name)
549{
550 Connection* connection = ConnectionSlot::getValue(connectionID);
551 if (connection==0) return -1;
552 try {
553 *value = connection->getDouble(name);
554 return 0;
555 } catch (...) {
556 connection->handleException();
557 return -1;
558 }
559}
560
561//------------------------------------------------------------------------------
562
563extern "C" ssize_t xplra_get_float_array(float* dest, size_t length, size_t offset,
564 int connectionID, const char* name)
565{
566 Connection* connection = ConnectionSlot::getValue(connectionID);
567 if (connection==0) return -1;
568 try {
569 return connection->getFloatArray(name, dest, length, offset);
570 } catch (...) {
571 connection->handleException();
572 return -1;
573 }
574}
575
576/*----------------------------------------------------------------------------*/
577
578extern "C" float* xplra_get_float_array_new(size_t* length, size_t offset,
579 int connectionID, const char* name)
580{
581 Connection* connection = ConnectionSlot::getValue(connectionID);
582 if (connection==0) return 0;
583 try {
584 return connection->getFloatArray(name, *length, offset);
585 } catch (...) {
586 connection->handleException();
587 return 0;
588 }
589}
590
591//------------------------------------------------------------------------------
592
593extern "C" ssize_t xplra_get_int_array(int32_t* dest, size_t length, size_t offset,
594 int connectionID, const char* name)
595{
596 Connection* connection = ConnectionSlot::getValue(connectionID);
597 if (connection==0) return -1;
598 try {
599 return connection->getIntArray(name, dest, length, offset);
600 } catch (...) {
601 connection->handleException();
602 return -1;
603 }
604}
605
606/*----------------------------------------------------------------------------*/
607
608extern "C" int32_t* xplra_get_int_array_new(size_t* length, size_t offset,
609 int connectionID, const char* name)
610{
611 Connection* connection = ConnectionSlot::getValue(connectionID);
612 if (connection==0) return 0;
613 try {
614 return connection->getIntArray(name, *length, offset);
615 } catch (...) {
616 connection->handleException();
617 return 0;
618 }
619}
620
621//------------------------------------------------------------------------------
622
623extern "C" ssize_t xplra_get_byte_array(void* dest, size_t length, size_t offset,
624 int connectionID, const char* name)
625{
626 Connection* connection = ConnectionSlot::getValue(connectionID);
627 if (connection==0) return -1;
628 try {
629 return connection->getByteArray(name, reinterpret_cast<uint8_t*>(dest),
630 length, offset);
631 } catch (...) {
632 connection->handleException();
633 return -1;
634 }
635}
636
637/*----------------------------------------------------------------------------*/
638
639extern "C" uint8_t* xplra_get_byte_array_new(size_t* length, size_t offset,
640 int connectionID, const char* name)
641{
642 Connection* connection = ConnectionSlot::getValue(connectionID);
643 if (connection==0) return 0;
644 try {
645 return connection->getByteArray(name, *length, offset);
646 } catch (...) {
647 connection->handleException();
648 return 0;
649 }
650}
651
652//------------------------------------------------------------------------------
653
654extern "C" int xplra_set_int(int connectionID, const char* name, int value)
655{
656 Connection* connection = ConnectionSlot::getValue(connectionID);
657 if (connection==0) return 0;
658 try {
659 connection->setInt(name, value);
660 return 0;
661 } catch (...) {
662 connection->handleException();
663 return -1;
664 }
665}
666
667//------------------------------------------------------------------------------
668
669extern "C" int xplra_set_float(int connectionID, const char* name, float value)
670{
671 Connection* connection = ConnectionSlot::getValue(connectionID);
672 if (connection==0) return 0;
673 try {
674 connection->setFloat(name, value);
675 return 0;
676 } catch (...) {
677 connection->handleException();
678 return -1;
679 }
680}
681
682//------------------------------------------------------------------------------
683
684extern "C" int xplra_set_double(int connectionID, const char* name,
685 double value)
686{
687 Connection* connection = ConnectionSlot::getValue(connectionID);
688 if (connection==0) return 0;
689 try {
690 connection->setDouble(name, value);
691 return 0;
692 } catch (...) {
693 connection->handleException();
694 return -1;
695 }
696}
697
698/*----------------------------------------------------------------------------*/
699
700extern "C" int xplra_set_float_array(int connectionID, const char* name,
701 const float* values,
702 size_t length, size_t offset)
703{
704 Connection* connection = ConnectionSlot::getValue(connectionID);
705 if (connection==0) return 0;
706 try {
707 connection->setFloatArray(name, values, length, offset);
708 return 0;
709 } catch (...) {
710 connection->handleException();
711 return -1;
712 }
713}
714
715/*----------------------------------------------------------------------------*/
716
717extern "C" int xplra_set_int_array(int connectionID, const char* name,
718 const int32_t* values,
719 size_t length, size_t offset)
720{
721 Connection* connection = ConnectionSlot::getValue(connectionID);
722 if (connection==0) return 0;
723 try {
724 connection->setIntArray(name, values, length, offset);
725 return 0;
726 } catch (...) {
727 connection->handleException();
728 return -1;
729 }
730}
731
732/*----------------------------------------------------------------------------*/
733
734extern "C" int xplra_set_byte_array(int connectionID, const char* name,
735 const void* values,
736 size_t length, size_t offset)
737{
738 Connection* connection = ConnectionSlot::getValue(connectionID);
739 if (connection==0) return 0;
740 try {
741 connection->setByteArray(name, reinterpret_cast<const uint8_t*>(values),
742 length, offset);
743 return 0;
744 } catch (...) {
745 connection->handleException();
746 return -1;
747 }
748}
749
750/*----------------------------------------------------------------------------*/
751
752extern "C" int xplra_set_string(int connectionID, const char* name,
753 const char* value,
754 size_t length, size_t offset)
755{
756 Connection* connection = ConnectionSlot::getValue(connectionID);
757 if (connection==0) return 0;
758 try {
759 connection->setString(name, value, length, offset);
760 return 0;
761 } catch (...) {
762 connection->handleException();
763 return -1;
764 }
765}
766
767//------------------------------------------------------------------------------
768//------------------------------------------------------------------------------
769
770extern "C" int xplra_multi_create_getter(int connectionID)
771{
772 Connection* connection = ConnectionSlot::getValue(connectionID);
773 return (connection==0) ? -1 : connection->createMultiGetter();
774}
775
776//------------------------------------------------------------------------------
777
778extern "C" int xplra_multi_create_setter(int connectionID)
779{
780 Connection* connection = ConnectionSlot::getValue(connectionID);
781 return (connection==0) ? -1 : connection->createMultiSetter();
782}
783
784//------------------------------------------------------------------------------
785
786extern "C" size_t xplra_multi_add_int(int bufferID, const char* name)
787{
788 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
789 return (buffer==0) ? INVALID_DATAREF_ID : buffer->addInt(name);
790}
791
792//------------------------------------------------------------------------------
793
794extern "C" size_t xplra_multi_add_float(int bufferID, const char* name)
795{
796 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
797 return (buffer==0) ? INVALID_DATAREF_ID : buffer->addFloat(name);
798}
799
800//------------------------------------------------------------------------------
801
802extern "C" size_t xplra_multi_add_double(int bufferID, const char* name)
803{
804 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
805 return (buffer==0) ? INVALID_DATAREF_ID : buffer->addDouble(name);
806}
807
808/*----------------------------------------------------------------------------*/
809
810extern "C" size_t xplra_multi_add_float_array(int bufferID, const char* name,
811 size_t length, size_t offset)
812{
813 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
814 return (buffer==0) ?
815 INVALID_DATAREF_ID : buffer->addFloatArray(name, length, offset);
816}
817
818/*----------------------------------------------------------------------------*/
819
820extern "C" size_t xplra_multi_add_int_array(int bufferID, const char* name,
821 size_t length, size_t offset)
822{
823 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
824 return (buffer==0) ?
825 INVALID_DATAREF_ID : buffer->addIntArray(name, length, offset);
826}
827
828/*----------------------------------------------------------------------------*/
829
830extern "C" size_t xplra_multi_add_byte_array(int bufferID, const char* name,
831 size_t length, size_t offset)
832{
833 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
834 return (buffer==0) ?
835 INVALID_DATAREF_ID : buffer->addByteArray(name, length, offset);
836}
837
838/*----------------------------------------------------------------------------*/
839
840extern "C" int xplra_multi_finalize(int bufferID)
841{
842 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
843 return (buffer==0) ? -1 : (buffer->finalize() ? 1 : 0);
844}
845
846/*----------------------------------------------------------------------------*/
847
848extern "C" int xplra_multi_register(int bufferID)
849{
850 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
851 if (buffer==0) return -1;
852 try {
853 buffer->registerInXPlane();
854 return 0;
855 } catch (...) {
856 Connection::get(*buffer).handleException();
857 return -1;
858 }
859}
860
861/*----------------------------------------------------------------------------*/
862
863extern "C" int xplra_multi_unregister(int bufferID)
864{
865 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
866 if (buffer==0) return -1;
867 try {
868 buffer->unregisterFromXPlane();
869 return 0;
870 } catch (...) {
871 Connection::get(*buffer).handleException();
872 return -1;
873 }
874}
875
876/*----------------------------------------------------------------------------*/
877
878extern "C" int xplra_multi_execute(int bufferID)
879{
880 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
881 if (buffer==0) return -1;
882 try {
883 buffer->execute();
884 return 0;
885 } catch (...) {
886 Connection::get(*buffer).handleException();
887 return -1;
888 }
889}
890
891//------------------------------------------------------------------------------
892
893extern "C" int xplra_multi_set_int(int bufferID, size_t datarefID, int value)
894{
895 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
896 if (buffer==0) return -1;
897 try {
898 buffer->setInt(datarefID, value);
899 return 0;
900 } catch (...) {
901 Connection::get(*buffer).handleException();
902 return -1;
903 }
904}
905
906/*----------------------------------------------------------------------------*/
907
908extern "C" int xplra_multi_get_int(int* dest, int bufferID, size_t datarefID)
909{
910 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
911 if (buffer==0) return -1;
912 try {
913 *dest = buffer->getInt(datarefID);
914 return 0;
915 } catch (...) {
916 Connection::get(*buffer).handleException();
917 return -1;
918 }
919}
920
921/*----------------------------------------------------------------------------*/
922
923extern "C"
924const int32_t* xplra_multi_get_int_const_ptr(int bufferID, size_t datarefID)
925{
926 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
927 if (buffer==0) return 0;
928 try {
929 return &(buffer->getIntRef(datarefID));
930 } catch (...) {
931 Connection::get(*buffer).handleException();
932 return 0;
933 }
934}
935
936/*----------------------------------------------------------------------------*/
937
938extern "C"
939int32_t* xplra_multi_get_int_ptr(int bufferID, size_t datarefID)
940{
941 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
942 if (buffer==0) return 0;
943 try {
944 return &(buffer->getIntRef(datarefID));
945 } catch (...) {
946 Connection::get(*buffer).handleException();
947 return 0;
948 }
949}
950
951//------------------------------------------------------------------------------
952
953extern "C"
954int xplra_multi_set_float(int bufferID, size_t datarefID, float value)
955{
956 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
957 if (buffer==0) return -1;
958 try {
959 buffer->setFloat(datarefID, value);
960 return 0;
961 } catch (...) {
962 Connection::get(*buffer).handleException();
963 return -1;
964 }
965}
966
967/*----------------------------------------------------------------------------*/
968
969extern "C"
970int xplra_multi_get_float(float* dest, int bufferID, size_t datarefID)
971{
972 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
973 if (buffer==0) return -1;
974 try {
975 *dest = buffer->getFloat(datarefID);
976 return 0;
977 } catch (...) {
978 Connection::get(*buffer).handleException();
979 return -1;
980 }
981}
982
983/*----------------------------------------------------------------------------*/
984
985extern "C"
986const float* xplra_multi_get_float_const_ptr(int bufferID, size_t datarefID)
987{
988 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
989 if (buffer==0) return 0;
990 try {
991 return &(buffer->getFloatRef(datarefID));
992 } catch (...) {
993 Connection::get(*buffer).handleException();
994 return 0;
995 }
996}
997
998/*----------------------------------------------------------------------------*/
999
1000extern "C"
1001float* xplra_multi_get_float_ptr(int bufferID, size_t datarefID)
1002{
1003 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1004 if (buffer==0) return 0;
1005 try {
1006 return &(buffer->getFloatRef(datarefID));
1007 } catch (...) {
1008 Connection::get(*buffer).handleException();
1009 return 0;
1010 }
1011}
1012
1013//------------------------------------------------------------------------------
1014
1015extern "C"
1016int xplra_multi_set_double(int bufferID, size_t datarefID, double value)
1017{
1018 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1019 if (buffer==0) return -1;
1020 try {
1021 buffer->setDouble(datarefID, value);
1022 return 0;
1023 } catch (...) {
1024 Connection::get(*buffer).handleException();
1025 return -1;
1026 }
1027}
1028
1029/*----------------------------------------------------------------------------*/
1030
1031extern "C"
1032int xplra_multi_get_double(double* dest, int bufferID, size_t datarefID)
1033{
1034 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1035 if (buffer==0) return -1;
1036 try {
1037 *dest = buffer->getDouble(datarefID);
1038 return 0;
1039 } catch (...) {
1040 Connection::get(*buffer).handleException();
1041 return -1;
1042 }
1043}
1044
1045/*----------------------------------------------------------------------------*/
1046
1047extern "C"
1048const double* xplra_multi_get_double_const_ptr(int bufferID, size_t datarefID)
1049{
1050 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1051 if (buffer==0) return 0;
1052 try {
1053 return &(buffer->getDoubleRef(datarefID));
1054 } catch (...) {
1055 Connection::get(*buffer).handleException();
1056 return 0;
1057 }
1058}
1059
1060/*----------------------------------------------------------------------------*/
1061
1062extern "C"
1063double* xplra_multi_get_double_ptr(int bufferID, size_t datarefID)
1064{
1065 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1066 if (buffer==0) return 0;
1067 try {
1068 return &(buffer->getDoubleRef(datarefID));
1069 } catch (...) {
1070 Connection::get(*buffer).handleException();
1071 return 0;
1072 }
1073}
1074
1075//------------------------------------------------------------------------------
1076
1077extern "C"
1078ssize_t xplra_multi_set_float_array(int bufferID, size_t datarefID,
1079 const float* value, size_t length,
1080 size_t offset)
1081{
1082 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1083 if (buffer==0) return -1;
1084 try {
1085 return buffer->setFloatArray(datarefID, value, length, offset);
1086 } catch (...) {
1087 Connection::get(*buffer).handleException();
1088 return -1;
1089 }
1090}
1091
1092/*----------------------------------------------------------------------------*/
1093
1094extern "C"
1095ssize_t xplra_multi_get_float_array(float* value,
1096 size_t length, size_t offset,
1097 int bufferID, size_t datarefID)
1098{
1099 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1100 if (buffer==0) return -1;
1101 try {
1102 return buffer->getFloatArray(datarefID, value, length, offset);
1103 } catch (...) {
1104 Connection::get(*buffer).handleException();
1105 return -1;
1106 }
1107}
1108
1109/*----------------------------------------------------------------------------*/
1110
1111extern "C"
1112const float* xplra_multi_get_float_array_ptr(int bufferID, size_t datarefID,
1113 size_t offset)
1114{
1115 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1116 if (buffer==0) return 0;
1117 try {
1118 return buffer->getFloatArray(datarefID, offset);
1119 } catch (...) {
1120 Connection::get(*buffer).handleException();
1121 return 0;
1122 }
1123}
1124
1125//------------------------------------------------------------------------------
1126
1127extern "C"
1128ssize_t xplra_multi_set_int_array(int bufferID, size_t datarefID,
1129 const int32_t* value, size_t length,
1130 size_t offset)
1131{
1132 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1133 if (buffer==0) return -1;
1134 try {
1135 return buffer->setIntArray(datarefID, value, length, offset);
1136 } catch (...) {
1137 Connection::get(*buffer).handleException();
1138 return -1;
1139 }
1140}
1141
1142/*----------------------------------------------------------------------------*/
1143
1144extern "C"
1145ssize_t xplra_multi_get_int_array(int32_t* value,
1146 size_t length, size_t offset,
1147 int bufferID, size_t datarefID)
1148{
1149 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1150 if (buffer==0) return -1;
1151 try {
1152 return buffer->getIntArray(datarefID, value, length, offset);
1153 } catch (...) {
1154 Connection::get(*buffer).handleException();
1155 return -1;
1156 }
1157}
1158
1159/*----------------------------------------------------------------------------*/
1160
1161extern "C"
1162const int32_t* xplra_multi_get_int_array_ptr(int bufferID, size_t datarefID,
1163 size_t offset)
1164{
1165 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1166 if (buffer==0) return 0;
1167 try {
1168 return buffer->getIntArray(datarefID, offset);
1169 } catch (...) {
1170 Connection::get(*buffer).handleException();
1171 return 0;
1172 }
1173}
1174
1175//------------------------------------------------------------------------------
1176
1177extern "C"
1178ssize_t xplra_multi_set_byte_array(int bufferID, size_t datarefID,
1179 const void* value, size_t length,
1180 size_t offset)
1181{
1182 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1183 if (buffer==0) return -1;
1184 try {
1185 return buffer->setByteArray(datarefID,
1186 reinterpret_cast<const uint8_t*>(value),
1187 length, offset);
1188 } catch (...) {
1189 Connection::get(*buffer).handleException();
1190 return -1;
1191 }
1192}
1193
1194/*----------------------------------------------------------------------------*/
1195
1196extern "C"
1197ssize_t xplra_multi_get_byte_array(void* value,
1198 size_t length, size_t offset,
1199 int bufferID, size_t datarefID)
1200{
1201 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1202 if (buffer==0) return -1;
1203 try {
1204 return buffer->getByteArray(datarefID,
1205 reinterpret_cast<uint8_t*>(value),
1206 length, offset);
1207 } catch (...) {
1208 Connection::get(*buffer).handleException();
1209 return -1;
1210 }
1211}
1212
1213/*----------------------------------------------------------------------------*/
1214
1215extern "C"
1216const uint8_t* xplra_multi_get_byte_array_ptr(int bufferID, size_t datarefID,
1217 size_t offset)
1218{
1219 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1220 if (buffer==0) return 0;
1221 try {
1222 return buffer->getByteArray(datarefID, offset);
1223 } catch (...) {
1224 Connection::get(*buffer).handleException();
1225 return 0;
1226 }
1227}
1228
1229//------------------------------------------------------------------------------
1230
1231extern "C" ssize_t xplra_multi_set_string(int bufferID, size_t datarefID,
1232 const char* value, size_t offset)
1233{
1234 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1235 if (buffer==0) return -1;
1236 try {
1237 return buffer->setString(datarefID, value, offset);
1238 } catch (...) {
1239 Connection::get(*buffer).handleException();
1240 return -1;
1241 }
1242}
1243
1244/*----------------------------------------------------------------------------*/
1245
1246extern "C"
1247const char* xplra_multi_get_string_ptr(int bufferID, size_t datarefID,
1248 size_t offset)
1249{
1250 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1251 if (buffer==0) return 0;
1252 try {
1253 return buffer->getStringPtr(datarefID, offset);
1254 } catch (...) {
1255 Connection::get(*buffer).handleException();
1256 return 0;
1257 }
1258}
1259
1260//------------------------------------------------------------------------------
1261
1262extern "C" int xplra_multi_destroy_buffer(int connectionID, int bufferID)
1263{
1264 Connection* connection = ConnectionSlot::getValue(connectionID);
1265 if (connection==0) return -1;
1266
1267 try {
1268 return connection->destroyMultiBuffer(bufferID) ? 0 : -1;
1269 } catch(...) {
1270 connection->handleException();
1271 return -1;
1272 }
1273}
1274
1275//------------------------------------------------------------------------------
1276//------------------------------------------------------------------------------
1277
1278extern "C" int xplra_disconnect(int connectionID)
1279{
1280 Connection* connection = ConnectionSlot::getValue(connectionID);
1281 if (connection==0) return -1;
1282
1283 ConnectionSlot::clearValue(connectionID);
1284 connection->disconnect();
1285 delete connection;
1286
1287 return 0;
1288}
1289
1290//------------------------------------------------------------------------------
1291//------------------------------------------------------------------------------
1292
1293// Local Variables:
1294// mode: C++
1295// c-basic-offset: 4
1296// indent-tabs-mode: nil
1297// End:
Note: See TracBrowser for help on using the repository browser.