Mercurial > hg > piper-cpp
comparison vamp-client/client.cpp @ 87:7a77a374b6b2
Fix decoding error due to misaligned array
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Wed, 12 Oct 2016 17:47:59 +0100 |
parents | 1b7c11bc5a88 |
children | bf2e6f939f9f |
comparison
equal
deleted
inserted
replaced
86:5a80e00375b1 | 87:7a77a374b6b2 |
---|---|
77 | 77 |
78 VampnProto::buildRpcRequest_Load(builder, request); | 78 VampnProto::buildRpcRequest_Load(builder, request); |
79 ReqId id = getId(); | 79 ReqId id = getId(); |
80 builder.getId().setNumber(id); | 80 builder.getId().setNumber(id); |
81 | 81 |
82 cerr << "id = " << id << endl; | |
83 | |
84 auto arr = messageToFlatArray(message); | 82 auto arr = messageToFlatArray(message); |
85 m_process->write(arr.asChars().begin(), arr.asChars().size()); | 83 m_process->write(arr.asChars().begin(), arr.asChars().size()); |
86 | 84 |
87 //!!! ... --> will also need some way to kill this process | 85 //!!! ... --> will also need some way to kill this process |
88 //!!! (from another thread) | 86 //!!! (from another thread) |
89 | 87 |
90 QByteArray buffer = readResponseBuffer(); | 88 QByteArray buffer = readResponseBuffer(); |
91 capnp::FlatArrayMessageReader responseMessage(toArrayPtr(buffer)); | 89 auto karr = toKJArray(buffer); |
90 capnp::FlatArrayMessageReader responseMessage(karr); | |
91 cerr << "made reader" << endl; | |
92 RpcResponse::Reader reader = responseMessage.getRoot<RpcResponse>(); | 92 RpcResponse::Reader reader = responseMessage.getRoot<RpcResponse>(); |
93 | 93 |
94 //!!! handle (explicit) error case | 94 //!!! handle (explicit) error case |
95 | 95 |
96 checkResponseType(reader, RpcResponse::Response::Which::LOAD, id); | 96 checkResponseType(reader, RpcResponse::Response::Which::LOAD, id); |
134 | 134 |
135 auto arr = messageToFlatArray(message); | 135 auto arr = messageToFlatArray(message); |
136 m_process->write(arr.asChars().begin(), arr.asChars().size()); | 136 m_process->write(arr.asChars().begin(), arr.asChars().size()); |
137 | 137 |
138 QByteArray buffer = readResponseBuffer(); | 138 QByteArray buffer = readResponseBuffer(); |
139 capnp::FlatArrayMessageReader responseMessage(toArrayPtr(buffer)); | 139 auto karr = toKJArray(buffer); |
140 capnp::FlatArrayMessageReader responseMessage(karr); | |
140 RpcResponse::Reader reader = responseMessage.getRoot<RpcResponse>(); | 141 RpcResponse::Reader reader = responseMessage.getRoot<RpcResponse>(); |
141 | 142 |
142 //!!! handle (explicit) error case | 143 //!!! handle (explicit) error case |
143 | 144 |
144 checkResponseType(reader, RpcResponse::Response::Which::CONFIGURE, id); | 145 checkResponseType(reader, RpcResponse::Response::Which::CONFIGURE, id); |
175 | 176 |
176 auto arr = messageToFlatArray(message); | 177 auto arr = messageToFlatArray(message); |
177 m_process->write(arr.asChars().begin(), arr.asChars().size()); | 178 m_process->write(arr.asChars().begin(), arr.asChars().size()); |
178 | 179 |
179 QByteArray buffer = readResponseBuffer(); | 180 QByteArray buffer = readResponseBuffer(); |
180 capnp::FlatArrayMessageReader responseMessage(toArrayPtr(buffer)); | 181 auto karr = toKJArray(buffer); |
182 capnp::FlatArrayMessageReader responseMessage(karr); | |
181 RpcResponse::Reader reader = responseMessage.getRoot<RpcResponse>(); | 183 RpcResponse::Reader reader = responseMessage.getRoot<RpcResponse>(); |
182 | 184 |
183 //!!! handle (explicit) error case | 185 //!!! handle (explicit) error case |
184 | 186 |
185 checkResponseType(reader, RpcResponse::Response::Which::PROCESS, id); | 187 checkResponseType(reader, RpcResponse::Response::Which::PROCESS, id); |
211 | 213 |
212 auto arr = messageToFlatArray(message); | 214 auto arr = messageToFlatArray(message); |
213 m_process->write(arr.asChars().begin(), arr.asChars().size()); | 215 m_process->write(arr.asChars().begin(), arr.asChars().size()); |
214 | 216 |
215 QByteArray buffer = readResponseBuffer(); | 217 QByteArray buffer = readResponseBuffer(); |
216 capnp::FlatArrayMessageReader responseMessage(toArrayPtr(buffer)); | 218 auto karr = toKJArray(buffer); |
219 capnp::FlatArrayMessageReader responseMessage(karr); | |
217 RpcResponse::Reader reader = responseMessage.getRoot<RpcResponse>(); | 220 RpcResponse::Reader reader = responseMessage.getRoot<RpcResponse>(); |
218 | 221 |
219 //!!! handle (explicit) error case | 222 //!!! handle (explicit) error case |
220 | 223 |
221 checkResponseType(reader, RpcResponse::Response::Which::FINISH, id); | 224 checkResponseType(reader, RpcResponse::Response::Which::FINISH, id); |
238 //!!! todo: mutex | 241 //!!! todo: mutex |
239 static ReqId m_nextId = 0; | 242 static ReqId m_nextId = 0; |
240 return m_nextId++; | 243 return m_nextId++; |
241 } | 244 } |
242 | 245 |
243 kj::ArrayPtr<const capnp::word> | 246 kj::Array<capnp::word> |
244 toArrayPtr(QByteArray arr) { | 247 toKJArray(QByteArray qarr) { |
248 // We could do this whole thing with fewer copies, but let's | |
249 // see whether it matters first | |
245 size_t wordSize = sizeof(capnp::word); | 250 size_t wordSize = sizeof(capnp::word); |
246 capnp::word *dptr = reinterpret_cast<capnp::word *>(arr.data()); | 251 size_t words = qarr.size() / wordSize; |
247 kj::ArrayPtr<const capnp::word> kptr(dptr, arr.size() / wordSize); | 252 cerr << "converting " << words << " words (" << (words * wordSize) << " bytes)" << endl; |
248 return kptr; | 253 kj::Array<capnp::word> karr(kj::heapArray<capnp::word>(words)); |
254 memcpy(karr.begin(), qarr.data(), words * wordSize); | |
255 return karr; | |
249 } | 256 } |
250 | 257 |
251 QByteArray | 258 QByteArray |
252 readResponseBuffer() { | 259 readResponseBuffer() { |
253 | 260 |
268 } | 275 } |
269 } else { | 276 } else { |
270 buffer.append(m_process->read(wordCount * wordSize)); | 277 buffer.append(m_process->read(wordCount * wordSize)); |
271 size_t haveWords = buffer.size() / wordSize; | 278 size_t haveWords = buffer.size() / wordSize; |
272 size_t expectedWords = | 279 size_t expectedWords = |
273 capnp::expectedSizeInWordsFromPrefix(toArrayPtr(buffer)); | 280 capnp::expectedSizeInWordsFromPrefix(toKJArray(buffer)); |
274 | 281 |
275 cerr << "haveWords = " << haveWords << ", expectedWords = " << expectedWords << endl; | 282 cerr << "haveWords = " << haveWords << ", expectedWords = " << expectedWords << endl; |
276 | 283 |
277 if (haveWords >= expectedWords) { | 284 if (haveWords >= expectedWords) { |
278 if (haveWords > expectedWords) { | 285 if (haveWords > expectedWords) { |
282 } | 289 } |
283 complete = true; | 290 complete = true; |
284 } | 291 } |
285 } | 292 } |
286 } | 293 } |
287 | 294 /* |
288 cerr << "buffer = "; | 295 cerr << "buffer = "; |
289 for (int i = 0; i < buffer.size(); ++i) { | 296 for (int i = 0; i < buffer.size(); ++i) { |
290 if (i % 16 == 0) cerr << "\n"; | 297 if (i % 16 == 0) cerr << "\n"; |
291 cerr << int(buffer[i]) << " "; | 298 cerr << int(buffer[i]) << " "; |
292 } | 299 } |
293 cerr << "\n"; | 300 cerr << "\n"; |
294 | 301 */ |
295 return buffer; | 302 return buffer; |
296 } | 303 } |
297 | 304 |
298 void | 305 void |
299 checkResponseType(const RpcResponse::Reader &r, | 306 checkResponseType(const RpcResponse::Reader &r, |
326 auto fl(features[0]); | 333 auto fl(features[0]); |
327 for (const auto &f: fl) { | 334 for (const auto &f: fl) { |
328 cerr << f.values[0] << endl; | 335 cerr << f.values[0] << endl; |
329 } | 336 } |
330 } | 337 } |
331 delete plugin; | 338 //!!! todo: make it possible to do both of the following -- |
339 (void)plugin->getRemainingFeatures(); | |
340 // delete plugin; | |
341 //!!! -- and also implement reset(), which will need to reconstruct internally | |
332 } | 342 } |
333 | 343 |