Showing the changes to interface/src/main.cpp (1 of 1 file). Show all
Unified · Split
| @@ -39,6 +39,8 @@ · 38 unmodified lines | ||
| 39 | 39 | #include <ifaddrs.h> |
| 40 | 40 | #endif |
| 41 | 41 | |
| 42 | +#include <pthread.h> | |
| 43 | + | |
| 42 | 44 | #include <glm/glm.hpp> |
| 43 | 45 | #include <glm/gtc/matrix_transform.hpp> |
| 44 | 46 | #include <glm/gtc/type_ptr.hpp> |
| @@ -85,6 +87,10 @@ using namespace std; · 40 unmodified lines | ||
| 85 | 87 | void reshape(int width, int height); // will be defined below |
| 86 | 88 | void loadViewFrustum(ViewFrustum& viewFrustum); // will be defined below |
| 87 | 89 | |
| 90 | +bool enableNetworkThread = true; | |
| 91 | +pthread_t networkReceiveThread; | |
| 92 | +bool stopNetworkReceiveThread = false; | |
| 93 | + | |
| 88 | 94 | unsigned char incomingPacket[MAX_PACKET_SIZE]; |
| 89 | 95 | int packetCount = 0; |
| 90 | 96 | int packetsPerSecond = 0; |
| @@ -416,6 +422,11 @@ void terminate () { · 325 unmodified lines | ||
| 416 | 422 | audio.terminate(); |
| 417 | 423 | #endif |
| 418 | 424 | |
| 425 | + if (enableNetworkThread) { | |
| 426 | + stopNetworkReceiveThread = true; | |
| 427 | + pthread_join(networkReceiveThread, NULL); | |
| 428 | + } | |
| 429 | + | |
| 419 | 430 | exit(EXIT_SUCCESS); |
| 420 | 431 | } |
| 421 | 432 | |
| @@ -1474,34 +1485,43 @@ void key(unsigned char k, int x, int y) · 1052 unmodified lines | ||
| 1474 | 1485 | } |
| 1475 | 1486 | |
| 1476 | 1487 | // Receive packets from other agents/servers and decide what to do with them! |
| 1477 | −void networkReceive() | |
| 1488 | +void* networkReceive(void* args) | |
| 1478 | 1489 | { |
| 1479 | 1490 | sockaddr senderAddress; |
| 1480 | 1491 | ssize_t bytesReceived; |
| 1481 | − | |
| 1482 | − while (AgentList::getInstance()->getAgentSocket().receive(&senderAddress, incomingPacket, &bytesReceived)) { | |
| 1483 | − packetCount++; | |
| 1484 | − bytesCount += bytesReceived; | |
| 1485 | − | |
| 1486 | − switch (incomingPacket[0]) { | |
| 1487 | − case PACKET_HEADER_TRANSMITTER_DATA: | |
| 1488 | − myAvatar.processTransmitterData(incomingPacket, bytesReceived); | |
| 1489 | − break; | |
| 1490 | − case PACKET_HEADER_VOXEL_DATA: | |
| 1491 | − case PACKET_HEADER_Z_COMMAND: | |
| 1492 | − case PACKET_HEADER_ERASE_VOXEL: | |
| 1493 | − voxels.parseData(incomingPacket, bytesReceived); | |
| 1494 | − break; | |
| 1495 | − case PACKET_HEADER_BULK_AVATAR_DATA: | |
| 1496 | − AgentList::getInstance()->processBulkAgentData(&senderAddress, | |
| 1497 | − incomingPacket, | |
| 1498 | − bytesReceived); | |
| 1499 | − break; | |
| 1500 | − default: | |
| 1501 | − AgentList::getInstance()->processAgentData(&senderAddress, incomingPacket, bytesReceived); | |
| 1502 | − break; | |
| 1492 | + | |
| 1493 | + while (!stopNetworkReceiveThread) { | |
| 1494 | + if (AgentList::getInstance()->getAgentSocket().receive(&senderAddress, incomingPacket, &bytesReceived)) { | |
| 1495 | + packetCount++; | |
| 1496 | + bytesCount += bytesReceived; | |
| 1497 | + | |
| 1498 | + switch (incomingPacket[0]) { | |
| 1499 | + case PACKET_HEADER_TRANSMITTER_DATA: | |
| 1500 | + myAvatar.processTransmitterData(incomingPacket, bytesReceived); | |
| 1501 | + break; | |
| 1502 | + case PACKET_HEADER_VOXEL_DATA: | |
| 1503 | + case PACKET_HEADER_Z_COMMAND: | |
| 1504 | + case PACKET_HEADER_ERASE_VOXEL: | |
| 1505 | + voxels.parseData(incomingPacket, bytesReceived); | |
| 1506 | + break; | |
| 1507 | + case PACKET_HEADER_BULK_AVATAR_DATA: | |
| 1508 | + AgentList::getInstance()->processBulkAgentData(&senderAddress, | |
| 1509 | + incomingPacket, | |
| 1510 | + bytesReceived); | |
| 1511 | + break; | |
| 1512 | + default: | |
| 1513 | + AgentList::getInstance()->processAgentData(&senderAddress, incomingPacket, bytesReceived); | |
| 1514 | + break; | |
| 1515 | + } | |
| 1516 | + } else if (!enableNetworkThread) { | |
| 1517 | + break; | |
| 1503 | 1518 | } |
| 1504 | 1519 | } |
| 1520 | + | |
| 1521 | + if (enableNetworkThread) { | |
| 1522 | + pthread_exit(0); | |
| 1523 | + } | |
| 1524 | + return NULL; | |
| 1505 | 1525 | } |
| 1506 | 1526 | |
| 1507 | 1527 | void idle(void) { |
| @@ -1536,7 +1556,9 @@ void idle(void) { · 28 unmodified lines | ||
| 1536 | 1556 | updateAvatar(deltaTime); |
| 1537 | 1557 | |
| 1538 | 1558 | // read incoming packets from network |
| 1539 | − networkReceive(); | |
| 1559 | + if (!enableNetworkThread) { | |
| 1560 | + networkReceive(0); | |
| 1561 | + } | |
| 1540 | 1562 | |
| 1541 | 1563 | //loop through all the other avatars and simulate them... |
| 1542 | 1564 | AgentList* agentList = AgentList::getInstance(); |
| @@ -1680,7 +1702,10 @@ int main(int argc, const char * argv[]) · 137 unmodified lines | ||
| 1680 | 1702 | listenPort = atoi(portStr); |
| 1681 | 1703 | } |
| 1682 | 1704 | AgentList::createInstance(AGENT_TYPE_AVATAR, listenPort); |
| 1683 | − AgentList::getInstance()->getAgentSocket().setBlocking(false); | |
| 1705 | + enableNetworkThread = !cmdOptionExists(argc, argv, "--nonblocking"); | |
| 1706 | + if (!enableNetworkThread) { | |
| 1707 | + AgentList::getInstance()->getAgentSocket().setBlocking(false); | |
| 1708 | + } | |
| 1684 | 1709 | |
| 1685 | 1710 | gettimeofday(&applicationStartupTime, NULL); |
| 1686 | 1711 | const char* domainIP = getCmdOption(argc, argv, "--domain"); |
| @@ -1763,6 +1788,12 @@ int main(int argc, const char * argv[]) · 76 unmodified lines | ||
| 1763 | 1788 | printLog("Local Voxel File loaded.\n"); |
| 1764 | 1789 | } |
| 1765 | 1790 | |
| 1791 | + // create thread for receipt of data via UDP | |
| 1792 | + if (enableNetworkThread) { | |
| 1793 | + pthread_create(&networkReceiveThread, NULL, networkReceive, NULL); | |
| 1794 | + printLog("Network receive thread created.\n"); | |
| 1795 | + } | |
| 1796 | + | |
| 1766 | 1797 | glutTimerFunc(1000, Timer, 0); |
| 1767 | 1798 | glutMainLoop(); |
| 1768 | 1799 | |