| 44 | |
| 45 | |
| 46 | RelaySocket::RelaySocket(const SocketManager& manager,UInt16 port) : UDPSocket(manager),port(port) { |
| 47 | |
| 48 | // executed in a parallel thread! |
| 49 | onError = [this](const Exception& ex) { DEBUG("Relay socket ", this->port, ", ", ex.error()); }; |
| 50 | |
| 51 | // executed in a parallel thread! |
| 52 | onPacket = [this](PoolBuffer& pBuffer, const SocketAddress& address) { |
| 53 | lock_guard<mutex> lock(_mutex); |
| 54 | auto it = _relayByAddress.find(address); |
| 55 | if(it==_relayByAddress.end()) { |
| 56 | DEBUG("Unknown relay ", address.toString()," address") |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | Relay& relay(*it->second); |
| 61 | if(relay.receive()) |
| 62 | INFO("Turn starting from ",relay.address1.toString()," to ",relay.address2.toString()," on ",this->port," relayed port") |
| 63 | |
| 64 | SocketAddress destinator(relay.address1 == address ? relay.address2 : relay.address1); |
| 65 | |
| 66 | DUMP("RELAY",pBuffer->data(), pBuffer->size(), "Relayed from ", address.toString(), " to ",destinator.toString()) |
| 67 | Exception ex; |
| 68 | send(ex, pBuffer->data(), pBuffer->size(), destinator); |
| 69 | if (ex) |
| 70 | WARN("Relay packet (size=", pBuffer->size(), ") from ", address.toString(), " to ", destinator.toString()," on ",this->port,", ",ex.error()) |
| 71 | else |
| 72 | DEBUG("Relay packet (size=", pBuffer->size(), ") from ", address.toString(), " to ", destinator.toString()," on ",this->port) |
| 73 | }; |
| 74 | |
| 75 | OnError::subscribe(onError); |
| 76 | OnPacket::subscribe(onPacket); |
| 77 | } |
| 78 | |
| 79 | RelaySocket::~RelaySocket() { |
| 80 | OnPacket::unsubscribe(onPacket); |