| 114 | } |
| 115 | |
| 116 | void main_loop() |
| 117 | { |
| 118 | humblenet_p2p_wait(100); |
| 119 | |
| 120 | if (myPeer == 0) { |
| 121 | // check to see if we finished connecting to the peer server |
| 122 | myPeer = humblenet_p2p_get_my_peer_id(); |
| 123 | if (myPeer != 0) { |
| 124 | std::cout << "We connected to the peer server with peer " << myPeer << std::endl; |
| 125 | if (startPeerStatus == PeerStatus::WAITING) { |
| 126 | // initiate the connection to the remote peer |
| 127 | startPeerLastHello = time(NULL); |
| 128 | startPeerStatus = PeerStatus::CONNECTING; |
| 129 | send_message(startPeerId, MessageType::HELLO, "", 0); |
| 130 | } |
| 131 | } |
| 132 | } else { |
| 133 | // check if we have connected to the other peer |
| 134 | switch (startPeerStatus) { |
| 135 | case PeerStatus::CONNECTING: |
| 136 | { |
| 137 | time_t now = time(NULL); |
| 138 | if (now > startPeerLastHello) { |
| 139 | // try once a second |
| 140 | send_message(startPeerId, MessageType::HELLO, "", 0); |
| 141 | startPeerLastHello = now; |
| 142 | } |
| 143 | } |
| 144 | break; |
| 145 | case PeerStatus::CONNECTED: |
| 146 | // we are connected |
| 147 | sendChat(startPeerId, "Hello World!"); |
| 148 | startPeerStatus = PeerStatus::SENT; |
| 149 | #ifdef EMSCRIPTEN |
| 150 | EM_ASM( |
| 151 | var button = document.getElementById('ChatButton'); |
| 152 | if (button) { |
| 153 | button.disabled = false; |
| 154 | } |
| 155 | ); |
| 156 | #endif |
| 157 | break; |
| 158 | default: |
| 159 | break; |
| 160 | } |
| 161 | |
| 162 | // we are connected so lets see if we have any data to process |
| 163 | // this buffer should be LARGE enough to handle any packet you might send OR use peek to see how big the next one is |
| 164 | uint8_t buff[MAX_MESSAGE_SIZE]; |
| 165 | bool done = false; |
| 166 | |
| 167 | while (!done) { |
| 168 | PeerId remotePeer = 0; |
| 169 | int ret = humblenet_p2p_recvfrom(buff, sizeof(buff), &remotePeer, CHANNEL); |
| 170 | if (ret < 0) { |
| 171 | if (remotePeer != 0) { |
| 172 | // disconnected client |
| 173 | } else { |
no test coverage detected