Skip to content

Commit

Permalink
Did some code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
Feqzz committed Aug 9, 2020
1 parent 19ea770 commit f8e458a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 180 deletions.
82 changes: 18 additions & 64 deletions handlers/tcpsockethandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,51 +172,20 @@ QTcpSocket *TcpSocketHandler::getSocket()
//Send header to server, and receive headers from other participants back
void TcpSocketHandler::writeHeader()
{
mHeader.prepend(mStreamId.toLocal8Bit().data());
mHeader.prepend(mStreamId.size());

mHeader.prepend(mDisplayName.toLocal8Bit().data());
mHeader.prepend(mDisplayName.size());

//Puts the roomId and its size at the front of the array
mHeader.prepend(mRoomId.toLocal8Bit().data());
mHeader.prepend(mRoomId.size());
prependDefaultHeader(mHeader);

qDebug() << "My Header: " << mHeader.length() << "\n" << mHeader;

mSocket->write(mHeader);
mHeader.clear();
}

QByteArray TcpSocketHandler::getHeader()
{
mHeader.prepend(mStreamId.toLocal8Bit().data());
mHeader.prepend(mStreamId.size());

mHeader.prepend(mDisplayName.toLocal8Bit().data());
mHeader.prepend(mDisplayName.size());

//Puts the roomId and its size at the front of the array
mHeader.prepend(mRoomId.toLocal8Bit().data());
mHeader.prepend(mRoomId.size());

return mHeader;
}

void TcpSocketHandler::sendChangedDisplayNameSignal()
{
QByteArray header;
header.append(NEW_DISPLAY_NAME);

header.prepend(mStreamId.toLocal8Bit().data());
header.prepend(mStreamId.size());

header.prepend(mDisplayName.toLocal8Bit().data());
header.prepend(mDisplayName.size());

//Puts the roomId and its size at the front of the array
header.prepend(mRoomId.toLocal8Bit().data());
header.prepend(mRoomId.size());
prependDefaultHeader(header);

qDebug() << "My Header: " << header.length() << "\n" << header;

Expand All @@ -230,16 +199,7 @@ void TcpSocketHandler::sendDisabledVideoSignal()
QByteArray header;
header.append(VIDEO_DISABLED);

header.prepend(mStreamId.toLocal8Bit().data());
header.prepend(mStreamId.size());

//Don't really need this, but removing it would make the server parser struggle.
header.prepend(mDisplayName.toLocal8Bit().data());
header.prepend(mDisplayName.size());

//Puts the roomId and its size at the front of the array
header.prepend(mRoomId.toLocal8Bit().data());
header.prepend(mRoomId.size());
prependDefaultHeader(header);

qDebug() << "My Header: " << header.length() << "\n" << header;

Expand All @@ -253,16 +213,7 @@ void TcpSocketHandler::sendDisabledAudioSignal()
QByteArray header;
header.append(AUDIO_DISABLED);

header.prepend(mStreamId.toLocal8Bit().data());
header.prepend(mStreamId.size());

//Don't really need this, but removing it would make the server parser struggle.
header.prepend(mDisplayName.toLocal8Bit().data());
header.prepend(mDisplayName.size());

//Puts the roomId and its size at the front of the array
header.prepend(mRoomId.toLocal8Bit().data());
header.prepend(mRoomId.size());
prependDefaultHeader(header);

qDebug() << "My Header: " << header.length() << "\n" << header;

Expand All @@ -278,30 +229,33 @@ void TcpSocketHandler::sendKickParticipantSignal(const QString& streamId)
header.append(streamId.size());
header.append(streamId.toLocal8Bit().data());

header.prepend(mStreamId.toLocal8Bit().data());
header.prepend(mStreamId.size());

//Don't really need this, but removing it would make the server parser struggle.
header.prepend(mDisplayName.toLocal8Bit().data());
header.prepend(mDisplayName.size());

//Puts the roomId and its size at the front of the array
header.prepend(mRoomId.toLocal8Bit().data());
header.prepend(mRoomId.size());
prependDefaultHeader(header);

qDebug() << "My Header: " << header.length() << "\n" << header;

mSocket->write(header);
header.clear();
}

