Exemplo n.º 1
0
bool Connection::readProtocolHeader()
{
    if (transferTimerId) {
        killTimer(transferTimerId);
        transferTimerId = 0;
    }

    if (readDataIntoBuffer() <= 0) {
        transferTimerId = startTimer(TransferTimeout);
        return false;
    }

    if (buffer == "CODE ") {
        qDebug()<<"Code buffer";
        currentDataType = Code;
    } else if (buffer == "MESSAGE ") {
        qDebug()<<"mesaage buffer";
        currentDataType = PlainText;
    } else if (buffer == "GREETING ") {
        qDebug()<<"greeting buffer";
        currentDataType = Greeting;
    } else {
        qDebug()<<"undefined buffer";
        currentDataType = Undefined;
        abort();
        return false;
    }

    buffer.clear();
    numBytesForCurrentDataType = dataLengthForCurrentDataType();
    return true;
}
Exemplo n.º 2
0
bool Connection::readProtocolHeader()
{
    if (transferTimerId) {
        killTimer(transferTimerId);
        transferTimerId = 0;
    }

    if (readDataIntoBuffer() <= 0) {
        transferTimerId = startTimer(TransferTimeout);
        return false;
    }

    if (buffer == "PING ") {
        currentDataType = Ping;
    } else if (buffer == "PONG ") {
        currentDataType = Pong;
    } else if (buffer == "MESSAGE ") {
        currentDataType = PlainText;
    } else if(buffer == "CARD "){
        currentDataType = Card;
    } else if(buffer == "GROUP "){
        currentDataType = Group;
    }else if (buffer == "GREETING ") {
        currentDataType = Greeting;
    } else {
        currentDataType = Undefined;
        abort();
        return false;
    }

    buffer.clear();
    numBytesForCurrentDataType = dataLengthForCurrentDataType();
    return true;
}
Exemplo n.º 3
0
bool Connection::readProtocolHeader()
{
    if(m_transferTimerId) {
        killTimer(m_transferTimerId);
        m_transferTimerId = 0;
    }

    if(readDataIntoBuffer() <= 0) {
        m_transferTimerId = startTimer(TransferTimeout);
        return false;
    }

    if(m_buffer == "GREETING ") {
        m_currentDataType = Greeting;
    } else if(m_buffer == "DIRECTION ") {
        m_currentDataType = Direction;
    } else if(m_buffer == "GAMESTATE ") {
        m_currentDataType = GameState;
    } else if(m_buffer == "SELECTSERVER ") {
        m_currentDataType = SelectServer;
    } else if(m_buffer == "ACKNOWLEDGE ") {
        m_currentDataType = Acknowledge;
    } else if(m_buffer == "PLAYERADDR ") {
        m_currentDataType = PlayerAddr;
    } else {
        m_currentDataType = Undefined;
        abort();
        return false;
    }

    m_buffer.clear();
    m_numBytesForCurrentDataType = dataLengthForCurrentDataType();
    return true;
}
Exemplo n.º 4
0
void DkConnection::processReadyRead() {
	if (readDataIntoBuffer() <= 0)
		return;
	if (!readProtocolHeader())
		return;
	checkState();

	readWhileBytesAvailable();
}
Exemplo n.º 5
0
int DkConnection::dataLengthForCurrentDataType() {
	if (bytesAvailable() <= 0 || readDataIntoBuffer() <= 0 || !mBuffer.endsWith(SeparatorToken))
		return 0;

	mBuffer.chop(1);
	int number = mBuffer.toInt();
	mBuffer.clear();
	return number;
}
Exemplo n.º 6
0
// 获取当前消息的消息长度
int ChatConnection::dataLengthForCurrentDataType()
{
    if(this->bytesAvailable() <= 0 || readDataIntoBuffer(MaxBufferSize) <= 0 || !buffer.endsWith(SeparatorToken))
        return 0;

    this->buffer.chop(1);
    int length = buffer.toInt();
    this->buffer.clear();
    return length;
}
Exemplo n.º 7
0
void DkConnection::readWhileBytesAvailable() {
	do {
		if (mCurrentDataType == Undefined) {
			if (readDataIntoBuffer() <= 0)
				return;
			if (!readProtocolHeader())
				return;
			checkState();
		}
		if (!hasEnoughData()) {
			return;
		}

		mBuffer = read(mNumBytesForCurrentDataType);
		if (mBuffer.size() != mNumBytesForCurrentDataType) {
			abort();
			return;
		}
		processData();

	} while (bytesAvailable() > 0);
}