Skip to content

Commit

Permalink
更新到v1.0.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
code4lala committed Mar 19, 2019
1 parent 41770bb commit 9cbe0fd
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 73 deletions.
4 changes: 3 additions & 1 deletion DrCOM_JLU_Qt.pro
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ RESOURCES += \
include(singleinstance/singleapplication.pri)
DEFINES += QAPPLICATION_CLASS=QApplication

VERSION = 1.0.0.2
VERSION = 1.0.0.4

# 更新日志:
# v 0.0.0.0 实现基本功能
# v 1.0.0.1 修复适配高DPI时只窗口大小适配但字号不适配的bug
# v 1.0.0.2 增加重启功能(能解决一些网络的错误
# 调整字体为微软雅黑10号(就是win下正常的字体
# v 1.0.0.3 没有这个版本,上次该发布0.2版本时候压缩包名字打错了。。。应该为1.0.0.2的,所以跳过这个版本号
# v 1.0.0.4 优化用户体验,调整掉线时的提示信息,增加掉线时直接重启客户端的提示

5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,8 @@ v 0.0.0.0 实现基本功能
v 1.0.0.1 修复适配高DPI时只窗口大小适配但字号不适配的bug

v 1.0.0.2 增加重启功能,调整字体为微软雅黑10号

v 1.0.0.3 没有这个版本,上次该发布0.2版本时候压缩包名字打错了。。。应该为1.0.0.2的,所以跳过这个版本号

v 1.0.0.4 优化用户体验,调整掉线时的提示信息,增加掉线时直接重启客户端的提示

49 changes: 20 additions & 29 deletions dogcom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void DogCom::print_packet(const char msg[], const unsigned char *packet, int len

void DogCom::run()
{
// 每一个ReportOffline后边都跟着一个return语句,防止程序逻辑混乱
// qDebug()<<"account:"<<account;
// qDebug()<<"password:"<<password;
// qDebug()<<"mac_addr:"<<mac_addr;
Expand All @@ -52,9 +53,8 @@ void DogCom::run()
PORT_BIND)){
qDebug()<<"Bind Failed!";
qDebug()<<udp_sender.error();
emit ReportOffline(OFF_BIND_FAILED);
// 清理操作
sleeper->Interrupt();
emit ReportOffline(OFF_BIND_FAILED);
udp_sender.close();
return;
}else{
Expand All @@ -65,24 +65,24 @@ void DogCom::run()
unsigned char auth_information[16];
if(!dhcp_challenge(udp_sender,seed)){
emit ReportOffline(OFF_CHALLENGE_FAILED);
udp_sender.close();
return;
}else{
//challenge成功 开始登录
qDebug()<<"trying to login...";
// 此时还没有确定在线,用户不能执行注销操作
// 因此可以放心地使用Sleep而不必担心被Interrupt
sleeper->Sleep(200);// 0.2 sec
qDebug()<<"Wait for 0.2 second done.";
int offLineReason;
if((offLineReason=dhcp_login(udp_sender,seed,auth_information))==-1){
// 登录成功
emit ReportOnline();
int keepalive_counter = 0;
int keepalive_try_counter = 0;
int first = 1;
while (true) {
if(udp_sender.state()!=QUdpSocket::BoundState){
emit ReportOffline(OFF_TIMEOUT);
break;
udp_sender.close();
return;
}
if (!keepalive_1(udp_sender, auth_information)) {
sleeper->Sleep(200); // 0.2 second
Expand All @@ -93,32 +93,23 @@ void DogCom::run()
if(!sleeper->Sleep(20000)){
qDebug()<<"Interruptted by user";
emit ReportOffline(OFF_USER_LOGOUT);
break;
}
} else {
// 这个 keepalive_try_counter 在这里貌似没什么用,因为Qt的QUdpSocket有一个state特别敏感
// 明明网线啥的都没问题就说当前是UnconnectedStat状态然后还不给你自动重连
// 所以外层循环先给判断了一下当前状态,如果这玩意报告说掉线了那就直接退出循环发包
// 另: 目前测试还没有遇到过外层state正常然后里边keepalive_try_counter增加计数的情况
if (keepalive_try_counter > 3) {
// 清理操作
emit ReportOffline(OFF_TIMEOUT);
sleeper->Interrupt();
udp_sender.close();
return;
}
keepalive_try_counter++;
continue;
} else {
// 清理操作
emit ReportOffline(OFF_TIMEOUT);
udp_sender.close();
return;
}
}
}else{
// 登录失败,提示用户失败原因
emit ReportOffline(offLineReason);
udp_sender.close();
return;
}
}
// 清理操作
sleeper->Interrupt();
udp_sender.close();
}

