void CUSB::Read(void *buffer, unsigned int bytesToRead) { if (!isUSB_open) throw RpcError(RpcError::READ_ERROR); unsigned long i; for (i=0; i<bytesToRead; i++) { if (m_posR<m_sizeR) ((unsigned char*)buffer)[i] = m_bufferR[m_posR++]; else { unsigned long n = bytesToRead-i; if (n>USBREADBUFFERSIZE) n = USBREADBUFFERSIZE; if (!FillBuffer(n)) throw RpcError(RpcError::READ_ERROR); if (m_sizeR < n) throw RpcError(RpcError::READ_ERROR); if (m_posR<m_sizeR) ((unsigned char*)buffer)[i] = m_bufferR[m_posR++]; else { // timeout (bytesRead < bytesToRead) throw RpcError(RpcError::READ_TIMEOUT); } } } }
void CUSB::Flush() { DWORD bytesWritten; DWORD bytesToWrite = m_posW; m_posW = 0; if (!isUSB_open) throw RpcError(RpcError::WRITE_ERROR); if (!bytesToWrite) return; ftStatus = FT_Write(ftHandle, m_bufferW, bytesToWrite, &bytesWritten); if (ftStatus != FT_OK) throw RpcError(RpcError::WRITE_ERROR); if (bytesWritten != bytesToWrite) { ftStatus = FT_IO_ERROR; throw RpcError(RpcError::WRITE_ERROR); } }
void ClientWS::processMessage(JSON::Value v) { try { JSON::Value result = v["result"]; JSON::Value error = v["error"]; JSON::Value context = v["context"]; JSON::Value method = v["method"]; JSON::Value id = v["id"]; JSON::Value params = v["params"]; if (result != null || error != null) { if (id != null && !id->isNull()) { natural reqid = id->getUInt(); const Promise<Result> *p = waitingResults.find(reqid); if (p == 0) onDispatchError(v); else { Promise<Result> q = *p; waitingResults.erase(reqid); if (p) { if (error == null || error->isNull()) { q.resolve(Result(result,context)); } else { q.reject(RpcError(THISLOCATION,error)); } } } }//id=null - invalid frame else { onDispatchError(v); } } else if (method != null && params != null) { if (id == null || id->isNull()) { onNotify(method->getStringUtf8(), params, context); } else { try { onIncomeRPC(method->getStringUtf8(), params,context,id); } catch (const RpcError &e) { sendResponse(id, jsonFactory->newValue(null), e.getError()); } catch (const jsonsrv::RpcCallError &c) { RpcError e(THISLOCATION,jsonFactory,c.getStatus(),c.getStatusMessage()); sendResponse(id, jsonFactory->newValue(null), e.getError()); } catch (const std::exception &s) { RpcError e(THISLOCATION,jsonFactory,500,s.what()); sendResponse(id, jsonFactory->newValue(null), e.getError()); } catch (...) { RpcError e(THISLOCATION,jsonFactory,500,"fatal"); sendResponse(id, jsonFactory->newValue(null), e.getError()); } } } } catch (...) { onDispatchError(v); } }
void CUSB::Write(const void *buffer, unsigned int bytesToWrite) { if (!isUSB_open) throw RpcError(RpcError::WRITE_ERROR); unsigned long k=0; for (k=0; k < bytesToWrite; k++) { if (m_posW >= USBWRITEBUFFERSIZE) Flush(); m_bufferW[m_posW++] = ((unsigned char*)buffer)[k]; } }