diff --git a/proto/service.proto b/proto/service.proto index 2b6f6a9e7..6b69ce7ed 100644 --- a/proto/service.proto +++ b/proto/service.proto @@ -39,7 +39,7 @@ service RealtimeServerManagementService { */ rpc DeleteRoom(DeleteRoomRequest) returns (DeleteRoomResponse); - rpc GetRoomsMembersCount(EmptyRequest) returns(GetRoomsMembersCountResponse); + rpc GetRoomsMembers(EmptyRequest) returns(GetRoomsMembersResponse); } @@ -135,15 +135,29 @@ message GetRoomsResponse { repeated uint64 rooms = 1; } -message GetRoomsMembersCountResponse { - repeated RoomMembersCountResponse rooms = 1; +message GetRoomsMembersResponse { + repeated RoomMembersResponse rooms = 1; } -message RoomMembersCountResponse { +message RoomMembersResponse { uint64 room = 1; - uint32 members = 2; - uint32 connected_members = 3; + repeated Member members = 2; } + +message Member { + uint64 id = 1; + MemberStatus status = 2; +} + +enum MemberStatus { + Created = 0; + CreatedNotConnectedAndDeleted = 1; + Connected = 2; + Attached = 3; + Disconnected = 4; + Detached = 5; +} + enum FieldType { Long = 0; Double = 1; diff --git a/rust/Server/src/server/manager/grpc/from.rs b/rust/Server/src/server/manager/grpc/from.rs index 61fefa389..7357e0f6e 100644 --- a/rust/Server/src/server/manager/grpc/from.rs +++ b/rust/Server/src/server/manager/grpc/from.rs @@ -1,8 +1,9 @@ use crate::server::manager::grpc; use crate::server::manager::grpc::proto; use crate::server::manager::grpc::proto::field_value::Variant; -use crate::server::manager::grpc::proto::{GameObjectConfig, GameObjectTemplate, ItemConfig}; +use crate::server::manager::grpc::proto::{GameObjectConfig, GameObjectTemplate, ItemConfig, Member, MemberStatus}; use crate::server::room::config::{member, object, room}; +use crate::server::room::member::{RoomMember, RoomMemberStatus}; use cheetah_common::room::access::AccessGroups; use cheetah_common::room::buffer::Buffer; use cheetah_common::room::field::FieldId; @@ -73,3 +74,25 @@ impl From for object::GameObjectCreateParams { } } } + +impl From for Member { + fn from(value: RoomMember) -> Self { + Member { + id: value.id, + status: MemberStatus::from(value.status).into(), + } + } +} + +impl From for MemberStatus { + fn from(value: RoomMemberStatus) -> Self { + match value { + RoomMemberStatus::Created => MemberStatus::Created, + RoomMemberStatus::CreatedNotConnectedAndDeleted => MemberStatus::CreatedNotConnectedAndDeleted, + RoomMemberStatus::Connected => MemberStatus::Connected, + RoomMemberStatus::Attached => MemberStatus::Attached, + RoomMemberStatus::Detached => MemberStatus::Detached, + RoomMemberStatus::Disconnected => MemberStatus::Disconnected, + } + } +} diff --git a/rust/Server/src/server/manager/grpc/mod.rs b/rust/Server/src/server/manager/grpc/mod.rs index d4c1ae9ad..e9fcd6103 100644 --- a/rust/Server/src/server/manager/grpc/mod.rs +++ b/rust/Server/src/server/manager/grpc/mod.rs @@ -1,8 +1,19 @@ use crate::server::manager::grpc::proto::realtime_server_management_service_server::RealtimeServerManagementService; -use crate::server::manager::grpc::proto::{ - CreateMemberRequest, CreateMemberResponse, CreateSuperMemberRequest, DeleteMemberRequest, DeleteMemberResponse, DeleteRoomRequest, DeleteRoomResponse, EmptyRequest, GetRoomsMembersCountResponse, - GetRoomsResponse, ProbeRequest, ProbeResponse, RoomIdResponse, RoomMembersCountResponse, RoomTemplate, -}; +use crate::server::manager::grpc::proto::CreateMemberRequest; +use crate::server::manager::grpc::proto::CreateMemberResponse; +use crate::server::manager::grpc::proto::CreateSuperMemberRequest; +use crate::server::manager::grpc::proto::DeleteMemberRequest; +use crate::server::manager::grpc::proto::DeleteMemberResponse; +use crate::server::manager::grpc::proto::DeleteRoomRequest; +use crate::server::manager::grpc::proto::DeleteRoomResponse; +use crate::server::manager::grpc::proto::EmptyRequest; +use crate::server::manager::grpc::proto::GetRoomsMembersResponse; +use crate::server::manager::grpc::proto::GetRoomsResponse; +use crate::server::manager::grpc::proto::ProbeRequest; +use crate::server::manager::grpc::proto::ProbeResponse; +use crate::server::manager::grpc::proto::RoomIdResponse; +use crate::server::manager::grpc::proto::RoomMembersResponse; +use crate::server::manager::grpc::proto::RoomTemplate; use crate::server::manager::{ManagementTaskError, ManagementTaskExecutionError}; use crate::server::room::command::ServerCommandError; use crate::server::room::config::member::MemberCreateParams; @@ -109,19 +120,18 @@ impl RealtimeServerManagementService for RealtimeServerManagementServiceImpl { server.delete_room(room_id).map(|_| Response::new(DeleteRoomResponse {})).map_err(Status::from) } - async fn get_rooms_members_count(&self, _request: Request) -> Result, Status> { + async fn get_rooms_members(&self, _request: Request) -> Result, Status> { self.server_manager .lock() .await .get_rooms_member_count() .map(|rooms| { - Response::new(GetRoomsMembersCountResponse { + Response::new(GetRoomsMembersResponse { rooms: rooms .into_iter() - .map(|r| RoomMembersCountResponse { + .map(|r| RoomMembersResponse { room: r.room_id, - members: r.members as u32, - connected_members: r.connected_members as u32, + members: r.members.into_iter().map(From::from).collect(), }) .collect(), }) @@ -150,7 +160,7 @@ impl From for Status { #[cfg(test)] mod test { use crate::server::manager::grpc::proto::realtime_server_management_service_server::RealtimeServerManagementService; - use crate::server::manager::grpc::proto::{DeleteMemberRequest, DeleteRoomRequest, EmptyRequest, RoomMembersCountResponse}; + use crate::server::manager::grpc::proto::{DeleteMemberRequest, DeleteRoomRequest, EmptyRequest, Member, MemberStatus, RoomMembersResponse}; use crate::server::manager::grpc::{RealtimeServerManagementServiceImpl, SUPER_MEMBER_KEY_ENV}; use crate::server::manager::ServerManager; use crate::server::room::config::member::MemberCreateParams; @@ -181,7 +191,7 @@ mod test { let service = RealtimeServerManagementServiceImpl::new(Arc::clone(&server_manager)); let room_1 = server_manager.lock().await.create_room(Default::default()).unwrap(); let room_2 = server_manager.lock().await.create_room(Default::default()).unwrap(); - server_manager + let member_id = server_manager .lock() .await .create_member( @@ -195,17 +205,18 @@ mod test { ) .unwrap(); - let rooms_response = service.get_rooms_members_count(Request::new(EmptyRequest::default())).await.unwrap(); + let rooms_response = service.get_rooms_members(Request::new(EmptyRequest::default())).await.unwrap(); let rooms = rooms_response.get_ref(); - assert!(rooms.rooms.contains(&RoomMembersCountResponse { + assert!(rooms.rooms.contains(&RoomMembersResponse { room: room_1, - members: 1, - connected_members: 0 + members: vec![Member { + id: member_id, + status: MemberStatus::Created.into() + }], })); - assert!(rooms.rooms.contains(&RoomMembersCountResponse { + assert!(rooms.rooms.contains(&RoomMembersResponse { room: room_2, - members: 0, - connected_members: 0 + members: Default::default(), })); assert_eq!(rooms.rooms.len(), 2); } diff --git a/rust/Server/src/server/manager/mod.rs b/rust/Server/src/server/manager/mod.rs index 4c4a66164..7fde486f9 100644 --- a/rust/Server/src/server/manager/mod.rs +++ b/rust/Server/src/server/manager/mod.rs @@ -1,21 +1,25 @@ -pub mod grpc; -use crate::server::room::command::ServerCommandError; -use crate::server::room::config::member::MemberCreateParams; -use crate::server::room::config::room::RoomCreateParams; -use crate::server::room::Room; -use crate::server::room_registry::RoomNotFoundError; -use crate::server::Server; -use cheetah_game_realtime_protocol::coniguration::ProtocolConfiguration; -use cheetah_game_realtime_protocol::others::member_id::MemberAndRoomId; -use cheetah_game_realtime_protocol::{RoomId, RoomMemberId}; use std::net::UdpSocket; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::{RecvTimeoutError, SendError, Sender}; use std::sync::Arc; use std::thread; use std::time::Duration; + +use cheetah_game_realtime_protocol::coniguration::ProtocolConfiguration; +use cheetah_game_realtime_protocol::others::member_id::MemberAndRoomId; +use cheetah_game_realtime_protocol::{RoomId, RoomMemberId}; use thiserror::Error; +use crate::server::room::command::ServerCommandError; +use crate::server::room::config::member::MemberCreateParams; +use crate::server::room::config::room::RoomCreateParams; +use crate::server::room::member::RoomMember; +use crate::server::room::Room; +use crate::server::room_registry::RoomNotFoundError; +use crate::server::Server; + +pub mod grpc; + /// /// Управление сервером /// - запуск сервера в отдельном потоке @@ -34,7 +38,7 @@ pub enum ManagementTask { Dump(RoomId), GetRooms, GetCreatedRoomsCount, - GetRoomsMemberCount, + GetRoomsMembers, DeleteRoom(RoomId), } @@ -45,16 +49,15 @@ pub enum ManagementTaskResult { DeleteMember, Dump(Option), GetRooms(Vec), - GetRoomsMemberCount(Vec), + GetRoomsMemberCount(Vec), GetCreatedRoomsCount(usize), DeleteRoom, } #[derive(Debug)] -pub struct RoomMembersCount { +pub struct RoomMembers { pub room_id: RoomId, - pub members: usize, - pub connected_members: usize, + pub members: Vec, } #[derive(Error, Debug)] @@ -139,8 +142,8 @@ impl ServerManager { })? } - pub(crate) fn get_rooms_member_count(&self) -> Result, ManagementTaskError> { - self.execute_task(ManagementTask::GetRoomsMemberCount).map(|res| { + pub(crate) fn get_rooms_member_count(&self) -> Result, ManagementTaskError> { + self.execute_task(ManagementTask::GetRoomsMembers).map(|res| { if let ManagementTaskResult::GetRoomsMemberCount(rooms) = res { Ok(rooms) } else { @@ -210,12 +213,15 @@ impl ServerManager { #[cfg(test)] mod test { + use std::time::Duration; + + use cheetah_game_realtime_protocol::coniguration::ProtocolConfiguration; + + use cheetah_common::network::bind_to_free_socket; + use crate::server::manager::ServerManager; use crate::server::room::config::member::MemberCreateParams; use crate::server::room::config::room::RoomCreateParams; - use cheetah_common::network::bind_to_free_socket; - use cheetah_game_realtime_protocol::coniguration::ProtocolConfiguration; - use std::time::Duration; #[test] fn should_get_rooms() { diff --git a/rust/Server/src/server/mod.rs b/rust/Server/src/server/mod.rs index 7824d63c9..f9adad698 100644 --- a/rust/Server/src/server/mod.rs +++ b/rust/Server/src/server/mod.rs @@ -1,4 +1,4 @@ -use crate::server::manager::{ManagementTask, ManagementTaskChannel, ManagementTaskExecutionError, ManagementTaskResult, RoomMembersCount}; +use crate::server::manager::{ManagementTask, ManagementTaskChannel, ManagementTaskExecutionError, ManagementTaskResult, RoomMembers}; use crate::server::measurer::Measurer; use crate::server::network::Network; use crate::server::room::command::ServerCommandError; @@ -91,13 +91,12 @@ impl Server { ManagementTask::DeleteMember(id) => self.delete_member(id).map(|_| ManagementTaskResult::DeleteMember)?, ManagementTask::Dump(room_id) => ManagementTaskResult::Dump(self.rooms.get(&room_id).cloned()), ManagementTask::GetRooms => ManagementTaskResult::GetRooms(self.rooms.rooms().map(|r| r.0).copied().collect()), - ManagementTask::GetRoomsMemberCount => ManagementTaskResult::GetRoomsMemberCount( + ManagementTask::GetRoomsMembers => ManagementTaskResult::GetRoomsMemberCount( self.rooms .rooms() - .map(|(room_id, room)| RoomMembersCount { + .map(|(room_id, room)| RoomMembers { room_id: *room_id, - members: room.members.len(), - connected_members: room.members.iter().filter(|p| p.1.connected).count(), + members: room.members.iter().map(|i| i.1.clone()).collect(), }) .collect(), ), diff --git a/rust/Server/src/server/network.rs b/rust/Server/src/server/network.rs index 162eb7b5e..1acc9a562 100644 --- a/rust/Server/src/server/network.rs +++ b/rust/Server/src/server/network.rs @@ -244,7 +244,7 @@ mod tests { use crate::server::network::Network; use crate::server::room::config::member::MemberCreateParams; - use crate::server::room::member::RoomMember; + use crate::server::room::member::{RoomMember, RoomMemberStatus}; use crate::server::room_registry::Rooms; use cheetah_common::network::bind_to_free_socket; use cheetah_game_realtime_protocol::codec::cipher::Cipher; @@ -296,8 +296,7 @@ mod tests { let member_template = MemberCreateParams::new_member(Default::default(), Default::default()); let member = RoomMember { id: 100, - connected: false, - attached: false, + status: RoomMemberStatus::Created, template: member_template.clone(), out_commands: Default::default(), }; diff --git a/rust/Server/src/server/room/command/create.rs b/rust/Server/src/server/room/command/create.rs index b0caacc8e..6100576fd 100644 --- a/rust/Server/src/server/room/command/create.rs +++ b/rust/Server/src/server/room/command/create.rs @@ -54,7 +54,7 @@ mod tests { #[test] fn should_create() { let (mut room, member_id) = setup(AccessGroups(0b11)); - room.mark_as_connected_in_test(member_id).unwrap(); + room.mark_as_attached_in_test(member_id).unwrap(); let object_id = GameObjectId::new(1, GameObjectOwner::Member(member_id)); let command = CreateGameObject { diff --git a/rust/Server/src/server/room/command/created.rs b/rust/Server/src/server/room/command/created.rs index e610138e3..1854fb6d6 100644 --- a/rust/Server/src/server/room/command/created.rs +++ b/rust/Server/src/server/room/command/created.rs @@ -70,8 +70,8 @@ mod tests { #[test] pub(crate) fn should_send_commands() { let (mut room, object_id, member1, member2) = setup_two_players(); - room.mark_as_connected_in_test(member1).unwrap(); - room.mark_as_connected_in_test(member2).unwrap(); + room.mark_as_attached_in_test(member1).unwrap(); + room.mark_as_attached_in_test(member2).unwrap(); let command = C2SCreatedGameObject::new(object_id, false, None); created_object(&command, &mut room, member1).unwrap(); diff --git a/rust/Server/src/server/room/command/delete.rs b/rust/Server/src/server/room/command/delete.rs index 630089516..54542abb1 100644 --- a/rust/Server/src/server/room/command/delete.rs +++ b/rust/Server/src/server/room/command/delete.rs @@ -40,8 +40,8 @@ mod tests { let mut room = Room::new(0, template); let member_a_id = room.register_member(MemberCreateParams::stub(access_groups)); let member_b_id = room.register_member(MemberCreateParams::stub(access_groups)); - room.mark_as_connected_in_test(member_a_id).unwrap(); - room.mark_as_connected_in_test(member_b_id).unwrap(); + room.mark_as_attached_in_test(member_a_id).unwrap(); + room.mark_as_attached_in_test(member_b_id).unwrap(); let object_id = room.test_create_object_with_created_state(GameObjectOwner::Member(member_a_id), access_groups, Default::default()).id; room.test_out_commands.clear(); diff --git a/rust/Server/src/server/room/command/event.rs b/rust/Server/src/server/room/command/event.rs index 84ec023b0..1e8fdd958 100644 --- a/rust/Server/src/server/room/command/event.rs +++ b/rust/Server/src/server/room/command/event.rs @@ -64,9 +64,9 @@ mod tests { let member2 = room.register_member(MemberCreateParams::stub(access_groups)); let member3 = room.register_member(MemberCreateParams::stub(access_groups)); - room.mark_as_connected_in_test(member1).unwrap(); - room.mark_as_connected_in_test(member2).unwrap(); - room.mark_as_connected_in_test(member3).unwrap(); + room.mark_as_attached_in_test(member1).unwrap(); + room.mark_as_attached_in_test(member2).unwrap(); + room.mark_as_attached_in_test(member3).unwrap(); let object = room.test_create_object_with_not_created_state(GameObjectOwner::Member(member1), access_groups, Default::default()); object.created = true; diff --git a/rust/Server/src/server/room/command/room.rs b/rust/Server/src/server/room/command/room.rs index 7add33769..b478eb984 100644 --- a/rust/Server/src/server/room/command/room.rs +++ b/rust/Server/src/server/room/command/room.rs @@ -1,4 +1,5 @@ use crate::server::room::command::ServerCommandError; +use crate::server::room::member::RoomMemberStatus; use crate::server::room::object::S2CCommandsCollector; use crate::server::room::Room; use cheetah_common::room::object::GameObjectTemplateId; @@ -6,7 +7,7 @@ use cheetah_game_realtime_protocol::RoomMemberId; pub fn attach_to_room(room: &mut Room, member_id: RoomMemberId) -> Result<(), ServerCommandError> { let member = room.get_member_mut(&member_id)?; - member.attached = true; + member.status = RoomMemberStatus::Attached; let access_group = member.template.groups; let mut command_collector = Vec::<(GameObjectTemplateId, S2CCommandsCollector)>::new(); room.objects @@ -27,7 +28,7 @@ pub fn attach_to_room(room: &mut Room, member_id: RoomMemberId) -> Result<(), Se pub fn detach_from_room(room: &mut Room, member_id: RoomMemberId) -> Result<(), ServerCommandError> { let member = room.get_member_mut(&member_id)?; - member.attached = false; + member.status = RoomMemberStatus::Detached; Ok(()) } @@ -50,8 +51,8 @@ mod tests { let groups_b = AccessGroups(0b10); let member_b = room.register_member(MemberCreateParams::stub(groups_b)); - room.mark_as_connected_in_test(member_a).unwrap(); - room.mark_as_connected_in_test(member_b).unwrap(); + room.mark_as_attached_in_test(member_a).unwrap(); + room.mark_as_attached_in_test(member_b).unwrap(); let object_a_1 = room.test_create_object_with_not_created_state(GameObjectOwner::Member(member_b), groups_a, Default::default()); object_a_1.created = true; @@ -78,7 +79,7 @@ mod tests { let mut room = Room::new(0, template); let groups = AccessGroups(0b100); let member = room.register_member(MemberCreateParams::stub(groups)); - room.mark_as_connected_in_test(member).unwrap(); + room.mark_as_attached_in_test(member).unwrap(); let object = room.test_create_object_with_not_created_state(GameObjectOwner::Member(member), groups, Default::default()); object.created = true; diff --git a/rust/Server/src/server/room/member.rs b/rust/Server/src/server/room/member.rs index 0b2c9a2e8..d4079fdaa 100644 --- a/rust/Server/src/server/room/member.rs +++ b/rust/Server/src/server/room/member.rs @@ -6,8 +6,23 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct RoomMember { pub id: RoomMemberId, - pub connected: bool, - pub attached: bool, + pub status: RoomMemberStatus, pub template: MemberCreateParams, pub out_commands: Vec, } + +#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)] +pub enum RoomMemberStatus { + Created, + CreatedNotConnectedAndDeleted, + Connected, + Attached, + Detached, + Disconnected, +} + +impl RoomMemberStatus { + pub fn is_online(&self) -> bool { + return *self == Self::Connected || *self == Self::Attached || *self == Self::Detached; + } +} diff --git a/rust/Server/src/server/room/mod.rs b/rust/Server/src/server/room/mod.rs index 21fc14dce..b16746f4e 100644 --- a/rust/Server/src/server/room/mod.rs +++ b/rust/Server/src/server/room/mod.rs @@ -12,6 +12,7 @@ use serde::{Deserialize, Serialize}; use crate::server::room::command::{execute, ServerCommandError}; use crate::server::room::config::member::MemberCreateParams; use crate::server::room::config::object::GameObjectConfig; +use crate::server::room::member::RoomMemberStatus; use crate::server::room::object::{GameObject, S2CCommandsCollector}; use cheetah_common::commands::guarantees::{ChannelGroup, ReliabilityGuarantees}; use cheetah_common::commands::s2c::S2CCommand; @@ -115,7 +116,7 @@ impl Room { /// pub fn execute_commands(&mut self, member_id: RoomMemberId, commands: &[CommandWithReliabilityGuarantees]) { if let Some(member) = self.members.get(&member_id) { - if !member.connected { + if !member.status.is_online() { if let Err(e) = self.connect_member(member_id) { e.log_error(self.id, member_id); return; @@ -163,7 +164,7 @@ impl Room { } let member = self.members.get_mut(&member_id).ok_or(ServerCommandError::MemberNotFound(member_id))?; - member.connected = true; + member.status = RoomMemberStatus::Connected; Ok(()) } @@ -172,8 +173,7 @@ impl Room { let member_id = self.member_id_generator; let member = RoomMember { id: member_id, - connected: false, - attached: false, + status: RoomMemberStatus::Created, template, out_commands: Default::default(), }; @@ -283,6 +283,7 @@ mod tests { use crate::server::room::config::member::MemberCreateParams; use crate::server::room::config::object::GameObjectCreateParams; use crate::server::room::config::room::RoomCreateParams; + use crate::server::room::member::RoomMemberStatus; use crate::server::room::object::GameObject; use crate::server::room::Room; use cheetah_common::commands::c2s::C2SCommand; @@ -320,10 +321,9 @@ mod tests { self.get_object_mut(id).unwrap() } - pub fn mark_as_connected_in_test(&mut self, member_id: RoomMemberId) -> Result<(), ServerCommandError> { + pub fn mark_as_attached_in_test(&mut self, member_id: RoomMemberId) -> Result<(), ServerCommandError> { let member = self.get_member_mut(&member_id)?; - member.connected = true; - member.attached = true; + member.status = RoomMemberStatus::Attached; Ok(()) } @@ -490,7 +490,7 @@ mod tests { let mut room = Room::default(); let member_template = MemberCreateParams::stub(AccessGroups(8)); let member_id = room.register_member(member_template); - room.mark_as_connected_in_test(member_id).unwrap(); + room.mark_as_attached_in_test(member_id).unwrap(); let member = room.get_member_mut(&member_id).unwrap(); member.out_commands.push(CommandWithChannelType { channel_type: ReliabilityGuarantees::ReliableUnordered, @@ -523,10 +523,10 @@ mod tests { let access_groups = AccessGroups(10); let mut room = Room::new(0, template); let member_1 = room.register_member(MemberCreateParams::stub(access_groups)); - room.mark_as_connected_in_test(member_1).unwrap(); + room.mark_as_attached_in_test(member_1).unwrap(); let member_2 = room.register_member(MemberCreateParams::stub(access_groups)); - room.mark_as_connected_in_test(member_2).unwrap(); + room.mark_as_attached_in_test(member_2).unwrap(); room.connect_member(member_2).unwrap(); assert_eq!(S2CCommand::MemberConnected(MemberConnected { member_id: member_2 }), room.get_member_out_commands_for_test(member_1)[0]); @@ -538,11 +538,11 @@ mod tests { let access_groups = AccessGroups(10); let mut room = Room::new(0, template); let member_1 = room.register_member(MemberCreateParams::stub(access_groups)); - room.mark_as_connected_in_test(member_1).unwrap(); room.connect_member(member_1).unwrap(); + room.mark_as_attached_in_test(member_1).unwrap(); let member_2 = room.register_member(MemberCreateParams::stub(access_groups)); - room.mark_as_connected_in_test(member_2).unwrap(); + room.mark_as_attached_in_test(member_2).unwrap(); room.connect_member(member_2).unwrap(); room.disconnect_member(member_2).unwrap(); diff --git a/rust/Server/src/server/room/sender.rs b/rust/Server/src/server/room/sender.rs index 72b8cdb7c..9e063bf1b 100644 --- a/rust/Server/src/server/room/sender.rs +++ b/rust/Server/src/server/room/sender.rs @@ -6,7 +6,7 @@ use cheetah_common::commands::{BothDirectionCommand, CommandWithChannelType}; use cheetah_common::room::access::AccessGroups; use crate::server::room::command::ServerCommandError; -use crate::server::room::member::RoomMember; +use crate::server::room::member::{RoomMember, RoomMemberStatus}; use crate::server::room::Room; /// @@ -26,8 +26,7 @@ impl Room { let members_for_send = self .members .values_mut() - .filter(|member| member.attached) - .filter(|member| member.connected) + .filter(|member| member.status == RoomMemberStatus::Attached) .filter(|member| member.template.groups.contains_any(&access_groups)) .filter(|member| filter(member)); @@ -47,7 +46,7 @@ impl Room { let channel = self.current_channel.unwrap_or(ReliabilityGuarantees::ReliableSequence(ChannelGroup(0))); let member = self.get_member_mut(member_id)?; - if member.attached && member.connected { + if member.status == RoomMemberStatus::Attached { for command in commands { let member = self.get_member_mut(member_id)?; member.out_commands.push(CommandWithChannelType { @@ -86,7 +85,7 @@ mod tests { object.access_groups = access_groups; object.created = true; let object_id = object.id; - room.mark_as_connected_in_test(member_id).unwrap(); + room.mark_as_attached_in_test(member_id).unwrap(); room.send_command_from_action(object_id, member_id, None, |_| { Ok(Some(S2CCommand::SetLong(LongField { @@ -129,7 +128,7 @@ mod tests { let _member_source_id = room.register_member(MemberCreateParams::stub(groups)); let member_target_id = room.register_member(MemberCreateParams::stub(groups)); - room.mark_as_connected_in_test(member_target_id).unwrap(); + room.mark_as_attached_in_test(member_target_id).unwrap(); let object = room.test_create_object_with_not_created_state(GameObjectOwner::Member(member_target_id), groups, Default::default()); object.created = true; object.template_id = object_template; @@ -158,8 +157,8 @@ mod tests { let member_2 = room.register_member(MemberCreateParams::stub(access_groups)); let object = room.test_create_object_with_not_created_state(GameObjectOwner::Member(member_1), access_groups, Default::default()); let object_id = object.id; - room.mark_as_connected_in_test(member_1).unwrap(); - room.mark_as_connected_in_test(member_2).unwrap(); + room.mark_as_attached_in_test(member_1).unwrap(); + room.mark_as_attached_in_test(member_2).unwrap(); room.send_command_from_action(object_id, member_1, None, |_| Ok(Some(S2CCommand::SetLong(LongField { object_id, field_id: 100, value: 200 })))) .unwrap(); diff --git a/rust/tmp/trace.rs b/rust/tmp/trace.rs deleted file mode 100644 index fb3c087ee..000000000 --- a/rust/tmp/trace.rs +++ /dev/null @@ -1,22 +0,0 @@ -use std::fmt::Debug; - -use tracing::error; - -pub trait Trace { - fn trace_err(self, details: impl Debug) -> Result; -} - -impl Trace for Result { - fn trace_err(self, details: impl Debug) -> Result { - match self { - Ok(v) => Ok(v), - Err(e) => Err(err(details, e)), - } - } -} - -pub fn err(details: impl Debug, object: impl Debug) -> String { - let msg = format!("{details:?} {object:?}"); - error!("{}", msg); - msg -} diff --git a/rust/tmp/trace_collector.rs b/rust/tmp/trace_collector.rs deleted file mode 100644 index a35fb3166..000000000 --- a/rust/tmp/trace_collector.rs +++ /dev/null @@ -1,152 +0,0 @@ -use std::collections::VecDeque; -use std::sync::Mutex; - -use lazy_static::lazy_static; -use tracing::Event; -use tracing_core::field::Visit; -use tracing_core::{Field, LevelFilter}; -use tracing_log::{log, LogTracer}; -use tracing_subscriber::layer::{Context, SubscriberExt}; -use tracing_subscriber::{Layer, Registry}; - -lazy_static! { - pub static ref TRACER_COLLECTOR: Mutex = Mutex::new(TracerCollector::setup()); -} - -/// -/// Сохранение трейсов для передачи в Unity/Unreal/etc -/// -#[derive(Debug)] -pub struct TracerCollector { - pub level: tracing_core::Level, - pub items: VecDeque, -} - -#[derive(Debug, Clone)] -pub struct Trace { - pub level: tracing_core::Level, - pub message: String, -} - -impl TracerCollector { - const MAX_SIZE: usize = 100; - fn setup() -> Self { - LogTracer::builder().with_max_level(log::LevelFilter::Info).init().unwrap(); - let subscriber = Registry::default().with(TracerCollectorLayer); - tracing::subscriber::set_global_default(subscriber).expect("Setting default subscriber failed"); - Self { - level: tracing_core::Level::INFO, - items: Default::default(), - } - } - - pub fn set_log_level(&mut self, log_level: tracing_core::Level) { - self.level = log_level; - } - - fn on_event(&mut self, event: &Event<'_>) { - let filter = LevelFilter::from_level(self.level); - if filter >= *event.metadata().level() { - let mut visitor = ValueVisitor::new("message"); - event.record(&mut visitor); - let message = visitor.result.unwrap_or_default(); - let level = *event.metadata().level(); - let message = format!("{} in {}:{}", message, event.metadata().file().unwrap_or(""), event.metadata().line().unwrap_or(0)); - self.items.push_back(Trace { level, message }); - if self.items.len() > TracerCollector::MAX_SIZE { - self.items.pop_front(); - } - } - } -} - -struct TracerCollectorLayer; - -impl Layer for TracerCollectorLayer { - fn on_event(&self, event: &Event<'_>, _context: Context<'_, S>) { - let collector = &mut TRACER_COLLECTOR.lock().unwrap(); - collector.on_event(event); - } -} - -#[derive(Default)] -pub struct ValueVisitor { - name: String, - result: Option, -} - -impl ValueVisitor { - pub fn new>(name: S) -> Self { - Self { - name: name.as_ref().to_owned(), - result: None, - } - } -} - -impl Visit for ValueVisitor { - fn record_debug(&mut self, field: &Field, value: &dyn std::fmt::Debug) { - if field.name() == self.name { - self.result = Some(format!("{value:?}")); - } - } -} - -#[cfg(test)] -mod tests { - use std::path::PathBuf; - use std::sync::{LockResult, Mutex, MutexGuard}; - - use lazy_static::lazy_static; - - use crate::trace_collector::TRACER_COLLECTOR; - - lazy_static! { - pub static ref LOCK: Mutex<()> = Mutex::new(()); - } - - #[test] - fn should_collect_trace() { - let _lock = setup(tracing_core::Level::ERROR); - tracing::error!("some error"); - - let mut path = PathBuf::new(); - for v in ["Protocol", "src", "trace_collector.rs"] { - path.push(v); - } - let view_path = path.display(); - let error = format!("some error in {view_path}"); - - assert!(contains(&error)); - } - - #[test] - fn should_not_collect_trace_if_wrong_level() { - let _lock = setup(tracing_core::Level::ERROR); - let msg = "should_not_collect_trace_if_wrong_level"; - tracing::info!("{}", msg); - assert!(!contains(msg)); - } - - #[test] - fn should_set_level() { - let _lock = setup(tracing_core::Level::INFO); - let msg = "should_set_level"; - tracing::info!("{}", msg); - assert!(contains(msg)); - } - - fn setup(log_level: tracing_core::Level) -> LockResult> { - let lock = LOCK.lock(); - { - let collector = &mut TRACER_COLLECTOR.lock().unwrap(); - collector.set_log_level(log_level); - } - lock - } - - fn contains(item: &str) -> bool { - let items = &TRACER_COLLECTOR.lock().unwrap().items; - items.iter().any(|t| t.message.contains(item)) - } -}