diff --git a/src/PXMessenger.pro b/src/PXMessenger.pro index fece8dc..c275cde 100644 --- a/src/PXMessenger.pro +++ b/src/PXMessenger.pro @@ -1,6 +1,6 @@ TEMPLATE = app TARGET = PXMessenger -VERSION = 1.2.0 +VERSION = 1.2.1 QMAKE_TARGET_COMPANY = Bolar Code Solutions QMAKE_TARGET_PRODUCT = PXMessenger QMAKE_TARGET_DESCRIPTION = Instant Messenger @@ -37,7 +37,8 @@ SOURCES += \ pxmessenger.cpp \ pxmdebugwindow.cpp \ pxmsettingsdialog.cpp \ - pxmtextbrowser.cpp + pxmtextbrowser.cpp \ + uuidcompression.cpp HEADERS += \ pxmdefinitions.h \ @@ -50,7 +51,8 @@ HEADERS += \ pxmserver.h \ pxmclient.h \ pxmdebugwindow.h \ - pxmtextbrowser.h + pxmtextbrowser.h \ + uuidcompression.h DISTFILES += diff --git a/src/pxmclient.cpp b/src/pxmclient.cpp index a324fa3..1ff2b7d 100644 --- a/src/pxmclient.cpp +++ b/src/pxmclient.cpp @@ -66,7 +66,6 @@ void PXMClient::connectToPeer(evutil_socket_t, sockaddr_in socketAddr, buffereve void PXMClient::sendMsg(BevWrapper *bw, const char *msg, size_t msgLen, PXMConsts::MESSAGE_TYPE type, QUuid uuidSender, QUuid uuidReceiver) { - using PXMConsts::MSG; int bytesSent = 0; int packetLen; uint16_t packetLenNBO; @@ -77,9 +76,9 @@ void PXMClient::sendMsg(BevWrapper *bw, const char *msg, size_t msgLen, PXMConst emit resultOfTCPSend(-1, uuidReceiver, QString("Message too Long!"), print, bw); return; } - packetLen = PXMConsts::PACKED_UUID_BYTE_LENGTH + sizeof(uint8_t) + msgLen; + packetLen = UUIDCompression::PACKED_UUID_BYTE_LENGTH + sizeof(uint8_t) + msgLen; - if(type == MSG) + if(type == PXMConsts::MSG_TEXT) { print = true; } @@ -88,9 +87,9 @@ void PXMClient::sendMsg(BevWrapper *bw, const char *msg, size_t msgLen, PXMConst packetLenNBO = htons(packetLen); - packUuid(full_mess, &uuidSender); - memcpy(full_mess+PXMConsts::PACKED_UUID_BYTE_LENGTH, &type, sizeof(uint8_t)); - memcpy(full_mess+PXMConsts::PACKED_UUID_BYTE_LENGTH+sizeof(uint8_t), msg, msgLen); + UUIDCompression::packUUID(full_mess, &uuidSender); + memcpy(full_mess+UUIDCompression::PACKED_UUID_BYTE_LENGTH, &type, sizeof(uint8_t)); + memcpy(full_mess+UUIDCompression::PACKED_UUID_BYTE_LENGTH+sizeof(uint8_t), msg, msgLen); full_mess[packetLen] = 0; bw->lockBev(); @@ -119,25 +118,6 @@ void PXMClient::sendMsgSlot(BevWrapper *bw, QByteArray msg, PXMConsts::MESSAGE_T { this->sendMsg(bw, msg.constData(), msg.length(), type, uuid, theiruuid); } -size_t PXMClient::packUuid(char *buf, QUuid *uuid) -{ - int index = 0; - - uint32_t uuidSectionL = htonl(uuid->data1); - memcpy(&buf[index], &(uuidSectionL), sizeof(uint32_t)); - index += sizeof(uint32_t); - uint16_t uuidSectionS = htons(uuid->data2); - memcpy(&buf[index], &(uuidSectionS), sizeof(uint16_t)); - index += sizeof(uint16_t); - uuidSectionS = htons(uuid->data3); - memcpy(&buf[index], &(uuidSectionS), sizeof(uint16_t)); - index += sizeof(uint16_t); - unsigned char *uuidSectionC = uuid->data4; - memcpy(&buf[index], uuidSectionC, 8); - index += 8; - - return index; -} void PXMClient::sendIpsSlot(BevWrapper *bw, char *msg, size_t len, PXMConsts::MESSAGE_TYPE type, QUuid uuid, QUuid theiruuid) { diff --git a/src/pxmclient.h b/src/pxmclient.h index 5e4b3ee..16aa720 100644 --- a/src/pxmclient.h +++ b/src/pxmclient.h @@ -17,6 +17,7 @@ #include #include "pxmdefinitions.h" +#include "uuidcompression.h" #ifdef _WIN32 #include @@ -36,7 +37,6 @@ class PXMClient : public QObject public: PXMClient(QObject *parent, in_addr multicast); ~PXMClient() {qDebug() << "Shutdown of PXMClient Successful";} - static size_t packUuid(char *buf, QUuid *uuid); public slots: void sendMsg(BevWrapper *bw, const char *msg, size_t msgLen, PXMConsts::MESSAGE_TYPE type, QUuid uuidSender, QUuid uuidReceiver); /*! @@ -61,7 +61,7 @@ public slots: * \param msg Message to send * \param port Port to send to in the multicast group */ - int sendUDP(const char *msg, unsigned short port); + int sendUDP(const char* msg, unsigned short port); /*! * \brief connectToPeer * diff --git a/src/pxmdefinitions.h b/src/pxmdefinitions.h index 412a49f..4831eba 100644 --- a/src/pxmdefinitions.h +++ b/src/pxmdefinitions.h @@ -21,8 +21,7 @@ Q_DECLARE_METATYPE(bufferevent*); namespace PXMConsts { const int BACKLOG = 200; -const char* const DEFAULT_MULTICAST_ADDRESS = "239.192.13.13"; -const size_t PACKED_UUID_BYTE_LENGTH = 16; +const char * const DEFAULT_MULTICAST_ADDRESS = "239.192.13.13"; const int MESSAGE_HISTORY_LENGTH = 500; const int MIDNIGHT_TIMER_INTERVAL_MINUTES = 1; #ifdef QT_DEBUG @@ -33,8 +32,9 @@ const int DEBUG_PADDING = 0; const unsigned short DEFAULT_UDP_PORT = 53273; const int TEXT_EDIT_MAX_LENGTH = 2000; const int MAX_HOSTNAME_LENGTH = 24; -enum MESSAGE_TYPE : uint8_t {UUID = 1, MSG, SYNC, SYNC_REQUEST, GLOBAL}; +enum MESSAGE_TYPE : const uint8_t {MSG_UUID = 1, MSG_TEXT, MSG_SYNC, MSG_SYNC_REQUEST, MSG_GLOBAL, MSG_DISCOVER = 48, MSG_INFO}; } +Q_DECLARE_METATYPE(PXMConsts::MESSAGE_TYPE); class BevWrapper { public: @@ -51,46 +51,47 @@ class BevWrapper { }; struct peerDetails{ - bool isConnected; - bool isAuthenticated; - evutil_socket_t socketDescriptor; + QUuid identifier; sockaddr_in ipAddressRaw; QString hostname; QLinkedList messages; - QUuid identifier; BevWrapper *bw; + evutil_socket_t socketDescriptor; + bool isConnected; + bool isAuthenticated; peerDetails() { - isConnected = false; - isAuthenticated = false; - socketDescriptor = -1; + identifier = QUuid(); memset(&ipAddressRaw, 0, sizeof(sockaddr_in)); - messages = QLinkedList(); hostname = QString(); - identifier = QUuid(); + messages = QLinkedList(); bw = new BevWrapper(); + socketDescriptor = -1; + isConnected = false; + isAuthenticated = false; } peerDetails(bool iC, bool iA, evutil_socket_t sD, sockaddr_in iAR, QString h, QLinkedList m, QUuid iD, BevWrapper *bw) { - isConnected = iC; - isAuthenticated = iA; - socketDescriptor = sD; + identifier = iD; ipAddressRaw = iAR; hostname = h; messages = m; - identifier = iD; this->bw = bw; + socketDescriptor = sD; + isConnected = iC; + isAuthenticated = iA; } - peerDetails(const peerDetails& p) : isConnected(p.isConnected), - isAuthenticated(p.isAuthenticated), - socketDescriptor(p.socketDescriptor), + peerDetails(const peerDetails& p) : + identifier(p.identifier), ipAddressRaw(p.ipAddressRaw), hostname(p.hostname), messages(p.messages), - identifier(p.identifier), - bw(p.bw) {} + bw(p.bw), + socketDescriptor(p.socketDescriptor), + isConnected(p.isConnected), + isAuthenticated(p.isAuthenticated) {} }; struct initialSettings{ int uuidNum; diff --git a/src/pxmessenger.cpp b/src/pxmessenger.cpp index 5ca6dd4..1dd5f0b 100644 --- a/src/pxmessenger.cpp +++ b/src/pxmessenger.cpp @@ -65,6 +65,7 @@ int main(int argc, char **argv) qRegisterMetaType(); qRegisterMetaType("size_t"); qRegisterMetaType(); + qRegisterMetaType(); #ifdef QT_DEBUG qDebug() << "Running in debug mode"; @@ -86,7 +87,7 @@ int main(int argc, char **argv) QApplication::setApplicationName("PXMessenger"); QApplication::setOrganizationName("PXMessenger"); QApplication::setOrganizationDomain("PXMessenger"); - QApplication::setApplicationVersion("1.2.0"); + QApplication::setApplicationVersion("1.2.1"); MessIniReader iniReader; initialSettings presets; @@ -144,6 +145,7 @@ int main(int argc, char **argv) presets.windowSize = iniReader.getWindowSize(QSize(700, 500)); presets.mute = iniReader.getMute(); presets.preventFocus = iniReader.getFocus(); + presets.multicast = iniReader.getMulticastAddress(); PXMWindow *window = new PXMWindow(presets); window->startThreadsAndShow(); diff --git a/src/pxminireader.cpp b/src/pxminireader.cpp index 22e4f4d..7ce08a7 100644 --- a/src/pxminireader.cpp +++ b/src/pxminireader.cpp @@ -129,14 +129,15 @@ QString MessIniReader::getMulticastAddress() { return QString(""); } + return ipFull; + /* QStringList ipList = ipFull.split("."); for(int i = 0; i < 4; i++) { if(ipList[i].toUInt() > 255) ipList[i] = "0"; } - - return ipList.join(""); + */ } int MessIniReader::setMulticastAddress(QStringList ip) { diff --git a/src/pxmmainwindow.cpp b/src/pxmmainwindow.cpp index 25061b2..1f5bd8d 100644 --- a/src/pxmmainwindow.cpp +++ b/src/pxmmainwindow.cpp @@ -419,7 +419,7 @@ void PXMWindow::changeEvent(QEvent *event) { if(event->type() == QEvent::WindowStateChange) { - if(isMinimized()) + if(this->isMinimized()) { this->hide(); event->ignore(); @@ -513,11 +513,11 @@ int PXMWindow::sendButtonClicked() if( ( uuidOfSelectedItem == globalChatUuid) ) { - emit sendMsg(msg, PXMConsts::GLOBAL, ourUUID, QUuid()); + emit sendMsg(msg, PXMConsts::MSG_GLOBAL, ourUUID, QUuid()); } else { - emit sendMsg(msg, PXMConsts::MSG, ourUUID, uuidOfSelectedItem); + emit sendMsg(msg, PXMConsts::MSG_TEXT, ourUUID, uuidOfSelectedItem); } messTextEdit->setText(""); } @@ -551,12 +551,16 @@ int PXMWindow::focusWindow() { qApp->alert(this, 0); } - else if(this->isMinimized() && !(focusCheckBox->isChecked())) + else if(this->isMinimized()) { + if(!this->focusCheckBox->isChecked()) + { + this->setWindowState(Qt::WindowActive); + } + else + this->setWindowState(Qt::WindowNoState); this->show(); qApp->alert(this, 0); - this->raise(); - this->setWindowState(Qt::WindowActive); } return 0; } diff --git a/src/pxmpeerworker.cpp b/src/pxmpeerworker.cpp index 1f73dfd..021f41b 100644 --- a/src/pxmpeerworker.cpp +++ b/src/pxmpeerworker.cpp @@ -87,13 +87,15 @@ void PXMPeerWorker::currentThreadInit() nextSyncTimer->setInterval(2000); QObject::connect(nextSyncTimer, &QTimer::timeout, syncer, &PXMSync::syncNext); - QTimer::singleShot(5000, this, SLOT(discoveryTimerSingleShot())); + QTimer::singleShot(10000, this, SLOT(discoveryTimerSingleShot())); midnightTimer = new QTimer(this); midnightTimer->setInterval(PXMConsts::MIDNIGHT_TIMER_INTERVAL_MINUTES * 60000); QObject::connect(midnightTimer, &QTimer::timeout, this, &PXMPeerWorker::midnightTimerPersistent); midnightTimer->start(); + discoveryTimer= new QTimer(this); + startClient(); startServerThread(); } @@ -173,7 +175,7 @@ void PXMPeerWorker::syncPacketIterator(char *ipHeapArray, size_t len, QUuid send qDebug() << "Recieved connection list from" << peerDetailsHash[senderUuid].socketDescriptor; size_t index = 0; - while(index + PXMConsts::PACKED_UUID_BYTE_LENGTH + 6 <= len) + while(index + UUIDCompression::PACKED_UUID_BYTE_LENGTH + 6 <= len) { sockaddr_in addr; addr.sin_family = AF_INET; @@ -181,8 +183,8 @@ void PXMPeerWorker::syncPacketIterator(char *ipHeapArray, size_t len, QUuid send index += sizeof(uint32_t); memcpy(&(addr.sin_port), &ipHeapArray[index], sizeof(uint16_t)); index += sizeof(uint16_t); - QUuid uuid = PXMServer::unpackUUID((unsigned char*)&ipHeapArray[index]); - index += PXMConsts::PACKED_UUID_BYTE_LENGTH; + QUuid uuid = UUIDCompression::unpackUUID((unsigned char*)&ipHeapArray[index]); + index += UUIDCompression::PACKED_UUID_BYTE_LENGTH; qDebug() << inet_ntoa(addr.sin_addr) << ":" << ntohs(addr.sin_port) << ":" << uuid.toString(); attemptConnection(addr, uuid); @@ -206,13 +208,13 @@ void PXMPeerWorker::newTcpConnection(bufferevent *bev) } void PXMPeerWorker::sendAuthPacket(BevWrapper *bw) { - emit sendMsg(bw, (localHostname % QStringLiteral("::::") % ourListenerPort).toUtf8(), PXMConsts::UUID, localUUID, ""); + emit sendMsg(bw, (localHostname % QStringLiteral("::::") % ourListenerPort).toUtf8(), PXMConsts::MSG_UUID, localUUID, ""); } void PXMPeerWorker::requestSyncPacket(BevWrapper *bw, QUuid uuid) { waitingOnSyncFrom = uuid; qDebug() << "Requesting ips from" << peerDetailsHash.value(uuid).hostname; - emit sendMsg(bw, "", PXMConsts::SYNC_REQUEST, localUUID, ""); + emit sendMsg(bw, "", PXMConsts::MSG_SYNC_REQUEST, localUUID, ""); } void PXMPeerWorker::attemptConnection(sockaddr_in addr, QUuid uuid) { @@ -286,22 +288,22 @@ void PXMPeerWorker::sendSyncPacketBev(bufferevent *bev, QUuid uuid) void PXMPeerWorker::sendSyncPacket(BevWrapper *bw, QUuid uuid) { qDebug() << "Sending ips to" << peerDetailsHash.value(uuid).hostname; - char *msgRaw = new char[peerDetailsHash.size() * (sizeof(uint32_t) + sizeof(uint16_t) + PXMConsts::PACKED_UUID_BYTE_LENGTH) + 1]; + char *msgRaw = new char[peerDetailsHash.size() * (sizeof(uint32_t) + sizeof(uint16_t) + UUIDCompression::PACKED_UUID_BYTE_LENGTH) + 1]; size_t index = 0; for(auto & itr : peerDetailsHash) { if(itr.isAuthenticated) { - memcpy(msgRaw + index, &(itr.ipAddressRaw.sin_addr.s_addr),sizeof(uint32_t)); + memcpy(&msgRaw[index], &(itr.ipAddressRaw.sin_addr.s_addr),sizeof(uint32_t)); index += sizeof(uint32_t); - memcpy(msgRaw + index, &(itr.ipAddressRaw.sin_port), sizeof(uint16_t)); + memcpy(&msgRaw[index], &(itr.ipAddressRaw.sin_port), sizeof(uint16_t)); index += sizeof(uint16_t); - index += PXMClient::packUuid(msgRaw + index, &(itr.identifier)); + index += UUIDCompression::packUUID(&msgRaw[index], &(itr.identifier)); } } msgRaw[index+1] = 0; - emit sendIpsPacket(bw, msgRaw, index, PXMConsts::SYNC, localUUID, ""); + emit sendIpsPacket(bw, msgRaw, index, PXMConsts::MSG_SYNC, localUUID, ""); } void PXMPeerWorker::resultOfConnectionAttempt(evutil_socket_t socket, bool result, bufferevent *bev) { @@ -320,7 +322,7 @@ void PXMPeerWorker::resultOfConnectionAttempt(evutil_socket_t socket, bool resul if(result) { - bufferevent_setcb(bev, PXMServer::tcpReadUUID, NULL, PXMServer::tcpError, (void*)messServer); + bufferevent_setcb(bev, PXMServer::tcpReadUUID, NULL, PXMServer::tcpError, messServer); bufferevent_setwatermark(bev, EV_READ, sizeof(uint16_t), sizeof(uint16_t)); bufferevent_enable(bev, EV_READ|EV_WRITE); peerDetailsHash[uuid].bw->lockBev(); @@ -482,21 +484,20 @@ void PXMPeerWorker::setSelfCommsBufferevent(bufferevent *bev) } void PXMPeerWorker::sendMsgAccessor(QByteArray msg, PXMConsts::MESSAGE_TYPE type, QUuid uuid1, QUuid uuid2) { - if(type == PXMConsts::MSG) - { - emit sendMsg(peerDetailsHash.value(uuid2).bw, msg, PXMConsts::MSG, uuid1, uuid2); - } - else if(type == PXMConsts::GLOBAL) - { + switch (type) { + case PXMConsts::MSG_TEXT: + emit sendMsg(peerDetailsHash.value(uuid2).bw, msg, PXMConsts::MSG_TEXT, uuid1, uuid2); + break; + case PXMConsts::MSG_GLOBAL: for(auto &itr : peerDetailsHash) { if(itr.isConnected) - emit sendMsg(itr.bw, msg, PXMConsts::GLOBAL, uuid1, uuid2); + emit sendMsg(itr.bw, msg, PXMConsts::MSG_GLOBAL, uuid1, uuid2); } - } - else - { + break; + default: qDebug() << "Bad message type"; + break; } } void PXMPeerWorker::printFullHistory(QUuid uuid) @@ -521,8 +522,7 @@ void PXMPeerWorker::printInfoToDebug() QString str; QString pad = QString(PXMConsts::DEBUG_PADDING, ' '); str.append(QStringLiteral("\n")); - str.append(pad % "---Program Info---\n"); - //str.append(QStringLiteral("---Program Info---\n")); + str.append(pad % QStringLiteral("---Program Info---\n")); str.append(pad % QStringLiteral("Program Name: ") % qApp->applicationName() % QStringLiteral("\n")); str.append(pad % QStringLiteral("Version: ") % qApp->applicationVersion() % QStringLiteral("\n")); @@ -580,11 +580,10 @@ void PXMPeerWorker::discoveryTimerSingleShot() { if(!multicastIsFunctioning) { - emit warnBox("Network Problem", "Could not find anyone, even ourselves, on the network.\nThis could indicate a problem with your configuration.\n\nWe'll keep looking..."); + emit warnBox(QStringLiteral("Network Problem"), QStringLiteral("Could not find anyone, even ourselves, on the network.\nThis could indicate a problem with your configuration.\n\nWe'll keep looking...")); } if(peerDetailsHash.count() < 3) { - discoveryTimer= new QTimer(this); discoveryTimer->setInterval(30000); QObject::connect(discoveryTimer, &QTimer::timeout, this, &PXMPeerWorker::discoveryTimerPersistent); emit sendUDP("/discover", ourUDPListenerPort.toUShort()); @@ -609,7 +608,7 @@ void PXMPeerWorker::midnightTimerPersistent() str.append(dt.date().toString()); str.append(' '); str.append(QString(7, QChar('-'))); - str.append("
"); + str.append("
"); str.append("
"); this->addMessageToAllPeers(str, false, false); diff --git a/src/pxmserver.cpp b/src/pxmserver.cpp index 278feb8..764cbf9 100644 --- a/src/pxmserver.cpp +++ b/src/pxmserver.cpp @@ -44,7 +44,7 @@ void PXMServer::accept_new(evutil_socket_t s, short, void *arg) struct bufferevent *bev; evutil_make_socket_nonblocking(result); bev = bufferevent_socket_new(PXMServer::base, result, BEV_OPT_THREADSAFE); - bufferevent_setcb(bev, PXMServer::tcpReadUUID, NULL, PXMServer::tcpError, (void*)realServer); + bufferevent_setcb(bev, PXMServer::tcpReadUUID, NULL, PXMServer::tcpError, realServer); bufferevent_setwatermark(bev, EV_READ, 2, sizeof(uint16_t)); bufferevent_enable(bev, EV_READ|EV_WRITE); @@ -76,16 +76,16 @@ void PXMServer::tcpReadUUID(struct bufferevent *bev, void *arg) bufLen = ntohs(nboBufLen); evutil_socket_t socket = bufferevent_getfd(bev); - unsigned char bufUUID[PXMConsts::PACKED_UUID_BYTE_LENGTH]; - if(bufferevent_read(bev, bufUUID, PXMConsts::PACKED_UUID_BYTE_LENGTH) < PXMConsts::PACKED_UUID_BYTE_LENGTH) + unsigned char bufUUID[UUIDCompression::PACKED_UUID_BYTE_LENGTH]; + if(bufferevent_read(bev, bufUUID, UUIDCompression::PACKED_UUID_BYTE_LENGTH) < UUIDCompression::PACKED_UUID_BYTE_LENGTH) { qDebug() << "Bad Auth packet length, closing socket..."; bufferevent_disable(bev, EV_READ|EV_WRITE); realServer->peerQuit(socket, bev); return; } - QUuid quuid = PXMServer::unpackUUID(bufUUID); - bufLen -= PXMConsts::PACKED_UUID_BYTE_LENGTH; + QUuid quuid = UUIDCompression::unpackUUID(bufUUID); + bufLen -= UUIDCompression::PACKED_UUID_BYTE_LENGTH; if(quuid.isNull()) { qDebug() << "Bad Auth packet UUID, closing socket..."; @@ -98,7 +98,7 @@ void PXMServer::tcpReadUUID(struct bufferevent *bev, void *arg) bufferevent_read(bev, buf, bufLen); buf[bufLen] = 0; qDebug().noquote() << QString::fromUtf8(buf); - if(*(uint8_t*)&buf[0] == PXMConsts::UUID) + if(*(uint8_t*)&buf[0] == PXMConsts::MSG_UUID) { QStringList hpsplit = (QString::fromUtf8(&buf[1], bufLen-1)).split("::::"); if(hpsplit.length() != 2) @@ -118,7 +118,7 @@ void PXMServer::tcpReadUUID(struct bufferevent *bev, void *arg) } realServer->authenticationReceived(hpsplit[0], port, socket, quuid, bev); bufferevent_setwatermark(bev, EV_READ, sizeof(uint16_t), sizeof(uint16_t)); - bufferevent_setcb(bev, PXMServer::tcpRead, NULL, PXMServer::tcpError, (void*)realServer); + bufferevent_setcb(bev, PXMServer::tcpRead, NULL, PXMServer::tcpError, realServer); } else { @@ -162,16 +162,16 @@ void PXMServer::tcpRead(struct bufferevent *bev, void *arg) bufferevent_read(bev, &nboBufLen, 2); bufLen = ntohs(nboBufLen); - if(bufLen <= PXMConsts::PACKED_UUID_BYTE_LENGTH) + if(bufLen <= UUIDCompression::PACKED_UUID_BYTE_LENGTH) { evbuffer_drain(bufferevent_get_input(bev), UINT16_MAX); return; } - unsigned char rawUUID[PXMConsts::PACKED_UUID_BYTE_LENGTH]; - bufferevent_read(bev, rawUUID, PXMConsts::PACKED_UUID_BYTE_LENGTH); + unsigned char rawUUID[UUIDCompression::PACKED_UUID_BYTE_LENGTH]; + bufferevent_read(bev, rawUUID, UUIDCompression::PACKED_UUID_BYTE_LENGTH); - QUuid uuid = PXMServer::unpackUUID(rawUUID); + QUuid uuid = UUIDCompression::unpackUUID(rawUUID); if(uuid.isNull()) { evbuffer_drain(bufferevent_get_input(bev), UINT16_MAX); @@ -179,7 +179,7 @@ void PXMServer::tcpRead(struct bufferevent *bev, void *arg) } qDebug() << "Sender Uuid for message" << uuid.toString(); - bufLen -= PXMConsts::PACKED_UUID_BYTE_LENGTH; + bufLen -= UUIDCompression::PACKED_UUID_BYTE_LENGTH; //char *buf = new char[bufLen + 1]; char *buf = new char[bufLen]; bufferevent_read(bev, buf, bufLen); @@ -228,21 +228,20 @@ void PXMServer::tcpError(struct bufferevent *bev, short error, void *arg) } int PXMServer::singleMessageIterator(bufferevent *bev, char *buf, uint16_t bufLen, QUuid quuid) { - //using namespace PXMConsts; using namespace PXMConsts; if(bufLen == 0) { qDebug() << "Blank message! -- Not Good!"; return -1; } - uint8_t type = *(uint8_t*)(&buf[0]); + uint8_t type = (uint8_t)(buf[0]); switch (type) { - case MSG: + case MSG_TEXT: qDebug().noquote() << "MSG :" << QString::fromUtf8(&buf[1], bufLen-1); emit messageRecieved(QString::fromUtf8(&buf[1], bufLen-1), quuid, bev, false); break; - case SYNC: + case MSG_SYNC: { char *ipHeapArray = new char[bufLen-1]; memcpy(ipHeapArray, &buf[1], bufLen-1); @@ -250,12 +249,12 @@ int PXMServer::singleMessageIterator(bufferevent *bev, char *buf, uint16_t bufLe emit syncPacketIterator(ipHeapArray, bufLen-1, quuid); break; } - case SYNC_REQUEST: + case MSG_SYNC_REQUEST: qDebug().noquote() << "SYNC_REQUEST received" << QString::fromUtf8(&buf[1], bufLen-1); emit sendSyncPacket(bev, quuid); break; - case GLOBAL: - qDebug().noquote() << "/global :" << QString::fromUtf8(&buf[1], bufLen-1); + case MSG_GLOBAL: + qDebug().noquote() << "GLOBAL :" << QString::fromUtf8(&buf[1], bufLen-1); emit messageRecieved(QString::fromUtf8(&buf[1], bufLen-1), quuid, bev, true); break; default: @@ -264,74 +263,7 @@ int PXMServer::singleMessageIterator(bufferevent *bev, char *buf, uint16_t bufLe break; } return 0; - /* - //These packets should be formatted like "/msghostname: msg\0" - if( !( strncmp(&buf[0], "/msg", 4) ) ) - { - qDebug().noquote() << "/msg :" << QString::fromUtf8(&buf[4], bufLen-4); - emit messageRecieved(QString::fromUtf8(&buf[4], bufLen-4), quuid, bev, false); - } - //These packets should come formatted like "/ip:hostname@192.168.1.1:hostname2@192.168.1.2\0" - else if( !( strncmp(&buf[0], "/ip", 3) ) ) - { - char *ipHeapArray = new char[bufLen-3]; - memcpy(ipHeapArray, &buf[3], bufLen-3); - qDebug().noquote() << "/ip received"; - emit syncPacketIterator(ipHeapArray, bufLen-3, quuid); - } - //This packet is asking us to communicate our list of peers with the sender, leads to us sending an /ip packet - //These packets should come formatted like "/request\0" - else if(!(strncmp(&buf[0], "/request", 8))) - { - qDebug().noquote() << "/request received"; - emit sendSyncPacket(bev, quuid); - } - //These packets are messages sent to the global chat room - //These packets should come formatted like "/globalhostname: msg\0" - else if(!(strncmp(&buf[0], "/global", 7))) - { - qDebug().noquote() << "/global :" << QString::fromUtf8(&buf[7], bufLen-7); - emit messageRecieved(QString::fromUtf8(&buf[7], bufLen-7), quuid, bev, true); - } - //This packet is an updated hostname for the computer that sent it - //These packets should come formatted like "/hostnameHostname1\0" - else if(!(strncmp(&buf[0], "/hostname", 9))) - { - qDebug().noquote() << "/hostname received"; - emit setPeerHostname(QString::fromUtf8(&buf[9], bufLen-9), quuid); - } - //This packet is asking us to communicate an updated hostname to the sender - //These packets should come formatted like "/namerequest\0" - else if(!(strncmp(&buf[0], "/namerequest", 12))) - { - qDebug().noquote() << "/namerequest received from"; - //emit sendName(bev, quuid.toString(), localUUID); - } - else - { - qDebug().noquote() << "Bad message type in the packet, discarding the rest"; - return -1; - } - */ - - return 0; } -QUuid PXMServer::unpackUUID(const unsigned char *src) -{ - QUuid uuid; - size_t index = 0; - uuid.data1 = ntohl( *( (uint32_t*)(&src[index]) ) ); - index += sizeof(uint32_t); - uuid.data2 = ntohs( *( (uint16_t*)(&src[index]) ) ); - index += sizeof(uint16_t); - uuid.data3 = ntohs( *( (uint16_t*)(&src[index]) ) ); - index += sizeof(uint16_t); - memcpy(&(uuid.data4), &src[index], 8); - //index += 8; - - return uuid; -} - void PXMServer::udpRecieve(evutil_socket_t socketfd, short int, void *args) { PXMServer *realServer = static_cast(args); @@ -355,17 +287,21 @@ void PXMServer::udpRecieve(evutil_socket_t socketfd, short int, void *args) si_other.sin_port = htons(realServer->udpListenerPort); - char name[QString::number(realServer->tcpListenerPort).length() + realServer->localUUID.toByteArray().length() + 7]; + int len = sizeof(uint16_t) + UUIDCompression::PACKED_UUID_BYTE_LENGTH + strlen("/name:") + 1; + + char name[len]; strcpy(name, "/name:"); - snprintf(&name[6], sizeof(name), "%d", realServer->tcpListenerPort); - strcat(name, realServer->localUUID.toByteArray().constData()); - int len = strlen(name); + uint16_t port = htons(realServer->tcpListenerPort); + memcpy(&name[strlen("/name:")], &(port), sizeof(port)); + UUIDCompression::packUUID(&name[strlen("/name:") + sizeof(port)], &(realServer->localUUID)); + + name[len] = 0; for(int k = 0; k < 2; k++) { - if(sendto(replySocket, name, len+1, 0, (struct sockaddr *)&si_other, si_other_len) != len+1) + if(sendto(replySocket, name, len, 0, (struct sockaddr *)&si_other, si_other_len) != len) qDebug().noquote() << "sendto: " + QString::fromUtf8(strerror(errno)); } evutil_closesocket(replySocket); @@ -374,11 +310,8 @@ void PXMServer::udpRecieve(evutil_socket_t socketfd, short int, void *args) //when this is recieved it add the sender to the list of peers and connects to him else if ((strncmp(&buf[0], "/name:", 6)) == 0) { - QString fullidStr = QString::fromUtf8(&buf[6]); - QString portNumber = fullidStr.left(fullidStr.indexOf("{")); - QUuid uuid = fullidStr.right(fullidStr.length() - fullidStr.indexOf("{")); - - si_other.sin_port = htons(portNumber.toInt()); + si_other.sin_port = *(uint16_t*)(&buf[6]); + QUuid uuid = UUIDCompression::unpackUUID((unsigned char*)&buf[8]); realServer->attemptConnection(si_other, uuid); } else @@ -447,7 +380,6 @@ evutil_socket_t PXMServer::setupUDPSocket(evutil_socket_t s_listen) qDebug().noquote() << "Port number for Multicast: " + QString::number(udpSocketNumber); - //send our discover packet to find other computers emit sendUDP("/discover:", udpSocketNumber); diff --git a/src/pxmserver.h b/src/pxmserver.h index c932f41..11e3e6a 100644 --- a/src/pxmserver.h +++ b/src/pxmserver.h @@ -25,6 +25,7 @@ #include #include "pxmdefinitions.h" +#include "uuidcompression.h" #ifdef _WIN32 #include @@ -65,7 +66,6 @@ class PXMServer : public QThread ~PXMServer(); static void tcpError(bufferevent *bev, short error, void *arg); static void tcpReadUUID(bufferevent *bev, void *arg); - static QUuid unpackUUID(const unsigned char *src); static void tcpRead(bufferevent *bev, void *arg); static void accept_new(evutil_socket_t socketfd, short, void *arg); static void udpRecieve(evutil_socket_t socketfd, short, void *args); diff --git a/src/uuidcompression.cpp b/src/uuidcompression.cpp new file mode 100644 index 0000000..5f6931f --- /dev/null +++ b/src/uuidcompression.cpp @@ -0,0 +1,36 @@ +#include "uuidcompression.h" + +QUuid UUIDCompression::unpackUUID(const unsigned char *src) +{ + QUuid uuid; + size_t index = 0; + uuid.data1 = ntohl(*(uint32_t*)(&src[index])); + index += 4; + uuid.data2 = ntohs(*(uint16_t*)(&src[index])); + index += 2; + uuid.data3 = ntohs(*(uint16_t*)(&src[index])); + index += 2; + memcpy(&(uuid.data4), &src[index], 8); + //index += 8; + + return uuid; +} +size_t UUIDCompression::packUUID(char *buf, QUuid *uuid) +{ + int index = 0; + + uint32_t uuidSectionL = htonl(uuid->data1); + memcpy(&buf[index], &(uuidSectionL), 4); + index += 4; + uint16_t uuidSectionS = htons(uuid->data2); + memcpy(&buf[index], &(uuidSectionS), 2); + index += 2; + uuidSectionS = htons(uuid->data3); + memcpy(&buf[index], &(uuidSectionS), 2); + index += 2; + unsigned char *uuidSectionC = uuid->data4; + memcpy(&buf[index], uuidSectionC, 8); + index += 8; + + return index; +} diff --git a/src/uuidcompression.h b/src/uuidcompression.h new file mode 100644 index 0000000..1285632 --- /dev/null +++ b/src/uuidcompression.h @@ -0,0 +1,17 @@ +#ifndef UUIDCOMPRESSION_H +#define UUIDCOMPRESSION_H + +#include +#ifdef __WIN32 +#include +#else +#include +#endif + +namespace UUIDCompression { +const size_t PACKED_UUID_BYTE_LENGTH = 16; +QUuid unpackUUID(const unsigned char *src); +size_t packUUID(char *buf, QUuid *uuid); +} + +#endif // UUIDCOMPRESSION_H