void printHosts() { printf("Hosts:\n"); for (Host::Iterator it = ep.hosts(); it.hasNext(); ) { Host h = it.next(); printf(" %s", h.keyStr().c_str()); Address addr = h.address(); if (addr.isValid()) printf(" - %s", addr.toString().c_str()); printf("\n"); } }
QString ServersComboBox::makeServerText(const Host &host) { QString name; name = host.address(); if (host.isValid()) { if (name.indexOf(':') >= 0) { name = '[' + name + ']'; } name += ':' + QString::number(host.port()); } return name; }
QVariant HostModel::data(const QModelIndex &index, int role) const { if (index.row() < 0 || index.row() >= m_hosts.count()) return QVariant(); Host* host = m_hosts.at(index.row()); switch (index.column()) { case 0: { switch (role) { case NameRole: return host->name(); case AddressRole: return host->address(); } break; } case 1: if (role == Qt::DisplayRole) return host->name(); break; case 2: if (role == Qt::DisplayRole) return host->productVersion(); break; case 3: if (role == Qt::DisplayRole) return host->systemName(); break; case 4: if (role == Qt::DisplayRole) return QString("%1:%2").arg(host->address()).arg(host->port()); break; } return QVariant(); }
bool UdpSocket::send(const Host& host, const char* data, int size) { const uint32_t addr = host.address(); const uint16_t port = htons(host.port()); sockaddr_in& to = mPriv->to; if (to.sin_addr.s_addr != addr || to.sin_port != port) { if (to.sin_addr.s_addr) close(mPriv->client); to.sin_family = AF_INET; to.sin_addr.s_addr = addr; to.sin_port = port; mPriv->client = socket(AF_INET, SOCK_DGRAM, 0); if (mPriv->client == INVALID_SOCKET) { memset(&to, 0, sizeof(sockaddr_in)); const int err = socketError(); fprintf(stderr, "socket failed in send: %d %s\n", err, socketErrorMessage(err).c_str()); return false; } } int err; ssize_t total = 0, sent; do { sent = sendto(mPriv->client, &data[total], size - total, 0, reinterpret_cast<sockaddr*>(&to), sizeof(sockaddr_in)); if (sent == SOCKET_ERROR) { const int err = socketError(); if (err == EINTR) continue; fprintf(stderr, "sendto failed in send: %d %s (addr %u %u)\n", err, socketErrorMessage(err).c_str(), addr, host.port()); close(mPriv->client); memset(&to, 0, sizeof(sockaddr_in)); return false; } total += sent; } while (total < size); assert(total == size); return true; }