(client, server) feat:Add UDP client and server
This commit is contained in:
@ -568,6 +568,50 @@ Transform:
|
|||||||
m_Children: []
|
m_Children: []
|
||||||
m_Father: {fileID: 613797070}
|
m_Father: {fileID: 613797070}
|
||||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
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
|
--- !u!1 &2036983430
|
||||||
GameObject:
|
GameObject:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
@ -684,4 +728,5 @@ SceneRoots:
|
|||||||
- {fileID: 1057087090}
|
- {fileID: 1057087090}
|
||||||
- {fileID: 613797070}
|
- {fileID: 613797070}
|
||||||
- {fileID: 2104915508}
|
- {fileID: 2104915508}
|
||||||
|
- {fileID: 1388451206}
|
||||||
- {fileID: 2036983432}
|
- {fileID: 2036983432}
|
||||||
|
28
Client/Assets/Scripts/Network/UnityUdpClient.cs
Normal file
28
Client/Assets/Scripts/Network/UnityUdpClient.cs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
using System.Net.Sockets;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using UnityEngine;
|
||||||
|
using Utils;
|
||||||
|
|
||||||
|
namespace Network
|
||||||
|
{
|
||||||
|
public class UnityUdpClient : Singleton<UnityUdpClient>
|
||||||
|
{
|
||||||
|
private readonly UdpClient _client;
|
||||||
|
|
||||||
|
public UnityUdpClient()
|
||||||
|
{
|
||||||
|
_client = new UdpClient();
|
||||||
|
_client.Connect("127.0.0.1", 12345);
|
||||||
|
|
||||||
|
Application.quitting += () => _client.Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<byte[]> SendAndReceiveData(byte[] data)
|
||||||
|
{
|
||||||
|
await _client.SendAsync(data, data.Length);
|
||||||
|
|
||||||
|
var result = await _client.ReceiveAsync();
|
||||||
|
return result.Buffer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
2
Client/Assets/Scripts/Network/UnityUdpClient.cs.meta
Normal file
2
Client/Assets/Scripts/Network/UnityUdpClient.cs.meta
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 1dde9b247fc7ffc478f6b77a79c1e11c
|
19
Client/Assets/Scripts/Test/UdpClientTest.cs
Normal file
19
Client/Assets/Scripts/Test/UdpClientTest.cs
Normal file
@ -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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
2
Client/Assets/Scripts/Test/UdpClientTest.cs.meta
Normal file
2
Client/Assets/Scripts/Test/UdpClientTest.cs.meta
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: d76ad8f9d40092848abc97f05b1e2131
|
@ -3,9 +3,11 @@ mod grpc_server;
|
|||||||
mod protocol;
|
mod protocol;
|
||||||
mod server_logger;
|
mod server_logger;
|
||||||
mod services;
|
mod services;
|
||||||
|
mod udp_server;
|
||||||
|
|
||||||
use grpc_server::GrpcServer;
|
use grpc_server::GrpcServer;
|
||||||
use server_logger::ServerLogger;
|
use server_logger::ServerLogger;
|
||||||
|
use udp_server::UdpServer;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
@ -14,6 +16,7 @@ async fn main() {
|
|||||||
log::info!("Starting server...");
|
log::info!("Starting server...");
|
||||||
|
|
||||||
GrpcServer::init().await;
|
GrpcServer::init().await;
|
||||||
|
UdpServer::init();
|
||||||
|
|
||||||
log::info!("Server successfully started!");
|
log::info!("Server successfully started!");
|
||||||
|
|
||||||
|
27
Server/src/udp_server.rs
Normal file
27
Server/src/udp_server.rs
Normal file
@ -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}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user