| 9 | |
| 10 | namespace humblenet { |
| 11 | ha_bool P2PSignalConnection::processMsg(const HumblePeer::Message* msg) |
| 12 | { |
| 13 | auto msgType = msg->message_type(); |
| 14 | |
| 15 | // we don't accept anything but HelloServer from a peer which hasn't sent one yet |
| 16 | if (!this->peerId) { |
| 17 | if (msgType != HumblePeer::MessageType::HelloServer) { |
| 18 | LOG_WARNING("Got non-HelloServer message (%s) from non-authenticated peer \"%s\"\n", HumblePeer::EnumNameMessageType(msgType), this->url.c_str()); |
| 19 | // TODO: send error message |
| 20 | return false; |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | switch (msgType) { |
| 25 | case HumblePeer::MessageType::P2POffer: |
| 26 | { |
| 27 | auto p2p = reinterpret_cast<const HumblePeer::P2POffer*>(msg->message()); |
| 28 | auto peer = p2p->peerId(); |
| 29 | |
| 30 | // look up target peer, send message on |
| 31 | // TODO: should ensure there's not already a negotiation going |
| 32 | |
| 33 | bool emulated = (p2p->flags() & 0x1); |
| 34 | |
| 35 | LOG_INFO("P2POffer from peer %u (%s) to peer %u", this->peerId, url.c_str(), peer); |
| 36 | if (emulated) { |
| 37 | LOG_INFO(", emulated connections not allowed\n"); |
| 38 | // TODO: better error message |
| 39 | sendNoSuchPeer(this, peer); |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | auto it = game->peers.find(peer); |
| 44 | if (it == game->peers.end()) { |
| 45 | LOG_WARNING(", no such peer\n"); |
| 46 | sendNoSuchPeer(this, peer); |
| 47 | } else { |
| 48 | P2PSignalConnection *otherPeer = it->second; |
| 49 | assert(otherPeer != NULL); |
| 50 | |
| 51 | // if not emulated check if otherPeer supports WebRTC |
| 52 | // to avoid unnecessary round trip |
| 53 | if (!emulated && !otherPeer->webRTCsupport) { |
| 54 | // webrtc connections not supported |
| 55 | LOG_INFO("(%s), refusing because target doesn't support WebRTC\n", otherPeer->url.c_str()); |
| 56 | sendPeerRefused(this, peer); |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | LOG_INFO("(%s)\n", otherPeer->url.c_str()); |
| 61 | |
| 62 | // TODO: should check that it doesn't exist already |
| 63 | this->connectedPeers.insert(otherPeer); |
| 64 | |
| 65 | // set peer id to originator so target knows who wants to connect |
| 66 | sendP2PConnect(otherPeer, this->peerId, p2p->flags(), p2p->offer()->c_str()); |
| 67 | } |
| 68 |
no test coverage detected