Changeset 14:bb6484eceb1e in xplra for src
- Timestamp:
- 01/27/13 13:26:56 (12 years ago)
- Branch:
- default
- Phase:
- public
- Location:
- src/client/c/hu/varadiistvan/xplra
- Files:
-
- 4 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/client/c/hu/varadiistvan/xplra/Makefile.am
r13 r14 1 AM_CXXFLAGS=$(VSCPL_CFLAGS) -I$(top_srcdir)/src/plugin/src 1 2 2 3 lib_LTLIBRARIES=libxplra.la 3 4 4 5 libxplra_la_SOURCES=\ 5 XPlane.cc 6 XPlane.cc \ 7 Exception.cc \ 8 xplra.cc 9 10 libxplra_la_LIBADD=$(VSCPL_LIBS) 6 11 7 12 if TARGET_API_WIN32 … … 11 16 include_xplradir=$(includedir)/hu/varadiistvan/xplra 12 17 include_xplra_HEADERS=\ 13 XPlane.h 18 XPlane.h \ 19 Exception.h \ 20 xplra.h -
src/client/c/hu/varadiistvan/xplra/XPlane.cc
r13 r14 31 31 #include "XPlane.h" 32 32 33 #include <hu/varadiistvan/scpl/io/LocalClientSocket.h> 34 #include <hu/varadiistvan/scpl/io/DataStream.h> 35 36 #include <xplra/Protocol.h> 37 38 #include <memory> 39 33 40 //------------------------------------------------------------------------------ 34 41 35 42 using hu::varadiistvan::xplra::XPlane; 36 43 44 using hu::varadiistvan::scpl::io::LocalClientSocket; 45 using hu::varadiistvan::scpl::io::LocalConnector; 46 using hu::varadiistvan::scpl::io::DataStream; 47 48 using xplra::Protocol; 49 50 using std::auto_ptr; 51 37 52 //------------------------------------------------------------------------------ 53 54 inline void XPlane::checkStream() throw(NotConnectedException, IOException) 55 { 56 if (stream==0) { 57 throw NotConnectedException(); 58 } else if (stream->failed()) { 59 throw IOException(stream->getErrorCode()); 60 } 61 } 62 63 //------------------------------------------------------------------------------ 64 65 void XPlane::checkResult(uint8_t result) throw(ProtocolException) 66 { 67 switch(result) { 68 case Protocol::RESULT_OK: 69 return; 70 case Protocol::RESULT_INVALID_COMMAND: 71 throw ProtocolException(ProtocolException::INVALID_COMMAND); 72 case Protocol::RESULT_UNKNOWN_DATAREF: 73 throw ProtocolException(ProtocolException::UNKNOWN_DATAREF); 74 case Protocol::RESULT_INVALID_TYPE: 75 throw ProtocolException(ProtocolException::INVALID_TYPE); 76 case Protocol::RESULT_INVALID_LENGTH: 77 throw ProtocolException(ProtocolException::INVALID_LENGTH); 78 case Protocol::RESULT_INVALID_OFFSET: 79 throw ProtocolException(ProtocolException::INVALID_OFFSET); 80 case Protocol::RESULT_INVALID_COUNT: 81 throw ProtocolException(ProtocolException::INVALID_COUNT); 82 case Protocol::RESULT_INVALID_ID: 83 throw ProtocolException(ProtocolException::INVALID_ID); 84 case Protocol::RESULT_OTHER_ERROR: 85 default: 86 throw ProtocolException(ProtocolException::OTHER); 87 } 88 } 89 90 //------------------------------------------------------------------------------ 91 //------------------------------------------------------------------------------ 92 93 XPlane::~XPlane() throw() 94 { 95 disconnect(); 96 } 97 98 //------------------------------------------------------------------------------ 99 100 void XPlane::connect() throw(IOException) 101 { 102 if (socket!=0) return; 103 104 auto_ptr<LocalClientSocket> clientSocket(new LocalClientSocket("xplra", 105 &waiter)); 106 LocalConnector& connector = clientSocket->getConnector(); 107 108 while (!connector.connect()) { 109 if (connector.failed()) { 110 throw IOException(connector.getErrorCode()); 111 } 112 waiter.wait(); 113 if (waiter.failed()) { 114 throw IOException(waiter.getErrorCode()); 115 } 116 } 117 118 socket = clientSocket.release(); 119 stream = new DataStream(*socket); 120 } 121 122 //------------------------------------------------------------------------------ 123 124 void XPlane::disconnect() throw() 125 { 126 if (socket==0) return; 127 128 delete stream; stream = 0; 129 delete socket; socket = 0; 130 } 131 132 //------------------------------------------------------------------------------ 133 134 int XPlane::getInt(const char* name) throw(Exception) 135 { 136 checkStream(); 137 138 stream->writeU8(Protocol::COMMAND_GET_SINGLE); 139 stream->writeString(name, strlen(name)); 140 stream->writeU8(Protocol::TYPE_INT); 141 if (!stream->flush()) checkStream(); 142 143 uint8_t result = stream->readU8(); 144 checkStream(); 145 checkResult(result); 146 147 int value = stream->readS32(); 148 checkStream(); 149 return value; 150 } 151 38 152 //------------------------------------------------------------------------------ 39 153 -
src/client/c/hu/varadiistvan/xplra/XPlane.h
r13 r14 29 29 #ifndef HU_VARADIISTVAN_XPLRA_XPLANE_H 30 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 <inttypes.h> 38 39 //----------------------------------------------------------------------------- 40 41 namespace hu { namespace varadiistvan { namespace scpl { namespace io { 42 43 class LocalClientSocket; 44 class DataStream; 45 46 } /* namespace hu::varadiistvan::scpl::io */ } /* namespace hu::varadiistvan::scpl */ } /* namespace hu::varadiistvan */ } /* namespace hu */ 47 31 48 //------------------------------------------------------------------------------ 32 49 … … 35 52 //------------------------------------------------------------------------------ 36 53 54 /** 55 * The main class to access X-Plane. 56 * 57 * The calls are synchronous and not thread-safe. 58 */ 37 59 class XPlane 38 60 { 61 private: 62 /** 63 * The waiter to use. 64 */ 65 scpl::io::Waiter waiter; 66 67 /** 68 * The local client socket over which we are communicating with X-Plane. 69 */ 70 scpl::io::LocalClientSocket* socket; 71 72 /** 73 * The data stream to handle the data conversions. 74 */ 75 scpl::io::DataStream* stream; 76 77 public: 78 /** 79 * Construct the object. It will not be connected to the simulator 80 * yet. 81 */ 82 XPlane() throw(); 83 84 /** 85 * Destroy the object. If connected, the connection will be 86 * closed. 87 */ 88 ~XPlane() throw(); 89 90 /** 91 * Connect to the simulator. 92 */ 93 void connect() throw(IOException); 94 95 /** 96 * Check if we are connected to the simulator. 97 */ 98 bool isConnected() const throw(); 99 100 /** 101 * Disconnect from the simulator. 102 */ 103 void disconnect() throw(); 104 105 /** 106 * Get the integer value of the dataref with the given name. 107 */ 108 int getInt(const char* name) throw(Exception); 109 110 private: 111 /** 112 * Check if we have a stream and if it has not failed. 113 */ 114 void checkStream() throw(NotConnectedException, IOException); 115 116 /** 117 * Check the given protocol result. If it signifies an error, 118 * throw a ProtocolException with the correct error code. 119 */ 120 void checkResult(uint8_t result) throw(ProtocolException); 39 121 }; 122 123 //------------------------------------------------------------------------------ 124 // Inline definitions 125 //------------------------------------------------------------------------------ 126 127 inline XPlane::XPlane() throw() : 128 socket(0), 129 stream(0) 130 { 131 } 132 133 //------------------------------------------------------------------------------ 134 135 inline bool XPlane::isConnected() const throw() 136 { 137 return socket!=0; 138 } 40 139 41 140 //------------------------------------------------------------------------------
Note:
See TracChangeset
for help on using the changeset viewer.