Skip to content

Commit

Permalink
feat: Remove [] from Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
avkviring committed Apr 25, 2024
1 parent 84fac9d commit 3323fca
Show file tree
Hide file tree
Showing 27 changed files with 212 additions and 129 deletions.
12 changes: 12 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rust/Client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ tracing.workspace = true
tracing-core.workspace = true
tracing-log.workspace = true
tracing-subscriber.workspace = true
serde = { version = "1.0.188", features = ["derive"] }
serde_arrays = "0.1.0"

[dev-dependencies]
cheetah-server = { path = "../Server" }
Expand Down
6 changes: 3 additions & 3 deletions rust/Client/src/clients/application_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ impl ApplicationThreadClient {
}
S2CCommand::SetStructure(command) => {
command_ffi.command_type = CommandTypeId::SetStructure;
command_ffi.command.buffer_field = *command;
command_ffi.command.buffer_field = command.into();
}

S2CCommand::Event(command) => {
command_ffi.command_type = CommandTypeId::SendEvent;
command_ffi.command.buffer_field = *command;
command_ffi.command.buffer_field = command.into();
}
S2CCommand::Delete(command) => {
command_ffi.command_type = CommandTypeId::DeleteObject;
Expand All @@ -145,7 +145,7 @@ impl ApplicationThreadClient {
}
S2CCommand::AddItem(command) => {
command_ffi.command_type = CommandTypeId::AddItem;
command_ffi.command.buffer_field = *command;
command_ffi.command.buffer_field = command.into();
}
}
*count += 1;
Expand Down
6 changes: 3 additions & 3 deletions rust/Client/src/ffi/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::sync::atomic::Ordering;
use std::time::Duration;

