#include "tcpclient.h"
#include <QDebug>
#include <dbinterface.h>
bool TcpClient::isconfig =false;
bool TcpClient::isdisplay =true;
const string TcpClient::HEX_DIGITS[16]={"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
TcpClient::TcpClient(QObject *parent) :
QObject(parent)
{
}
void TcpClient::setsock(QTcpSocket* p)
{
m_lasthearttime=QDateTime::currentDateTime();
ac_socket =p;
//ac_socket->moveToThread(pts);
connect(p,SIGNAL(readyRead()), this, SLOT(slot_readdata()));
connect(this,SIGNAL(ss()), this, SLOT(stop()));
connect(this,SIGNAL(se()), this, SLOT(getmsg_send()));
connect(this,SIGNAL(addrow1(string,string,QString,string)),&db,SLOT(addlogs11(string,string,QString,string)));
}
TcpClient::~TcpClient()
{
}
void TcpClient::setConfig(bool b)
{
printf("isconfig is %d\n",isconfig);
isconfig=b;
printf("isconfig is %d\n",isconfig);
fflush(stdout);
}
void TcpClient::init()
{
/*port = 8899;
if(( socket_fd = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0 ){
throw "socket() failed";
}
memset(&myserver,0,sizeof(myserver));
myserver.sin_family = AF_INET;
myserver.sin_addr.s_addr = htonl(INADDR_ANY);
myserver.sin_port = qToBigEndian(port);
if( bind(socket_fd,(sockaddr*) &myserver,sizeof(myserver)) < 0 ) {
printf("bind socket error: %s(errno:%d)\n)",strerror(errno),errno);
fflush(stdout);
return ; ;
}
if( listen(socket_fd,10) < 0 ) {
throw "listen() failed";
}
this->moveToThread(&acceptthread);
connect(&acceptthread,SIGNAL(started()),this,SLOT(acceptsocket()));
acceptthread.start();
*/
}
void TcpClient::start()
{
running=true;
if(!srthread.isRunning()){
this->moveToThread(&srthread);
connect(&srthread,SIGNAL(started()),this,SLOT(sendrecv()));
srthread.start();
}
//m_lasthearttime.fromMSecsSinceEpoch(0);
m_lasthearttime=QDateTime::currentDateTime();
connect(this,SIGNAL(addPacket(string,string,QTime,RecvStream)),&db,SLOT(addlog(string,string,QTime,RecvStream)));
connect(this,SIGNAL(addrow(string,string,QTime,string)),&db,SLOT(addlogs(string,string,QTime,string)));
connect(this,SIGNAL(addrow1(string,string,QString,string)),&db,SLOT(addlogs1(string,string,QString,string)));
}
void TcpClient::stop()
{
running =false;
ac_socket->close();
isRunning =false;
}
int TcpClient::getMsgFromBuf()
{
RecvStream recvstream,rs;
static int i=-1,j=-1;
static int t=0;
int r=0;
// QDateTime now=QDateTime::currentDateTime();
// if(m_lasthearttime.toMSecsSinceEpoch() != 0){
// //long span=now.currentMSecsSinceEpoch() - m_lasthearttime.currentMSecsSinceEpoch();
// int span=m_lasthearttime.secsTo(now);
// if (span >10) {
// isRunning=false;
// //emit ss();
// stop();
// return 0;
// }
// }
//mutexRecvStream.lock();
if(recvbuf.size>0)
{
r=recvbuf.getDataFromBuf(recvstream.stream, &(recvstream.size));
if ((i!= recvbuf.size)|| (j !=recvstream.size))
{
i= recvbuf.size;
j=recvstream.size;
printf("recvbuf size %d,r %d,recvstream.size %d\n", recvbuf.size,r,recvstream.size);
}
}
//mutexRecvStream.unlock();
if(r)
{
for(int i=0; i<recvstream.size;i++)
printf("%02x ", recvstream.stream[i]);
printf("\n");
fflush(stdout);
//r =0;
toOriginalMsg(recvstream.stream,recvstream.size, rs.stream,&rs.size);
int j = checkCode(rs.stream,rs.size);
if( j < 0)
{
//continue;
}
if(j ==1)
{
MsgHeader header;
int i = header.fromStream(rs.stream);
QTime ttt1;
QDate d1;
string s,s1,s2;
s = toHexStr((unsigned char*)header.phoneNumber,6);
//mw->ui->tableWidget->setItem(t,0,new QTableWidgetItem(s.c_str()));
MYWORD tmp=qFromBigEndian(header.msgId);
s1 = toHexStr((unsigned char*)&(tmp),2);
//mw->ui->tableWidget->setItem(t,1,new QTableWidgetItem(s.c_str()));
s2 = toHexStr(rs.stream,rs.size);
//mw->ui->tableWidget->setItem(t,3,new QTableWidgetItem(s1.c_str()));
ttt1=QTime::currentTime();
d1=QDate::currentDate();
QString sss=d1.toString("yyyy-MM-dd")+" "+ttt1.toString("hh:mm:ss.zzz");
//emit addPacket(s,s1,ttt1,rs);
// cout <<"**************"<< s1 << s2<< s;
emit addrow1(s,s1,sss,s2);
if(isdisplay)
emit addPacket(s,s1,s2);
t++;
m_lasthearttime=QDateTime::currentDateTime();
//cout << "msgid "<<hex<<header.msgId<<endl;
switch(header.msgId)
{
case 0x0100: handleRegister(&rs);break;
case 0x0102: handleAuthentication(&rs);break;
case 0x0002:handleheartbeat(&rs);
m_lasthearttime=QDateTime::currentDateTime();
break;
case 0x0704:handleblind(&rs);break;
case 0x0200:handleposition(&rs);break;
case 0x0705:handcan(&rs);break;
case 0x0fff: handlebox(&rs);break;
case 0x0001: handleboxconfig(&rs);break;
default:break;
}
}
}
return r;
//this->stop();
}
void TcpClient::handleboxconfig(RecvStream* prs)
{
TerminalAck ta;
ta.fromStream(prs->stream);
MYWORD sn=ta.serialNumber;
removesnfrommsglist(sn);
}
void TcpClient::removesnfrommsglist(MYWORD sn)
{
Msg *pmsg = NULL;
list<Msg>::iterator it1;
mutex.lock();
for(it1 = msgList.begin(); it1 != msgList.end(); )
{
pmsg = &(*it1);
printf("p->sn %d sn %d isack iscomplete\n",pmsg->msgSerialNumber,sn,pmsg->complete );
if((pmsg->msgSerialNumber == sn)&&(!pmsg->isAck)&&(pmsg->complete))
//if((pmsg->complete)&&((pmsg->resendTimes >= m_resendtimes)||(pmsg->isAck)))
{
msgList.erase(it1++);
}
else
{
++it1;
}
}
mutex.unlock();
}
void gettime(MYBCD tttt[6])
{
int h= QTime::currentTime().hour();
int m=QTime::currentTime().minute();
int s=QTime::currentTime().second();
int y=QDate::currentDate().year();
int M=QDate::currentDate().month();
int d=QDate::currentDate().day();
printf("y %d %d %d %d %d %d\n", y,M,d,h,m,s);
fflush(stdout);
tttt[0]=(((y%100/10)&0x0f)<<4)|((y%10)&0x0f);
tttt[1]=(((M/10)&0x0f)<<4)|((M%10)&0x0f);
tttt[2]=(((d/10)&0x0f)<<4)|((d%10)&0x0f);
tttt[3]=(((h/10)&0x0f)<<4)|((h%10)&0x0f);
tttt[4]=(((m/10)&0x0f)<<4)|((m%10)&0x0f);
tttt[5]=(((s/10)&0x0f)<<4)|((s%10)&0x0f);
for(int i=0; i<6;i++)
printf("%02x ", tttt[i]);
printf("\n");
fflush(stdout);
}
void setTime(TrushBoxAck *ta)
{
int h,m;
QTime qti=QTime::currentTime();
QTime st =qti.addSecs(1500);
QTime ed =qti.addSecs(2880);
h=st.hour(); m=st.minute();
ta->worktime[0]=(((h/10)&0x0f)<<4)|((h%10)&0x0f);
ta->worktime[1]=(((m/10)&0x0f)<<4)|((m%10)&0x0f);
h=ed.hour(); m=ed.minute();
ta->worktime[2]=(((h/10)&0x0f)<<4)|((h%10)&0x0f);
ta->worktime[3]=(((m/10)&0x0f)<<4)|((m%10)&0x0f);
for(int i
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
server1displayflush.zip (17个子文件)
server1
server.pro.user 103KB
mainwindow.ui 6KB
dbinterface.cpp 6KB
dbinterface.h 783B
tcpserverlisten.cpp 3KB
mainwindow.cpp 3KB
tcpserverlisten.h 600B
tcpclient.cpp 21KB
server.pro.user.ff59762 18KB
tcpserver.h 1KB
main.cpp 876B
tcpclient.h 3KB
server.pro.user.3.2-pre1 18KB
mainwindow.h 760B
tcpserver.cpp 7KB
datastruct.h 15KB
server.pro 567B
共 17 条
- 1
资源评论
- weixin_454378882020-06-30不行行不行想想
- 超网8882017-11-06这个怎么样,还没试
lsoftp
- 粉丝: 0
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功