-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
177 lines (166 loc) · 5.49 KB
/
client.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "client.h"
#include <QDateTime>
Client::Client(QObject *parent) : QObject(parent)
{
qInfo() << "Client::构造客户端";
}
Client::~Client()
{
qInfo() << "Client::析构客户端";
if(socket)
{
socket->close();
delete socket;
socket = nullptr;
}
}
void Client::connectToServer(QUrl url, int port)
{
qDebug() << "Client::客户端连接到服务器" << url << ":"<< port;
socket = new QTcpSocket(this);
socket->connectToHost(QHostAddress(url.toString()), port);
qDebug() << "Client::打印tcpsocket的端口号: " << socket->localPort();
waitForConnection();
connect(socket, &QTcpSocket::readyRead, this, &Client::readFromServer);
connect(socket, &QTcpSocket::disconnected, this, [=]()
{
qDebug() << "Client::客户端断开连接";
socket->deleteLater();
socket = nullptr;
});
}
void Client::connectToLocalServer(int port)
{
socket = new QTcpSocket(this);
socket->connectToHost(QHostAddress(QHostAddress::LocalHost), port);
waitForConnection();
qDebug() << "Client::打印tcpsocket的端口: " << socket->localPort();
connect(socket, &QTcpSocket::readyRead, this, &Client::readFromServer);
connect(socket,&QTcpSocket::disconnected,this,[=]()
{
qDebug() << "Client::客户端断开连接";
socket->deleteLater();
socket = nullptr;
});
}
void Client::waitForConnection()
{
if (socket->waitForConnected(-1))
{
qDebug() << "Client::客户端连接成功";
}
else
{
qDebug() << "Client::客户端连接失败,失败原因: " << socket->errorString();
}
}
//同步消息,聊天消息,用户消息,播放源消息
void Client::writeToServer(QString msg, QString type) //type can be "msg","snc","usr","url",""prt
{
qInfo() << "Client::发送消息给服务器,msg: " << msg << ",type: " << type;
QByteArray buffer = type.toUtf8() + msg.toUtf8() + "\n"; //canReadLine,先发送类型
socket->write(buffer);
socket->flush(); //立即发送数据
}
void Client::disconnect()
{
socket->disconnect();
socket->disconnectFromHost();
}
//HTTP服务使用
QString Client::getPeerIpAddress() const
{
return socket->peerAddress().toString();
}
void Client::readFromServer()
{
while (socket->canReadLine())
{
QByteArray buffer = socket->readLine();
qDebug() << "Client::读取缓冲区的内容: " << buffer;
QString header = buffer.mid(0, 3);
QString content = buffer.mid(3).trimmed();
qInfo() << "Client::从服务端读取数据,header: " << header << ", content: " << content;
if (header == "snc")
{
qInfo() << "Client::来自服务端的同步消息: " << content;
QString syncType = content.mid(0, 4);
qint64 position = content.mid(4).toLongLong();
if (syncType == "play")
{
emit remotePlay(position);
}
else if (syncType == "paus")
{
emit remotePause(position);
}
else if (syncType == "stop")
{
emit remoteStop();
}
else if (syncType == "seek")
{
emit remoteSeek(position);
}
//服务端每一秒的同步消息
else if (syncType == "sync")
{
emit remoteSync(position);
}
}
else if (header == "msg")
{
emit newChatMsg(content);
}
else if (header == "src")
{
QString srcType = content.mid(0, 3);
content = content.mid(3);
if (srcType == "net")
{
emit remoteSetVideoSource(content);
}
if (srcType == "lcl")
{
qDebug() << "Client::视频源是本地文件: " << content;
emit remoteSetLocalVideoSource(content);
}
emit newChatMsg("设置播放源: " + content); //将播放源路径发送到聊天列表
}
else if (header == "usr")
{
QString usrType = content.mid(0,3); //con or dis
QString usrName = content.mid(3);
if (usrType == "con")
{
qDebug() << "Client::用户连接";
// 非服务端客户端连接,先添加已经存在的客户端列表到参与者列表
emit userConnected(usrName); //发射信号将用户添加到参与者列表中
emit newChatMsg(QString(usrName + " connected.")); //添加到聊天列表中
}
else if (usrType == "dis")
{
emit userDisconnected(usrName);
emit newChatMsg(QString(usrName + " disconnected."));
}
else if (usrType == "lst")
{
//服务端发送过来的时候,每一个客户端都用&&分割,这里就分割掉&&,获取每一个客户端
QStringList userList = usrName.split("&&");
for (const auto &user : userList)
{
//用户连接
qDebug() << "Client::添加客户端列表到参与者列表";
emit userConnected(user);
}
}
}
}
}
//设置UDP端口,发送给服务端
void Client::setUdpPort(quint16 udpPort)
{
qInfo() << "Client::设置Udp端口: " << udpPort;
writeToServer(QString::number(udpPort), "prt");
//prt+port
}