source: xplra/src/xplra/ServerThread.cc@ 5:94fdd791268f

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

Extracted the protocol-related constants into their own class

File size: 5.3 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 "ServerThread.h"
32
33#include "RequestQueue.h"
34#include "Protocol.h"
35#include "GetDataRefTask.h"
36#include "TaskRequest.h"
37
38#include <xplcommon/Util.h>
39
40#include <cstdio>
41
42//------------------------------------------------------------------------------
43
44using xplra::ServerThread;
45
46using xplcommon::LocalAcceptor;
47using xplcommon::Mutex;
48using xplcommon::Util;
49
50using std::string;
51
52//------------------------------------------------------------------------------
53
54Mutex ServerThread::instancesMutex;
55
56ServerThread::instances_t ServerThread::instances;
57
58//------------------------------------------------------------------------------
59
60void ServerThread::quitAll()
61{
62 instancesMutex.lock();
63 for(instances_t::iterator i = instances.begin(); i!=instances.end(); ++i) {
64 (*i)->quit();
65 }
66 instancesMutex.unlock();
67}
68
69//------------------------------------------------------------------------------
70
71ServerThread::ServerThread(RequestQueue& requestQueue, LocalAcceptor& acceptor) :
72 Thread(true),
73 requestQueue(requestQueue),
74 bufferedStream(acceptor.getSocket(&waiter)),
75 stream(*bufferedStream)
76{
77 instancesMutex.lock();
78 instances.insert(this);
79 instancesMutex.unlock();
80}
81
82//------------------------------------------------------------------------------
83
84ServerThread::~ServerThread()
85{
86 delete bufferedStream;
87
88 instancesMutex.lock();
89 instances.erase(this);
90 instancesMutex.unlock();
91}
92
93//------------------------------------------------------------------------------
94
95void ServerThread::quit()
96{
97 stream.interrupt();
98}
99
100//------------------------------------------------------------------------------
101
102void ServerThread::run()
103{
104 Util::debug("hu.varadiistvan.xplra.ServerThread[%p]::run\n", this);
105 while(stream) {
106 uint8_t command = stream.readU8();
107 if (!stream) continue;
108
109 // Util::debug("hu.varadiistvan.xplra.ServerThread[%p]::run: command=0x%02x\n",
110 // this, command);
111
112 if (command==Protocol::CMD_GET_SINGLE) {
113 string name = stream.readString();
114 uint8_t type = stream.readU8();
115 if (!stream) continue;
116 // Util::debug("hu.varadiistvan.xplra.ServerThread[%p]::run: name='%s', type=0x%02x\n",
117 // this, name.c_str(), type);
118 if (type==Protocol::TYPE_BYTE_ARRAY) {
119 int length = stream.readS32();
120 if (!stream) continue;
121
122 // Util::debug("hu.varadiistvan.xplra.ServerThread[%p]::run: length=%lu\n",
123 // this, static_cast<unsigned long>(length));
124
125 GetByteArrayDataRefTask* task =
126 new GetByteArrayDataRefTask(name, length);
127 TaskRequest request(task);
128
129 if (requestQueue.execute(&request)) {
130 // Util::debug("hu.varadiistvan.xplra.ServerThread[%p]::run: request executed\n");
131 stream.writeU8(Protocol::RESULT_OK);
132 int length = task->getLength();
133 stream.writeS32(length);
134 if (length>0) {
135 stream.write(task->getData(), length);
136 }
137 } else {
138 break;
139 }
140 } else {
141 stream.writeU8(Protocol::RESULT_OTHER_ERROR);
142 }
143 } else {
144 stream.writeU8(Protocol::RESULT_INVALID_COMMAND);
145 }
146 stream.flush();
147 }
148
149 Util::debug("hu.varadiistvan.xplra.ServerThread[%p]::run: quitting\n", this);
150}
151
152//------------------------------------------------------------------------------
153
154// Local Variables:
155// mode: C++
156// c-basic-offset: 4
157// indent-tabs-mode: nil
158// End:
Note: See TracBrowser for help on using the repository browser.