-
Notifications
You must be signed in to change notification settings - Fork 0
/
commonsocket.cpp
121 lines (112 loc) · 3.96 KB
/
commonsocket.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
#include "commonsocket.h"
#include <QMutexLocker>
#include <QDebug>
using namespace DG;
#include "messagepacket.h"
#include "screenpacket.h"
#include "mouseeventspacket.h"
#include "keyboardeventpacket.h"
#include <QTemporaryFile>
#include <QDataStream>
CommonSocket::CommonSocket(QObject* parent): QTcpSocket(parent){
headerSize = sizeof(DG::Packet::CommonHeader);
payloadSize = 0;
readerState = Header;
sockStream.setDevice(this);
sockStream.setVersion(QDataStream::Qt_4_7);
connect(this, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(stateChangedSlot(QAbstractSocket::SocketState)));
connect(this, SIGNAL(readyRead()), this, SLOT(readAvailableSlot()));
}
quint32 CommonSocket::currentReadSize() const{
return readerState == Header ? headerSize : payloadSize;
}
void CommonSocket::stateChangedSlot(QAbstractSocket::SocketState socketState){
if(socketState == QAbstractSocket::ConnectedState){
state = Connected;
}
qDebug() << ">> Socket State Changed: " << socketState;
}
CommonSocket::~CommonSocket(){
}
/*
QByteArray CommonSocket::rcv(){
return _conversationBuffer;
}
*/
DG::Packet* CommonSocket::rcv(){
qDebug() << "Mutex" << &rcvMutex;
QMutexLocker locker(&rcvMutex);
if(!packetQueue.isEmpty()){
//qDebug() << "!! packetQueue.size" << packetQueue.size();
DG::Packet* packet = packetQueue.dequeue();
if(packet->type() == Packet::MessagePacket){
DG::MessagePacket* m = dynamic_cast<DG::MessagePacket*>(packet);
qDebug() << "<<" << m->message();
}
return packet;
}
return 0x0;
}
/*
quint64 CommonSocket::send(const QByteArray& bytes){
sockStream << bytes.size();
return writeData(bytes.data(), bytes.size());
}
*/
quint64 CommonSocket::send(DG::Packet* packet){
qDebug() << "Mutex" << &sendMutex;
QMutexLocker locker(&sendMutex);
DG::Packet::CommonHeader header = DG::Packet::CommonHeader(packet->size(), packet->type());
sockStream << header;
qDebug() << "Sending Header: " << header.id << header.size;
sockStream << *packet;
if(packet->type() == Packet::MessagePacket){
DG::MessagePacket* m = dynamic_cast<DG::MessagePacket*>(packet);
qDebug() << ">>" << m->message();
}else if(packet->type() == Packet::ScreenPacket){
DG::ScreenPacket* s = dynamic_cast<DG::ScreenPacket*>(packet);
qDebug() << ">> screen [" << s->rect().left << s->rect().top << "]";
}else if(packet->type() == Packet::MouseEventPacket){
DG::MouseEventsPacket* ms = dynamic_cast<DG::MouseEventsPacket*>(packet);
qDebug() << ">> Mouse Events " << ms->count();
}
return packet->size();
}
CommonSocket::State CommonSocket::currentState(){
return state;
}
void CommonSocket::readAvailableSlot(){
qDebug() << "Mutex" << &readMutex;
QMutexLocker locker(&readMutex);
while(bytesAvailable() >= currentReadSize()){
qDebug() << "readerState " << (int)readerState << "bytesAvailable() " << bytesAvailable() << "currentReadSize() " << currentReadSize();
if(readerState == Header){
DG::Packet::CommonHeader* header = new DG::Packet::CommonHeader;
sockStream >> *header;
readerState = Payload;
payloadSize = header->size;
lastHeader = header;
qDebug() << "Header: " << header->id << header->size;
}else{
DG::Packet* packet = 0x0;
if(lastHeader->packetType == Packet::MessagePacket){
packet = new MessagePacket;
}else if(lastHeader->packetType == Packet::ScreenPacket){
packet = new ScreenPacket;
}else if(lastHeader->packetType == Packet::MouseEventPacket){
packet = new MouseEventsPacket;
}else if(lastHeader->packetType == Packet::KeyboardEventPacket){
packet = new KeyboardEventPacket;
}
sockStream >> *packet;
payloadSize = 0;
packetQueue.append(packet);
readerState = Header;
qDebug() << "recieved packet of size " << packet->size() << "packetQueue.size() " << packetQueue.size();
emit msgWaiting();
}
}
}
DG::Packet::PacketType CommonSocket::lastHeaderType() const{
return lastHeader->packetType;
}