aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2023-10-16 18:54:12 +0800
committerchai <215380520@qq.com>2023-10-16 18:54:12 +0800
commit63cbd04e7104bb67e6ebdcdd20f80b935e5c9168 (patch)
treecd5af89122db5ed70ab39e600961bb6d7f3458cb
parentc0713e68706185dc7549490acc91b677971eeae9 (diff)
*misc
-rw-r--r--MultiplayerToolkit/Assets/MultiplayerToolkit/Backend/Wrap/NetServer.cs141
-rw-r--r--MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestWrap/TestWrapClient.cs2
-rw-r--r--MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestWrap/TestWrapServer.cs11
3 files changed, 141 insertions, 13 deletions
diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/Backend/Wrap/NetServer.cs b/MultiplayerToolkit/Assets/MultiplayerToolkit/Backend/Wrap/NetServer.cs
index 0e9e0f6..215682b 100644
--- a/MultiplayerToolkit/Assets/MultiplayerToolkit/Backend/Wrap/NetServer.cs
+++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Backend/Wrap/NetServer.cs
@@ -2,13 +2,131 @@ using JetBrains.Annotations;
using System;
using System.Collections;
using System.Collections.Generic;
+using System.IO;
using System.Net;
using System.Net.Sockets;
using UnityEditor;
using UnityEngine;
+using static MultiplayerToolkit.Backend.Wrap.NetClient;
namespace MultiplayerToolkit.Backend.Wrap
{
+
+ public struct NetMemberInfo
+ {
+ }
+
+ /// <summary>
+ /// 服务器端,多个成员
+ /// </summary>
+ public class NetMember
+ {
+ public static int RECEIVE_BUFFER_SIZE = 4096;
+
+ public Action<byte[], int> onReceiveData;
+
+ private TcpClient m_TcpClient;
+ private NetworkStream m_TcpStream;
+
+ private UdpClient m_UdpClient; // server UdpClient
+ private IPEndPoint m_UdpEndPoint;
+
+ public void BindTcpClient(TcpClient client)
+ {
+ m_TcpClient = client;
+ m_TcpStream = client.GetStream();
+ ReceiveTcpAsync();
+ }
+
+ public void BindUdpClient(UdpClient server, IPEndPoint udpEndPoint)
+ {
+ m_UdpClient = server;
+ m_UdpEndPoint = udpEndPoint;
+ ReceiveUdpAsync();
+ }
+
+ public bool IsBindWith(TcpClient client)
+ {
+ return m_TcpClient == client;
+ }
+
+ public bool IsBindWith(IPEndPoint udpEP)
+ {
+ return m_UdpEndPoint == udpEP;
+ }
+
+ private async void ReceiveTcpAsync()
+ {
+ byte[] receiveBuffer = new byte[RECEIVE_BUFFER_SIZE];
+ while (true)
+ {
+ int length = await m_TcpStream.ReadAsync(receiveBuffer, 0, RECEIVE_BUFFER_SIZE);
+ OnReceiveData(receiveBuffer, length);
+ }
+ }
+
+ private async void SendTcpData(byte[] data, int offset, int length)
+ {
+ try
+ {
+ await m_TcpStream.WriteAsync(data, offset, length);
+ }
+ catch (Exception e)
+ {
+ LogNet.LogError(e.Message);
+ }
+ }
+
+ private async void ReceiveUdpAsync()
+ {
+ while (true)
+ {
+ UdpReceiveResult result = await m_UdpClient.ReceiveAsync();
+ byte[] buffer = result.Buffer;
+ OnReceiveData(buffer, result.Buffer.Length);
+ }
+ }
+
+ private async void SendUdpData(byte[] data, int offset, int length)
+ {
+ try
+ {
+ if (m_UdpClient != null)
+ {
+ byte[] buf = data;
+ if (offset != 0)
+ {
+ buf = new byte[length];
+ Array.Copy(data, offset, buf, 0, length);
+ }
+ await m_UdpClient.SendAsync(buf, length, m_UdpEndPoint);
+ }
+ }
+ catch (Exception e)
+ {
+ }
+ }
+
+ private void OnReceiveData(byte[] data, int length)
+ {
+ if(onReceiveData != null)
+ {
+ onReceiveData(data, length);
+ }
+ }
+
+ public void SendData(byte[] data, int offset, int length, ESendOption option = ESendOption.Reliable)
+ {
+ if (option == ESendOption.Reliable)
+ {
+ SendTcpData(data, offset, length);
+ }
+ else
+ {
+ SendUdpData(data, offset, length);
+ }
+ }
+ }
public struct NetServerSetting
{
@@ -24,6 +142,7 @@ namespace MultiplayerToolkit.Backend.Wrap
/// </summary>
public class NetServer
{
+
private TcpListener m_TcpListener;
private UdpClient m_UdpClient;
@@ -90,17 +209,17 @@ namespace MultiplayerToolkit.Backend.Wrap
}
}
- public async void SendUDPData(IPEndPoint remoteEP, byte[] data, int length)
- {
- try
- {
- await m_UdpClient.SendAsync(data, length, remoteEP);
- }
- catch(Exception e)
- {
- LogNet.LogError(e.Message);
- }
- }
+ //public async void SendUDPData(IPEndPoint remoteEP, byte[] data, int length)
+ //{
+ // try
+ // {
+ // await m_UdpClient.SendAsync(data, length, remoteEP);
+ // }
+ // catch (Exception e)
+ // {
+ // LogNet.LogError(e.Message);
+ // }
+ //}
}
} \ No newline at end of file
diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestWrap/TestWrapClient.cs b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestWrap/TestWrapClient.cs
index ad796da..dfcdaba 100644
--- a/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestWrap/TestWrapClient.cs
+++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestWrap/TestWrapClient.cs
@@ -34,8 +34,10 @@ public class TestWrapClient : MonoBehaviour
Packet packet = Packet.GetWritablePacket();
packet.Write("hello, world");
+ packet.Write(Quaternion.identity);
client.SendData(packet.ToArray(), 0, packet.GetWritableBufferLength(), NetClient.ESendOption.Reliable);
+ client.SendData(packet.ToArray(), 0, packet.GetWritableBufferLength(), NetClient.ESendOption.Unreliable);
}
}
diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestWrap/TestWrapServer.cs b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestWrap/TestWrapServer.cs
index 7776878..44a1fe7 100644
--- a/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestWrap/TestWrapServer.cs
+++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestWrap/TestWrapServer.cs
@@ -6,6 +6,7 @@ using MultiplayerToolkit.Backend.Wrap;
using System.Net;
using System.Net.Sockets;
using Steamworks;
+using System;
public class TestWrapServer : MonoBehaviour
{
@@ -42,7 +43,7 @@ public class TestWrapServer : MonoBehaviour
var buffer = new byte[4096];
var stream = client.GetStream();
int len = await stream.ReadAsync(buffer, 0, 4096);
- Packet packet = Packet.GetReadablePacket(buffer, 0, len);
+ using Packet packet = Packet.GetReadablePacket(buffer, 0, len);
string str = packet.ReadString();
Debug.Log(str);
}
@@ -55,7 +56,13 @@ public class TestWrapServer : MonoBehaviour
private void OnUdpReceiveData(UdpReceiveResult result)
{
-
+ if (result.Buffer == null || result.Buffer.Length == 0)
+ return;
+ Packet packet = Packet.GetReadablePacket(result.Buffer, 0, result.Buffer.Length);
+ string str = packet.ReadString();
+ Quaternion qua = packet.ReadQuaternion();
+ Debug.Log(str);
+ Debug.Log(qua);
}
} \ No newline at end of file