bool JDownloadGameFileSocket::waitForConnected( int msecs) { JElapsedTimer timer; timer.start(); while(timer.elapsed()<msecs) { if(socketState()!=QAbstractSocket::ConnectingState || m_isError) { break; } QCoreApplication::processEvents(); } return socketState()==QAbstractSocket::ConnectedState; }
// Called by the socket on state change void TcpConnector::sState(QAbstractSocket::SocketState state) { // logMessage( socketState( state )); if(state == QAbstractSocket::UnconnectedState) { checkboxOnline->setCheckState(Qt::Unchecked); } else if(state == QAbstractSocket::ConnectedState) { checkboxOnline->setCheckState(Qt::Checked); //button->setStyleSheet("background-color: #33ff33; color: #000000"); } else { checkboxOnline->setCheckState(Qt::PartiallyChecked); } if(mConnect && state == QAbstractSocket::UnconnectedState) { mConnect = false; _connectSocket(); } //button->setText( socketState( state )); emit stateChange( socketState(state) ); }
void NewNet::TcpClientSocket::connect(const std::string & host, unsigned int port) { assert((descriptor() == -1) || (socketState() == SocketUninitialized)); setSocketState(SocketConnecting); NNLOG("newnet.net.debug", "Resolving host '%s'.", host.c_str()); struct hostent *h = gethostbyname(host.c_str()); if(! h) { NNLOG("newnet.net.warn", "Cannot resolve host '%s'.", host.c_str()); setSocketError(ErrorCannotResolve); cannotConnectEvent(this); return; } struct sockaddr_in address; memset(&address, 0, sizeof(address)); address.sin_family = AF_INET; memcpy(&(address.sin_addr.s_addr), *(h->h_addr_list), sizeof(address.sin_addr.s_addr)); address.sin_port = htons(port); NNLOG("newnet.net.debug", "Connecting to host '%s:%u'.", host.c_str(), port); int s = socket(PF_INET, SOCK_STREAM, 0); if (!setnonblocking(s)) NNLOG("newnet.net.warn", "Couldn't set socket %i to non blocking (errno: %i)", s, errno); setDescriptor(s); if(s < 0) { NNLOG("newnet.net.warn", "Cannot connect to host '%s:%u', error: %i.", host.c_str(), port, WSAGetLastError()); setSocketError(ErrorCannotConnect); cannotConnectEvent(this); return; } // Add a connection timeout if (reactor()) { m_ConnectionTimeout = reactor()->addTimeout(120000, this, &TcpClientSocket::onConnectionTimeout); } connectedEvent.connect(this, &TcpClientSocket::onConnected); if(::connect(s, (struct sockaddr *)&address, sizeof(struct sockaddr_in)) == 0) { // When using non blocking socket (most of the time), we don't get here. NNLOG("newnet.net.debug", "Connected to host '%s:%u'.", host.c_str(), port); setSocketState(SocketConnected); connectedEvent(this); } else if(WSAGetLastError() != WSAEWOULDBLOCK) { // When using non blocking socket (most of the time), we don't get here. NNLOG("newnet.net.warn", "Cannot connect to host '%s:%u', error: %i.", host.c_str(), port, WSAGetLastError()); setSocketError(ErrorCannotConnect); cannotConnectEvent(this); } }
void NewNet::UnixServerSocket::disconnect() { if((descriptor() == -1) || (socketState() != SocketListening)) { NNLOG("newnet.net.warn", "Trying to disconnect an uninitialized unix server socket."); return; } ServerSocket::disconnect(); unlink(m_Path.c_str()); }
uint8_t REDFLY::socketSendPGM(uint8_t socket, PGM_P stream, uint8_t *ip, uint16_t port) { uint8_t len; uint16_t size = strlen_P(stream); //socket uitoa(socket, (char*)buffer); //size strcat_P((char*)buffer, PSTR(",")); len = strlen((char*)buffer); uitoa(size, (char*)&buffer[len]); //ip if(ip && (socketState(socket) == SOCKET_UDP)) { strcat_P((char*)buffer, PSTR(",")); len = strlen((char*)buffer); iptoa(ip, (char*)&buffer[len]); } else { strcat_P((char*)buffer, PSTR(",0")); } //port if(port && (socketState(socket) == SOCKET_UDP)) { strcat_P((char*)buffer, PSTR(",")); len = strlen((char*)buffer); uitoa(port, (char*)&buffer[len]); } else { strcat_P((char*)buffer, PSTR(",0")); } //data strcat_P((char*)buffer, PSTR(",")); return cmd(PSTR(CMD_SEND), (char*)buffer, stream); //x,xxxx,xxx.xxx.xxx.xxx,xxxxx, }
void TcpConnector::_connectSocket() { QAbstractSocket::SocketState s = socket->state(); logMessage(QString("Connecting to %1:%2").arg(mHost).arg(mPort)); if(socket->state() != QAbstractSocket::UnconnectedState) { logMessage(QString("Socket already connected while connecting - state is: %1").arg( socketState(s))); return; } socket->connectToHost( mHost, mPort ); }
uint16_t REDFLY::socketRead(uint8_t *socket, uint16_t *len, uint8_t *ip, uint16_t *port, uint8_t *dst, uint16_t dst_size) { uint8_t stop=0; uint16_t rd=0; static uint8_t last_socket=INVALID_SOCKET; static uint16_t last_len=0; static uint8_t buf[8], pos=0; static uint16_t udp_port=0; static uint8_t udp_ip[4]; if(read_state == 2) //we are currently reading data { if((*socket != INVALID_SOCKET) && (*socket != last_socket)) //abort if not searched socket { return 0; } if(dst_size == 0) //only get len and socket { *socket = last_socket; *len = last_len; return 0; } } do { switch(read_state) { case 0: //restart searching read_state = 1; last_socket = INVALID_SOCKET; last_len = 0; pos = 0; udp_port = 0; udp_ip[0] = 0; udp_ip[1] = 0; udp_ip[2] = 0; udp_ip[3] = 0; case 1: //search while((pos<8) && available()) { buf[pos++] = read(); } if(pos >= 8) { if((buf[0] == 'R') && \ (buf[1] == 'S') && \ (buf[2] == 'I') && \ (buf[3] == '_') && \ (buf[4] == 'R') && \ (buf[5] == 'E') && \ (buf[6] == 'A') && \ (buf[7] == 'D')) //RSI_READ { last_socket = readwait(); last_len = readwait(); last_len |= readwait()<<8; if((last_socket == INVALID_SOCKET) || (last_len == 0)) { read_state = 0; last_socket = INVALID_SOCKET; last_len = 0; break; } read_state = 2; //get IP and port on UDP connection if(socketState(last_socket) == SOCKET_UDP) { udp_ip[0] = readwait(); udp_ip[1] = readwait(); udp_ip[2] = readwait(); udp_ip[3] = readwait(); udp_port = readwait(); udp_port |= readwait()<<8; } if((*socket != INVALID_SOCKET) && (*socket != last_socket)) //abort if not searched socket { return 0; } if(dst_size == 0) { stop = 1; //break loop } } else if((buf[0] == 'S') && \ (buf[1] == 'I') && \ (buf[2] == '_') && \ (buf[3] == 'C') && \ (buf[4] == 'L') && \ (buf[5] == 'O') && \ (buf[6] == 'S') && \ (buf[7] == 'E')) //SI_CLOSE { last_socket = readwait(); readwait(); readwait(); //trailing \r\n last_len = 0; rd = 0xFFFF; read_state = 0; stop = 1; //break loop for(uint8_t i=0; i<MAX_SOCKETS; i++) { if(socket_state[i].handle == last_socket) { socket_state[i].handle = INVALID_SOCKET; socket_state[i].state = SOCKET_CLOSED; break; } } } else { //move one byte pos = 7; buf[0] = buf[1]; buf[1] = buf[2]; buf[2] = buf[3]; buf[3] = buf[4]; buf[4] = buf[5]; buf[5] = buf[6]; buf[6] = buf[7]; } } break; case 2: //receive *dst = readwait(); dst_size--; rd++; if(--last_len == 0) { readwait(); readwait(); //trailing \r\n read_state = 0; stop = 1; //break loop } else if(dst_size == 0) { stop = 1; //break loop } break; } }while(available() && (stop == 0)); *socket = last_socket; *len = last_len; if(ip && udp_ip[0]) { ip[0] = udp_ip[0]; ip[1] = udp_ip[1]; ip[2] = udp_ip[2]; ip[3] = udp_ip[3]; } if(port && udp_port) { *port = udp_port; } return rd; }