source: xplra/src/client/c/hu/varadiistvan/xplra/XPlane.cc@ 30:d92d686b4d70

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

Fixed problems with detecting a broken connection

File size: 13.9 KB
Line 
1// Copyright (c) 2013 by István Váradi
2
3// This file is part of XPLRA, a remote-access plugin for X-Plane
4
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7
8// 1. Redistributions of source code must retain the above copyright notice, this
9// list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
18// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25// The views and conclusions contained in the software and documentation are those
26// of the authors and should not be interpreted as representing official policies,
27// either expressed or implied, of the FreeBSD Project.
28
29//------------------------------------------------------------------------------
30
31#include "XPlane.h"
32
33#include "MultiGetter.h"
34#include "MultiSetter.h"
35
36#include <hu/varadiistvan/scpl/io/LocalClientSocket.h>
37#include <hu/varadiistvan/scpl/io/DataStream.h>
38
39#include <xplra/Protocol.h>
40
41#include <memory>
42
43#ifndef _WIN32
44#include <signal.h>
45#endif
46
47//------------------------------------------------------------------------------
48
49using hu::varadiistvan::xplra::XPlane;
50using hu::varadiistvan::xplra::MultiGetter;
51using hu::varadiistvan::xplra::MultiSetter;
52
53using hu::varadiistvan::scpl::io::LocalClientSocket;
54using hu::varadiistvan::scpl::io::LocalConnector;
55using hu::varadiistvan::scpl::io::DataStream;
56
57using xplra::Protocol;
58
59using std::auto_ptr;
60using std::string;
61
62//------------------------------------------------------------------------------
63
64void XPlane::checkStream() throw(NotConnectedException, IOException)
65{
66 if (stream==0) {
67 throw NotConnectedException();
68 } else if (stream->failed()) {
69 throw IOException(stream->getErrorCode());
70 }
71}
72
73//------------------------------------------------------------------------------
74
75void XPlane::checkResult(uint8_t result) throw(ProtocolException)
76{
77 switch(result) {
78 case Protocol::RESULT_OK:
79 return;
80 case Protocol::RESULT_INVALID_COMMAND:
81 throw ProtocolException(ProtocolException::INVALID_COMMAND);
82 case Protocol::RESULT_UNKNOWN_DATAREF:
83 throw ProtocolException(ProtocolException::UNKNOWN_DATAREF);
84 case Protocol::RESULT_INVALID_TYPE:
85 throw ProtocolException(ProtocolException::INVALID_TYPE);
86 case Protocol::RESULT_INVALID_LENGTH:
87 throw ProtocolException(ProtocolException::INVALID_LENGTH);
88 case Protocol::RESULT_INVALID_OFFSET:
89 throw ProtocolException(ProtocolException::INVALID_OFFSET);
90 case Protocol::RESULT_INVALID_COUNT:
91 throw ProtocolException(ProtocolException::INVALID_COUNT);
92 case Protocol::RESULT_INVALID_ID:
93 throw ProtocolException(ProtocolException::INVALID_ID);
94 case Protocol::RESULT_OTHER_ERROR:
95 default:
96 throw ProtocolException(ProtocolException::OTHER);
97 }
98}
99
100//------------------------------------------------------------------------------
101
102void XPlane::checkResult() throw(ProtocolException, IOException)
103{
104 uint8_t result = stream->readU8();
105 checkStream();
106 checkResult(result);
107}
108
109//------------------------------------------------------------------------------
110//------------------------------------------------------------------------------
111
112XPlane::~XPlane() throw()
113{
114 disconnect();
115 for(multiBuffers_t::iterator i = multiBuffers.begin();
116 i!=multiBuffers.end(); ++i)
117 {
118 MultiBuffer* buffer = *i;
119 buffer->forgetRegistration();
120 delete buffer;
121 }
122}
123
124//------------------------------------------------------------------------------
125
126void XPlane::connect() throw(IOException)
127{
128 if (socket!=0) return;
129
130 auto_ptr<LocalClientSocket> clientSocket(new LocalClientSocket("xplra",
131 &waiter));
132 LocalConnector& connector = clientSocket->getConnector();
133
134 while (!connector.connect()) {
135 if (connector.failed()) {
136 throw IOException(connector.getErrorCode());
137 }
138 waiter.wait();
139 if (waiter.failed()) {
140 throw IOException(waiter.getErrorCode());
141 }
142 }
143
144#ifndef _WIN32
145 signal(SIGPIPE, SIG_IGN);
146#endif
147
148 socket = clientSocket.release();
149 stream = new DataStream(*socket);
150}
151
152//------------------------------------------------------------------------------
153
154void XPlane::disconnect() throw()
155{
156 if (socket==0) return;
157
158 delete stream; stream = 0;
159 delete socket; socket = 0;
160}
161
162//------------------------------------------------------------------------------
163
164MultiGetter& XPlane::createMultiGetter() throw()
165{
166 MultiGetter* getter = new MultiGetter(*this);
167 multiBuffers.insert(getter);
168 return *getter;
169}
170
171//------------------------------------------------------------------------------
172
173MultiSetter& XPlane::createMultiSetter() throw()
174{
175 MultiSetter* setter = new MultiSetter(*this);
176 multiBuffers.insert(setter);
177 return *setter;
178}
179
180//------------------------------------------------------------------------------
181
182bool XPlane::destroyMultiBuffer(MultiBuffer& buffer) throw(Exception)
183{
184 multiBuffers_t::iterator i = multiBuffers.find(&buffer);
185 if (i==multiBuffers.end()) return false;
186
187 multiBuffers.erase(i);
188 delete &buffer;
189
190 return true;
191}
192
193//------------------------------------------------------------------------------
194
195void XPlane::getScalar(const char* name, uint8_t type) throw(Exception)
196{
197 checkStream();
198
199 stream->writeU8(Protocol::COMMAND_GET_SINGLE);
200 stream->writeString(name, strlen(name));
201 stream->writeU8(type);
202 if (!stream->flush()) checkStream();
203
204 checkResult();
205}
206
207//------------------------------------------------------------------------------
208
209int XPlane::getInt(const char* name) throw(Exception)
210{
211 getScalar(name, Protocol::TYPE_INT);
212
213 int value = stream->readS32();
214 checkStream();
215 return value;
216}
217
218//------------------------------------------------------------------------------
219
220float XPlane::getFloat(const char* name) throw(Exception)
221{
222 getScalar(name, Protocol::TYPE_FLOAT);
223
224 float value = stream->readFloat();
225 checkStream();
226 return value;
227}
228
229//------------------------------------------------------------------------------
230
231double XPlane::getDouble(const char* name) throw(Exception)
232{
233 getScalar(name, Protocol::TYPE_DOUBLE);
234
235 double value = stream->readDouble();
236 checkStream();
237 return value;
238}
239
240//------------------------------------------------------------------------------
241
242size_t XPlane::getArray(const char* name, uint8_t type,
243 ssize_t length, size_t offset) throw(Exception)
244{
245 checkStream();
246
247 stream->writeU8(Protocol::COMMAND_GET_SINGLE);
248 stream->writeString(name, strlen(name));
249 stream->writeU8(type);
250 stream->writeS32(static_cast<int32_t>(length));
251 stream->writeS32(static_cast<int32_t>(offset));
252 if (!stream->flush()) checkStream();
253
254 uint8_t result = stream->readU8();
255 length = stream->readS32();
256 checkStream();
257 checkResult(result);
258
259 return length;
260}
261
262//------------------------------------------------------------------------------
263
264size_t XPlane::getFloatArray(const char* name, float* dest,
265 size_t length, size_t offset) throw(Exception)
266{
267 length = getArray(name, Protocol::TYPE_FLOAT_ARRAY, length, offset);
268
269 if (!stream->read(dest, length*sizeof(float))) checkStream();
270
271 return length;
272}
273
274//------------------------------------------------------------------------------
275
276float* XPlane::getFloatArray(const char* name, size_t& length,
277 size_t offset) throw(Exception)
278{
279 length = getArray(name, Protocol::TYPE_FLOAT_ARRAY, -1, offset);
280
281 auto_ptr<float> data(new float[length]);
282 if (!stream->read(data.get(), length*sizeof(float))) checkStream();
283 return data.release();
284}
285
286//------------------------------------------------------------------------------
287
288size_t XPlane::getIntArray(const char* name, int32_t* dest,
289 size_t length, size_t offset) throw(Exception)
290{
291 length = getArray(name, Protocol::TYPE_INT_ARRAY, length, offset);
292
293 if (!stream->read(dest, length*sizeof(int32_t))) checkStream();
294
295 return length;
296}
297
298//------------------------------------------------------------------------------
299
300int32_t* XPlane::getIntArray(const char* name, size_t& length,
301 size_t offset) throw(Exception)
302{
303 length = getArray(name, Protocol::TYPE_INT_ARRAY, -1, offset);
304
305 auto_ptr<int32_t> data(new int32_t[length]);
306 if (!stream->read(data.get(), length*sizeof(int32_t))) checkStream();
307 return data.release();
308}
309
310//------------------------------------------------------------------------------
311
312size_t XPlane::getByteArray(const char* name, uint8_t* dest,
313 size_t length, size_t offset) throw(Exception)
314{
315 length = getArray(name, Protocol::TYPE_BYTE_ARRAY, length, offset);
316
317 if (!stream->read(dest, length*sizeof(uint8_t))) checkStream();
318
319 return length;
320}
321
322//------------------------------------------------------------------------------
323
324uint8_t* XPlane::getByteArray(const char* name, size_t& length,
325 size_t offset) throw(Exception)
326{
327 length = getArray(name, Protocol::TYPE_BYTE_ARRAY, -1, offset);
328
329 auto_ptr<uint8_t> data(new uint8_t[length]);
330 if (!stream->read(data.get(), length*sizeof(uint8_t))) checkStream();
331 return data.release();
332}
333
334//------------------------------------------------------------------------------
335
336string XPlane::getString(const char* name, size_t offset) throw(Exception)
337{
338 size_t length = 0;
339 auto_ptr<uint8_t> data(getByteArray(name, length, offset));
340 return string(reinterpret_cast<char*>(data.get()));
341}
342
343//------------------------------------------------------------------------------
344
345void XPlane::setScalar(const char* name, uint8_t type) throw(Exception)
346{
347 stream->writeU8(Protocol::COMMAND_SET_SINGLE);
348 stream->writeString(name, strlen(name));
349 stream->writeU8(type);
350}
351
352//------------------------------------------------------------------------------
353
354void XPlane::setInt(const char* name, int value) throw(Exception)
355{
356 setScalar(name, Protocol::TYPE_INT);
357 stream->writeS32(value);
358 stream->flush();
359 checkResult();
360}
361
362//------------------------------------------------------------------------------
363
364void XPlane::setFloat(const char* name, float value) throw(Exception)
365{
366 setScalar(name, Protocol::TYPE_FLOAT);
367 stream->writeFloat(value);
368 stream->flush();
369 checkResult();
370}
371
372//------------------------------------------------------------------------------
373
374void XPlane::setDouble(const char* name, double value) throw(Exception)
375{
376 setScalar(name, Protocol::TYPE_DOUBLE);
377 stream->writeDouble(value);
378 stream->flush();
379 checkResult();
380}
381
382//------------------------------------------------------------------------------
383
384void XPlane::setArray(const char* name, uint8_t type, size_t length,
385 size_t offset) throw(Exception)
386{
387 stream->writeU8(Protocol::COMMAND_SET_SINGLE);
388 stream->writeString(name, strlen(name));
389 stream->writeU8(type);
390 stream->writeS32(static_cast<int32_t>(length));
391 stream->writeS32(static_cast<int32_t>(offset));
392}
393
394//------------------------------------------------------------------------------
395
396void XPlane::setFloatArray(const char* name, const float* values, size_t length,
397 size_t offset) throw(Exception)
398{
399 setArray(name, Protocol::TYPE_FLOAT_ARRAY, length, offset);
400 stream->write(values, length*sizeof(*values));
401 stream->flush();
402 checkResult();
403}
404
405//------------------------------------------------------------------------------
406
407void XPlane::setIntArray(const char* name, const int32_t* values, size_t length,
408 size_t offset) throw(Exception)
409{
410 setArray(name, Protocol::TYPE_INT_ARRAY, length, offset);
411 stream->write(values, length*sizeof(*values));
412 stream->flush();
413 checkResult();
414}
415
416//------------------------------------------------------------------------------
417
418void XPlane::setByteArray(const char* name, const uint8_t* values, size_t length,
419 size_t offset) throw(Exception)
420{
421 setArray(name, Protocol::TYPE_BYTE_ARRAY, length, offset);
422 stream->write(values, length*sizeof(*values));
423 stream->flush();
424 checkResult();
425}
426
427//------------------------------------------------------------------------------
428
429void XPlane::setString(const char* name, const char* value, size_t length,
430 size_t offset) throw(Exception)
431{
432 size_t valueLength = strlen(value);
433 if ((valueLength+1)>=length) {
434 setByteArray(name, reinterpret_cast<const uint8_t*>(value),
435 length, offset);
436 } else {
437 auto_ptr<uint8_t> buffer(new uint8_t[length]);
438 memcpy(buffer.get(), value, valueLength);
439 memset(buffer.get() + valueLength, 0, length - valueLength);
440 setByteArray(name, buffer.get(), length, offset);
441 }
442}
443//------------------------------------------------------------------------------
444
445// Local Variables:
446// mode: C++
447// c-basic-offset: 4
448// indent-tabs-mode: nil
449// End:
Note: See TracBrowser for help on using the repository browser.