diff --git a/Client/Assets/Scenes/Test.unity b/Client/Assets/Scenes/Test.unity index e530bdc..61a32da 100644 --- a/Client/Assets/Scenes/Test.unity +++ b/Client/Assets/Scenes/Test.unity @@ -568,6 +568,50 @@ Transform: m_Children: [] m_Father: {fileID: 613797070} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &1388451205 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1388451206} + - component: {fileID: 1388451207} + m_Layer: 0 + m_Name: UdpClientTest + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1388451206 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1388451205} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &1388451207 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1388451205} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: d76ad8f9d40092848abc97f05b1e2131, type: 3} + m_Name: + m_EditorClassIdentifier: Assembly-CSharp::Test.UdpClientTest --- !u!1 &2036983430 GameObject: m_ObjectHideFlags: 0 @@ -684,4 +728,5 @@ SceneRoots: - {fileID: 1057087090} - {fileID: 613797070} - {fileID: 2104915508} + - {fileID: 1388451206} - {fileID: 2036983432} diff --git a/Client/Assets/Scripts/Network/UnityUdpClient.cs b/Client/Assets/Scripts/Network/UnityUdpClient.cs new file mode 100644 index 0000000..b682523 --- /dev/null +++ b/Client/Assets/Scripts/Network/UnityUdpClient.cs @@ -0,0 +1,28 @@ +using System.Net.Sockets; +using System.Threading.Tasks; +using UnityEngine; +using Utils; + +namespace Network +{ + public class UnityUdpClient : Singleton + { + private readonly UdpClient _client; + + public UnityUdpClient() + { + _client = new UdpClient(); + _client.Connect("127.0.0.1", 12345); + + Application.quitting += () => _client.Close(); + } + + public async Task SendAndReceiveData(byte[] data) + { + await _client.SendAsync(data, data.Length); + + var result = await _client.ReceiveAsync(); + return result.Buffer; + } + } +} diff --git a/Client/Assets/Scripts/Network/UnityUdpClient.cs.meta b/Client/Assets/Scripts/Network/UnityUdpClient.cs.meta new file mode 100644 index 0000000..38c4989 --- /dev/null +++ b/Client/Assets/Scripts/Network/UnityUdpClient.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 1dde9b247fc7ffc478f6b77a79c1e11c \ No newline at end of file diff --git a/Client/Assets/Scripts/Test/UdpClientTest.cs b/Client/Assets/Scripts/Test/UdpClientTest.cs new file mode 100644 index 0000000..58e8a13 --- /dev/null +++ b/Client/Assets/Scripts/Test/UdpClientTest.cs @@ -0,0 +1,19 @@ +using Network; +using System.Text; +using UnityEngine; + +namespace Test +{ + public class UdpClientTest : MonoBehaviour + { + private async void Start() + { + var sendBytes = Encoding.UTF8.GetBytes("Test 汉语 and English simultaneously!"); + + var receivedBytes = await UnityUdpClient.Instance.SendAndReceiveData(sendBytes); + var receivedString = Encoding.UTF8.GetString(receivedBytes); + + Debug.Log($"Received string: {receivedString}"); + } + } +} \ No newline at end of file diff --git a/Client/Assets/Scripts/Test/UdpClientTest.cs.meta b/Client/Assets/Scripts/Test/UdpClientTest.cs.meta new file mode 100644 index 0000000..2ad7462 --- /dev/null +++ b/Client/Assets/Scripts/Test/UdpClientTest.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: d76ad8f9d40092848abc97f05b1e2131 \ No newline at end of file diff --git a/Server/src/main.rs b/Server/src/main.rs index 57f447e..e0563a4 100644 --- a/Server/src/main.rs +++ b/Server/src/main.rs @@ -3,9 +3,11 @@ mod grpc_server; mod protocol; mod server_logger; mod services; +mod udp_server; use grpc_server::GrpcServer; use server_logger::ServerLogger; +use udp_server::UdpServer; #[tokio::main] async fn main() { @@ -14,6 +16,7 @@ async fn main() { log::info!("Starting server..."); GrpcServer::init().await; + UdpServer::init(); log::info!("Server successfully started!"); diff --git a/Server/src/udp_server.rs b/Server/src/udp_server.rs new file mode 100644 index 0000000..117de73 --- /dev/null +++ b/Server/src/udp_server.rs @@ -0,0 +1,27 @@ +use std::net::UdpSocket; + +use tokio::task; + +pub(crate) struct UdpServer; + +impl UdpServer { + pub(crate) fn init() { + match UdpSocket::bind("127.0.0.1:12345") { + Ok(socket) => { + task::spawn(async move { + loop { + let mut buf = [0; 1500]; + let (amt, src) = socket.recv_from(&mut buf).unwrap(); + + log::info!("Received message from client {src}"); + + // TODO: Process received data in an independent method. + let buf = &buf[..amt]; + socket.send_to(buf, src).unwrap(); + } + }); + } + Err(e) => log::error!("Failed to bind to address: {e}"), + } + } +}