X-Plane Remote Access Plugin and Client Library
XPlane.h
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 #ifndef HU_VARADIISTVAN_XPLRA_XPLANE_H
30 #define HU_VARADIISTVAN_XPLRA_XPLANE_H
31 //-----------------------------------------------------------------------------
32 
33 #include "Exception.h"
34 
35 #include <hu/varadiistvan/scpl/io/Waiter.h>
36 
37 #include <set>
38 #include <memory>
39 
40 #include <inttypes.h>
41 
42 //-----------------------------------------------------------------------------
43 
44 namespace hu { namespace varadiistvan { namespace scpl { namespace io {
45 
46 class BufferedStream;
47 class DataStream;
48 
49 } /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */
50 
51 //------------------------------------------------------------------------------
52 
53 namespace hu { namespace varadiistvan { namespace xplra {
54 
55 //------------------------------------------------------------------------------
56 
57 class MultiBuffer;
58 class MultiGetter;
59 class MultiSetter;
60 
61 //------------------------------------------------------------------------------
62 
68 class XPlane
69 {
70 private:
74  static const unsigned short defaultTCPPort = 0x5852;
75 
79  typedef std::set<MultiBuffer*> multiBuffers_t;
80 
84  scpl::io::Waiter waiter;
85 
89  scpl::io::BufferedStream* socket;
90 
94  scpl::io::DataStream* stream;
95 
100 
101 public:
106  XPlane() noexcept;
107 
113  ~XPlane() noexcept;
114 
118  void connect();
119 
123  void connectTCP(const std::string& address,
124  unsigned short port = defaultTCPPort);
125 
129  bool isConnected() const noexcept;
130 
134  void disconnect() noexcept;
135 
140  MultiGetter& createMultiGetter() noexcept;
141 
146  MultiSetter& createMultiSetter() noexcept;
147 
156  bool destroyMultiBuffer(MultiBuffer& buffer);
157 
161  void getVersions(int& xplaneVersion, int& xplmVersion, int& xplraVersion);
162 
167  void reloadPlugins();
168 
173  void saveSituation(const char* path);
174 
178  int getInt(const char* name);
179 
183  float getFloat(const char* name);
184 
188  double getDouble(const char* name);
189 
200  size_t getFloatArray(const char* name, float* dest,
201  size_t length, size_t offset = 0);
202 
211  float* getFloatArray(const char* name, size_t& length,
212  size_t offset = 0);
213 
224  size_t getIntArray(const char* name, int32_t* dest,
225  size_t length, size_t offset = 0);
226 
235  int32_t* getIntArray(const char* name, size_t& length,
236  size_t offset = 0);
237 
248  size_t getByteArray(const char* name, uint8_t* dest,
249  size_t length, size_t offset = 0);
250 
259  uint8_t* getByteArray(const char* name, size_t& length,
260  size_t offset = 0);
261 
265  std::string getString(const char* name, size_t offset = 0);
266 
270  void setInt(const char* name, int value);
271 
275  void setFloat(const char* name, float value);
276 
280  void setDouble(const char* name, double value);
281 
285  void setFloatArray(const char* name, const float* values, size_t length,
286  size_t offset = 0);
287 
291  void setIntArray(const char* name, const int32_t* values, size_t length,
292  size_t offset = 0);
293 
297  void setByteArray(const char* name, const uint8_t* values, size_t length,
298  size_t offset = 0);
299 
306  void setString(const char* name, const char* value, size_t length,
307  size_t offset = 0);
308 
312  void showMessage(const char* message, float duration);
313 
318  void registerHotkeys(const uint16_t* codes, size_t length);
319 
323  void queryHotkeys(uint8_t* states, size_t length);
324 
328  void unregisterHotkeys();
329 
330 private:
334  void checkStream();
335 
340  void checkResult(uint8_t result, bool hasParameter = false,
341  long parameter = 0);
342 
348  void checkResult(bool multi = false);
349 
354  void getScalar(const char* name, uint8_t type);
355 
363  size_t getArray(const char* name, uint8_t type,
364  ssize_t length, size_t offset);
365 
366 
370  void setScalar(const char* name, uint8_t type);
371 
375  void setArray(const char* name, uint8_t type, size_t length,
376  size_t offset);
377 
381  template <class ClientSocket>
382  void connect(std::unique_ptr<ClientSocket> clientSocket);
383 
384  friend class MultiBuffer;
385  friend class MultiGetter;
386  friend class MultiSetter;
387 };
388 
389 //------------------------------------------------------------------------------
390 // Inline definitions
391 //------------------------------------------------------------------------------
392 
393 inline XPlane::XPlane() noexcept :
394  socket(0),
395  stream(0)
396 {
397 }
398 
399 //------------------------------------------------------------------------------
400 
401 inline bool XPlane::isConnected() const noexcept
402 {
403  return socket!=0;
404 }
405 
406 //------------------------------------------------------------------------------
407 
408 } /* namespace hu::varadiistvan::xplra */ } /* namespace hu::varadiistvan */ } /* namespace hu */
409 
410 //------------------------------------------------------------------------------
411 #endif // HU_VARADIISTVAN_XPLRA_XPLANE_H
412 
413 // Local Variables:
414 // mode: C++
415 // c-basic-offset: 4
416 // indent-tabs-mode: nil
417 // End:
void setArray(const char *name, uint8_t type, size_t length, size_t offset)
Definition: XPlane.cc:470
void registerHotkeys(const uint16_t *codes, size_t length)
Definition: XPlane.cc:543
std::set< MultiBuffer * > multiBuffers_t
Definition: XPlane.h:79
int getInt(const char *name)
Definition: XPlane.cc:295
size_t getIntArray(const char *name, int32_t *dest, size_t length, size_t offset=0)
Definition: XPlane.cc:374
void getScalar(const char *name, uint8_t type)
Definition: XPlane.cc:281
size_t getByteArray(const char *name, uint8_t *dest, size_t length, size_t offset=0)
Definition: XPlane.cc:398
bool destroyMultiBuffer(MultiBuffer &buffer)
Definition: XPlane.cc:234
void setInt(const char *name, int value)
Definition: XPlane.cc:440
size_t getArray(const char *name, uint8_t type, ssize_t length, size_t offset)
Definition: XPlane.cc:328
float getFloat(const char *name)
Definition: XPlane.cc:306
void disconnect() noexcept
Definition: XPlane.cc:206
multiBuffers_t multiBuffers
Definition: XPlane.h:99
void queryHotkeys(uint8_t *states, size_t length)
Definition: XPlane.cc:555
scpl::io::BufferedStream * socket
Definition: XPlane.h:89
static const unsigned short defaultTCPPort
Definition: XPlane.h:74
MultiGetter & createMultiGetter() noexcept
Definition: XPlane.cc:216
size_t getFloatArray(const char *name, float *dest, size_t length, size_t offset=0)
Definition: XPlane.cc:350
void showMessage(const char *message, float duration)
Definition: XPlane.cc:532
void setDouble(const char *name, double value)
Definition: XPlane.cc:460
void setFloatArray(const char *name, const float *values, size_t length, size_t offset=0)
Definition: XPlane.cc:482
scpl::io::DataStream * stream
Definition: XPlane.h:94
void saveSituation(const char *path)
Definition: XPlane.cc:271
void setScalar(const char *name, uint8_t type)
Definition: XPlane.cc:431
scpl::io::Waiter waiter
Definition: XPlane.h:84
void setIntArray(const char *name, const int32_t *values, size_t length, size_t offset=0)
Definition: XPlane.cc:493
void connectTCP(const std::string &address, unsigned short port=defaultTCPPort)
Definition: XPlane.cc:194
void checkResult(uint8_t result, bool hasParameter=false, long parameter=0)
Definition: XPlane.cc:113
void setString(const char *name, const char *value, size_t length, size_t offset=0)
Definition: XPlane.cc:515
void setByteArray(const char *name, const uint8_t *values, size_t length, size_t offset=0)
Definition: XPlane.cc:504
double getDouble(const char *name)
Definition: XPlane.cc:317
void getVersions(int &xplaneVersion, int &xplmVersion, int &xplraVersion)
Definition: XPlane.cc:247
bool isConnected() const noexcept
Definition: XPlane.h:401
MultiSetter & createMultiSetter() noexcept
Definition: XPlane.cc:225
std::string getString(const char *name, size_t offset=0)
Definition: XPlane.cc:422
void setFloat(const char *name, float value)
Definition: XPlane.cc:450
Python client module for the X-Plane Remote Access plugin.
Definition: DataRefTask.h:41