|
| 1 | +use hyperion_proto::{ChunkPosition, ServerToProxyMessage, UpdateChannelPosition}; |
| 2 | + |
| 3 | +use crate::net::{ConnectionId, ProxyId}; |
| 4 | + |
| 5 | +#[derive(Clone, PartialEq)] |
| 6 | +pub struct UpdatePlayerPositions { |
| 7 | + pub stream: Vec<ConnectionId>, |
| 8 | + pub positions: Vec<ChunkPosition>, |
| 9 | +} |
| 10 | + |
| 11 | +#[derive(Clone, Copy, PartialEq, Eq)] |
| 12 | +pub struct AddChannel<'a> { |
| 13 | + pub channel_id: u32, |
| 14 | + |
| 15 | + pub unsubscribe_packets: &'a [u8], |
| 16 | +} |
| 17 | + |
| 18 | +#[derive(Clone, PartialEq)] |
| 19 | +pub struct UpdateChannelPositions<'a> { |
| 20 | + pub updates: &'a [UpdateChannelPosition], |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Clone, Copy, PartialEq, Eq)] |
| 24 | +pub struct RemoveChannel { |
| 25 | + pub channel_id: u32, |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Clone, Copy, PartialEq, Eq)] |
| 29 | +pub struct SubscribeChannelPackets<'a> { |
| 30 | + pub channel_id: u32, |
| 31 | + pub exclude: Option<ConnectionId>, |
| 32 | + |
| 33 | + pub data: &'a [u8], |
| 34 | +} |
| 35 | + |
| 36 | +#[derive(Clone, Copy, PartialEq, Eq)] |
| 37 | +pub struct SetReceiveBroadcasts { |
| 38 | + pub stream: ConnectionId, |
| 39 | +} |
| 40 | + |
| 41 | +#[derive(Clone, PartialEq, Eq)] |
| 42 | +pub struct BroadcastGlobal<'a> { |
| 43 | + pub exclude: Option<ConnectionId>, |
| 44 | + |
| 45 | + pub data: &'a [u8], |
| 46 | +} |
| 47 | + |
| 48 | +#[derive(Clone, PartialEq)] |
| 49 | +pub struct BroadcastLocal<'a> { |
| 50 | + pub center: ChunkPosition, |
| 51 | + pub exclude: Option<ConnectionId>, |
| 52 | + |
| 53 | + pub data: &'a [u8], |
| 54 | +} |
| 55 | + |
| 56 | +#[derive(Clone, PartialEq, Eq)] |
| 57 | +pub struct BroadcastChannel<'a> { |
| 58 | + pub channel_id: u32, |
| 59 | + pub exclude: Option<ConnectionId>, |
| 60 | + |
| 61 | + pub data: &'a [u8], |
| 62 | +} |
| 63 | + |
| 64 | +#[derive(Clone, PartialEq, Eq)] |
| 65 | +pub struct Unicast<'a> { |
| 66 | + pub stream: ConnectionId, |
| 67 | + |
| 68 | + pub data: &'a [u8], |
| 69 | +} |
| 70 | + |
| 71 | +#[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 72 | +pub struct Shutdown { |
| 73 | + pub stream: ConnectionId, |
| 74 | +} |
| 75 | + |
| 76 | +#[derive(Clone, PartialEq)] |
| 77 | +pub enum IntermediateServerToProxyMessage<'a> { |
| 78 | + UpdatePlayerPositions(UpdatePlayerPositions), |
| 79 | + AddChannel(AddChannel<'a>), |
| 80 | + UpdateChannelPositions(UpdateChannelPositions<'a>), |
| 81 | + RemoveChannel(RemoveChannel), |
| 82 | + SubscribeChannelPackets(SubscribeChannelPackets<'a>), |
| 83 | + BroadcastGlobal(BroadcastGlobal<'a>), |
| 84 | + BroadcastLocal(BroadcastLocal<'a>), |
| 85 | + BroadcastChannel(BroadcastChannel<'a>), |
| 86 | + Unicast(Unicast<'a>), |
| 87 | + SetReceiveBroadcasts(SetReceiveBroadcasts), |
| 88 | + Shutdown(Shutdown), |
| 89 | +} |
| 90 | + |
| 91 | +impl IntermediateServerToProxyMessage<'_> { |
| 92 | + /// Whether the result of [`IntermediateServerToProxyMessage::transform_for_proxy`] will be |
| 93 | + /// affected by the proxy id provided |
| 94 | + #[must_use] |
| 95 | + pub const fn affected_by_proxy(&self) -> bool { |
| 96 | + match self { |
| 97 | + Self::UpdatePlayerPositions(_) |
| 98 | + | Self::SubscribeChannelPackets(_) |
| 99 | + | Self::BroadcastGlobal(_) |
| 100 | + | Self::BroadcastLocal(_) |
| 101 | + | Self::BroadcastChannel(_) |
| 102 | + | Self::Unicast(_) |
| 103 | + | Self::SetReceiveBroadcasts(_) |
| 104 | + | Self::Shutdown(_) => true, |
| 105 | + Self::AddChannel(_) | Self::UpdateChannelPositions(_) | Self::RemoveChannel(_) => false, |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /// Transforms an intermediate message to a message suitable for sending to a particular proxy. |
| 110 | + /// Returns `None` if this message should not be sent to the proxy. |
| 111 | + #[must_use] |
| 112 | + pub fn transform_for_proxy(&self, proxy_id: ProxyId) -> Option<ServerToProxyMessage<'_>> { |
| 113 | + let filter_map_connection_id = |
| 114 | + |id: ConnectionId| (id.proxy_id() == proxy_id).then(|| id.inner()); |
| 115 | + match self { |
| 116 | + Self::UpdatePlayerPositions(message) => { |
| 117 | + Some(ServerToProxyMessage::UpdatePlayerPositions( |
| 118 | + hyperion_proto::UpdatePlayerPositions { |
| 119 | + stream: message |
| 120 | + .stream |
| 121 | + .iter() |
| 122 | + .copied() |
| 123 | + .filter_map(filter_map_connection_id) |
| 124 | + .collect::<Vec<_>>(), |
| 125 | + positions: message.positions.clone(), |
| 126 | + }, |
| 127 | + )) |
| 128 | + } |
| 129 | + Self::AddChannel(message) => Some(ServerToProxyMessage::AddChannel( |
| 130 | + hyperion_proto::AddChannel { |
| 131 | + channel_id: message.channel_id, |
| 132 | + unsubscribe_packets: message.unsubscribe_packets, |
| 133 | + }, |
| 134 | + )), |
| 135 | + Self::UpdateChannelPositions(message) => { |
| 136 | + Some(ServerToProxyMessage::UpdateChannelPositions( |
| 137 | + hyperion_proto::UpdateChannelPositions { |
| 138 | + updates: message.updates, |
| 139 | + }, |
| 140 | + )) |
| 141 | + } |
| 142 | + Self::RemoveChannel(message) => Some(ServerToProxyMessage::RemoveChannel( |
| 143 | + hyperion_proto::RemoveChannel { |
| 144 | + channel_id: message.channel_id, |
| 145 | + }, |
| 146 | + )), |
| 147 | + Self::SubscribeChannelPackets(message) => { |
| 148 | + Some(ServerToProxyMessage::SubscribeChannelPackets( |
| 149 | + hyperion_proto::SubscribeChannelPackets { |
| 150 | + channel_id: message.channel_id, |
| 151 | + exclude: message |
| 152 | + .exclude |
| 153 | + .and_then(filter_map_connection_id) |
| 154 | + .unwrap_or_default(), |
| 155 | + data: message.data, |
| 156 | + }, |
| 157 | + )) |
| 158 | + } |
| 159 | + Self::BroadcastGlobal(message) => Some(ServerToProxyMessage::BroadcastGlobal( |
| 160 | + hyperion_proto::BroadcastGlobal { |
| 161 | + exclude: message |
| 162 | + .exclude |
| 163 | + .and_then(filter_map_connection_id) |
| 164 | + .unwrap_or_default(), |
| 165 | + data: message.data, |
| 166 | + }, |
| 167 | + )), |
| 168 | + Self::BroadcastLocal(message) => Some(ServerToProxyMessage::BroadcastLocal( |
| 169 | + hyperion_proto::BroadcastLocal { |
| 170 | + center: message.center, |
| 171 | + exclude: message |
| 172 | + .exclude |
| 173 | + .and_then(filter_map_connection_id) |
| 174 | + .unwrap_or_default(), |
| 175 | + data: message.data, |
| 176 | + }, |
| 177 | + )), |
| 178 | + Self::BroadcastChannel(message) => Some(ServerToProxyMessage::BroadcastChannel( |
| 179 | + hyperion_proto::BroadcastChannel { |
| 180 | + channel_id: message.channel_id, |
| 181 | + exclude: message |
| 182 | + .exclude |
| 183 | + .and_then(filter_map_connection_id) |
| 184 | + .unwrap_or_default(), |
| 185 | + data: message.data, |
| 186 | + }, |
| 187 | + )), |
| 188 | + Self::Unicast(message) => { |
| 189 | + Some(ServerToProxyMessage::Unicast(hyperion_proto::Unicast { |
| 190 | + stream: filter_map_connection_id(message.stream)?, |
| 191 | + data: message.data, |
| 192 | + })) |
| 193 | + } |
| 194 | + Self::SetReceiveBroadcasts(message) => Some( |
| 195 | + ServerToProxyMessage::SetReceiveBroadcasts(hyperion_proto::SetReceiveBroadcasts { |
| 196 | + stream: filter_map_connection_id(message.stream)?, |
| 197 | + }), |
| 198 | + ), |
| 199 | + Self::Shutdown(message) => Some(ServerToProxyMessage::SetReceiveBroadcasts( |
| 200 | + hyperion_proto::SetReceiveBroadcasts { |
| 201 | + stream: filter_map_connection_id(message.stream)?, |
| 202 | + }, |
| 203 | + )), |
| 204 | + } |
| 205 | + } |
| 206 | +} |
0 commit comments