void processStream(const QByteArray &in) { inStream += in; ObjectSessionWatcher watch(&sess); while(1) { QByteArray packet; // try to extract ChannelData or a STUN message from // the stream packet = StunAllocate::readChannelData((const quint8 *)inStream.data(), inStream.size()); if(packet.isNull()) { packet = StunMessage::readStun((const quint8 *)inStream.data(), inStream.size()); if(packet.isNull()) break; } inStream = inStream.mid(packet.size()); // processDatagram may cause the session to be reset // or the object to be deleted processDatagram(packet); if(!watch.isValid()) break; } }
// Main sender thread void *senderMain(void *b) { // Initialize and bind to socket sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (sockfd < 0) error("ERROR opening socket"); bzero((char *) &serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = INADDR_ANY; serv_addr.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) error("ERROR on binding"); rcvlen = sizeof(rcv_addr); // Wait for datagrams from receiver, process, and return response int done = 0; while (1) { recvfrom(sockfd, messageBuf, PKTSIZE, 0, (struct sockaddr *) &rcv_addr, &rcvlen); done = processDatagram(); if(done) { printf("Done transmitting file!\n\n"); resetSender(); } } /* end of while */ pthread_exit(0); }
void UdpSocketHandler::readPendingDatagrams() { while (d->socket->hasPendingDatagrams()) { QHostAddress sender; quint16 senderPort; QByteArray *ba = new QByteArray(); ba->resize(d->socket->pendingDatagramSize()); const qint64 bytesRead = d->socket->readDatagram(ba->data(), ba->size(), &sender, &senderPort); d->totalBytesRead += bytesRead; if (!processDatagram(ba, sender, senderPort)) { delete ba; } } }
void MediaStreamingSocketHandler::Private::onReadyReadDatagrams() { while (_socket->hasPendingDatagrams()) { QHostAddress sender; quint16 senderPort; QByteArray data; data.resize(_socket->pendingDatagramSize()); const qint64 bytesRead = _socket->readDatagram(data.data(), data.size(), &sender, &senderPort); processDatagram(&data, sender, senderPort); _totalBytesRead += bytesRead; } }
void Daemon::on_read_Datagrams() { //QMessageBox::critical(NULL, tr("提示"), tr("on_read_Datagrams.") ); while (udpSocket->hasPendingDatagrams()) { QByteArray block; block.resize(udpSocket->pendingDatagramSize()); if ( -1 == udpSocket->readDatagram(block.data(), block.size(), &senderIp, &senderPort)) continue; processDatagram(block); } }
// private void UdpReceiver::onReadyRead() { if (!inSocket->waitForReadyRead(8)) { // 1/60fps ~= 16.7ms, 1/120fps ~= 8.3ms return; } // TODO: add failsafe // if a command not recieved then slowly return all channel to neutral // while (inSocket->hasPendingDatagrams()) { QByteArray datagram; datagram.resize(inSocket->pendingDatagramSize()); quint64 datagramSize; datagramSize = inSocket->readDatagram(datagram.data(), datagram.size()); processDatagram(datagram); } }
// Check all sockets foreach (sACNRxSocket* m_socket, m_sockets) { while(m_socket->hasPendingDatagrams()) { QByteArray data; data.resize(m_socket->pendingDatagramSize()); QHostAddress sender; quint16 senderPort; m_socket->readDatagram(data.data(), data.size(), &sender, &senderPort); processDatagram( data, m_socket->localAddress(), sender); } }
void processStream(const QByteArray &in) { inStream += in; while(1) { QByteArray packet; // try to extract ChannelData or a STUN message from // the stream packet = StunAllocate::readChannelData((const quint8 *)inStream.data(), inStream.size()); if(packet.isNull()) { packet = StunMessage::readStun((const quint8 *)inStream.data(), inStream.size()); if(packet.isNull()) break; } inStream = inStream.mid(packet.size()); processDatagram(packet); } }
//***** void DtmfDataLinkLayer::encode( boost::circular_buffer< Packet > *downIn, boost::circular_buffer< Frame > *downOut, boost::circular_buffer< Frame > *upIn, boost::circular_buffer< unsigned int > *upOut) { #ifdef DEBUG DEBUG_OUT << std::endl << "---------- ### ENCODE ### ----------" << std::endl; #endif //update pointers datagramDown = downIn; frameDown = downOut; frameUp = upIn; datagramUp = upOut; //do I have token? if(hasToken == 1) { //is the token expired? if(checkToken() == 1) return; } else { #ifdef DEBUG DEBUG_OUT << "has no token" << std::endl; #endif return; } //check if ready to process if(awaitsReply == 1) { //check current time time_t nowClock; time ( &nowClock ); //compare with saved timestamp from when transmission ended if((nowClock-timestampAwaitsReply)>MAX_TIME_TO_REPLY) { //if time expired #ifdef DEBUG DEBUG_OUT << "Time for receiver to reply is up" << std::endl; #endif //resend all resendData(0); } else { //if there is time left to reply #ifdef DEBUG std::cout << "Time left for receiver to reply: " << (MAX_TIME_TO_REPLY-(nowClock-timestampAwaitsReply)) << " seconds" << std::endl; #endif } } //start processing if there is data in the buffer if(!(datagramDown->empty())) { while(true) { //check if list is full if(nextInSendSequence >= 8) { #ifdef DEBUG DEBUG_OUT << "List is full...ending transmission" << std::endl; #endif endTransmission(); break; } //check if end of datagram is reached if(datagramIterator >= datagramLength) { //previous datagram has ended and all data has been sent and confirmed if(dataInSendList == 0 && receiverNeedsUpdate == 0) { datagramDown->pop_front(); datagramIterator = 0; receiverNeedsUpdate = 1; if(datagramDown->empty()) { #ifdef DEBUG DEBUG_OUT << "Last element popped" << std::endl; #endif return; } } if(receiverNeedsUpdate == 1) { //get address of receiver currentReceiver = datagramDown->front().recvAddr(); datagramLength = datagramDown->front().totalLength(); receiverNeedsUpdate = 0; #ifdef DEBUG DEBUG_OUT << "Setting receiver address to " << currentReceiver << std::endl; #endif } else { //datagram has ended but sent data has not yet been confirmed #ifdef DEBUG DEBUG_OUT << "Buffer is empty...ending transmission" << std::endl; #endif //Set EOT bit in last frame endTransmission(); break; } } else { //still data to process in this datagram #ifdef DEBUG DEBUG_OUT << "Popping from datagrambuffer..." << (int)datagramDown->front()[datagramIterator] << std::endl; #endif processDatagram((unsigned int)datagramDown->front()[datagramIterator]); datagramIterator++; } } } else { //buffer is empty #ifdef DEBUG DEBUG_OUT << "Buffer is empty...nothing to process" << std::endl; #endif if(hasToken == 1 && tokenAlreadyOffered == 0 && dataInSendList == 0) { // nothing to process. Might as well pass token #ifdef DEBUG DEBUG_OUT << "No need for token...passing" << std::endl; #endif offerToken(); } } }