void connect() { Q_ASSERT(m_service); //qDebug() << "Connecting to: " << m_service->serviceInfo()->device().address().toString(); m_error = QDeclarativeBluetoothSocket::NoError; if (m_socket) m_socket->deleteLater(); QBluetoothServiceInfo::Protocol socketProtocol; if (m_service->serviceInfo()->socketProtocol() == QBluetoothServiceInfo::L2capProtocol) socketProtocol = QBluetoothServiceInfo::L2capProtocol; else if (m_service->serviceInfo()->socketProtocol() == QBluetoothServiceInfo::RfcommProtocol) socketProtocol = QBluetoothServiceInfo::RfcommProtocol; else socketProtocol = QBluetoothServiceInfo::UnknownProtocol; m_socket = new QBluetoothSocket(socketProtocol); m_socket->connectToService(*m_service->serviceInfo()); QObject::connect(m_socket, SIGNAL(connected()), m_dbs, SLOT(socket_connected())); QObject::connect(m_socket, SIGNAL(disconnected()), m_dbs, SLOT(socket_disconnected())); QObject::connect(m_socket, SIGNAL(error(QBluetoothSocket::SocketError)), m_dbs, SLOT(socket_error(QBluetoothSocket::SocketError))); QObject::connect(m_socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState)), m_dbs, SLOT(socket_state(QBluetoothSocket::SocketState))); QObject::connect(m_socket, SIGNAL(readyRead()), m_dbs, SLOT(socket_readyRead())); }
int timeoutconn6(int s,const char ip[16],uint16 port,unsigned int scope_id,unsigned int timeout) { struct taia now; struct taia deadline; iopause_fd x; if (socket_connect6(s,ip,port,scope_id) == -1) { if ((errno != EWOULDBLOCK) && (errno != EINPROGRESS)) return -1; x.fd = s; x.events = IOPAUSE_WRITE; taia_now(&now); taia_uint(&deadline,timeout); taia_add(&deadline,&now,&deadline); for (;;) { taia_now(&now); iopause(&x,1,&deadline,&now); if (x.revents) break; if (taia_less(&deadline,&now)) { errno = ETIMEDOUT; /* note that connect attempt is continuing */ return -1; } } if (!socket_connected(s)) return -1; } if (ndelay_off(s) == -1) return -1; return 0; }
/** * soup_socket_connect_sync: * @sock: a client #SoupSocket (which must not already be connected) * @cancellable: a #GCancellable, or %NULL * * Attempt to synchronously connect @sock to its remote address. * * If @cancellable is non-%NULL, it can be used to cancel the * connection, in which case soup_socket_connect_sync() will return * %SOUP_STATUS_CANCELLED. * * Return value: a success or failure code. **/ guint soup_socket_connect_sync (SoupSocket *sock, GCancellable *cancellable) { SoupSocketPrivate *priv; GSocketClient *client; GSocketConnection *conn; GError *error = NULL; g_return_val_if_fail (SOUP_IS_SOCKET (sock), SOUP_STATUS_MALFORMED); priv = SOUP_SOCKET_GET_PRIVATE (sock); g_return_val_if_fail (!priv->is_server, SOUP_STATUS_MALFORMED); g_return_val_if_fail (priv->gsock == NULL, SOUP_STATUS_MALFORMED); g_return_val_if_fail (priv->remote_addr != NULL, SOUP_STATUS_MALFORMED); if (cancellable) g_object_ref (cancellable); else cancellable = g_cancellable_new (); priv->connect_cancel = cancellable; client = new_socket_client (sock); conn = g_socket_client_connect (client, G_SOCKET_CONNECTABLE (priv->remote_addr), priv->connect_cancel, &error); g_object_unref (client); return socket_connected (sock, conn, error); }
int timeoutconn(int s,char ip[4],uint16 port,unsigned int timeout) { struct taia now; struct taia deadline; iopause_fd x; if (socket_connect4(s,ip,port) == -1) { if ((errno != error_wouldblock) && (errno != error_inprogress)) return -1; x.fd = s; x.events = IOPAUSE_WRITE; taia_now(&now); taia_uint(&deadline,timeout); taia_add(&deadline,&now,&deadline); for (;;) { taia_now(&now); iopause(&x,1,&deadline,&now); if (x.revents) break; if (taia_less(&deadline,&now)) { errno = error_timeout; /* note that connect attempt is continuing */ return -1; } } if (!socket_connected(s)) return -1; } if (ndelay_off(s) == -1) return -1; return 0; }
LocalSocketIpcClient::LocalSocketIpcClient(QString remoteServername, QObject *parent) : QObject(parent) { m_socket = new QLocalSocket(this); m_serverName = remoteServername; connect(m_socket, SIGNAL(connected()), this, SLOT(socket_connected())); connect(m_socket, SIGNAL(disconnected()), this, SLOT(socket_disconnected())); connect(m_socket, SIGNAL(readyRead()), this, SLOT(socket_readReady())); connect(m_socket, SIGNAL(error(QLocalSocket::LocalSocketError)), this, SLOT(socket_error(QLocalSocket::LocalSocketError))); }
/* Can write new sync data to the stream */ static void handle_write( int64 peersocket ) { proxy_peer *peer = io_getcookie( peersocket ); if( !peer ) { /* Can't happen ;) */ io_close( peersocket ); return; } switch( peer->state & FLAG_MASK ) { case FLAG_DISCONNECTED: default: /* Should not happen */ io_close( peersocket ); break; case FLAG_CONNECTING: /* Ensure that the connection is established and handle connection error */ if( peer->state & FLAG_OUTGOING && !socket_connected( peersocket ) ) { fprintf( stderr, "failed\n" ); reset_info_block( peer ); io_close( peersocket ); break; } if( io_trywrite( peersocket, (void*)&g_tracker_id, sizeof( g_tracker_id ) ) == sizeof( g_tracker_id ) ) { PROXYPEER_SETWAITTRACKERID( peer->state ); io_dontwantwrite( peersocket ); io_wantread( peersocket ); } else { fprintf( stderr, "Handshake incomplete, closing socket\n" ); io_close( peersocket ); reset_info_block( peer ); } break; case FLAG_CONNECTED: switch( iob_send( peersocket, &peer->outdata ) ) { case 0: /* all data sent */ io_dontwantwrite( peersocket ); break; case -3: /* an error occured */ io_close( peersocket ); reset_info_block( peer ); break; default: /* Normal operation or eagain */ break; } break; } return; }
QDeclarativeBluetoothSocket::QDeclarativeBluetoothSocket(QBluetoothSocket *socket, QDeclarativeBluetoothService *service, QObject *parent) : QObject(parent) { d = new QDeclarativeBluetoothSocketPrivate(this); d->m_service = service; d->m_socket = socket; d->m_connected = true; d->m_componentCompleted = true; QObject::connect(socket, SIGNAL(connected()), this, SLOT(socket_connected())); QObject::connect(socket, SIGNAL(disconnected()), this, SLOT(socket_disconnected())); QObject::connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(socket_error(QBluetoothSocket::SocketError))); QObject::connect(socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState)), this, SLOT(socket_state(QBluetoothSocket::SocketState))); QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(socket_readyRead())); }
void connect() { Q_ASSERT(m_service); qDebug() << "Connecting to: " << m_service->serviceInfo()->device().address().toString(); m_error = QLatin1String("No Error"); if(m_socket) m_socket->deleteLater(); // delete m_socket; m_socket = new QBluetoothSocket(); m_socket->connectToService(*m_service->serviceInfo()); QObject::connect(m_socket, SIGNAL(connected()), m_dbs, SLOT(socket_connected())); QObject::connect(m_socket, SIGNAL(disconnected()), m_dbs, SLOT(socket_disconnected())); QObject::connect(m_socket, SIGNAL(error(QBluetoothSocket::SocketError)), m_dbs, SLOT(socket_error(QBluetoothSocket::SocketError))); QObject::connect(m_socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState)), m_dbs, SLOT(socket_state(QBluetoothSocket::SocketState))); QObject::connect(m_socket, SIGNAL(readyRead()), m_dbs, SLOT(socket_readyRead())); m_stream = new QDataStream(m_socket); }
static void async_connected (GObject *client, GAsyncResult *result, gpointer data) { SoupSocketAsyncConnectData *sacd = data; SoupSocketPrivate *priv = SOUP_SOCKET_GET_PRIVATE (sacd->sock); GError *error = NULL; GSocketConnection *conn; guint status; if (priv->async_context && !priv->use_thread_context) g_main_context_pop_thread_default (priv->async_context); conn = g_socket_client_connect_finish (G_SOCKET_CLIENT (client), result, &error); status = socket_connected (sacd->sock, conn, error); sacd->callback (sacd->sock, status, sacd->user_data); g_object_unref (sacd->sock); g_slice_free (SoupSocketAsyncConnectData, sacd); }
void QDeclarativeBluetoothSocket::newSocket(QBluetoothSocket *socket, QDeclarativeBluetoothService *service) { if (d->m_socket){ delete d->m_socket; } d->m_service = service; d->m_socket = socket; d->m_connected = true; d->m_componentCompleted = true; d->m_error = NoError; QObject::connect(socket, SIGNAL(connected()), this, SLOT(socket_connected())); QObject::connect(socket, SIGNAL(disconnected()), this, SLOT(socket_disconnected())); QObject::connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SLOT(socket_error(QBluetoothSocket::SocketError))); QObject::connect(socket, SIGNAL(stateChanged(QBluetoothSocket::SocketState)), this, SLOT(socket_state(QBluetoothSocket::SocketState))); QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(socket_readyRead())); socket_state(socket->state()); emit connectedChanged(); }
void connect() { Q_ASSERT(!uri.isEmpty()); m_error = QLatin1String("No Error"); if (m_socket) m_socket->deleteLater(); m_socket = new QLlcpSocket; Q_Q(const QDeclarativeNearFieldSocket); QObject::connect(m_socket, SIGNAL(connected()), q, SLOT(socket_connected())); QObject::connect(m_socket, SIGNAL(disconnected()), q, SLOT(socket_disconnected())); QObject::connect(m_socket, SIGNAL(error(QLlcpSocket::SocketError)), q, SLOT(socket_error(QLlcpSocket::SocketError))); QObject::connect(m_socket, SIGNAL(stateChanged(QLlcpSocket::SocketState)), q, SLOT(socket_state(QLlcpSocket::SocketState))); QObject::connect(m_socket, SIGNAL(readyRead()), q, SLOT(socket_readyRead())); m_socket->connectToService(0, uri); }
/** Connect a socket to an IPv4 address, honouring a timeout. * * \note The socket will have nonblocking mode turned on on return from * this function. */ int socket_connect4_timeout(int sock, const ipv4addr* ip, ipv4port port, int timeout) { iopoll_fd pf; if (!nonblock_on(sock)) return 0; if (socket_connect4(sock, ip, port)) return 1; if (errno != EINPROGRESS && errno != EWOULDBLOCK) return 0; pf.fd = sock; pf.events = IOPOLL_WRITE; switch (iopoll_restart(&pf, 1, timeout)) { case 0: errno = ETIMEDOUT; return 0; case 1: if (socket_connected(sock)) return 1; /* No break, fall through */ default: return 0; } }
int timeoutaccept6(int s,char ip[16],uint16 *port,unsigned int timeout,uint32 *scope_id) { struct taia now; struct taia deadline; iopause_fd x; x.fd = s; x.events = IOPAUSE_READ; taia_now(&now); taia_uint(&deadline,timeout); taia_add(&deadline,&now,&deadline); for (;;) { taia_now(&now); iopause(&x,1,&deadline,&now); if (x.revents) break; if (taia_less(&deadline,&now)) { errno = ETIMEDOUT; /* note that connect attempt is continuing */ return -1; } } if (!socket_connected(s)) return -1; if (ndelay_off(s) == -1) return -1; return socket_accept6(s,ip,port,scope_id); }
int dns_transmit_get(struct dns_transmit *d,const iopause_fd *x,const struct taia *when) { char udpbuf[513]; unsigned char ch; int r; int fd; errno = error_io; fd = d->s1 - 1; if (!x->revents) { if (taia_less(when,&d->deadline)) return 0; errno = error_timeout; if (d->tcpstate == 0) return nextudp(d); return nexttcp(d); } if (d->tcpstate == 0) { /* have attempted to send UDP query to each server udploop times have sent query to curserver on UDP socket s */ r = recv(fd,udpbuf,sizeof udpbuf,0); if (r <= 0) { if (errno == error_connrefused) if (d->udploop == 2) return 0; return nextudp(d); } if (r + 1 > sizeof udpbuf) return 0; if (irrelevant(d,udpbuf,r)) return 0; if (serverwantstcp(udpbuf,r)) return firsttcp(d); if (serverfailed(udpbuf,r)) { if (d->udploop == 2) return 0; return nextudp(d); } socketfree(d); d->packetlen = r; d->packet = alloc(d->packetlen); if (!d->packet) { dns_transmit_free(d); return -1; } byte_copy(d->packet,d->packetlen,udpbuf); queryfree(d); return 1; } if (d->tcpstate == 1) { /* have sent connection attempt to curserver on TCP socket s pos not defined */ if (!socket_connected(fd)) return nexttcp(d); d->pos = 0; d->tcpstate = 2; return 0; } if (d->tcpstate == 2) { /* have connection to curserver on TCP socket s have sent pos bytes of query */ r = write(fd,d->query + d->pos,d->querylen - d->pos); if (r <= 0) return nexttcp(d); d->pos += r; if (d->pos == d->querylen) { struct taia now; taia_now(&now); taia_uint(&d->deadline,10); taia_add(&d->deadline,&d->deadline,&now); d->tcpstate = 3; } return 0; } if (d->tcpstate == 3) { /* have sent entire query to curserver on TCP socket s pos not defined */ r = read(fd,&ch,1); if (r <= 0) return nexttcp(d); d->packetlen = ch; d->tcpstate = 4; return 0; } if (d->tcpstate == 4) { /* have sent entire query to curserver on TCP socket s pos not defined have received one byte of packet length into packetlen */ r = read(fd,&ch,1); if (r <= 0) return nexttcp(d); d->packetlen <<= 8; d->packetlen += ch; d->tcpstate = 5; d->pos = 0; d->packet = alloc(d->packetlen); if (!d->packet) { dns_transmit_free(d); return -1; } return 0; } if (d->tcpstate == 5) { /* have sent entire query to curserver on TCP socket s have received entire packet length into packetlen packet is allocated have received pos bytes of packet */ r = read(fd,d->packet + d->pos,d->packetlen - d->pos); if (r <= 0) return nexttcp(d); d->pos += r; if (d->pos < d->packetlen) return 0; socketfree(d); if (irrelevant(d,d->packet,d->packetlen)) return nexttcp(d); if (serverwantstcp(d->packet,d->packetlen)) return nexttcp(d); if (serverfailed(d->packet,d->packetlen)) return nexttcp(d); queryfree(d); return 1; } return 0; }