A Meaningless PR #10
@ -303,6 +303,50 @@ Transform:
|
|||||||
- {fileID: 9462743}
|
- {fileID: 9462743}
|
||||||
m_Father: {fileID: 0}
|
m_Father: {fileID: 0}
|
||||||
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
|
||||||
|
--- !u!1 &912467177
|
||||||
|
GameObject:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
serializedVersion: 6
|
||||||
|
m_Component:
|
||||||
|
- component: {fileID: 912467178}
|
||||||
|
- component: {fileID: 912467179}
|
||||||
|
m_Layer: 0
|
||||||
|
m_Name: NetworkConnectionTest
|
||||||
|
m_TagString: Untagged
|
||||||
|
m_Icon: {fileID: 0}
|
||||||
|
m_NavMeshLayer: 0
|
||||||
|
m_StaticEditorFlags: 0
|
||||||
|
m_IsActive: 1
|
||||||
|
--- !u!4 &912467178
|
||||||
|
Transform:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 912467177}
|
||||||
|
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 &912467179
|
||||||
|
MonoBehaviour:
|
||||||
|
m_ObjectHideFlags: 0
|
||||||
|
m_CorrespondingSourceObject: {fileID: 0}
|
||||||
|
m_PrefabInstance: {fileID: 0}
|
||||||
|
m_PrefabAsset: {fileID: 0}
|
||||||
|
m_GameObject: {fileID: 912467177}
|
||||||
|
m_Enabled: 1
|
||||||
|
m_EditorHideFlags: 0
|
||||||
|
m_Script: {fileID: 11500000, guid: 289a7efc822fe7347adfbf218522e8a2, type: 3}
|
||||||
|
m_Name:
|
||||||
|
m_EditorClassIdentifier:
|
||||||
--- !u!1 &1057087086
|
--- !u!1 &1057087086
|
||||||
GameObject:
|
GameObject:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
@ -601,3 +645,4 @@ SceneRoots:
|
|||||||
- {fileID: 1371068282}
|
- {fileID: 1371068282}
|
||||||
- {fileID: 1057087090}
|
- {fileID: 1057087090}
|
||||||
- {fileID: 613797070}
|
- {fileID: 613797070}
|
||||||
|
- {fileID: 912467178}
|
||||||
|
26
Client/Assets/Scripts/Test/NetworkConnectionTest.cs
Normal file
26
Client/Assets/Scripts/Test/NetworkConnectionTest.cs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
using System.Threading.Tasks;
|
||||||
|
using Network;
|
||||||
|
using UnityEngine;
|
||||||
|
|
||||||
|
namespace Test
|
||||||
|
{
|
||||||
|
public class NetworkConnectionTest : MonoBehaviour
|
||||||
|
{
|
||||||
|
private async void Start()
|
||||||
|
{
|
||||||
|
await BasicTest();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task BasicTest()
|
||||||
|
{
|
||||||
|
var result = await UnityTcpClient.Instance.Connect("127.0.0.1", 12345);
|
||||||
|
if (result) Debug.Log("Connected to server!");
|
||||||
|
else Debug.LogError("Failed to connect to server!");
|
||||||
|
|
||||||
|
string buffer = null;
|
||||||
|
while (buffer is null) buffer = await UnityTcpClient.Instance.Receive();
|
||||||
|
|
||||||
|
Debug.Log($"Received contents: {buffer}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
2
Client/Assets/Scripts/Test/NetworkConnectionTest.cs.meta
Normal file
2
Client/Assets/Scripts/Test/NetworkConnectionTest.cs.meta
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 289a7efc822fe7347adfbf218522e8a2
|
@ -1,4 +1,4 @@
|
|||||||
use tokio::io::AsyncWriteExt;
|
use tokio::io::{ErrorKind, Interest};
|
||||||
use tokio::net::{TcpListener, TcpStream};
|
use tokio::net::{TcpListener, TcpStream};
|
||||||
|
|
||||||
pub(crate) struct TcpServer;
|
pub(crate) struct TcpServer;
|
||||||
@ -20,10 +20,44 @@ impl TcpServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn process(mut socket: TcpStream) {
|
async fn process(socket: TcpStream) {
|
||||||
socket
|
loop {
|
||||||
.write_all("Hello from server written in Rust!".as_bytes())
|
match socket.ready(Interest::READABLE | Interest::WRITABLE).await {
|
||||||
.await
|
Ok(ready) => {
|
||||||
.unwrap();
|
if ready.is_readable() {
|
||||||
|
let mut buffer = Vec::with_capacity(1024);
|
||||||
|
match socket.try_read_buf(&mut buffer) {
|
||||||
|
// We need to write to client after reading from that,
|
||||||
|
// so we use `continue` here instead if `break`.
|
||||||
|
Ok(_) => {
|
||||||
|
log::info!("Received contents");
|
||||||
|
|
||||||
|
// TODO: Start dispatching messages here.
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Err(e) if e.kind() == ErrorKind::WouldBlock => continue,
|
||||||
|
Err(e) => {
|
||||||
|
log::error!("Failed to read from TCP client socket: {e}");
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if ready.is_writable() {
|
||||||
|
match socket.try_write(b"Hello from server written in Rust!") {
|
||||||
|
Ok(_) => break,
|
||||||
|
Err(e) if e.kind() == ErrorKind::WouldBlock => continue,
|
||||||
|
Err(e) => {
|
||||||
|
log::error!("Failed to write to TCP client socket: {e}");
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => log::error!("Failed to wait for any states: {e}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user