source: xplra/src/client/c/hu/varadiistvan/xplra/xplra.cc@ 40:ec5dde8a6ff6

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

Implemented the client support for the new commands and updated the basic test programs with tests showing messages

File size: 38.7 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_reload_plugins(int connectionID)
518{
519 Connection* connection = ConnectionSlot::getValue(connectionID);
520 if (connection==0) return -1;
521 try {
522 connection->reloadPlugins();
523 return 0;
524 } catch (...) {
525 connection->handleException();
526 return -1;
527 }
528}
529
530//------------------------------------------------------------------------------
531
532extern "C" int xplra_get_int(int* value, int connectionID, const char* name)
533{
534 Connection* connection = ConnectionSlot::getValue(connectionID);
535 if (connection==0) return -1;
536 try {
537 *value = connection->getInt(name);
538 return 0;
539 } catch (...) {
540 connection->handleException();
541 return -1;
542 }
543}
544
545//------------------------------------------------------------------------------
546
547extern "C" int xplra_get_float(float* value, int connectionID, const char* name)
548{
549 Connection* connection = ConnectionSlot::getValue(connectionID);
550 if (connection==0) return -1;
551 try {
552 *value = connection->getFloat(name);
553 return 0;
554 } catch (...) {
555 connection->handleException();
556 return -1;
557 }
558}
559
560//------------------------------------------------------------------------------
561
562extern "C" int xplra_get_double(double* value,
563 int connectionID, const char* name)
564{
565 Connection* connection = ConnectionSlot::getValue(connectionID);
566 if (connection==0) return -1;
567 try {
568 *value = connection->getDouble(name);
569 return 0;
570 } catch (...) {
571 connection->handleException();
572 return -1;
573 }
574}
575
576//------------------------------------------------------------------------------
577
578extern "C" ssize_t xplra_get_float_array(float* dest, size_t length, size_t offset,
579 int connectionID, const char* name)
580{
581 Connection* connection = ConnectionSlot::getValue(connectionID);
582 if (connection==0) return -1;
583 try {
584 return connection->getFloatArray(name, dest, length, offset);
585 } catch (...) {
586 connection->handleException();
587 return -1;
588 }
589}
590
591/*----------------------------------------------------------------------------*/
592
593extern "C" float* xplra_get_float_array_new(size_t* length, size_t offset,
594 int connectionID, const char* name)
595{
596 Connection* connection = ConnectionSlot::getValue(connectionID);
597 if (connection==0) return 0;
598 try {
599 return connection->getFloatArray(name, *length, offset);
600 } catch (...) {
601 connection->handleException();
602 return 0;
603 }
604}
605
606//------------------------------------------------------------------------------
607
608extern "C" ssize_t xplra_get_int_array(int32_t* dest, size_t length, size_t offset,
609 int connectionID, const char* name)
610{
611 Connection* connection = ConnectionSlot::getValue(connectionID);
612 if (connection==0) return -1;
613 try {
614 return connection->getIntArray(name, dest, length, offset);
615 } catch (...) {
616 connection->handleException();
617 return -1;
618 }
619}
620
621/*----------------------------------------------------------------------------*/
622
623extern "C" int32_t* xplra_get_int_array_new(size_t* length, size_t offset,
624 int connectionID, const char* name)
625{
626 Connection* connection = ConnectionSlot::getValue(connectionID);
627 if (connection==0) return 0;
628 try {
629 return connection->getIntArray(name, *length, offset);
630 } catch (...) {
631 connection->handleException();
632 return 0;
633 }
634}
635
636//------------------------------------------------------------------------------
637
638extern "C" ssize_t xplra_get_byte_array(void* dest, size_t length, size_t offset,
639 int connectionID, const char* name)
640{
641 Connection* connection = ConnectionSlot::getValue(connectionID);
642 if (connection==0) return -1;
643 try {
644 return connection->getByteArray(name, reinterpret_cast<uint8_t*>(dest),
645 length, offset);
646 } catch (...) {
647 connection->handleException();
648 return -1;
649 }
650}
651
652/*----------------------------------------------------------------------------*/
653
654extern "C" uint8_t* xplra_get_byte_array_new(size_t* length, size_t offset,
655 int connectionID, const char* name)
656{
657 Connection* connection = ConnectionSlot::getValue(connectionID);
658 if (connection==0) return 0;
659 try {
660 return connection->getByteArray(name, *length, offset);
661 } catch (...) {
662 connection->handleException();
663 return 0;
664 }
665}
666
667//------------------------------------------------------------------------------
668
669extern "C" int xplra_set_int(int connectionID, const char* name, int value)
670{
671 Connection* connection = ConnectionSlot::getValue(connectionID);
672 if (connection==0) return 0;
673 try {
674 connection->setInt(name, value);
675 return 0;
676 } catch (...) {
677 connection->handleException();
678 return -1;
679 }
680}
681
682//------------------------------------------------------------------------------
683
684extern "C" int xplra_set_float(int connectionID, const char* name, float value)
685{
686 Connection* connection = ConnectionSlot::getValue(connectionID);
687 if (connection==0) return 0;
688 try {
689 connection->setFloat(name, value);
690 return 0;
691 } catch (...) {
692 connection->handleException();
693 return -1;
694 }
695}
696
697//------------------------------------------------------------------------------
698
699extern "C" int xplra_set_double(int connectionID, const char* name,
700 double value)
701{
702 Connection* connection = ConnectionSlot::getValue(connectionID);
703 if (connection==0) return 0;
704 try {
705 connection->setDouble(name, value);
706 return 0;
707 } catch (...) {
708 connection->handleException();
709 return -1;
710 }
711}
712
713/*----------------------------------------------------------------------------*/
714
715extern "C" int xplra_set_float_array(int connectionID, const char* name,
716 const float* values,
717 size_t length, size_t offset)
718{
719 Connection* connection = ConnectionSlot::getValue(connectionID);
720 if (connection==0) return 0;
721 try {
722 connection->setFloatArray(name, values, length, offset);
723 return 0;
724 } catch (...) {
725 connection->handleException();
726 return -1;
727 }
728}
729
730/*----------------------------------------------------------------------------*/
731
732extern "C" int xplra_set_int_array(int connectionID, const char* name,
733 const int32_t* values,
734 size_t length, size_t offset)
735{
736 Connection* connection = ConnectionSlot::getValue(connectionID);
737 if (connection==0) return 0;
738 try {
739 connection->setIntArray(name, values, length, offset);
740 return 0;
741 } catch (...) {
742 connection->handleException();
743 return -1;
744 }
745}
746
747/*----------------------------------------------------------------------------*/
748
749extern "C" int xplra_set_byte_array(int connectionID, const char* name,
750 const void* values,
751 size_t length, size_t offset)
752{
753 Connection* connection = ConnectionSlot::getValue(connectionID);
754 if (connection==0) return 0;
755 try {
756 connection->setByteArray(name, reinterpret_cast<const uint8_t*>(values),
757 length, offset);
758 return 0;
759 } catch (...) {
760 connection->handleException();
761 return -1;
762 }
763}
764
765/*----------------------------------------------------------------------------*/
766
767extern "C" int xplra_set_string(int connectionID, const char* name,
768 const char* value,
769 size_t length, size_t offset)
770{
771 Connection* connection = ConnectionSlot::getValue(connectionID);
772 if (connection==0) return 0;
773 try {
774 connection->setString(name, value, length, offset);
775 return 0;
776 } catch (...) {
777 connection->handleException();
778 return -1;
779 }
780}
781
782//------------------------------------------------------------------------------
783//------------------------------------------------------------------------------
784
785extern "C" int xplra_multi_create_getter(int connectionID)
786{
787 Connection* connection = ConnectionSlot::getValue(connectionID);
788 return (connection==0) ? -1 : connection->createMultiGetter();
789}
790
791//------------------------------------------------------------------------------
792
793extern "C" int xplra_multi_create_setter(int connectionID)
794{
795 Connection* connection = ConnectionSlot::getValue(connectionID);
796 return (connection==0) ? -1 : connection->createMultiSetter();
797}
798
799//------------------------------------------------------------------------------
800
801extern "C" size_t xplra_multi_add_int(int bufferID, const char* name)
802{
803 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
804 return (buffer==0) ? INVALID_DATAREF_ID : buffer->addInt(name);
805}
806
807//------------------------------------------------------------------------------
808
809extern "C" size_t xplra_multi_add_float(int bufferID, const char* name)
810{
811 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
812 return (buffer==0) ? INVALID_DATAREF_ID : buffer->addFloat(name);
813}
814
815//------------------------------------------------------------------------------
816
817extern "C" size_t xplra_multi_add_double(int bufferID, const char* name)
818{
819 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
820 return (buffer==0) ? INVALID_DATAREF_ID : buffer->addDouble(name);
821}
822
823/*----------------------------------------------------------------------------*/
824
825extern "C" size_t xplra_multi_add_float_array(int bufferID, const char* name,
826 size_t length, size_t offset)
827{
828 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
829 return (buffer==0) ?
830 INVALID_DATAREF_ID : buffer->addFloatArray(name, length, offset);
831}
832
833/*----------------------------------------------------------------------------*/
834
835extern "C" size_t xplra_multi_add_int_array(int bufferID, const char* name,
836 size_t length, size_t offset)
837{
838 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
839 return (buffer==0) ?
840 INVALID_DATAREF_ID : buffer->addIntArray(name, length, offset);
841}
842
843/*----------------------------------------------------------------------------*/
844
845extern "C" size_t xplra_multi_add_byte_array(int bufferID, const char* name,
846 size_t length, size_t offset)
847{
848 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
849 return (buffer==0) ?
850 INVALID_DATAREF_ID : buffer->addByteArray(name, length, offset);
851}
852
853/*----------------------------------------------------------------------------*/
854
855extern "C" int xplra_multi_finalize(int bufferID)
856{
857 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
858 return (buffer==0) ? -1 : (buffer->finalize() ? 1 : 0);
859}
860
861/*----------------------------------------------------------------------------*/
862
863extern "C" int xplra_multi_register(int bufferID)
864{
865 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
866 if (buffer==0) return -1;
867 try {
868 buffer->registerInXPlane();
869 return 0;
870 } catch (...) {
871 Connection::get(*buffer).handleException();
872 return -1;
873 }
874}
875
876/*----------------------------------------------------------------------------*/
877
878extern "C" int xplra_multi_unregister(int bufferID)
879{
880 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
881 if (buffer==0) return -1;
882 try {
883 buffer->unregisterFromXPlane();
884 return 0;
885 } catch (...) {
886 Connection::get(*buffer).handleException();
887 return -1;
888 }
889}
890
891/*----------------------------------------------------------------------------*/
892
893extern "C" int xplra_multi_execute(int bufferID)
894{
895 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
896 if (buffer==0) return -1;
897 try {
898 buffer->execute();
899 return 0;
900 } catch (...) {
901 Connection::get(*buffer).handleException();
902 return -1;
903 }
904}
905
906//------------------------------------------------------------------------------
907
908extern "C" int xplra_multi_set_int(int bufferID, size_t datarefID, int value)
909{
910 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
911 if (buffer==0) return -1;
912 try {
913 buffer->setInt(datarefID, value);
914 return 0;
915 } catch (...) {
916 Connection::get(*buffer).handleException();
917 return -1;
918 }
919}
920
921/*----------------------------------------------------------------------------*/
922
923extern "C" int xplra_multi_get_int(int* dest, int bufferID, size_t datarefID)
924{
925 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
926 if (buffer==0) return -1;
927 try {
928 *dest = buffer->getInt(datarefID);
929 return 0;
930 } catch (...) {
931 Connection::get(*buffer).handleException();
932 return -1;
933 }
934}
935
936/*----------------------------------------------------------------------------*/
937
938extern "C"
939const int32_t* xplra_multi_get_int_const_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"
954int32_t* xplra_multi_get_int_ptr(int bufferID, size_t datarefID)
955{
956 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
957 if (buffer==0) return 0;
958 try {
959 return &(buffer->getIntRef(datarefID));
960 } catch (...) {
961 Connection::get(*buffer).handleException();
962 return 0;
963 }
964}
965
966//------------------------------------------------------------------------------
967
968extern "C"
969int xplra_multi_set_float(int bufferID, size_t datarefID, float value)
970{
971 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
972 if (buffer==0) return -1;
973 try {
974 buffer->setFloat(datarefID, value);
975 return 0;
976 } catch (...) {
977 Connection::get(*buffer).handleException();
978 return -1;
979 }
980}
981
982/*----------------------------------------------------------------------------*/
983
984extern "C"
985int xplra_multi_get_float(float* dest, int bufferID, size_t datarefID)
986{
987 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
988 if (buffer==0) return -1;
989 try {
990 *dest = buffer->getFloat(datarefID);
991 return 0;
992 } catch (...) {
993 Connection::get(*buffer).handleException();
994 return -1;
995 }
996}
997
998/*----------------------------------------------------------------------------*/
999
1000extern "C"
1001const float* xplra_multi_get_float_const_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"
1016float* xplra_multi_get_float_ptr(int bufferID, size_t datarefID)
1017{
1018 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1019 if (buffer==0) return 0;
1020 try {
1021 return &(buffer->getFloatRef(datarefID));
1022 } catch (...) {
1023 Connection::get(*buffer).handleException();
1024 return 0;
1025 }
1026}
1027
1028//------------------------------------------------------------------------------
1029
1030extern "C"
1031int xplra_multi_set_double(int bufferID, size_t datarefID, double value)
1032{
1033 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1034 if (buffer==0) return -1;
1035 try {
1036 buffer->setDouble(datarefID, value);
1037 return 0;
1038 } catch (...) {
1039 Connection::get(*buffer).handleException();
1040 return -1;
1041 }
1042}
1043
1044/*----------------------------------------------------------------------------*/
1045
1046extern "C"
1047int xplra_multi_get_double(double* dest, int bufferID, size_t datarefID)
1048{
1049 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1050 if (buffer==0) return -1;
1051 try {
1052 *dest = buffer->getDouble(datarefID);
1053 return 0;
1054 } catch (...) {
1055 Connection::get(*buffer).handleException();
1056 return -1;
1057 }
1058}
1059
1060/*----------------------------------------------------------------------------*/
1061
1062extern "C"
1063const double* xplra_multi_get_double_const_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"
1078double* xplra_multi_get_double_ptr(int bufferID, size_t datarefID)
1079{
1080 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1081 if (buffer==0) return 0;
1082 try {
1083 return &(buffer->getDoubleRef(datarefID));
1084 } catch (...) {
1085 Connection::get(*buffer).handleException();
1086 return 0;
1087 }
1088}
1089
1090//------------------------------------------------------------------------------
1091
1092extern "C"
1093ssize_t xplra_multi_set_float_array(int bufferID, size_t datarefID,
1094 const float* value, size_t length,
1095 size_t offset)
1096{
1097 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1098 if (buffer==0) return -1;
1099 try {
1100 return buffer->setFloatArray(datarefID, value, length, offset);
1101 } catch (...) {
1102 Connection::get(*buffer).handleException();
1103 return -1;
1104 }
1105}
1106
1107/*----------------------------------------------------------------------------*/
1108
1109extern "C"
1110ssize_t xplra_multi_get_float_array(float* value,
1111 size_t length, size_t offset,
1112 int bufferID, size_t datarefID)
1113{
1114 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1115 if (buffer==0) return -1;
1116 try {
1117 return buffer->getFloatArray(datarefID, value, length, offset);
1118 } catch (...) {
1119 Connection::get(*buffer).handleException();
1120 return -1;
1121 }
1122}
1123
1124/*----------------------------------------------------------------------------*/
1125
1126extern "C"
1127const float* xplra_multi_get_float_array_ptr(int bufferID, size_t datarefID,
1128 size_t offset)
1129{
1130 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1131 if (buffer==0) return 0;
1132 try {
1133 return buffer->getFloatArray(datarefID, offset);
1134 } catch (...) {
1135 Connection::get(*buffer).handleException();
1136 return 0;
1137 }
1138}
1139
1140//------------------------------------------------------------------------------
1141
1142extern "C"
1143ssize_t xplra_multi_set_int_array(int bufferID, size_t datarefID,
1144 const int32_t* value, size_t length,
1145 size_t offset)
1146{
1147 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1148 if (buffer==0) return -1;
1149 try {
1150 return buffer->setIntArray(datarefID, value, length, offset);
1151 } catch (...) {
1152 Connection::get(*buffer).handleException();
1153 return -1;
1154 }
1155}
1156
1157/*----------------------------------------------------------------------------*/
1158
1159extern "C"
1160ssize_t xplra_multi_get_int_array(int32_t* value,
1161 size_t length, size_t offset,
1162 int bufferID, size_t datarefID)
1163{
1164 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1165 if (buffer==0) return -1;
1166 try {
1167 return buffer->getIntArray(datarefID, value, length, offset);
1168 } catch (...) {
1169 Connection::get(*buffer).handleException();
1170 return -1;
1171 }
1172}
1173
1174/*----------------------------------------------------------------------------*/
1175
1176extern "C"
1177const int32_t* xplra_multi_get_int_array_ptr(int bufferID, size_t datarefID,
1178 size_t offset)
1179{
1180 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1181 if (buffer==0) return 0;
1182 try {
1183 return buffer->getIntArray(datarefID, offset);
1184 } catch (...) {
1185 Connection::get(*buffer).handleException();
1186 return 0;
1187 }
1188}
1189
1190//------------------------------------------------------------------------------
1191
1192extern "C"
1193ssize_t xplra_multi_set_byte_array(int bufferID, size_t datarefID,
1194 const void* value, size_t length,
1195 size_t offset)
1196{
1197 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1198 if (buffer==0) return -1;
1199 try {
1200 return buffer->setByteArray(datarefID,
1201 reinterpret_cast<const uint8_t*>(value),
1202 length, offset);
1203 } catch (...) {
1204 Connection::get(*buffer).handleException();
1205 return -1;
1206 }
1207}
1208
1209/*----------------------------------------------------------------------------*/
1210
1211extern "C"
1212ssize_t xplra_multi_get_byte_array(void* value,
1213 size_t length, size_t offset,
1214 int bufferID, size_t datarefID)
1215{
1216 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1217 if (buffer==0) return -1;
1218 try {
1219 return buffer->getByteArray(datarefID,
1220 reinterpret_cast<uint8_t*>(value),
1221 length, offset);
1222 } catch (...) {
1223 Connection::get(*buffer).handleException();
1224 return -1;
1225 }
1226}
1227
1228/*----------------------------------------------------------------------------*/
1229
1230extern "C"
1231const uint8_t* xplra_multi_get_byte_array_ptr(int bufferID, size_t datarefID,
1232 size_t offset)
1233{
1234 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1235 if (buffer==0) return 0;
1236 try {
1237 return buffer->getByteArray(datarefID, offset);
1238 } catch (...) {
1239 Connection::get(*buffer).handleException();
1240 return 0;
1241 }
1242}
1243
1244//------------------------------------------------------------------------------
1245
1246extern "C" ssize_t xplra_multi_set_string(int bufferID, size_t datarefID,
1247 const char* value, size_t offset)
1248{
1249 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1250 if (buffer==0) return -1;
1251 try {
1252 return buffer->setString(datarefID, value, offset);
1253 } catch (...) {
1254 Connection::get(*buffer).handleException();
1255 return -1;
1256 }
1257}
1258
1259/*----------------------------------------------------------------------------*/
1260
1261extern "C"
1262const char* xplra_multi_get_string_ptr(int bufferID, size_t datarefID,
1263 size_t offset)
1264{
1265 MultiBuffer* buffer = MultiBufferSlot::getValue(bufferID);
1266 if (buffer==0) return 0;
1267 try {
1268 return buffer->getStringPtr(datarefID, offset);
1269 } catch (...) {
1270 Connection::get(*buffer).handleException();
1271 return 0;
1272 }
1273}
1274
1275//------------------------------------------------------------------------------
1276
1277extern "C" int xplra_multi_destroy_buffer(int connectionID, int bufferID)
1278{
1279 Connection* connection = ConnectionSlot::getValue(connectionID);
1280 if (connection==0) return -1;
1281
1282 try {
1283 return connection->destroyMultiBuffer(bufferID) ? 0 : -1;
1284 } catch(...) {
1285 connection->handleException();
1286 return -1;
1287 }
1288}
1289
1290//------------------------------------------------------------------------------
1291//------------------------------------------------------------------------------
1292
1293extern "C" int xplra_show_message(int connectionID,
1294 const char* message, float duration)
1295{
1296 Connection* connection = ConnectionSlot::getValue(connectionID);
1297 if (connection==0) return -1;
1298
1299 try {
1300 connection->showMessage(message, duration);
1301 return 0;
1302 } catch(...) {
1303 connection->handleException();
1304 return -1;
1305 }
1306}
1307
1308//------------------------------------------------------------------------------
1309//------------------------------------------------------------------------------
1310
1311extern "C" int xplra_disconnect(int connectionID)
1312{
1313 Connection* connection = ConnectionSlot::getValue(connectionID);
1314 if (connection==0) return -1;
1315
1316 ConnectionSlot::clearValue(connectionID);
1317 connection->disconnect();
1318 delete connection;
1319
1320 return 0;
1321}
1322
1323//------------------------------------------------------------------------------
1324//------------------------------------------------------------------------------
1325
1326// Local Variables:
1327// mode: C++
1328// c-basic-offset: 4
1329// indent-tabs-mode: nil
1330// End:
Note: See TracBrowser for help on using the repository browser.