void BmNetworkTCPClient::init()
{
    mMessageSize = 0;
    mMessageSizeLeft = 0;
    mReceive = 0;
    mReceivedBytes = 0;
    mSentBytes = 0;

    QObject::connect( mSocket, SIGNAL(readyRead()), this, SLOT(receive()) ); // each time the socket receive a packet, receive() is called
    QObject::connect( mSocket, SIGNAL(connected()), this, SLOT(connectionSet()) );
    QObject::connect( mSocket, SIGNAL(disconnected()), this, SLOT(disconnected()) );
    QObject::connect( mSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(exceptionManagement(QAbstractSocket::SocketError)) );

    // No delay socket
    mSocket->setSocketOption( QTcpSocket::LowDelayOption, 1 );

    // Allocate the ping buffers
    mBytes = ByteBuffer::allocate( 32 );

    // Prepare ping block of 32 bytes (sizeof int + 28 bytes message)
    mBytes->putInt( 28 );
    QByteArray sArray( "ping", 4 );
    mBytes->put( &sArray );

    mReceive = new unsigned long[4];
    mSend = new unsigned long[4];
    for ( int i=0; i<4; i++ )
    {
        mReceive[i] = 0;
        mSend[i] = 0;
    }
}
Beispiel #2
0
int ex_error_reply(const ExHttp *pHttp , int stscode)
{
	char buf[128];
	char *pBuf = buf;

	pBuf += codeSet(pBuf, stscode);
	pBuf += connectionSet(pBuf);
	return sendHead(pHttp, buf, pBuf - buf);
}
Beispiel #3
0
int ex_send_msg(ExHttp *pHttp, const char *type, const char *buf, size_t len)
{
	char hBuf[BUFSIZ];
	char *pBuf = hBuf;
	int ret;
	pBuf += codeSet(pBuf, 200);
	pBuf += typeSet(pBuf, type);
	pBuf += lengthSet(pBuf, len);
	pBuf += connectionSet(pBuf);

	do {
		if ((ret = sendHead(pHttp, hBuf, pBuf - hBuf)) < 0)
			break;
		if ((ret = ex_sock_nwrite(pHttp->sock, (char *) buf, len)) < 0)
			break;
	} while (0);
	return ret;
}
Beispiel #4
0
int ex_send_file(ExHttp *pHttp , const char *filePath)
{
	char buf[BUFSIZ];
	char *pBuf = buf;
	int ret = 0;
	pBuf += codeSet(pBuf, 200);
	pHttp->url = (char *) filePath;
	stat(filePath, &pHttp->st);
	pBuf += fileSet(pBuf, pHttp);
	pBuf += connectionSet(pBuf);
	do {
		if ((ret = sendHead(pHttp, buf, pBuf - buf)) < 0)
			break;

		if ((ret = sendFileStream(pHttp, filePath)) < 0)
			break;
	} while (0);
	return ret;
}
Beispiel #5
0
static int staticProcess(const ExHttp *pHttp)
{
	char buf[BUFSIZ];
	char *pBuf = buf;
	int ret = 0;

	int code = cacheCheck(pHttp);
	pBuf += codeSet(pBuf , code);
	if (code == 200) {
		pBuf += fileSet(pBuf , pHttp);
		pBuf += connectionSet(pBuf);
	}
	do {
		if ((ret = sendHead(pHttp, buf , pBuf - buf)) < 0)
			break;

		if (code == 304 || 'H' == *(pHttp->method))
			break;
		ret = sendFileStream(pHttp, pHttp->url);
	} while (0);
	return ret;
}