void Connection::processReadyRead() { if (state == WaitingForGreeting) { if (!readProtocolHeader()) return; if (currentDataType != Greeting) { abort(); return; } state = ReadingGreeting; } if (state == ReadingGreeting) { if (!hasEnoughData()) return; buffer = read(numBytesForCurrentDataType); if (buffer.size() != numBytesForCurrentDataType) { abort(); return; } username = QString(buffer) + '@' + peerAddress().toString() + ':' + QString::number(peerPort()); currentDataType = Undefined; numBytesForCurrentDataType = 0; buffer.clear(); if (!isValid()) { abort(); return; } if (!isGreetingMessageSent) sendGreetingMessage(); pingTimer.start(); pongTime.start(); state = ReadyForUse; emit readyForUse(); } do { if (currentDataType == Undefined) { if (!readProtocolHeader()) return; } if (!hasEnoughData()) return; processData(); } while (bytesAvailable() > 0); }
std::size_t ConnectionContext::decode(const char* buffer, std::size_t size) { qpid::sys::ScopedLock<qpid::sys::Monitor> l(lock); QPID_LOG(trace, id << " decode(" << size << ")"); if (readHeader) { size_t decoded = readProtocolHeader(buffer, size); if (decoded < size) { decoded += decode(buffer + decoded, size - decoded); } return decoded; } //TODO: Fix pn_engine_input() to take const buffer ssize_t n = pn_transport_input(engine, const_cast<char*>(buffer), size); if (n > 0 || n == PN_EOS) { //If engine returns EOS, have no way of knowing how many bytes //it processed, but can assume none need to be reprocessed so //consider them all read: if (n == PN_EOS) n = size; QPID_LOG_CAT(debug, network, id << " decoded " << n << " bytes from " << size) pn_transport_tick(engine, 0); lock.notifyAll(); return n; } else if (n == PN_ERR) { throw qpid::Exception(QPID_MSG("Error on input: " << getError())); } else { return 0; } }
void DkConnection::processReadyRead() { if (readDataIntoBuffer() <= 0) return; if (!readProtocolHeader()) return; checkState(); readWhileBytesAvailable(); }
void Connection::processReadyRead() { if(!isValid()) { abort(); return; } do { if(m_currentDataType == Undefined) { if(!readProtocolHeader()) return; } if(!hasEnoughData()) return; processData(); } while(bytesAvailable() > 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); }
// 处理socket接收消息的核心函数,根据连接状态进行不同的操作 void ChatConnection::processReadyRead() { if(this->state == WaitingForGreeting) { if(!this->readProtocolHeader()) return; if(this->currentDataType != GREETING) { this->abort(); return; } this->state = ReadingGreeting; } if(this->state == ReadingGreeting) { if(!this->hasEnoughData()) return; this->buffer = read(this->numBytesForCurrentDataType); if(buffer.size() != this->numBytesForCurrentDataType) { this->abort(); return; } QString greeting = QString::fromUtf8(this->buffer); if(DEBUG) qDebug() << "Get greeting message: " << greeting; QStringList addrList = greeting.split(":"); this->userName = addrList.at(0); QString ipStr = addrList.at(1); QString portStr = addrList.at(2); this->ip = QHostAddress(ipStr); this->port = portStr.toInt(); this->currentDataType = UNDEFINED; this->numBytesForCurrentDataType = 0; this->buffer.clear(); if(!this->isValid()) { this->abort(); return; } if(!this->isGreetingMessageSent) this->sendGreetingMessage(); this->state = ReadyForUse; emit this->endGreeting(this); } do { if(this->currentDataType == UNDEFINED) { if(!readProtocolHeader()) return; } if(!this->hasEnoughData()) return; this->processData(); } while(this->bytesAvailable() > 0); }