/* answer whether the socket has data available for reading: if the socket is not connected, answer "false"; if the socket is open and data can be read, answer "true". if the socket is open and no data is currently readable, answer "false"; if the socket is closed by peer, change the state to OtherEndClosed and answer "false"; */ sqInt sqSocketReceiveDataAvailable(SocketPtr s) { if (!socketValid(s)) return false; if (SOCKETSTATE(s) == Connected) { int fd= SOCKET(s); int n= socketReadable(fd); if (n > 0) { FPRINTF((stderr, "receiveDataAvailable(%d) -> true\n", fd)); return true; } else if (n < 0) { FPRINTF((stderr, "receiveDataAvailable(%d): other end closed\n", fd)); SOCKETSTATE(s)= OtherEndClosed; } } else /* (SOCKETSTATE(s) != Connected) */ { FPRINTF((stderr, "receiveDataAvailable(%d): socket not connected\n", SOCKET(s))); } aioHandle(SOCKET(s), dataHandler, AIO_RX); FPRINTF((stderr, "receiveDataAvailable(%d) -> false [aioHandle is set]\n", SOCKET(s))); return false; }
sqInt sqSocketConnectionStatus(SocketPtr s) { if (!socketValid(s)) return Invalid; /* we now know that the net session is valid, so if state is Invalid... */ if (SOCKETSTATE(s) == Invalid) /* see acceptHandler() */ { fprintf(stderr, "socketStatus: freeing invalidated pss=%p\n", PSP(s)); /*free(PSP(s));*/ /* this almost never happens -- safer not to free()?? */ _PSP(s)= 0; interpreterProxy->success(false); return Invalid; } #if 0 /* check for connection closed by peer */ if (SOCKETSTATE(s) == Connected) { int fd= SOCKET(s); int n= socketReadable(fd); if (n < 0) { FPRINTF((stderr, "socketStatus(%d): detected other end closed\n", fd)); SOCKETSTATE(s)= OtherEndClosed; } } #endif FPRINTF((stderr, "socketStatus(%d) -> %d\n", SOCKET(s), SOCKETSTATE(s))); return SOCKETSTATE(s); }
void ContactRequestClient::sendRequest() { close(); socket = new Tor::TorSocket(this); connect(socket, SIGNAL(connected()), this, SLOT(socketConnected())); connect(socket, SIGNAL(readyRead()), this, SLOT(socketReadable())); socket->setMaxAttemptInterval(600); state = WaitConnect; socket->connectToHost(user->hostname(), user->port()); }
void SocketHandler::socketReadable() { int sessionId = -1; QLocalSocket* socket = (QLocalSocket*)sender(); ((QLocalSocket*)sender())->read((char*)&sessionId, sizeof(int)); disconnect(socket, SIGNAL(readyRead()), this, SLOT(socketReadable())); if (sessionId >= 0) { if(!m_idMap.contains(sessionId)) m_idMap.insert(sessionId, new SessionData((QLocalSocket*)sender(), this)); } else { sensordLogC() << "[SocketHandler]: Failed to read valid session ID from client. Closing socket."; socket->abort(); } }
void SocketHandler::newConnection() { sensordLogT() << "[SocketHandler]: New connection received."; while (m_server->hasPendingConnections()) { QLocalSocket* socket = m_server->nextPendingConnection(); connect(socket, SIGNAL(readyRead()), this, SLOT(socketReadable())); connect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected())); connect(socket, SIGNAL(error(QLocalSocket::LocalSocketError)), this, SLOT(socketError(QLocalSocket::LocalSocketError))); // Initialize socket socket->write("\n", 1); socket->waitForBytesWritten(); } }
void PacketSocket::socketReadable() { switch(_state) { case stateReadingHeader: readHeader(); break; case stateReadingBody: readBody(); break; } if (_socket->bytesAvailable() > 0) { socketReadable(); } }
bool SocketHandler::removeSession(int sessionId) { if (!(m_idMap.keys().contains(sessionId))) { sensordLogW() << "[SocketHandler]: Trying to remove nonexistent session."; return false; } QLocalSocket* socket = (*m_idMap.find(sessionId))->stealSocket(); if (socket) { disconnect(socket, SIGNAL(readyRead()), this, SLOT(socketReadable())); disconnect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected())); disconnect(socket, SIGNAL(error(QLocalSocket::LocalSocketError)), this, SLOT(socketError(QLocalSocket::LocalSocketError))); socket->deleteLater(); } delete m_idMap.take(sessionId); return true; }
static void dataHandler(int fd, void *data, int flags) { privateSocketStruct *pss= (privateSocketStruct *)data; FPRINTF((stderr, "dataHandler(%d=%d, %p, %d)\n", fd, pss->s, data, flags)); if (pss == NULL) { fprintf(stderr, "dataHandler: pss is NULL fd=%d data=%p flags=0x%x\n", fd, data, flags); return; } if (flags & AIO_R) { int n= socketReadable(fd); if (n == 0) { fprintf(stderr, "dataHandler: selected socket fd=%d flags=0x%x would block (why?)\n", fd, flags); } if (n != 1) { pss->sockError= socketError(fd); pss->sockState= OtherEndClosed; } } if (flags & AIO_X) { /* assume out-of-band data has arrived */ /* NOTE: Squeak's socket interface is currently incapable of reading * OOB data. We have no choice but to discard it. Ho hum. */ char buf[1]; int n= recv(fd, (void *)buf, 1, MSG_OOB); if (n == 1) fprintf(stderr, "socket: received OOB data: %02x\n", buf[0]); } if (flags & AIO_R) notify(pss, READ_NOTIFY); if (flags & AIO_W) notify(pss, WRITE_NOTIFY); }