Skip to content

Commit

Permalink
创建会议 协议 接收
Browse files Browse the repository at this point in the history
  • Loading branch information
muleimulei committed Jun 9, 2021
1 parent 4867700 commit 96018fe
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
64 changes: 59 additions & 5 deletions mytcpsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ MyTcpSocket::MyTcpSocket()
connect(_socktcp, SIGNAL(readyRead()), this, SLOT(recvFromSocket())); //接受数据
connect(_socktcp, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(errorDetect(QAbstractSocket::SocketError)));
qRegisterMetaType<QAbstractSocket::SocketError>();

}

void MyTcpSocket::errorDetect(QAbstractSocket::SocketError error)
Expand All @@ -26,8 +27,6 @@ void MyTcpSocket::errorDetect(QAbstractSocket::SocketError error)

void MyTcpSocket::run()
{


quint64 bytestowrite = 0;
//构造消息头
sendbuf[0] = '$';
Expand Down Expand Up @@ -88,11 +87,66 @@ void MyTcpSocket::run()
}
}



quint64 MyTcpSocket::readn(char * buf, quint64 maxsize, int n)
{
quint64 hastoread = n;
quint64 hasread = 0;
do
{
qint64 ret = _socktcp->read(buf + hasread, hastoread );
if(ret == 0)
{
return hasread;
}
hasread += ret;
hastoread -= ret;
}while(hastoread > 0 && hasread < maxsize);
return hasread;
}


void MyTcpSocket::recvFromSocket()
{
char buf[100];
_socktcp->read(buf, 100);
qDebug() << QString(buf);
char *buf = (char *)malloc(4 * MB);
quint64 rlen = this->readn(buf, 4 * MB, 11); // 11 消息头长度
if(rlen < 11)
{
qDebug() << "data size < 11";
free(buf);
return;
}
qDebug() << "datalen = " << rlen;
if(buf[0] == '$')
{
MSG_TYPE msgtype;
qFromBigEndian<quint16>(buf + 1, 2, &msgtype);
if(msgtype == CREATE_MEETING_RESPONSE)
{
quint32 datalen;
qFromBigEndian<quint32>(buf + 7, 4, &datalen);

//read data
rlen = this->readn(buf, 4 * MB, datalen + 1);
if(rlen < datalen + 1)
{
qDebug() << "data size < datalen + 1";
free(buf);
return;
}
else
{
qint32 room;
qFromBigEndian<qint32>(buf, 4, &room);
qDebug() << room;
}
}
}
else
{
qDebug() << "data format error";
}
}

MyTcpSocket::~MyTcpSocket()
Expand Down
1 change: 1 addition & 0 deletions mytcpsocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class MyTcpSocket: public QThread
void disconnectFromHost();
private:
void run() override;
quint64 readn(char *, quint64, int);
QTcpSocket *_socktcp;
QThread *_sockThread;
uchar *sendbuf;
Expand Down
8 changes: 7 additions & 1 deletion netheader.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include <QImage>
#include <QWaitCondition>
#define QUEUE_MAXSIZE 1500
#ifndef MB
#define MB 1024*1024
#endif
enum MSG_TYPE
{
IMG_SEND = 0,
Expand All @@ -16,7 +19,10 @@ enum MSG_TYPE
TEXT_RECV,
CREATE_MEETING,
EXIT_MEETING,
JOIN_MEETING
JOIN_MEETING,

CREATE_MEETING_RESPONSE = 20,

};

enum IMG_FORMAT
Expand Down

0 comments on commit 96018fe

Please sign in to comment.