-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreInterface.cpp
More file actions
107 lines (97 loc) · 2.63 KB
/
PreInterface.cpp
File metadata and controls
107 lines (97 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "PreInterface.h"
#include "ui_PreInterface.h"
PreInterface::PreInterface(QWidget *parent) :
QDialog(parent),
ui(new Ui::PreInterface)
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
web=new Web();
}
PreInterface::~PreInterface()
{
delete ui;
}
void PreInterface::on_btnStart_clicked()
{
ui->labelTip->setText(tr("正在连接中,请稍候"));
timer = new QTimer;
web->socketA = new QTcpSocket;
web->socketA->connectToHost(QHostAddress("127.0.0.1"),8888);
/*if(web->socketA->waitForConnected(100))
{
qDebug("1");
web->connectNum+=1;
}*/
loop=new QEventLoop;
connect(web->socketA,SIGNAL(connected()),this,SLOT(on_connected()));
connect(timer,SIGNAL(timeout()),this,SLOT(on_timeout()));
timer->start(100);
loop->exec();
web->socketB = new QTcpSocket;
web->socketB->connectToHost(QHostAddress("127.0.0.1"),8899);
/*if(web->socketB->waitForConnected(100))
{
qDebug("2");
web->connectNum+=1;
}*/
loop=new QEventLoop;
connect(web->socketB,SIGNAL(connected()),this,SLOT(on_connected()));
connect(timer,SIGNAL(timeout()),this,SLOT(on_timeout()));
timer->start(100);
loop->exec();
if(web->connectNum==2){
web->Id=web->C;
this->accept();
}
else if(web->connectNum==1)
{
web->Id=web->B;
delete web->socketB;
web->socketB=nullptr;
web->server=new QTcpServer;
web->server->listen(QHostAddress("127.0.0.1"),8899);
connect(web->server,SIGNAL(newConnection()),this,SLOT(on_newConnection()));
}
else if(web->connectNum==0)
{
web->Id=web->A;
delete web->socketA;
delete web->socketB;
web->socketA=nullptr;
web->socketB=nullptr;
web->server=new QTcpServer;
web->server->listen(QHostAddress("127.0.0.1"),8888);
connect(web->server,SIGNAL(newConnection()),this,SLOT(on_newConnection()));
}
}
void PreInterface::on_connected()
{
web->connectNum+=1;
loop->exit();
}
void PreInterface::on_timeout()
{
timer->stop();
loop->exit();
}
void PreInterface::on_newConnection()
{
QTcpSocket *tmp=new QTcpSocket;
tmp=web->server->nextPendingConnection();
if(web->Id==web->A)
{
if(web->socketA==nullptr)web->socketA=tmp;
else web->socketB=tmp;
}
else if (web->Id==web->B)
{
web->socketB=tmp;
}
web->connectNum+=1;
if(web->connectNum==2)this->accept();
}
Web* PreInterface::getWeb()
{
return web;
}