bool UTPSocket::connectTo(const net::Address& addr) { if (!bt::Globals::instance().isUTPEnabled()) return false; UTPServer & srv = bt::Globals::instance().getUTPServer(); reset(); conn = srv.connectTo(addr); Connection::Ptr ptr = conn.toStrongRef(); if (!ptr) return false; m_state = CONNECTING; ptr->setBlocking(blocking); if (blocking) { bool ret = ptr->waitUntilConnected(); if (ret) m_state = CONNECTED; return ret; } return ptr->connectionState() == CS_CONNECTED; }
bool UTPSocket::ready(const net::Poll* p, net::Poll::Mode mode) const { Q_UNUSED(p); Connection::Ptr ptr = conn.toStrongRef(); if (!ptr) return false; if (mode == net::Poll::OUTPUT) { if (polled_for_writing) { polled_for_writing = false; return ptr->isWriteable(); } } else { if (polled_for_reading) { polled_for_reading = false; return bytesAvailable() > 0 || ptr->connectionState() == CS_CLOSED; } } return false; }
int UTPSocket::recv(bt::Uint8* buf, int max_len) { Connection::Ptr ptr = conn.toStrongRef(); if (!ptr || ptr->connectionState() == CS_CLOSED) return 0; try { if (ptr->bytesAvailable() == 0) { if (blocking) { if (ptr->waitForData()) return ptr->recv(buf, max_len); else return 0; // connection should be closed now } else return -1; // No data ready and not blocking so return -1 } else return ptr->recv(buf, max_len); } catch (Connection::TransmissionError & err) { close(); return -1; } }
bool UTPSocket::connectSuccesFull() { Connection::Ptr ptr = conn.toStrongRef(); if (ptr && ptr->connectionState() == CS_CONNECTED) { setRemoteAddress(ptr->remoteAddress()); m_state = CONNECTED; return true; } else return false; }
void UTPSocket::prepare(net::Poll* p, net::Poll::Mode mode) { Connection::Ptr ptr = conn.toStrongRef(); if (ptr && ptr->connectionState() != CS_CLOSED) { UTPServer & srv = bt::Globals::instance().getUTPServer(); srv.preparePolling(p, mode, ptr); if (mode == net::Poll::OUTPUT) polled_for_writing = true; else polled_for_reading = true; } }
bool UTPSocket::ok() const { Connection::Ptr ptr = conn.toStrongRef(); return ptr && ptr->connectionState() != CS_CLOSED; }