void PairingHandler::sendPairRequest() { // grab the node socket from the NodeList singleton UDPSocket *nodeSocket = NodeList::getInstance()->getNodeSocket(); // prepare the pairing request packet // use the getLocalAddress helper to get this client's listening address int localAddress = getLocalAddress(); char pairPacket[24] = {}; sprintf(pairPacket, "Find %d.%d.%d.%d:%hu", localAddress & 0xFF, (localAddress >> 8) & 0xFF, (localAddress >> 16) & 0xFF, (localAddress >> 24) & 0xFF, NodeList::getInstance()->getSocketListenPort()); qDebug("Sending pair packet: %s\n", pairPacket); sockaddr_in pairingServerSocket; pairingServerSocket.sin_family = AF_INET; // lookup the pairing server IP by the hostname struct hostent* hostInfo = gethostbyname(PAIRING_SERVER_HOSTNAME); memcpy(&pairingServerSocket.sin_addr, hostInfo->h_addr_list[0], hostInfo->h_length); pairingServerSocket.sin_port = htons(PAIRING_SERVER_PORT); // send the pair request to the pairing server nodeSocket->send((sockaddr*) &pairingServerSocket, pairPacket, strlen(pairPacket)); }
int main(int argc,char **argv) { ALvoid *data; ALsizei bits,freq,size; ALenum format; ALboolean success; // Default packet size (in bytes) // Note that this doesn't necessarily have to have anything to do with the // buffer size of the receiver. Just set it to something appropriate // (depending on the connection), and the receiver will take care of the // playing the sound correctly... unsigned int packetsize=10000; const char defaultfile[]="bee.wav"; const char *filename; if(argc>1) filename=argv[1]; else filename=defaultfile; if(argc>2) packetsize=atoi(argv[2]); try { success=alutLoadWAV(filename,&data,&format,&size,&bits,&freq); std::cerr << "Bits: " << bits << " Freq: " << freq << std::endl; if(success==AL_FALSE) { std::cerr << "Error loading " << filename << "\n"; exit(1); } UDPSocket socket; socket.setPeer(InetHostAddress("127.0.0.1"),33333); int totalsent=0; while(totalsent<size) { // Send data in packets with a 60 ms delay between packets if((totalsent+packetsize)>size) packetsize=size-totalsent; totalsent+=socket.send((char *)data+totalsent,packetsize); ost::Thread::sleep(60); } free(data); } catch(...) { std::cerr << "Error caught!\n"; } return 0; }
Ping( bool is_server ) : socket( ios, is_server ? Address(ios,SERVER_ADDR) : Address() ) { if (is_server) { socket.addPacketListener( this ); for (;;) ios.poll(); } else { char msg[128] = "Hello"; socket.send( Address(ios,SERVER_ADDR), msg, strlen(msg)+1 ); } }
bool PacketSender::process() { uint64_t USECS_PER_SECOND = 1000 * 1000; uint64_t SEND_INTERVAL_USECS = (_packetsPerSecond == 0) ? USECS_PER_SECOND : (USECS_PER_SECOND / _packetsPerSecond); // keep track of our process call times, so we have a reliable account of how often our caller calls us uint64_t now = usecTimestampNow(); uint64_t elapsedSinceLastCall = now - _lastProcessCallTime; _lastProcessCallTime = now; _averageProcessCallTime.updateAverage(elapsedSinceLastCall); if (_packets.size() == 0) { if (isThreaded()) { usleep(SEND_INTERVAL_USECS); } else { return isStillRunning(); // in non-threaded mode, if there's nothing to do, just return, keep running till they terminate us } } int packetsPerCall = _packets.size(); // in threaded mode, we just empty this! int packetsThisCall = 0; // if we're in non-threaded mode, then we actually need to determine how many packets to send per call to process // based on how often we get called... We do this by keeping a running average of our call times, and we determine // how many packets to send per call if (!isThreaded()) { int averageCallTime; const int TRUST_AVERAGE_AFTER = AVERAGE_CALL_TIME_SAMPLES * 2; if (_usecsPerProcessCallHint == 0 || _averageProcessCallTime.getSampleCount() > TRUST_AVERAGE_AFTER) { averageCallTime = _averageProcessCallTime.getAverage(); } else { averageCallTime = _usecsPerProcessCallHint; } // we can determine how many packets we need to send per call to achieve our desired // packets per second send rate. int callsPerSecond = USECS_PER_SECOND / averageCallTime; packetsPerCall = ceil(_packetsPerSecond / callsPerSecond); // send at least one packet per call, if we have it if (packetsPerCall < 1) { packetsPerCall = 1; } } int packetsLeft = _packets.size(); bool keepGoing = packetsLeft > 0; while (keepGoing) { NetworkPacket& packet = _packets.front(); // send the packet through the NodeList... UDPSocket* nodeSocket = NodeList::getInstance()->getNodeSocket(); nodeSocket->send(&packet.getAddress(), packet.getData(), packet.getLength()); packetsThisCall++; if (_notify) { _notify->packetSentNotification(packet.getLength()); } lock(); _packets.erase(_packets.begin()); unlock(); packetsLeft = _packets.size(); // in threaded mode, we go till we're empty if (isThreaded()) { keepGoing = packetsLeft > 0; // dynamically sleep until we need to fire off the next set of voxels we only sleep in threaded mode if (keepGoing) { uint64_t elapsed = now - _lastSendTime; int usecToSleep = std::max(SEND_INTERVAL_USECS, SEND_INTERVAL_USECS - elapsed); // we only sleep in non-threaded mode if (usecToSleep > 0) { usleep(usecToSleep); } } } else { // in non-threaded mode, we send as many packets as we need per expected call to process() keepGoing = (packetsThisCall < packetsPerCall) && (packetsLeft > 0); } _lastSendTime = now; } return isStillRunning(); // keep running till they terminate us }
main() { UDPSocket s; s.send("test","localhost",9999); s.close(); }