send: client send part.
This commit is contained in:
@@ -28,3 +28,51 @@ bool ClientCore::Connect(const QString& ip, quint16 port)
|
|||||||
void ClientCore::Disconnect()
|
void ClientCore::Disconnect()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ClientCore::onReadyRead()
|
||||||
|
{
|
||||||
|
QByteArray data = socket_->readAll();
|
||||||
|
recvBuffer_.append(data);
|
||||||
|
while (true) {
|
||||||
|
auto frame = Protocol::ParseBuffer(recvBuffer_);
|
||||||
|
if (frame == nullptr) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
UseFrame(frame);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClientCore::onDisconnected()
|
||||||
|
{
|
||||||
|
qCritical() << QString("client %1 disconnected...").arg(remoteID_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClientCore::UseFrame(QSharedPointer<FrameBuffer> frame)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ClientCore::Send(QSharedPointer<FrameBuffer> frame)
|
||||||
|
{
|
||||||
|
if (frame == nullptr) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
auto data = Protocol::PackBuffer(frame);
|
||||||
|
if (data.size() == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return Send(data.constData(), data.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ClientCore::Send(const char* data, qint64 len)
|
||||||
|
{
|
||||||
|
if (socket_->state() != QAbstractSocket::ConnectedState) {
|
||||||
|
qCritical() << QString("client %1 not connected...").arg(remoteID_);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
qint64 bytesWritten = socket_->write(data, len);
|
||||||
|
if (bytesWritten == -1 || !socket_->waitForBytesWritten(5000)) {
|
||||||
|
qCritical() << QString("Send data to server failed. %1").arg(socket_->errorString());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,15 +21,22 @@ public:
|
|||||||
bool Connect(const QString& ip, quint16 port);
|
bool Connect(const QString& ip, quint16 port);
|
||||||
void Disconnect();
|
void Disconnect();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void onReadyRead();
|
||||||
|
void onDisconnected();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void UseFrame(QSharedPointer<FrameBuffer> frame);
|
||||||
|
bool Send(QSharedPointer<FrameBuffer> frame);
|
||||||
|
bool Send(const char* data, qint64 len);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
QTcpSocket* socket_;
|
|
||||||
QMutex conMutex_;
|
QMutex conMutex_;
|
||||||
// QSharedPointer<ClientUserInterface> cf;
|
QString remoteID_;
|
||||||
|
QTcpSocket* socket_;
|
||||||
|
QByteArray recvBuffer_;
|
||||||
|
|
||||||
std::function<void(const QString& path)> pathCall_;
|
std::function<void(const QString& path)> pathCall_;
|
||||||
|
|
||||||
QString remoteID_;
|
|
||||||
QByteArray recvBuffer_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CLIENTCORE_H
|
#endif // CLIENTCORE_H
|
||||||
Reference in New Issue
Block a user