void TcpSocketHandler::prependDefaultHeader(QByteArray& data) const
{
data.prepend(mStreamId.toLocal8Bit().data());
data.prepend(mStreamId.size());

//Don't really need this all the time, but removing it would make the server parser struggle.
data.prepend(mDisplayName.toLocal8Bit().data());
data.prepend(mDisplayName.size());

//Puts the roomId and its size at the front of the array
data.prepend(mRoomId.toLocal8Bit().data());
data.prepend(mRoomId.size());
}

void TcpSocketHandler::appendToHeader(const QByteArray &data)
{
mHeader.append(data);
}


void TcpSocketHandler::connected()
{
qDebug() << "Tcp socket connected to " << mAddress << "on port " << mPort;
Expand Down
16 changes: 6 additions & 10 deletions handlers/tcpsockethandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class TcpSocketHandler : public QObject
{
Q_OBJECT
public:
explicit TcpSocketHandler(InputStreamHandler* inputStreamHandler, QString streamId, QString roomId, QString displayName, QHostAddress address, int port = 1338, QObject* parent = nullptr);
explicit TcpSocketHandler(InputStreamHandler* inputStreamHandler, QString streamId, QString roomId, QString displayName, QHostAddress address, int port, QObject* parent = nullptr);
~TcpSocketHandler();
void updateDisplayName(const QString& displayName);
void sendChangedDisplayNameSignal();
Expand All @@ -32,10 +32,8 @@ class TcpSocketHandler : public QObject
bool isOpen() const;
bool isReady() const;
QByteArray getReply() const;
QByteArray getHeader();
QTcpSocket *getSocket();


public slots:
void writeHeader();
void connected();
Expand All @@ -44,23 +42,21 @@ public slots:
void readyRead();

private:
//Should match enum in Server::TcpServerHandler
enum mTcpReturnValues { STREAM_ID_NOT_FOUND, ROOM_ID_NOT_FOUND, SESSION_STARTED };
enum mTcpHeaderCodes { VIDEO_HEADER, REMOVE_PARTICIPANT, NEW_DISPLAY_NAME, VIDEO_DISABLED, AUDIO_DISABLED, KICK_PARTICIPANT };
QHostAddress mAddress;
void addStream();
void prependDefaultHeader(QByteArray& data) const;
int mPort;
int mBytesWritten;
bool mReady;
QHostAddress mAddress;
QTcpSocket* mSocket;
QByteArray mRequest;
QByteArray mReply;
QByteArray mHeader;
bool mReady;
QString mRoomId;
QString mStreamId;
QString mDisplayName;
InputStreamHandler* mInputStreamHandler;
std::vector<QByteArray> videoHeaders;
void addStream();

enum mTcpHeaderCodes { VIDEO_HEADER, REMOVE_PARTICIPANT, NEW_DISPLAY_NAME, VIDEO_DISABLED, AUDIO_DISABLED, KICK_PARTICIPANT };
};
#endif // TCPSOCKETHANDLER_H
41 changes: 7 additions & 34 deletions handlers/udpsockethandler.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "udpsockethandler.h"

UdpSocketHandler::UdpSocketHandler(int bufferSize, int _port, InputStreamHandler* inputStreamHandler,
QString streamId, QString roomId, QHostAddress address,QObject *parent) : QObject(parent)
QString streamId, QString roomId, QHostAddress address, QObject *parent) : QObject(parent)
{
mBufferSize = bufferSize;
mInputStreamHandler = inputStreamHandler;
Expand All @@ -14,16 +14,16 @@ UdpSocketHandler::UdpSocketHandler(int bufferSize, int _port, InputStreamHandler
//char buf[BUFLEN];
//char *message = "test message very many bytes to send with this message";

if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
if ((mCppUdpSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
exit(1);
}

memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(PORT);
si_other.sin_port = htons(mPort);

if (inet_aton(SERVER , &si_other.sin_addr) == 0)
if (inet_aton(mAddress.toString().toUtf8() , &si_other.sin_addr) == 0)
{
fprintf(stderr, "inet_aton() failed\n");
exit(1);
Expand Down Expand Up @@ -81,7 +81,7 @@ void UdpSocketHandler::readPendingDatagrams()
}*/

const int streamIdLength = data[0];
data.remove(0,1);
data.remove(0, 1);

//Finds the streamId header, stores it and removes it;
QByteArray streamIdArray = QByteArray(data, streamIdLength);
Expand Down Expand Up @@ -135,33 +135,6 @@ void UdpSocketHandler::readPendingDatagrams()
signalCount++;
}

/**
* When recieving a UDP datagram, we need to know who owns the stream.
* The index will let readPendingDatagrams know which buffer, mutex and playbackhandler to use for both audio and video.
* @param streamId QString to find in mStreamIdVector
* @return int index where streamId was found
*/
/*
int UdpSocketHandler::findStreamIdIndex(QString streamId)
{
if(mInputStreamHandler->getStreamIdVector().size() >= 1)
{
for(size_t i = 0; i < mInputStreamHandler->getStreamIdVector().size(); i++)
{
if(QString::compare(streamId, mInputStreamHandler->getStreamIdVector()[i], Qt::CaseSensitive) == 0)
{
return i;
}
}
}
//TODO handle this?
qDebug() << Q_FUNC_INFO << " failed to find streamId: " << streamId;
qDebug() << "This means the server is sending datagrams to people it should not";
qDebug() << "StreamIdVector: " << mInputStreamHandler->getStreamIdVector();
return -1;
}
*/
/**
* Send the QByteArray through the current udpSocket,
* if the size is larger than 512, it will be divided into smaller arrays.
Expand Down Expand Up @@ -242,13 +215,13 @@ int UdpSocketHandler::sendDatagram(QByteArray arr)
* @param data QByteArray containing data to be sent
* @return int with how many bytes sent, or the error code
*/
int UdpSocketHandler::sendArray(QByteArray data)
int UdpSocketHandler::sendArray(const QByteArray& data)
{
static bool openPortWithQUDP = false;
if(openPortWithQUDP)
{
//Returns number of bytes sent, or -1 or error
return sendto(s, data, data.size(), 0 , (struct sockaddr *) &si_other, slen);
return sendto(mCppUdpSocket, data, data.size(), 0 , (struct sockaddr *) &si_other, slen);
}
else
{
Expand Down
15 changes: 5 additions & 10 deletions handlers/udpsockethandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
#include<arpa/inet.h>
#include<sys/socket.h>

#define SERVER "46.250.220.57"
#define BUFLEN 512 //Max length of buffer
#define PORT 1337 //The port on which to send data

class VideoPlaybackHandler;
class AudioPlaybackHandler;
class InputStreamHandler;
Expand All @@ -37,20 +33,19 @@ public slots:
void readPendingDatagrams();

private:
int sendArray(QByteArray data);
struct sockaddr_in si_other;
int s, i, slen=sizeof(si_other);
void addStreamToVector(QString, int);
//int findStreamIdIndex(QString);
int sendArray(const QByteArray& data);
int mCppUdpSocket;
int slen = sizeof(si_other);
int mBufferSize;
int mPort;
QTcpSocket* mTcpSocket;
QUdpSocket* mUdpSocket;
QHostAddress mAddress;
int mPort;
QString mRoomId;
QString mStreamId;
InputStreamHandler* mInputStreamHandler;
uint signalCount = 0;
struct sockaddr_in si_other;
struct mBufferAndLockStruct
{
QByteArray* buffer;
Expand Down
Loading

0 comments on commit f8e458a

Please sign in to comment.