提交 9bc41c98 编写于 作者: 丁劲犇's avatar 丁劲犇 😸

finished 0x1003 msg, need test in future works

上级 748802aa
......@@ -7,6 +7,7 @@
#include <QMutexLocker>
#include <QSettings>
#include <QSqlError>
#include <QSet>
namespace SmartLink{
//0x0001 msg, stMsg_HostRegistReq
bool st_clientNodeAppLayer::RegisitNewBoxNode()
......@@ -200,7 +201,11 @@ bool st_clientNodeAppLayer::LoginBox()
m_bUUIDRecieved = true;
m_uuid = ncurrid;
m_pClientTable->regisitClientUUID(this);
if (false == loadRelations())
{
strcpy(reply.TextInfo,"Error, fail to load relations from db.");
reply.DoneCode = 3;
}
}
else
strcpy(reply.TextInfo,"ID Not matched.");
......@@ -311,6 +316,11 @@ bool st_clientNodeAppLayer::LoginClient()
m_bUUIDRecieved = true;
m_uuid = ncurrid;
m_pClientTable->regisitClientUUID(this);
if (false == loadRelations())
{
strcpy(reply.TextInfo,"Error, fail to load relations from db.");
reply.DoneCode = 3;
}
}
else
strcpy(reply.TextInfo,"UserID Is Invalid.Accunt locked by svr");
......@@ -427,56 +437,26 @@ bool st_clientNodeAppLayer::Box2Svr_UploadUserTable()
stMsg_UploadUserListRsp & reply = pApp->MsgUnion.msg_UploadUserListRsp;
reply.DoneCode = 3;
reply.DoneCode = 1;
strcpy(reply.TextInfo,"Unknown error");
//Check the database, find current equipment info
QSqlDatabase db = m_pClientTable->dbRes()->databse(m_pClientTable->Database_UserAcct());
if (db.isValid()==true && db.isOpen()==true )
if (loadRelations()==true )
{
QSqlQuery query(db);
QString sql = "select user_name,user_id,password from users where user_name = ? and password = ?;";
query.prepare(sql);
// query.addBindValue(strUserName);
// query.addBindValue(strPasswd);
if (true==query.exec())
for (quint32 i = 0; i <pAppLayer->MsgUnion.msg_UploadUserListReq.UserNum && i<4096;i++)
{
if (query.next())
{
bool bOk = false;
quint32 ncurrid = query.value(1).toUInt(&bOk);
if (bOk==true)
{
if (ncurrid>= (unsigned int)0x80000000 && ncurrid <= (unsigned int)0xAFFFFFFF )
{
reply.TextInfo[0] = 0;
reply.DoneCode = 0;
}
else
strcpy(reply.TextInfo,"UserID Is Invalid.Accunt locked by svr");
}
else
strcpy(reply.TextInfo,"UserID Is Invalid.Accunt locked by svr");
}
else
{
// No such device
strcpy(reply.TextInfo,"No such user or password.");
}
if (bIsValidUserId(pAppLayer->MsgUnion.msg_UploadUserListReq.pUserIDList[i])==false)
continue;
m_matched_nodes.insert(pAppLayer->MsgUnion.msg_UploadUserListReq.pUserIDList[i]);
}
else
if (true == saveRelations())
{
strcpy(reply.TextInfo,"Server Access Error.");
emit evt_Message(tr("Database Access Error :")+query.lastError().text());
reply.DoneCode = 0;
strcpy(reply.TextInfo,"Succeeded.");
}
}
else
{
//Server db is currently not accessable, wait.
strcpy(reply.TextInfo,"Server Not Accessable Now.");
strcpy(reply.TextInfo,"Failed to load current relations from db.");
}
......
#include "st_clientnode_applayer.h"
#include "st_client_table.h"
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlError>
namespace SmartLink{
st_clientNodeAppLayer::st_clientNodeAppLayer(st_client_table * pClientTable, QObject * pClientSock ,QObject *parent) :
st_clientNode_baseTrans(pClientTable,pClientSock,parent)
......@@ -12,6 +15,120 @@ st_clientNodeAppLayer::st_clientNodeAppLayer(st_client_table * pClientTable, QOb
}
bool st_clientNodeAppLayer::loadRelations()
{
QSqlDatabase db = m_pClientTable->dbRes()->databse(m_pClientTable->Database_UserAcct());
if (db.isValid()==true && db.isOpen()==true )
{
QString sql = "select ";
if (m_serialNum[0]!=0)
sql += "user_id from relations where equip_id = ?;";
else if (m_username[0]!=0)
sql += "equip_id from relations where user_id = ?;";
else
{
emit evt_Message(tr("try to save relations before login!"));
return false;
}
QSqlQuery query(db);
query.prepare(sql);
query.addBindValue((quint32)m_uuid);
if (false== query.exec())
{
emit evt_Message(tr("try to get relations Failed! ")+ query.lastError().text());
return false;
}
m_matched_nodes.clear();
while (query.next())
{
quint32 val = query.value(0).toUInt();
m_matched_nodes.insert(val);
}
return true;
}
else
{
//Server db is currently not accessable, wait.
emit evt_Message("Server Not Accessable Now.");
}
return false;
}
bool st_clientNodeAppLayer::saveRelations()
{
QSqlDatabase db = m_pClientTable->dbRes()->databse(m_pClientTable->Database_UserAcct());
if (db.isValid()==true && db.isOpen()==true )
{
QString sql = "delete from relations where ";
if (m_serialNum[0]!=0)
{
QSqlQuery query(db);
sql += "equip_id = ?;";
query.prepare(sql);
query.addBindValue((quint32)m_uuid);
if (false== query.exec())
{
emit evt_Message(tr("try to del old relations Failed! ")+ query.lastError().text());
return false;
}
}
else if (m_username[0]!=0)
{
QSqlQuery query(db);
sql += "user_id = ?;";
query.prepare(sql);
query.addBindValue((quint32)m_uuid);
if (false== query.exec())
{
emit evt_Message(tr("try to del old relations Failed! ")+ query.lastError().text());
return false;
}
}
else
{
emit evt_Message(tr("try to save relations before login!"));
return false;
}
sql = "insert into relations (equip_id,user_id) values (?,?);";
//the forigen key can automatic avoid non-existing values.
foreach(quint32 nodeid,m_matched_nodes)
{
QSqlQuery query(db);
if (m_serialNum[0]!=0)
{
if (bIsValidUserId(nodeid)==false)
continue;
query.prepare(sql);
query.addBindValue((quint32)m_uuid);
query.addBindValue(nodeid);
}
else if (m_username[0]!=0)
{
if (bIsValidEquipId(nodeid)==false)
continue;
query.prepare(sql);
query.addBindValue(nodeid);
query.addBindValue((quint32)m_uuid);
}
if (false== query.exec())
{
emit evt_Message(tr("try to insert new relations Failed! ")+ query.lastError().text());
return false;
}
}
return true;
}
else
{
//Server db is currently not accessable, wait.
emit evt_Message("Server Not Accessable Now.");
}
return false;
}
//!deal current message
int st_clientNodeAppLayer::deal_current_message_block()
......@@ -32,7 +149,7 @@ int st_clientNodeAppLayer::deal_current_message_block()
}
else
{
if (m_currentHeader.source_id>= 0x00010000 && m_currentHeader.source_id <= 0x0FFFFFFF)
if (bIsValidEquipId(m_currentHeader.source_id))
{
//Deal Box->Svr Msgs
if (false==Deal_Box2Svr_Msgs())
......@@ -42,7 +159,7 @@ int st_clientNodeAppLayer::deal_current_message_block()
emit evt_close_client(this->sock());
}
}
else if (m_currentHeader.source_id>= (unsigned int)0x80000000 && m_currentHeader.source_id <= (unsigned int)0xAFFFFFFF )
else if (bIsValidUserId(m_currentHeader.source_id) )
{
//Deal Client->Svr Msgs
}
......
......@@ -15,6 +15,9 @@ class st_clientNodeAppLayer : public st_clientNode_baseTrans
public:
explicit st_clientNodeAppLayer(st_client_table * pClientTable, QObject * pClientSock,QObject *parent = 0);
protected:
bool loadRelations();
bool saveRelations();
//!in App-Layer, it Split the messages into dealers.
int deal_current_message_block();
//!Message Dealers, imp in st_clientnode_msgdeal.cpp
......
......@@ -193,14 +193,14 @@ int st_clientNode_baseTrans::deal_current_message_block()
//First, get uuid as soon as possible
if (m_bUUIDRecieved==false)
{
if (m_currentHeader.source_id>= 0x00010000 && m_currentHeader.source_id <= 0x0FFFFFFF )
if (bIsValidEquipId( m_currentHeader.source_id) )
{
m_bUUIDRecieved = true;
m_uuid = m_currentHeader.source_id;
//regisit client node to hash-table;
m_pClientTable->regisitClientUUID(this);
}
else if (m_currentHeader.source_id>= (unsigned int)0x80000000 && m_currentHeader.source_id <= (unsigned int)0xAFFFFFFF )
else if (bIsValidUserId( m_currentHeader.source_id) )
{
m_bUUIDRecieved = true;
m_uuid = m_currentHeader.source_id;
......@@ -220,9 +220,9 @@ int st_clientNode_baseTrans::deal_current_message_block()
}
else
{
if (!((m_currentHeader.source_id>= 0x00010000 && m_currentHeader.source_id <= 0x0FFFFFFF )
if (!(bIsValidEquipId(m_currentHeader.source_id)
||
(m_currentHeader.source_id>= (unsigned int)0x80000000 && m_currentHeader.source_id <= (unsigned int)0xAFFFFFFF )
bIsValidUserId(m_currentHeader.source_id)
||
(m_currentHeader.source_id==0xffffffff)
))
......
......@@ -47,6 +47,10 @@ protected:
//!in Trans-Layer, it does nothing.
virtual int deal_current_message_block();
//judge whether id is valid.
bool bIsValidEquipId(quint32 id){return id >=0x00010000 && id <=0x0FFFFFFF;}
bool bIsValidUserId(quint32 id){return id >=(unsigned int)0x80000000 && id <=(unsigned int)0xAFFFFFFF;}
protected:
//The current Read Offset, from m_list_RawData's beginning
int m_currentReadOffset;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册