use crate::clients::registry::ClientId;
use crate::ffi::command::S2CCommandFFI;
use crate::ffi::command::{BufferFFI, S2CCommandFFI};
use crate::ffi::{execute, execute_with_client, ClientError, LAST_ERROR};
use cheetah_common::network::ConnectionStatus;
use cheetah_common::room::buffer::Buffer;
Expand Down Expand Up @@ -124,7 +124,7 @@ pub extern "C" fn get_statistics(client_id: ClientId, statistics: &mut Statistic

#[no_mangle]
#[allow(clippy::cast_possible_truncation)]
pub extern "C" fn get_last_error_msg(buffer: &mut Buffer) {
pub extern "C" fn get_last_error_msg(buffer: &mut BufferFFI) {
let msg = LAST_ERROR.lock().unwrap();
let msg = msg.as_bytes();
let length = msg.len();
Expand All @@ -151,7 +151,7 @@ pub unsafe extern "C" fn create_client(
addr: *const c_char,
member_id: RoomMemberId,
room_id: RoomId,
private_key_buffer: &Buffer,
private_key_buffer: &BufferFFI,
disconnect_time_in_sec: u64,
out_client_id: &mut u16,
) -> u8 {
Expand Down
11 changes: 5 additions & 6 deletions rust/Client/src/ffi/command/event.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
use crate::clients::registry::ClientId;
use crate::ffi::command::send_command;
use crate::ffi::command::{send_command, BufferFFI};
use cheetah_common::commands::c2s::C2SCommand;
use cheetah_common::commands::types::event::TargetEvent;
use cheetah_common::commands::types::structure::BinaryField;
use cheetah_common::room::buffer::Buffer;
use cheetah_common::room::field::FieldId;
use cheetah_common::room::object::GameObjectId;
use cheetah_game_realtime_protocol::RoomMemberId;

#[no_mangle]
pub extern "C" fn send_event(client_id: ClientId, object_id: &GameObjectId, field_id: FieldId, event: &Buffer) -> u8 {
pub extern "C" fn send_event(client_id: ClientId, object_id: &GameObjectId, field_id: FieldId, event: &BufferFFI) -> u8 {
send_command(
client_id,
C2SCommand::Event(
BinaryField {
object_id: *object_id,
field_id,
value: *event,
value: event.into(),
}
.into(),
),
)
}

#[no_mangle]
pub extern "C" fn send_target_event(client_id: ClientId, target_member_id: RoomMemberId, object_id: &GameObjectId, field_id: FieldId, event: &Buffer) -> u8 {
pub extern "C" fn send_target_event(client_id: ClientId, target_member_id: RoomMemberId, object_id: &GameObjectId, field_id: FieldId, event: &BufferFFI) -> u8 {
let event_command = BinaryField {
object_id: *object_id,
field_id,
value: *event,
value: event.into(),
};
send_command(
client_id,
Expand Down
7 changes: 3 additions & 4 deletions rust/Client/src/ffi/command/items.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
use cheetah_common::commands::c2s::C2SCommand;
use cheetah_common::commands::types::structure::BinaryField;
use cheetah_common::room::buffer::Buffer;
use cheetah_common::room::field::FieldId;
use cheetah_common::room::object::GameObjectId;

use crate::clients::registry::ClientId;
use crate::ffi::command::send_command;
use crate::ffi::command::{send_command, BufferFFI};

#[no_mangle]
pub extern "C" fn add_item(client_id: ClientId, object_id: &GameObjectId, field_id: FieldId, structure: &Buffer) -> u8 {
pub extern "C" fn add_item(client_id: ClientId, object_id: &GameObjectId, field_id: FieldId, structure: &BufferFFI) -> u8 {
send_command(
client_id,
C2SCommand::AddItem(
BinaryField {
object_id: *object_id,
field_id,
value: *structure,
value: structure.into(),
}
.into(),
),
Expand Down
107 changes: 105 additions & 2 deletions rust/Client/src/ffi/command/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::fmt::{Debug, Formatter};

use serde::{Deserialize, Serialize};

use cheetah_common::commands::c2s::C2SCommand;
use cheetah_common::commands::types::create::{CreateGameObject, GameObjectCreated};
use cheetah_common::commands::types::field::DeleteField;
Expand All @@ -8,6 +10,8 @@ use cheetah_common::commands::types::long::LongField;
use cheetah_common::commands::types::member::{MemberConnected, MemberDisconnected};
use cheetah_common::commands::types::structure::BinaryField;
use cheetah_common::commands::CommandTypeId;
use cheetah_common::room::buffer::{Buffer, MAX_BUFFER_SIZE};
use cheetah_common::room::field::FieldId;
use cheetah_common::room::object::GameObjectId;

use crate::clients::registry::ClientId;
Expand All @@ -27,7 +31,7 @@ fn send_command(client_id: ClientId, command: C2SCommand) -> u8 {
}

#[repr(C)]
#[derive(Copy, Clone)]
#[derive(Clone)]
pub struct S2CCommandFFI {
pub command_type: CommandTypeId,
pub command: S2CommandUnionFFI,
Expand Down Expand Up @@ -69,9 +73,108 @@ pub union S2CommandUnionFFI {
pub created: GameObjectCreated,
pub set_long: LongField,
pub set_double: DoubleField,
pub buffer_field: BinaryField,
pub buffer_field: BinaryFieldFFI,
pub game_object_id: GameObjectId,
pub delete_field: DeleteField,
pub member_connect: MemberConnected,
pub member_disconnect: MemberDisconnected,
}

#[repr(C)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct BinaryFieldFFI {
pub object_id: GameObjectId,
pub field_id: FieldId,
pub value: BufferFFI,
}

#[repr(C)]
#[derive(Copy, Clone, PartialEq, Hash, Eq, Serialize, Deserialize, Debug)]
pub struct BufferFFI {
pub len: u16,
// используется в C#
pub pos: u16,
#[serde(with = "serde_arrays")]
pub buffer: [u8; MAX_BUFFER_SIZE],
}

impl Default for BufferFFI {
fn default() -> Self {
Self {
len: 0,
pos: 0,
buffer: [0; MAX_BUFFER_SIZE],
}
}
}

impl From<BinaryFieldFFI> for BinaryField {
fn from(value: BinaryFieldFFI) -> Self {
Self {
object_id: value.object_id,
field_id: value.field_id,
value: value.value.into(),
}
}
}

impl From<&BinaryFieldFFI> for BinaryField {
fn from(value: &BinaryFieldFFI) -> Self {
Self {
object_id: value.object_id,
field_id: value.field_id,
value: value.value.into(),
}
}
}

impl From<BinaryField> for BinaryFieldFFI {
fn from(value: BinaryField) -> Self {
Self {
object_id: value.object_id,
field_id: value.field_id,
value: value.value.into(),
}
}
}

impl From<BufferFFI> for Buffer {
fn from(value: BufferFFI) -> Self {
Self {
buffer: value.buffer[0..value.len as usize].to_vec(),
}
}
}

impl From<&BufferFFI> for Buffer {
fn from(value: &BufferFFI) -> Self {
Self {
buffer: value.buffer[0..value.len as usize].to_vec(),
}
}
}

impl From<Buffer> for BufferFFI {
fn from(value: Buffer) -> Self {
let mut result = Self {
len: value.buffer.len() as u16,
pos: 0,
buffer: [0; MAX_BUFFER_SIZE],
};
let source = &value.buffer.as_slice()[0..value.buffer.len()];
result.buffer[0..value.buffer.len()].copy_from_slice(source);
result
}
}

impl From<&[u8]> for BufferFFI {
fn from(value: &[u8]) -> Self {
let mut result = Self {
len: value.len() as u16,
pos: 0,
buffer: [0; MAX_BUFFER_SIZE],
};
result.buffer[0..value.len()].copy_from_slice(value);
result
}
}
10 changes: 6 additions & 4 deletions rust/Client/src/ffi/command/object.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use cheetah_common::commands::c2s::C2SCommand;
use cheetah_common::commands::types::create::C2SCreatedGameObject;
use cheetah_common::room::buffer::Buffer;
use cheetah_common::room::object::GameObjectId;

use crate::clients::registry::ClientId;
use crate::ffi::command::send_command;
use crate::ffi::command::{send_command, BufferFFI};
pub use crate::ffi::execute_with_client;

#[no_mangle]
Expand All @@ -17,9 +16,12 @@ pub extern "C" fn create_object(client_id: ClientId, template: u16, access_group
}

#[no_mangle]
pub extern "C" fn created_object(client_id: ClientId, object_id: &GameObjectId, room_owner: bool, singleton_key: &Buffer) -> u8 {
pub extern "C" fn created_object(client_id: ClientId, object_id: &GameObjectId, room_owner: bool, singleton_key: &BufferFFI) -> u8 {
let singleton_key = (singleton_key.len > 0).then(|| *singleton_key);
send_command(client_id, C2SCommand::CreatedGameObject(C2SCreatedGameObject::new(*object_id, room_owner, singleton_key).into()))
send_command(
client_id,
C2SCommand::CreatedGameObject(C2SCreatedGameObject::new(*object_id, room_owner, singleton_key.map(From::from)).into()),
)
}

#[no_mangle]
Expand Down
7 changes: 3 additions & 4 deletions rust/Client/src/ffi/command/structure.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
use cheetah_common::commands::c2s::C2SCommand;
use cheetah_common::commands::types::structure::BinaryField;
use cheetah_common::room::buffer::Buffer;
use cheetah_common::room::field::FieldId;
use cheetah_common::room::object::GameObjectId;

use crate::clients::registry::ClientId;
use crate::ffi::command::send_command;
use crate::ffi::command::{send_command, BufferFFI};

#[no_mangle]
pub extern "C" fn set_structure(client_id: ClientId, object_id: &GameObjectId, field_id: FieldId, structure: &Buffer) -> u8 {
pub extern "C" fn set_structure(client_id: ClientId, object_id: &GameObjectId, field_id: FieldId, structure: &BufferFFI) -> u8 {
send_command(
client_id,
C2SCommand::SetStructure(
BinaryField {
object_id: *object_id,
field_id,
value: *structure,
value: structure.into(),
}
.into(),
),
Expand Down
12 changes: 5 additions & 7 deletions rust/Client/tests/cmd_event.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use cheetah_client::ffi;
use cheetah_client::ffi::command::{S2CCommandFFI, S2CommandUnionFFI};
use cheetah_common::commands::types::structure::BinaryField;
use cheetah_client::ffi::command::{BinaryFieldFFI, BufferFFI, S2CCommandFFI, S2CommandUnionFFI};
use cheetah_common::commands::CommandTypeId;
use cheetah_common::room::buffer::Buffer;
use cheetah_common::room::object::GameObjectId;

use crate::helpers::helper::setup;
Expand All @@ -16,10 +14,10 @@ fn test() {
ffi::command::room::attach_to_room(client2);
let mut object_id = GameObjectId::default();
ffi::command::object::create_object(client1, 1, IntegrationTestServerBuilder::DEFAULT_ACCESS_GROUP.0, &mut object_id);
ffi::command::object::created_object(client1, &object_id, false, &Buffer::default());
ffi::command::object::created_object(client1, &object_id, false, &Default::default());
helper.receive(client2);

let mut event_buffer = Buffer { len: 1, ..Default::default() };
let mut event_buffer = BufferFFI { len: 1, ..Default::default() };
event_buffer.buffer[0] = 100;
let event_field_id = 10;
ffi::command::event::send_event(client1, &object_id, event_field_id, &event_buffer);
Expand All @@ -30,10 +28,10 @@ fn test() {
S2CCommandFFI {
command_type: CommandTypeId::SendEvent,
command: S2CommandUnionFFI {
buffer_field: BinaryField {
buffer_field: BinaryFieldFFI {
object_id,
field_id: event_field_id,
value: event_buffer,
value: event_buffer.into(),
}
}
}
Expand Down
Loading

0 comments on commit 3323fca

Please sign in to comment.