void eventLoop(int socketFD, int epollFD, struct epoll_event *events, int (*fnPtr)(int, char*, int)) { while (TRUE) { int n, i; n = epoll_wait (epollFD, events, MAXEVENTS, -1); for (i = 0; i < n; i++) { if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN))) { /* An error has occured on this fd, or the socketFD is not ready for reading (why were we notified then?) */ fprintf (stderr, "epoll error\n"); close (events[i].data.fd); continue; } else if (socketFD == events[i].data.fd) { processIncomingNewSocket(socketFD, epollFD); continue; } else { //Read all data from socket and do something with it if (readDataFromSocket(events[i].data.fd, fnPtr)) { printf ("Closed connection on descriptor %d\n", events[i].data.fd); close (events[i].data.fd); } } } } free (events); }
void buddyPicture::readSnac(quint16 length) { snac snacPacket; snacPacket.readData(buffer); length -= 10; switch ( snacPacket.getFamily()) { case 0x0001: switch(snacPacket.getSubType()) { case 0x0003: buffer->read(length); length = 0; if ( !alreadySentCap) sendCapab(); break; case 0x0007: buffer->read(length); length = 0; sendRateInfoClientReady(); break; case 0x0018: buffer->read(length); length = 0; sendInfoReq(); break; default: ; } break; case 0x0010: switch(snacPacket.getSubType()) { case 0x0007: saveAvatar(length); length = 0; break; default: ; } break; default: ; } if (length) buffer->read(length); if ( buffer->bytesAvailable() ) { readDataFromSocket(); } }
void buddyPicture::readDataFromSocket() { buffer->write(tcpSocket->readAll()); if ( readyToReadFlap ) { flapPacket flap; if ( !flap.readFromSocket( buffer ) ) { return; } channel = flap.getChannel(); flapLength = flap.getLength(); } if ( buffer->bytesAvailable() < flapLength ) { readyToReadFlap = false; return; } readyToReadFlap = true; if ( channel == 0x01) { // qDebug()<<"hi"; // alreadySentCap = true; buffer->read(flapLength); // sendCapab(); } if ( channel == 0x02 ) { readSnac(flapLength); } if ( channel == 0x03 ) buffer->read(flapLength); if ( channel == 0x04) buffer->read(flapLength); if ( channel >= 0x05 ) buffer->read(flapLength); if (buffer->bytesAvailable()) readDataFromSocket(); }
buddyPicture::buddyPicture(const QString &profile_name, const QString &mine_uin, QObject *parent) : QObject(parent) , m_profile_name(profile_name) , m_mine_uin(mine_uin) { readyToReadFlap = true; flapSeqNum = rand() % 0x8000; snacSeqNum = 0x00000000; connectedToServ = false; canSendReqForAvatars = false; alreadySentCap = false; refNum = 1; tcpSocket = new QTcpSocket(this); connect ( tcpSocket, SIGNAL(readyRead()), this, SLOT(readDataFromSocket())); connect ( tcpSocket, SIGNAL(disconnected()), this, SLOT(socketDisconnected())); connect ( tcpSocket, SIGNAL(connected()), this, SLOT(socketConnected())); buffer = new icqBuffer(this); buffer->open(QIODevice::ReadWrite); }