qghtcpclient.cpp 3.8 KB
Newer Older
丁劲犇's avatar
丁劲犇 已提交
1 2
#include "qghtcpclient.h"
#include <assert.h>
3 4
#include <QFile>
#include <QThread>
5 6 7 8


QGHTcpClient::QGHTcpClient(QObject *parent,int nPayLoad)
	: QTcpSocket(parent),
丁劲犇's avatar
丁劲犇 已提交
9
	  m_nPayLoad(nPayLoad)
丁劲犇's avatar
丁劲犇 已提交
10 11

{
丁劲犇's avatar
丁劲犇 已提交
12 13
	assert(m_nPayLoad>=256 && m_nPayLoad<=16*1024*1024);
	connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(some_data_sended(qint64)));
14 15
	m_uuid = 0;
}
16
quint32 QGHTcpClient::uuid()
17 18 19
{
	return m_uuid;
}
20
void QGHTcpClient::geneGlobalUUID(QString  globalUuidFile)
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
{
	QFile file(globalUuidFile);
	int ctp = 0;
	while (file.open(QIODevice::ReadWrite)==false)
	{
		ctp++;
		if (ctp>=100)
		{
			this->disconnectFromHost();
			return ;
		}
		QThread::currentThread()->sleep(20);
	}
	file.seek(0);
	quint32 oldUUID =1;
	if (file.size()>=sizeof(oldUUID))
		file.read((char *)&oldUUID,sizeof(oldUUID));
	oldUUID ++;
	this->m_uuid = oldUUID;
	file.seek(0);
	file.write((char *)&oldUUID,sizeof(oldUUID));
	file.flush();
	file.close();
}
45 46
QGHTcpClient::~QGHTcpClient()
{
47

48 49
}
void QGHTcpClient::some_data_sended(qint64 wsended)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
{
	while (m_buffer_sending.empty()==false)
	{
		QByteArray & arraySending = *m_buffer_sending.begin();
		qint64 & currentOffset = *m_buffer_sending_offset.begin();
		qint64 nTotalBytes = arraySending.size();
		assert(nTotalBytes>=currentOffset);
		qint64 nBytesWritten = write(arraySending.constData()+currentOffset,qMin((int)(nTotalBytes-currentOffset),m_nPayLoad));
		currentOffset += nBytesWritten;
		if (currentOffset>=nTotalBytes)
		{
			m_buffer_sending.pop_front();
			m_buffer_sending_offset.pop_front();
		}
		else
			break;
	}
}
丁劲犇's avatar
丁劲犇 已提交
68

69
void QGHTcpClient::SendData(QByteArray dtarray)
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
{
	if (dtarray.size())
	{
		if (m_buffer_sending.empty()==true)
		{
			qint64 bytesWritten = write(dtarray.constData(),qMin(dtarray.size(),m_nPayLoad));
			if (bytesWritten < dtarray.size())
			{
				m_buffer_sending.push_back(dtarray);
				m_buffer_sending_offset.push_back(bytesWritten);
			}
		}
		else
		{
			m_buffer_sending.push_back(dtarray);
			m_buffer_sending_offset.push_back(0);
		}
	}
丁劲犇's avatar
丁劲犇 已提交
88
}
89 90 91 92 93 94



#if (ZP_WANTSSL!=0)
QGHSslClient::QGHSslClient(QObject *parent,int nPayLoad)
	: QSslSocket(parent),
95
	  m_nPayLoad(nPayLoad)
丁劲犇's avatar
丁劲犇 已提交
96

97 98 99 100 101
{
	assert(m_nPayLoad>=256 && m_nPayLoad<=16*1024*1024);
	connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(some_data_sended(qint64)));
	m_uuid = 0;
}
102 103 104 105 106 107

QGHSslClient::~QGHSslClient()
{

}
quint32 QGHSslClient::uuid()
108 109 110
{
	return m_uuid;
}
111
void QGHSslClient::geneGlobalUUID(QString  globalUuidFile)
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
{
	QFile file(globalUuidFile);
	int ctp = 0;
	while (file.open(QIODevice::ReadWrite)==false)
	{
		ctp++;
		if (ctp>=100)
		{
			this->disconnectFromHost();
			return ;
		}
		QThread::currentThread()->sleep(20);
	}
	file.seek(0);
	quint32 oldUUID =1;
	if (file.size()>=sizeof(oldUUID))
		file.read((char *)&oldUUID,sizeof(oldUUID));
	oldUUID ++;
	this->m_uuid = oldUUID;
	file.seek(0);
	file.write((char *)&oldUUID,sizeof(oldUUID));
	file.flush();
	file.close();
丁劲犇's avatar
丁劲犇 已提交
135 136

}
137 138

void QGHSslClient::some_data_sended(qint64 wsended)
丁劲犇's avatar
丁劲犇 已提交
139
{
丁劲犇's avatar
丁劲犇 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	while (m_buffer_sending.empty()==false)
	{
		QByteArray & arraySending = *m_buffer_sending.begin();
		qint64 & currentOffset = *m_buffer_sending_offset.begin();
		qint64 nTotalBytes = arraySending.size();
		assert(nTotalBytes>=currentOffset);
		qint64 nBytesWritten = write(arraySending.constData()+currentOffset,qMin((int)(nTotalBytes-currentOffset),m_nPayLoad));
		currentOffset += nBytesWritten;
		if (currentOffset>=nTotalBytes)
		{
			m_buffer_sending.pop_front();
			m_buffer_sending_offset.pop_front();
		}
		else
			break;
	}
丁劲犇's avatar
丁劲犇 已提交
156 157
}

158
void QGHSslClient::SendData(QByteArray dtarray)
丁劲犇's avatar
丁劲犇 已提交
159
{
丁劲犇's avatar
丁劲犇 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	if (dtarray.size())
	{
		if (m_buffer_sending.empty()==true)
		{
			qint64 bytesWritten = write(dtarray.constData(),qMin(dtarray.size(),m_nPayLoad));
			if (bytesWritten < dtarray.size())
			{
				m_buffer_sending.push_back(dtarray);
				m_buffer_sending_offset.push_back(bytesWritten);
			}
		}
		else
		{
			m_buffer_sending.push_back(dtarray);
			m_buffer_sending_offset.push_back(0);
		}
	}
丁劲犇's avatar
丁劲犇 已提交
177
}
178 179

#endif