Beispiel #1
0
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);
	}

}