void idle() { static unsigned int packet_index; unsigned int failed_connections = 0; unsigned char current_connection; do { if (packet_index == total_no_of_packets) // wrap around to the beginning packet_index = 0; // proceed to the next packet packet = &packetArray[packet_index]; // get the current connection status current_connection = packet->connection; if (!current_connection) { // If all the connection attributes are false return // immediately to the main sketch if (++failed_connections == total_no_of_packets) return; } packet_index++; // if a packet has no connection get the next one }while (!current_connection); constructPacket(); }
int PushCliConn::handlePush(const Json::Value& vrequest){ //PSLogTrace("PushServer") << "<action:handlePush> <conid:" << getId() << ">"; Dispatcher* d = getDispatcher(); if(!d){ PSLogError("PushServer") << "<action:handlePush> <conid:" << getId() << "> <status: Dispatcher:NULL>"; return -1; } if(!pushRequestJsonCheck(vrequest)){ PSLogError("PushServer") << "<action:handlePush> <conid:" << getId() << "> <status: pushRequestJsonCheck error>"; return PS_RESULT_ARGS_ERROR; } Message msg; messageJsonToProtobuf(vrequest,msg); const std::string& to = msg.to(); const std::string& sn = msg.sn(); std::string mkey = MSG_KEY(to); std::string idkey = LAST_MSG_ID_KEY(to); int64 msgid =-1; int ret = PushMsgDBM::MQ()->incrId(to, idkey, msgid); if(ret < 0){ PSLogError("PushServer") << "<action:handlePush> <conid:" << getId() << "> <status: incrId error:" << ret << ">"; return PS_RESULT_INCR_MSGID_ERROR; } msg.set_id(msgid); ret = sendJ2C(msg); if(msg.expire() > 0){ if(PushMsgDBM::MQ()->add(to, mkey, msg) < 0){ PSLogError("PushServer") << "<action:handlePush> <conid:" << getId() << "> <status: addMsg error:>"; } } Json::Value response; response["cmd"] = PS_CMD_CALL_PUSH_RESP; response["result"] = ret; response["sn"] = sn; response["msgid"] = ef::itostr(msgid); std::string respbuf; head rh; rh.magic = MAGIC_NUMBER; rh.cmd = SERVICE_RESP; rh.len = 0; constructPacket(rh, Json::FastWriter().write(response), respbuf); ret = sendMessage(respbuf); return 0; }
MsgBase::MsgBase(QHostAddress address, quint16 port, QString additionalInfo, QString extendedInfo, quint32 flags) : m_ipAddress(address), m_port(port), m_additionalInfo(additionalInfo), m_extendedInfo(extendedInfo), m_flags(flags) { m_packetNoString = Helper::packetNoString(); m_owner = Global::userManager->ourself(); constructPacket(); }
unsigned int modbus_update(Packet* packets) { // Initialize the connection_status variable to the // total_no_of_packets. This value cannot be used as // an index (and normally you won't). Returning this // value to the main skecth informs the user that the // previously scanned packet has no connection error. unsigned int connection_status = total_no_of_packets; if (transmission_ready_Flag) { static unsigned int packet_index; unsigned int failed_connections = 0; unsigned char current_connection; do { if (packet_index == total_no_of_packets) // wrap around to the beginning packet_index = 0; // proceed to the next packet packet = &packets[packet_index]; // get the current connection status current_connection = packet->connection; if (!current_connection) { connection_status = packet_index; // If all the connection attributes are false return // immediately to the main sketch if (++failed_connections == total_no_of_packets) return connection_status; } packet_index++; }while (!current_connection); // while a packet has no connection get the next one constructPacket(); } checkResponse(); check_packet_status(); return connection_status; }
int PushCliConn::handleRequest(const std::string& jstr){ int ret = -1; try{ Json::Value v; Json::Reader r; if(!r.parse(jstr, v)){ PSLogError("PushServer") << "<action:handleRequest> <conid:" << getId() << "> <status: parse json error!>"; ret = -1; goto req_err; } int cmd = v["cmd"].asInt(); switch(cmd){ case PS_CMD_CALL_PUSH: ret = handlePush(v); break; default: PSLogError("PushServer") << "<action:handleRequest> <conid:" << getId() << "> <status: undefined json cmd>"; ret = PS_RESULT_UNDEFINED_CMD; break; } }catch(const std::exception& e){ PSLogError("PushServer") << "<action:handleRequest> <conid:" << getId() << "> <status:exception: " << e.what() << ">"; ret = PS_RESULT_JSON_ERROR; goto req_err; } req_err: if(ret < 0){ Json::Value response; response["cmd"] = PS_CMD_CALL_PUSH_RESP; response["result"] = ret; std::string respbuf; head rh; rh.magic = MAGIC_NUMBER; rh.cmd = SERVICE_RESP; rh.len = 0; constructPacket(rh, Json::FastWriter().write(response), respbuf); sendMessage(respbuf); } return 0; }
void Modbus::update() { if (_transmission_ready_flag) { static uint8_t packet_index = 0; unsigned int failed_connections = 0; unsigned char current_connection; do { if (packet_index == _total_packets) // wrap around to the beginning packet_index = 0; // proceed to the next packet _packet = &_packet_array[packet_index]; // get the current connection status current_connection = _packet->connection; if (!current_connection) { // If all the connection attributes are false return // immediately to the main sketch if (++failed_connections == _total_packets) return; } packet_index++; } while (!current_connection); // while a packet has no connection get the next one constructPacket(); } //check response packet checkPacket(); status(); }
int PushCliConn::handleKeepalive(const head& h){ std::string respbuf; constructPacket(h, "", respbuf); return sendMessage(respbuf); }