本文整理汇总了C++中QBuffer::canReadLine方法的典型用法代码示例。如果您正苦于以下问题:C++ QBuffer::canReadLine方法的具体用法?C++ QBuffer::canReadLine怎么用?C++ QBuffer::canReadLine使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QBuffer
的用法示例。
在下文中一共展示了QBuffer::canReadLine方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: writeData
qint64 Request::writeData(const char* data, qint64 maxSize)
{
if(m_responseState == WaitingForResponseHeaders)
{
m_headerBuffer.append(data, maxSize);
// We need to buffer the headers, so we can use the STATUS header appropriately
QBuffer buffer;
buffer.setData(m_headerBuffer);
buffer.open(QIODevice::ReadOnly);
buffer.seek(m_headerBufferPosition);
while(buffer.canReadLine())
{
const QByteArray line = buffer.readLine().trimmed();
if(line.isEmpty())
{
Q_ASSERT(m_responseHeaders.contains("STATUS"));
Q_ASSERT(m_requestHeaders.contains("SERVER_PROTOCOL"));
m_responseState = WaitingForResponseBody;
const QByteArray status = m_responseHeaders.take("STATUS");
m_socket->write(m_requestHeaders.value("SERVER_PROTOCOL"));
m_socket->write(" ", 1);
m_socket->write(status);
m_socket->write("\r\n", 2);
//qDebug() << Q_FUNC_INFO << m_requestHeaders << m_responseHeaders;
for(
HeaderMap::ConstIterator it = m_responseHeaders.constBegin();
it != m_responseHeaders.constEnd();
++it
)
{
m_socket->write(it.key());
m_socket->write(": ");
m_socket->write(it.value());
m_socket->write("\r\n");
}
m_socket->write("\r\n");
m_socket->write(buffer.readAll());
buffer.close();
m_headerBuffer.clear();
return maxSize;
}
const int lengthOfName = line.indexOf(':');
const QByteArray name = line.left(lengthOfName);
const QByteArray value = line.mid(lengthOfName + 2); // ": " after the name == 2 chars
m_responseHeaders.insertMulti(name, value);
}
m_headerBufferPosition = buffer.pos();
buffer.close();
return maxSize;
}
Q_ASSERT(m_responseState == WaitingForResponseBody);
return m_socket->write(data, maxSize);
}
示例2: callback_read_file
size_t callback_read_file(void *ptr, size_t size, size_t nmemb, void *userp)
{
// qDebug()<<__FILE__<<__LINE__<<__FUNCTION__<<size<<nmemb<<userp;
size_t tlen = size * nmemb, rlen = 0;
QBuffer strbuf;
QByteArray line;
int n = 0, wlen = 0;
CurlFtp *ftp = static_cast<CurlFtp*>(userp);
QLocalSocket *router = ftp->getDataSock2();
strbuf.setData((const char*)ptr, tlen);
strbuf.open(QBuffer::ReadOnly);
// Q_ASSERT(strbuf.canReadLine()); // ???
rlen = 0;
while (!strbuf.atEnd()) {
if (strbuf.canReadLine()) {
line = strbuf.readLine();
} else {
line = strbuf.readAll();
}
rlen += line.length();
wlen = router->write(line);
// qDebug()<<"Line: "<<n++<<line.length()<<wlen;
// fprintf(stdout, "%s", ".");
// fflush(stdout);
Q_ASSERT(line.length() == wlen);
// break;
}
strbuf.close();
router->flush();
// qDebug()<<"can rw:"<<router->isReadable()<<router->isWritable()<<router->isOpen();
// fprintf(stdout, "route read file:. %p %d %s", router, router->bytesAvailable(), "\n");
fflush(stdout);
return rlen;
return 0;
}
示例3: callback_read_dir
// 给多少数据就要读取多少,读取不完不行。
size_t callback_read_dir(void *ptr, size_t size, size_t nmemb, void *userp)
{
qDebug()<<__FILE__<<__LINE__<<__FUNCTION__<<size<<nmemb<<userp;
size_t tlen = size * nmemb, rlen = 0;
QBuffer strbuf;
QByteArray line;
int n = 0;
strbuf.setData((const char*)ptr, tlen);
strbuf.open(QBuffer::ReadOnly);
Q_ASSERT(strbuf.canReadLine()); // ???
rlen = 0;
while (!strbuf.atEnd()) {
line = strbuf.readLine();
rlen += line.length();
qDebug()<<"Line: "<<n++<<line;
// break;
}
strbuf.close();
return rlen;
}
示例4: riceviMessaggio
void Server::riceviMessaggio()
{
QTcpSocket* socket;
QBuffer* buffer;
QByteArray line;
socket = (QTcpSocket*)(sender());
buffer = buffers.value(socket);
qint64 bytes = buffer->write(socket->readAll()); //Per la ricezione del messaggio.
buffer->seek(buffer->pos() - bytes);
while (buffer->canReadLine()) //Leggo fino a quando sono presenti linee di testo.
{
line = buffer->readLine();
if(line.startsWith('#')) //Richiesta di autenticazione.
{
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("./users.sqldb");
if (!db.open()) {
socket->write("#false");
}
QSqlQuery s;
QString nome;
QString password;
QList<QByteArray> nomepassword;
nomepassword = line.split('/');
nome = nomepassword[1];
password = nomepassword[2];
password = password.remove("\n");
s.prepare("SELECT Username FROM Utenti WHERE Username = '" + nome + "' AND Password = '" + password +"'");
s.exec();
s.next();
QString username = s.value(0).toString();
if(username != "")
{
socket->write("#true\n");
}
else
{
socket->write("#false\n");
}
db.close();
}
else if(line.startsWith('!')) //Richiesta di iscrizione.
{
QString nome;
QString password;
QList<QByteArray> nomepassword = line.split('/');
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("./users.sqldb");
db.open();
QSqlQuery s;
nome = nomepassword[1];
password = nomepassword[2];
password = password.remove("\n");
s.prepare("INSERT INTO Utenti VALUES ('" + nome + "', '" + password + "')");
s.exec();
db.close();
}
else
foreach (QTcpSocket* connection, connections)
{
connection->write(line); //Invio il testo ricevuto a un'altra connessione.
}
}