I have the following question related to POCO library. My client listens the messages from our backend server using POCO library. All is well the first 50 minutes, then with the socket happens something strange and method "receiveFrame" begins to return an exception. After that, the socket does not become operational. I have made few tests the time after which I receive not operational socket is exactly 50 minutes. Also I need to note that our backend server doesn't send anything during all time. I have no idea what happens... Below is code of our Handshake and Read procedures:
void WebSocketManager::Handshake()
{
qDebug() << "WebSocketManager::Handshake";
try {
HTTPResponse response;
QString origin = Settings::Instance()->GetErPortal();
QString host = origin.remove("http://");
host = host.remove('/');
QString token = "/event/bus/ws/subscribe?auth_token=" + Settings::Instance()->token().toUtf8();
_wssession.setHost(host.toUtf8().constData());
_wssession.setPort(80);
HTTPRequest request(HTTPRequest::HTTP_GET, token.toUtf8().constData(),HTTPMessage::HTTP_1_1);
request.set("origin", origin.toUtf8().constData());
_wssock = new WebSocket(_wssession, request, response);
response.getStatus();
HTTPResponse::HTTPStatus status = response.getStatus();
qDebug() << "Handshake status is : " << status;
if(status == HTTPResponse::HTTPStatus::HTTP_SWITCHING_PROTOCOLS)
_status = true;
}
catch (std::exception &e)
{
qDebug() << "WebSocketManager::Handshake exception " << e.what();
}
}
void WebSocketManager::Read()
{
char receiveBuff[1024];
while(_status)
{
qDebug() << "WebSocketManager::Read wait data...., thread = " << QThread::currentThread();
try {
int flags=0;
int rlen=_wssock->receiveFrame(receiveBuff,1024,flags);
if(!rlen)
{
qDebug() << "WebSocketManager::Read error";
emit ConnectionFailed();
return;
}
else
{
qDebug() << "WebSocketManager::Read, len =" << rlen << ", flags = " << flags << ", data = " << receiveBuff;
ProcessBackendEvent(QString(receiveBuff));
}
}
catch (std::exception &e)
{
qDebug() << "WebSocketManager::Read exception " << e.what();
}
}
}
It seems it is bug of POCO library described here https://github.com/pocoproject/poco/issues/490
On 1.9.0 POCO library all work fine...
Related
could someone kindly help me with the RSocket issue? I'm trying to send a request from the Server to a connected Client, but the Client Responder does not fire callbacks. I'm using the RSocket-cpp library.
Server code:
TcpConnectionAcceptor::Options opts;
opts.address = folly::SocketAddress("::", FLAGS_port);
opts.threads = 2;
// RSocket server accepting on TCP
auto rs = std::make_unique<TcpConnectionAcceptor>(std::move(opts));
auto serverThread = std::thread([&rs] {
// start accepting connections
rs->start(
[](std::unique_ptr<DuplexConnection> connection, folly::EventBase& eventBase) {
LOG(INFO) << "new incoming connected" << std::endl;
auto client = RSocket::createClientFromConnection(
std::move(connection), *eventBase.getEventBase(), SetupParameters(), nullptr, std::make_shared<GenericRequestResponseResponder>());
LOG(INFO) << "send data" << std::endl;
client->getRequester()->requestResponse(Payload("hello2"))->subscribe([](Payload p) {
LOG(INFO) << "Received1 >> " << p.moveDataToString() << std::endl;
});
LOG(INFO) << "request is sent from server" << std::endl;
});
});
Output:
I0327 07:11:33.583813 23622 RequestResponseHelloWorld_Server.cpp:95] new incoming connected
I0327 07:11:33.602982 23622 RequestResponseHelloWorld_Server.cpp:100] send data
I0327 07:11:33.604566 23622 RequestResponseHelloWorld_Server.cpp:105] request is sent from server
Client code:
class GenericRequestResponseResponder : public rsocket::RSocketResponder
{
public:
std::shared_ptr<Single<Payload>> handleRequestResponse(
Payload request,
StreamId /*streamId*/) override
{
LOG(INFO) << "GenericRequestResponseResponder.handleRequestResponse "
<< request << std::endl;
// string from payload data
auto requestString = request.moveDataToString();
return Single<Payload>::create(
[name = std::move(requestString)](auto subscriber) {
std::stringstream ss;
ss << "Ack " << name << "!";
std::string s = ss.str();
subscriber->onSubscribe(SingleSubscriptions::empty());
subscriber->onSuccess(Payload(s, "metadata"));
});
}
void handleFireAndForget(
rsocket::Payload request,
rsocket::StreamId /*streamId*/) override
{
LOG(INFO) << "GenericRequestResponseResponder.handleRequestResponse "
<< request << std::endl;
}
};
folly::SocketAddress address{folly::SocketAddress(host, port)};
std::shared_ptr<TelemetryConnection> connection{nullptr};
RSocket::createConnectedClient(
std::make_unique<TcpConnectionFactory>(
*m_worker->getEventBase(), std::move(address)), SetupParameters(), std::make_shared<GenericRequestResponseResponder>())
.thenValue([this, host, port, &connection](auto&& client) {
LOG(INFO) << "client is created" << std::endl;
m_clientList.append(client);
})
.thenError(
folly::tag_t<std::exception>{},
[&](const std::exception&) {
LOG(ERROR) << "connection failed";
}).get();
I was expecting GenericRequestResponseResponder::``handleRequestResponse fired when the server sends the request, but client output is empty
Im trying to find out what goes wrong when doing a get request in qt.
I have the following slots attached to my networkmanager:
connect(mgr,SIGNAL(finished(QNetworkReply*)),this,SLOT(requestFinished(QNetworkReply*)));
connect(mgr, SIGNAL(error(QNetworkReply::NetworkError)),
this, SLOT(slotError(QNetworkReply::NetworkError)));
The request finished is like:
void FirebaseInteractor::requestFinished(QNetworkReply *rep)
{
QVariant statusCode = rep->attribute( QNetworkRequest::HttpStatusCodeAttribute );
int status = statusCode.toInt();
if ( status != 200 )
{
QString reason = rep->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString();
qDebug() << "Pushnotification Request failed : " <<reason;
}
else{
qDebug() << "Pushnotification has been send: ";
}
if ( !statusCode.isValid() )
{
QString status = statusCode.toString(); // or status_code.toInt();
qDebug() << "Failing " << status;
int code = statusCode.toInt();
qDebug() << "Pushnotification Request failed invalid status code." << QString::number(code);
QString reason = rep->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString();
qDebug() << "reason " << reason;
return;
}
}
However Status is always empty Failing and reason is printed but there is no value after it (i was expecting a reason e.g. timeout, 401 etc).
I also tried:
int status = statusCode.toInt();
if ( status != 200 )
{
QString reason = rep->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString();
qDebug() << "Pushnotification Request failed : " <<reason;
}
else{
qDebug() << "Pushnotification has been send: ";
}
But all reasons/codes are empty.
I also added:
void FirebaseInteractor::slotError(QNetworkReply::NetworkError error)
{
qDebug() << "slotError" << error;
}
but this is not called.
How can I find out whats going wrong?
You can get the error directly using:
qDebug() << reply->error();
This won't work if the network request never happened:
if ( !statusCode.isValid() )
Because this means that the QVariant itself is invalid and has type QMetaType::UnknownType Documentation. Hence it will not give any information about what went wrong in the http request. To fix this, here's a simple example:
if (statusCode.isValid()) { // it needs to be valid
qDebug() << "Status Code: " << statusCode.toString();
}
Alternatively, you can switch on QNetworkReply::Error()
void FirebaseInteractor::requestFinished(QNetworkReply *rep)
{
switch (rep->error())
{
case QNetworkReply::NoError:
// No error
return;
case QNetworkReply::TimeoutError:
{
auto httpStatus =
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
auto httpStatusMessage = reply->attribute(
QNetworkRequest::HttpReasonPhraseAttribute).toByteArray();
//...
break;
}
}
}
I am trying to write a cpp client for a bi-directional stream api.
With the following client code, I am able to instantiate a Stream observer on the server. However the problem is with the invocation of the onNext function on the Server StreamObserver.
Is there a certain protocol to make this call using a cpp client and java server ?
Proto file:
// A client-to-server stream RPC to append data
rpc append(stream ratis.common.RaftClientRequestProto)
returns (stream ratis.common.RaftClientReplyProto) {}
Server code
#Override
public void onNext(RaftClientRequestProto request) {
try {
final RaftClientRequest r = ClientProtoUtils.toRaftClientRequest(request);
LOG.info("recieved request " + r.getCallId());
final PendingAppend p = new PendingAppend(r);
slidingWindow.receivedRequest(p, this::processClientRequestAsync);
} catch (Throwable e) {
responseError(e, () -> "onNext for " + ClientProtoUtils.toString(request));
}
}
Client code
RaftClientRequestProto req = create_request(read_requet, sizeof(ContainerCommandRequestProto));
grpc::ClientContext ctx;
std::shared_ptr<ClientReaderWriter<RaftClientRequestProto, RaftClientReplyProto>> cli_stream(stub->append(&ctx));
std::thread writer([cli_stream]() {
RaftClientReplyProto resp;
std::cout << "goind to read " << std::endl;
cli_stream->Read(&resp);
std::cout << "read done" << std::endl;
});
std::cout << "Thread started" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(10000));
std::cout << "Doing writes" << std::endl;
cli_stream->Write(req);
cli_stream->WritesDone();
std::cout << "writes done" << std::endl;
This issue was because of difference in protobuf version.
I would need to customize the QModbusDataUnit by sending custom hex via modbus, below my code, I'm trying to customize the post but I do not understand how to do it.
I should send the following values: 0x01,0x08,0x00,0x00,0x00,0x00,0xE0,0x0B
void connessione::clickButton(){
QModbusDataUnit readUnit(QModbusDataUnit::HoldingRegisters, 1, QVector<quint16>({0x01,0x08,0x00,0x00,0x00,0x00,0xE0,0x0B}));
qDebug() << "readUnit" << clientX->state();
if (auto *reply = clientX->sendReadRequest(readUnit, 255)) // client id 255
{
if (!reply->isFinished())
{
// connect the finished signal of the request to your read slot
qDebug() << "connected" << reply->errorString();
connect(reply, &QModbusReply::finished, this, &connessione::readReady);
}
else
{
qDebug() << "Errore" << reply->errorString();
delete reply; // broadcast replies return immediately
}
}
else
{
qDebug() << "Errore" << reply->errorString();
// request error
}
}
but my response is:
D/libmodbusMobile.so(15006): (null):0 ((null)): qt.modbus: (TCP
client) Sent TCP PDU: 0x0300080008 with tId: 0
where set 0x03 ?
this is not right, it's not equal to my QVector send, how to solve ?
Try to test a QModbusDataUnit as follows:
QModbusDataUnit myUnit;
QModbusDataUnit::RegisterType myType;
myType = static_cast<QModbusDataUnit::RegisterType>(0x04);
myUnit = QModbusDataUnit(myType,0x0c,0x03);
cout << hex;
cout << myUnit.registerType() << endl;
cout << myUnit.startAddress() << endl;
Notes:
To use “cout” in a console QT application;
#include <iostream>
using namespace std;
To create a QT Modbus console application include the following in the .pro file;
QT += serialbus widgets
qtConfig(modbus-serialport): QT += serialport
TARGET = modbusserver
during CPPRest SDK (2.8) testing, I initialized an HTTP Request simulating user login to the local server, I am expecting a JSON string to be returned indicating if login succeed. here is the code I wrote.
void printJSON(json::value v)
{
if (!v.is_null()){
// Loop over each element in the object
for (auto iter = v.as_object().cbegin(); iter != v.as_object().cend(); ++iter){
const string &key = iter->first;
const json::value &value = iter->second;
if (value.is_object() || value.is_array()){
if(key.size() != 0){
std::wcout << "Parent: " << key.c_str() << std::endl;
}
printJSON(value);
if(key.size() != 0){
std::wcout << "End of Parent: " << key.c_str() << std::endl;
}
}else{
std::wcout << "Key: " << key.c_str() << ", Value: " << value.to_string().c_str() << std::endl;
}
}
}
}
void login(){
http_client client("http://localhost:8080/user");
http_request request(methods::POST);
request.headers().add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
request.headers().add("Content-Length", "100");
request.headers().add("Host", "testhost.com");
request.headers().add("X-Requested-With", "XMLHttpRequest");
request.set_body("u_id=test_admin&pwd=123456789");
pplx::task<void> task = client.request(request)
.then([](http_response response)-> pplx::task<json::value>{
if(response.status_code() == status_codes::OK){
return response.extract_json();
} else {
return pplx::task_from_result(json::value());
};})
.then([](pplx::task<json::value> previousTask){
try{
const json::value & v = previousTask.get();
printJSON(v);
} catch(const http_exception &e){
std::cout<<e.what()<<std::endl;
}
});
try{
task.wait();
} catch(std::exception &e){
std::cout<<e.what()<<std::endl;
}
}
When I run this code, nothing happened, it seems request never reaches to the Server which has been tested using JSP, So I am pretty sure something went wrong in my code. please help, thanks
Vague.
If you're saying that the request is not reaching the server, there might be a problem with the listener at the time you executed this code.
Your request format is correct and running, you may try to wrap the body (u_id, pwd) into a json, and see if it works.
The bottomline is debugging or sharing your server code would probably help clarify things a bit more.