public void RelayTo(short opCode, GcConnection connection) { if (!connection.IsConnected) { throw new Exception("Cannot relay to a connection which is closed"); } _relayConnections.TryAdd(opCode, connection); connection.InterceptIncomingData(data => { var flags = data[0]; // There's no peer id if ((flags & MessageFlags.PaddedPeerId) <= 0) { return(false); } var peerId = EndianBitConverter.Little.ToInt32(data, 3); var peer = connection.GetRelayedPeer(peerId); // Overide the peerId of the message // If it's not a virtual peer, but a direct one - // cleanup the peer id so that direct client doesn't know his id EndianBitConverter.Little.CopyBytes(peer.IsVirtual ? peer.PeerId : -1, data, 3); peer.SendRawData(data); return(true); }); }
public int GetPeerIdInRelayedServer(GcConnection connection) { _relayedPeerIds.TryGetValue(connection, out var result); // Return -1 instead of 0 for missing result, // because it's more "meaningful" return(result == 0 ? -1 : result); }
/// <summary> /// For forwarding messages. /// Saves information that this peer will be represented by a given peerId in a different server /// </summary> /// <param name="peerId"></param> /// <param name="connection"></param> public void SetPeerIdInRelayedServer(int peerId, GcConnection connection) { _relayedPeerIds.TryAdd(connection, peerId); }