/** * Read a number of bytes from the interface. * * @param data Pointer to the data byte array to write the bytes to * @param maxLength The maximum number of bytes to write **/ void SerialLink::readBytes() { if (!validateConnection()) return; if (mode_port) // ESC32 special data read mode return readEsc32Tele(); const qint64 maxLength = 2048; char data[maxLength]; qint64 numBytes = 0, rBytes = 0; dataMutex.lock(); while ((numBytes = port->bytesAvailable())) { /* Read as much data in buffer as possible without overflow */ rBytes = numBytes; if(maxLength < rBytes) rBytes = maxLength; if (port->read(data, rBytes) == -1) { // -1 result means error emit portError(); if (!m_linkLossExpected) emit communicationError(this->getName(), tr("Could not read data - link %1 is disconnected!").arg(this->getName())); return; } QByteArray b(data, rBytes); emit bytesReceived(this, b); bitsReceivedTotal += rBytes * 8; } dataMutex.unlock(); }
void SerialLink::writeBytes(const char* data, qint64 size) { if (!validateConnection()) return; int b = port->write(data, size); if (b > 0) { // Increase write counter bitsSentTotal += b * 8; } else if (b == -1) { emit portError(); if (!m_linkLossExpected) emit communicationError(this->getName(), tr("Could not send data - error on link %1").arg(this->porthandle)); } }
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) , ui(new Ui::MainWindow) , score(0) , surface(NULL) , IP(QString()) , port(0) { ui->setupUi(this); connect(ui->JoinButton, SIGNAL(clicked()), this, SLOT(validateConnection())); connect(ui->action_New_game, SIGNAL(triggered()), this, SLOT(newGame())); connect(ui->action_exit, SIGNAL(triggered()), this, SLOT(close())); }
void BlockingTCPAcceptor::run() { // rise level if port is assigned dynamically char ipAddrStr[48]; ipAddrToDottedIP(&_bindAddress.ia, ipAddrStr, sizeof(ipAddrStr)); LOG(logLevelDebug, "Accepting connections at %s.", ipAddrStr); bool socketOpen = true; char strBuffer[64]; while(socketOpen) { { Lock guard(_mutex); if (_destroyed) break; } osiSockAddr address; osiSocklen_t len = sizeof(sockaddr); SOCKET newClient = epicsSocketAccept(_serverSocketChannel, &address.sa, &len); if(newClient!=INVALID_SOCKET) { // accept succeeded ipAddrToDottedIP(&address.ia, ipAddrStr, sizeof(ipAddrStr)); LOG(logLevelDebug, "Accepted connection from PVA client: %s.", ipAddrStr); // enable TCP_NODELAY (disable Nagle's algorithm) int optval = 1; // true int retval = ::setsockopt(newClient, IPPROTO_TCP, TCP_NODELAY, (char *)&optval, sizeof(int)); if(retval<0) { epicsSocketConvertErrnoToString(strBuffer, sizeof(strBuffer)); LOG(logLevelDebug, "Error setting TCP_NODELAY: %s.", strBuffer); } // enable TCP_KEEPALIVE retval = ::setsockopt(newClient, SOL_SOCKET, SO_KEEPALIVE, (char *)&optval, sizeof(int)); if(retval<0) { epicsSocketConvertErrnoToString(strBuffer, sizeof(strBuffer)); LOG(logLevelDebug, "Error setting SO_KEEPALIVE: %s.", strBuffer); } // do NOT tune socket buffer sizes, this will disable auto-tunning // get TCP send buffer size osiSocklen_t intLen = sizeof(int); int _socketSendBufferSize; retval = getsockopt(newClient, SOL_SOCKET, SO_SNDBUF, (char *)&_socketSendBufferSize, &intLen); if(retval<0) { epicsSocketConvertErrnoToString(strBuffer, sizeof(strBuffer)); LOG(logLevelDebug, "Error getting SO_SNDBUF: %s.", strBuffer); } /** * Create transport, it registers itself to the registry. */ detail::BlockingServerTCPTransportCodec::shared_pointer transport = detail::BlockingServerTCPTransportCodec::create( _context, newClient, _responseHandler, _socketSendBufferSize, _receiveBufferSize); // validate connection if(!validateConnection(transport, ipAddrStr)) { // TODO // wait for negative response to be sent back and // hold off the client for retrying at very high rate epicsThreadSleep(1.0); transport->close(); LOG( logLevelDebug, "Connection to PVA client %s failed to be validated, closing it.", ipAddrStr); continue; } LOG(logLevelDebug, "Serving to PVA client: %s.", ipAddrStr); }// accept succeeded else socketOpen = false; } // while }
int cmd_connect(int argc, char* argv[]){ //check the arguments number if(argc != 3){ printf("Please Provide IP Address and Port Of Peer\n"); return EINVAL; } struct addrinfo hints, *servinfo; int result; int peerConnection; char ipaddr[INET6_ADDRSTRLEN]; char* peer = argv[1]; //safe PORT copying int pp = atoi(argv[2]); char peerPort[50]; sprintf(peerPort, "%d", pp); //validate if connection allowable result = validateConnection(peer); if(result == -1){ return EHOSTUNREACH; } memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; //Connection process initiated if((result = getaddrinfo(peer, peerPort, &hints, &servinfo)) < 0){ printf("getaddrinfo error:\n"); return result; } if((peerConnection = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol)) < 0){ perror("connect socket :"); return peerConnection; } if(connect(peerConnection, servinfo->ai_addr, servinfo->ai_addrlen) < 0){ perror("connect connect :"); return -1; } //printout info message about remote host inet_ntop(servinfo->ai_family, get_in_addr((struct sockaddr *)servinfo->ai_addr), ipaddr, sizeof(ipaddr)); printf("\nCLIENT CONNECTING TO %s\n", ipaddr); //prepare message containing listening port number and hostnamt char sendBuff[200]; sprintf(sendBuff, "%s\n", localInfo.port); if(sendall(peerConnection, sendBuff, sizeof(sendBuff)) == -1){ perror("Register: send "); close(peerConnection); return -1; } //update connections list addToConnections(peer, peerPort, ipaddr, peerConnection); //update descriptor in FD_SET updateSelect(ADD, peerConnection, &master_set, &fdmax); printf("\n"); return 0; }
void SerialLink::readEsc32Tele(){ qint64 numBytes = 0; if (!validateConnection()) return; // Clear up the input Buffer if ( this->firstRead == 1) { while(true) { port->read(data,1); if (data[0] == 'A') { break; } } this->firstRead = 2; } retryA: if (!port->isOpen() || !mode_port) return; while( true) { numBytes = port->bytesAvailable(); if ( numBytes > 0) break; msleep(1); if (!port->isOpen() || !mode_port) break; } if ( this->firstRead == 0) { numBytes = port->read(data,1); if ( numBytes >= 1 ) { if ( data[0] != 'A') { goto retryA; } } else if (numBytes <= 0){ msleep(5); goto retryA; } } else { this->firstRead = 0; } while( true) { numBytes = port->bytesAvailable(); if ( numBytes > 0) break; msleep(1); if (!port->isOpen() || !mode_port) break; } numBytes = port->read(data,1); if ( numBytes == 1 ) { if ( data[0] != 'q') { goto retryA; } } else if (numBytes <= 0){ msleep(5); goto retryA; } while( true) { numBytes = port->bytesAvailable(); if ( numBytes > 0) break; msleep(1); if (!port->isOpen() || !mode_port) break; } numBytes = port->read(data,1); if ( numBytes == 1 ) { if ( data[0] != 'T') { msleep(5); goto retryA; } } else if (numBytes <= 0){ msleep(5); goto retryA; } while( true) { numBytes = port->bytesAvailable(); if ( numBytes >= 2) break; msleep(1); if (!port->isOpen() || !mode_port) break; } port->read(data,2); rows = 0; cols = 0; rows = data[0]; cols = data[1]; int length_array = (((cols*rows)*sizeof(float))+2); if ( length_array > 300) { goto retryA; } while( true) { numBytes = port->bytesAvailable(); if ( numBytes >= length_array) break; msleep(1); if (!port->isOpen() || !mode_port) break; } numBytes = port->read(data,length_array); if (numBytes == length_array ){ QByteArray b(data, numBytes); emit teleReceived(b, rows, cols); } goto retryA; }