bool DogCom::dhcp_challenge(QUdpSocket &udp_sender, unsigned char seed[])
Expand All @@ -138,7 +129,7 @@ bool DogCom::dhcp_challenge(QUdpSocket &udp_sender, unsigned char seed[])
print_packet("[Challenge sent]", challenge_packet, 20);

qDebug()<<"reading from dest...";
udp_sender.waitForReadyRead(1000);
udp_sender.waitForReadyRead(1500);
if(udp_sender.readDatagram((char*)recv_packet,1024,server_address,&port_dest)<=0){
qDebug()<<"Failed to recv";
qDebug()<<udp_sender.error();
Expand Down Expand Up @@ -331,7 +322,7 @@ int DogCom::dhcp_login(QUdpSocket &udp_sender, unsigned char seed[], unsigned ch
udp_sender.writeDatagram((const char*)login_packet,login_packet_size,*server_address,port_dest);
print_packet("[Login sent]",login_packet,login_packet_size);

udp_sender.waitForReadyRead(1000);
udp_sender.waitForReadyRead(1500); // 注意这个包来得特别慢。。。。。。其他都等100ms就可以了这个500ms都不行。。。。。。
if(udp_sender.readDatagram((char*)recv_packet,1024,server_address,&port_dest)<=0){
qDebug()<<udp_sender.error();
qDebug()<<"Failed to recv data";
Expand Down Expand Up @@ -377,7 +368,7 @@ int DogCom::keepalive_1(QUdpSocket &udp_sender, unsigned char auth_information[]
udp_sender.writeDatagram((const char*)keepalive_1_packet1,8,*server_address,port_dest);
print_packet("[Keepalive1 sent]",keepalive_1_packet1,42);
while (1) {
udp_sender.waitForReadyRead();
udp_sender.waitForReadyRead(1500);
if(udp_sender.readDatagram((char*)recv_packet1,1024,server_address,&port_dest)<=0){
qDebug()<<"Failed to recv data";
qDebug()<<udp_sender.error();
Expand Down Expand Up @@ -411,7 +402,7 @@ int DogCom::keepalive_1(QUdpSocket &udp_sender, unsigned char auth_information[]

udp_sender.writeDatagram((const char*)keepalive_1_packet2,42,*server_address,port_dest);

udp_sender.waitForReadyRead();
udp_sender.waitForReadyRead(1500);
if(udp_sender.readDatagram((char*)recv_packet2,1024,server_address,&port_dest)<=0){
qDebug()<<"Failed to recv data";
qDebug()<<udp_sender.error();
Expand Down Expand Up @@ -440,7 +431,7 @@ int DogCom::keepalive_2(QUdpSocket &udp_sender, int *keepalive_counter, int *fir
udp_sender.writeDatagram((const char*)keepalive_2_packet,40,*server_address,port_dest);

print_packet("[Keepalive2_file sent]",keepalive_2_packet,40);
udp_sender.waitForReadyRead();
udp_sender.waitForReadyRead(1500);
if(udp_sender.readDatagram((char*)recv_packet,1024,server_address,&port_dest)<=0){
qDebug()<<udp_sender.error();
qDebug()<<"Failed to recv data";
Expand Down Expand Up @@ -470,7 +461,7 @@ int DogCom::keepalive_2(QUdpSocket &udp_sender, int *keepalive_counter, int *fir

print_packet("[Keepalive2_A sent]",keepalive_2_packet,40);

udp_sender.waitForReadyRead();
udp_sender.waitForReadyRead(1500);
if(udp_sender.readDatagram((char*)recv_packet,1024,server_address,&port_dest)<=0){
qDebug()<<udp_sender.error();
qDebug()<<"Failed to recv data";
Expand All @@ -496,7 +487,7 @@ int DogCom::keepalive_2(QUdpSocket &udp_sender, int *keepalive_counter, int *fir

print_packet("[Keepalive2_C sent]",keepalive_2_packet,40);

udp_sender.waitForReadyRead();
udp_sender.waitForReadyRead(1500);
if(udp_sender.readDatagram((char*)recv_packet,1024,server_address,&port_dest)<=0){
qDebug()<<udp_sender.error();
qDebug()<<"Failed to recv data";
Expand Down
3 changes: 0 additions & 3 deletions dogcomcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ DogcomController::DogcomController()
}

void DogcomController::Login(const QString &account,const QString &password,const QString &mac_addr){
qDebug()<<"Resetting sleeper...";
sleeper->Reset();
qDebug()<<"Reset sleeper done.";
qDebug()<<"Filling config...";
dogcom->FillConfig(account,password,mac_addr);
qDebug()<<"Fill config done.";
Expand Down
23 changes: 16 additions & 7 deletions interruptiblesleeper.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
#include "interruptiblesleeper.h"
#include <QDebug>

InterruptibleSleeper::InterruptibleSleeper(QObject *parent) : QObject(parent)
{
qDebug()<<"InterruptibleSleepe Constructor";
}

bool InterruptibleSleeper::Sleep(int timeout)
{
return !m.tryLock(timeout);
//获取到锁 睡眠失败 被中断了
//未获取到锁 睡眠成功 没有被中断
}

void InterruptibleSleeper::Reset(){
m.lock();
qDebug()<<"tryLock...";
m.tryLock();
qDebug()<<"locked!";
qDebug()<<"tryLock for"<<timeout<<"millseconds...";
if(m.tryLock(timeout)){
qDebug()<<"Interrupted! lock succeed";
//获取到锁 睡眠失败 被中断了
return false;
}else{
qDebug()<<"lock failed! sleep succeed";
//未获取到锁 睡眠成功 没有被中断
m.unlock();
return true;
}
}

void InterruptibleSleeper::Interrupt(){
Expand Down
5 changes: 2 additions & 3 deletions interruptiblesleeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

/**
* @brief The InterruptibleSleeper class
* 使用说明:先Reset,然后Sleep指定时长即可,单位是ms
* 使用说明:直接Sleep指定时长即可,单位是ms
* 中止后台时调用Interrupt,会立即响应
* Interrupt之后想重新用,再Reset一下,然后Sleep指定时长即可
* 通过判断Sleep的返回值确定是否被中断,true即睡眠成功未被中断,false即被中断
*/
class InterruptibleSleeper : public QObject
{
Expand All @@ -17,7 +17,6 @@ class InterruptibleSleeper : public QObject
explicit InterruptibleSleeper(QObject *parent = nullptr);
// 睡眠成功返回 true 被中断返回 false
bool Sleep(int timeout);
void Reset();
void Interrupt();

private:
Expand Down
28 changes: 26 additions & 2 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,18 @@ void MainWindow::HandleOffline(int reason)
break;
}
case OFF_CHALLENGE_FAILED:{
QMessageBox::critical(this,tr("Login failed"),tr("Challenge failed. Please check your connection:)"));
// 弹出一个提示框,带一个直接重启客户端的按钮
QMessageBox msgBox;
msgBox.setText(tr("Login failed")+" "+tr("Challenge failed. Please check your connection:)")+" "+
tr("Attention that you should connect to wifi or wired firstly and then start the drcom client. If you have connected, you may restart drcom to solve the problem.")
+" "+tr("Restart DrCOM?"));
QAbstractButton *pButtonYes=msgBox.addButton(tr("Yes"),QMessageBox::YesRole);
msgBox.addButton(tr("Nope"),QMessageBox::NoRole);
msgBox.exec();
if(msgBox.clickedButton()==pButtonYes){
qDebug()<<"Restart DrCOM confirmed";
RestartDrcom();
}
break;
}
case OFF_CHECK_MAC:{
Expand Down Expand Up @@ -347,7 +358,18 @@ void MainWindow::HandleOffline(int reason)
break;
}
case OFF_TIMEOUT:{
QMessageBox::critical(this,tr("You have been offline"),tr("Time out, please check your connection"));
// 弹出一个提示框,带一个直接重启客户端的按钮
QMessageBox msgBox;
msgBox.setText(tr("You have been offline")+" "+tr("Time out, please check your connection")
+" "+tr("Due to some reasons, you should connect to wifi or wired firstly and then start the drcom client. So you may not login until you restart DrCOM :D")
+" "+tr("Restart DrCOM?"));
QAbstractButton *pButtonYes=msgBox.addButton(tr("Yes"),QMessageBox::YesRole);
msgBox.addButton(tr("Nope"),QMessageBox::NoRole);
msgBox.exec();
if(msgBox.clickedButton()==pButtonYes){
qDebug()<<"Restart DrCOM confirmed";
RestartDrcom();
}
break;
}
case OFF_UNKNOWN:
Expand All @@ -368,6 +390,8 @@ void MainWindow::HandleOffline(int reason)
SetIcon(false);
// 禁用注销按钮
DisableLogOutButton(true);
// 显示出窗口
ShowLoginWindow();
}

void MainWindow::HandleLoggedIn()
Expand Down
Binary file modified ts/DrCOM_zh_CN.qm
Binary file not shown.
Loading

0 comments on commit 9cbe0fd

Please sign in to comment.