-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcpprocessor.cpp
91 lines (76 loc) · 2.09 KB
/
tcpprocessor.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
#include "TCPProcessor.h"
#include <QTextStream>
#include <QFile>
#include <QDateTime>
#include <QThread>
#include <QTimerEvent>
#include <QUuid>
static const int TimeOut = 25000;
TCPProcessor::TCPProcessor(QTcpSocket *socket,
std::function<void ()> timeOutCallbackFunction,
QObject *parent) :
QObject( parent )
, _socket( socket )
, _timeOutCallbackFunction( timeOutCallbackFunction )
, _fileName( QUuid::createUuid().toString() )
, _isHeaderRecived( false )
, _timerId( 0 )
{
startTimer( TimeOut );//time out
}
TCPProcessor::~TCPProcessor()
{
qDebug() << "TCPProcessor::~TCPProcessor()";
}
void TCPProcessor::ProcessDatagramm()
{
if ( _socket == nullptr )
return;
killTimer( _timerId );
// const quint8 CountEmptyStrings = 2;
// int counterEmptyStrings( 0 );
while( _socket->bytesAvailable() > 0 )
{
if( _isHeaderRecived == false )
{
QByteArray line = _socket->readLine();
QString stringLine( line );
stringLine = stringLine.trimmed();
if ( stringLine.isEmpty() )
_isHeaderRecived = true;
continue;
}
// if ( ++counterEmptyStrings != CountEmptyStrings )
// continue;
ProcessData( _socket->readAll() );
}
startTimer( TimeOut );
}
void TCPProcessor::timerEvent(QTimerEvent *event)
{
killTimer( event->timerId() );
if ( _socket )
{
QTextStream os( _socket );
os.setAutoDetectUnicode(true);
os << "HTTP/1.1 200 Ok\r\n"
"content-type: text/html; charset=\"utf-8\"\r\n"
"server: HttpServer\r\n"
"Connection: close\r\n"
"\r\n"
"<h1>Saved!</h1>\n"
<< QDateTime::currentDateTime().toString().toUtf8() << "\n";
}
_timeOutCallbackFunction();
}
void TCPProcessor::ProcessData( const QByteArray &data )
{
QFile file( _fileName + ".bin" );
if ( !file.open( QIODevice::WriteOnly | QIODevice::Append ) )
qDebug() << "File not opened!";
else
{
file.write( QByteArray::fromBase64( data ) );
file.close();
}
}