Showing the changes to interface/src/main.cpp (1 of 1 file). Show all
Unified · Split
| @@ -39,6 +39,8 @@ · 38 unmodified lines | |||
| 39 | #include <ifaddrs.h> | 39 | #include <ifaddrs.h> |
| 40 | #endif | 40 | #endif |
| 41 | 41 | ||
| 42 | #include <pthread.h> | ||
| 43 | |||
| 42 | #include <glm/glm.hpp> | 44 | #include <glm/glm.hpp> |
| 43 | #include <glm/gtc/matrix_transform.hpp> | 45 | #include <glm/gtc/matrix_transform.hpp> |
| 44 | #include <glm/gtc/type_ptr.hpp> | 46 | #include <glm/gtc/type_ptr.hpp> |
| @@ -85,6 +87,10 @@ using namespace std; · 40 unmodified lines | |||
| 85 | void reshape(int width, int height); // will be defined below | 87 | void reshape(int width, int height); // will be defined below |
| 86 | void loadViewFrustum(ViewFrustum& viewFrustum); // will be defined below | 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 | unsigned char incomingPacket[MAX_PACKET_SIZE]; | 94 | unsigned char incomingPacket[MAX_PACKET_SIZE]; |
| 89 | int packetCount = 0; | 95 | int packetCount = 0; |
| 90 | int packetsPerSecond = 0; | 96 | int packetsPerSecond = 0; |
| @@ -416,6 +422,11 @@ void terminate () { · 325 unmodified lines | |||
| 416 | audio.terminate(); | 422 | audio.terminate(); |
| 417 | #endif | 423 | #endif |
| 418 | 424 | ||
| 425 | if (enableNetworkThread) { | ||
| 426 | stopNetworkReceiveThread = true; | ||
| 427 | pthread_join(networkReceiveThread, NULL); | ||
| 428 | } | ||
| 429 | |||
| 419 | exit(EXIT_SUCCESS); | 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 | // Receive packets from other agents/servers and decide what to do with them! | 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 | sockaddr senderAddress; | 1490 | sockaddr senderAddress; |
| 1480 | ssize_t bytesReceived; | 1491 | ssize_t bytesReceived; |
| 1481 | 1492 | ||
| 1482 | while (AgentList::getInstance()->getAgentSocket().receive(&senderAddress, incomingPacket, &bytesReceived)) { | 1493 | while (!stopNetworkReceiveThread) { |
| 1483 | packetCount++; | 1494 | if (AgentList::getInstance()->getAgentSocket().receive(&senderAddress, incomingPacket, &bytesReceived)) { |
| 1484 | bytesCount += bytesReceived; | 1495 | packetCount++; |
| 1485 | 1496 | bytesCount += bytesReceived; | |
| 1486 | switch (incomingPacket[0]) { | 1497 | |
| 1487 | case PACKET_HEADER_TRANSMITTER_DATA: | 1498 | switch (incomingPacket[0]) { |
| 1488 | myAvatar.processTransmitterData(incomingPacket, bytesReceived); | 1499 | case PACKET_HEADER_TRANSMITTER_DATA: |
| 1489 | break; | 1500 | myAvatar.processTransmitterData(incomingPacket, bytesReceived); |
| 1490 | case PACKET_HEADER_VOXEL_DATA: | 1501 | break; |
| 1491 | case PACKET_HEADER_Z_COMMAND: | 1502 | case PACKET_HEADER_VOXEL_DATA: |
| 1492 | case PACKET_HEADER_ERASE_VOXEL: | 1503 | case PACKET_HEADER_Z_COMMAND: |
| 1493 | voxels.parseData(incomingPacket, bytesReceived); | 1504 | case PACKET_HEADER_ERASE_VOXEL: |
| 1494 | break; | 1505 | voxels.parseData(incomingPacket, bytesReceived); |
| 1495 | case PACKET_HEADER_BULK_AVATAR_DATA: | 1506 | break; |
| 1496 | AgentList::getInstance()->processBulkAgentData(&senderAddress, | 1507 | case PACKET_HEADER_BULK_AVATAR_DATA: |
| 1497 | incomingPacket, | 1508 | AgentList::getInstance()->processBulkAgentData(&senderAddress, |
| 1498 | bytesReceived); | 1509 | incomingPacket, |
| 1499 | break; | 1510 | bytesReceived); |
| 1500 | default: | 1511 | break; |
| 1501 | AgentList::getInstance()->processAgentData(&senderAddress, incomingPacket, bytesReceived); | 1512 | default: |
| 1502 | break; | 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 | void idle(void) { | 1527 | void idle(void) { |
| @@ -1536,7 +1556,9 @@ void idle(void) { · 28 unmodified lines | |||
| 1536 | updateAvatar(deltaTime); | 1556 | updateAvatar(deltaTime); |
| 1537 | 1557 | ||
| 1538 | // read incoming packets from network | 1558 | // read incoming packets from network |
| 1539 | networkReceive(); | 1559 | if (!enableNetworkThread) { |
| 1560 | networkReceive(0); | ||
| 1561 | } | ||
| 1540 | 1562 | ||
| 1541 | //loop through all the other avatars and simulate them... | 1563 | //loop through all the other avatars and simulate them... |
| 1542 | AgentList* agentList = AgentList::getInstance(); | 1564 | AgentList* agentList = AgentList::getInstance(); |
| @@ -1680,7 +1702,10 @@ int main(int argc, const char * argv[]) · 137 unmodified lines | |||
| 1680 | listenPort = atoi(portStr); | 1702 | listenPort = atoi(portStr); |
| 1681 | } | 1703 | } |
| 1682 | AgentList::createInstance(AGENT_TYPE_AVATAR, listenPort); | 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 | gettimeofday(&applicationStartupTime, NULL); | 1710 | gettimeofday(&applicationStartupTime, NULL); |
| 1686 | const char* domainIP = getCmdOption(argc, argv, "--domain"); | 1711 | const char* domainIP = getCmdOption(argc, argv, "--domain"); |
| @@ -1763,6 +1788,12 @@ int main(int argc, const char * argv[]) · 76 unmodified lines | |||
| 1763 | printLog("Local Voxel File loaded.\n"); | 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 | glutTimerFunc(1000, Timer, 0); | 1797 | glutTimerFunc(1000, Timer, 0); |
| 1767 | glutMainLoop(); | 1798 | glutMainLoop(); |
| 1768 | 1799 | ||