diff options
author | chai <215380520@qq.com> | 2023-10-17 09:24:19 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2023-10-17 09:24:19 +0800 |
commit | b4b6a3ac27bfa16309b130f8e4f8db8eb7ba9ed9 (patch) | |
tree | 29e38d79477a407108da614d2b3f869a08cd4589 | |
parent | 63cbd04e7104bb67e6ebdcdd20f80b935e5c9168 (diff) |
*misc
8 files changed, 219 insertions, 65 deletions
diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/Backend/Wrap/NetClient.cs b/MultiplayerToolkit/Assets/MultiplayerToolkit/Backend/Wrap/NetClient.cs index 603d027..be0f772 100644 --- a/MultiplayerToolkit/Assets/MultiplayerToolkit/Backend/Wrap/NetClient.cs +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Backend/Wrap/NetClient.cs @@ -34,7 +34,7 @@ namespace MultiplayerToolkit.Backend.Wrap { public static int RECEIVE_BUFFER_SIZE = 4096; - public Action<byte[], int> onReceiveData; + public Action<byte[], int, ESendOption> onReceiveData; private TcpClient connection; @@ -106,7 +106,7 @@ namespace MultiplayerToolkit.Backend.Wrap break; if (onReceiveData != null) { - onReceiveData(receiveBuffer, length); + onReceiveData(receiveBuffer, length, ESendOption.Reliable); } } } @@ -129,7 +129,7 @@ namespace MultiplayerToolkit.Backend.Wrap { public bool isConnected { get; set; } - public Action<byte[]> onReceiveData; + public Action<byte[], int, ESendOption> onReceiveData; private UdpClient m_Connection; @@ -150,7 +150,7 @@ namespace MultiplayerToolkit.Backend.Wrap byte[] buffer = result.Buffer; if(onReceiveData != null) { - onReceiveData(buffer); + onReceiveData(buffer, buffer.Length, ESendOption.Reliable); } } } @@ -190,20 +190,19 @@ namespace MultiplayerToolkit.Backend.Wrap /// </summary> public class NetClient { - public enum ESendOption - { - Reliable, - Unreliable, - } - private TcpConnection m_TcpConnection; private UdpConncetion m_UdpConnection; + public event Action<byte[], int, ESendOption> onReceiveData; + public async Task<bool> ConnectToServer(NetClientSetting setting) { m_TcpConnection = new TcpConnection(); m_UdpConnection = new UdpConncetion(); + m_TcpConnection.onReceiveData = OnReceiveData; + m_UdpConnection.onReceiveData = OnReceiveData; + await m_TcpConnection.Connect(setting); m_UdpConnection.Conncet(setting); LogNet.Log("NetClient.ConnectToServer() successful"); @@ -222,6 +221,14 @@ namespace MultiplayerToolkit.Backend.Wrap } } + private void OnReceiveData(byte[] data, int length, ESendOption option ) + { + if(onReceiveData != null) + { + onReceiveData(data, length, option); + } + } + public void Disconnect() { m_TcpConnection.Disconnect(); diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/Backend/Wrap/NetServer.cs b/MultiplayerToolkit/Assets/MultiplayerToolkit/Backend/Wrap/NetServer.cs index 215682b..7c09bbe 100644 --- a/MultiplayerToolkit/Assets/MultiplayerToolkit/Backend/Wrap/NetServer.cs +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Backend/Wrap/NetServer.cs @@ -31,6 +31,18 @@ namespace MultiplayerToolkit.Backend.Wrap private UdpClient m_UdpClient; // server UdpClient private IPEndPoint m_UdpEndPoint; + private bool m_IsTcpBind; + public bool isTcpBind { get { return m_IsTcpBind; } } + + private bool m_IsUdpBind; + public bool isUdpBind { get { return m_IsUdpBind; } } + + public NetMember() + { + m_IsTcpBind = false; + m_IsUdpBind = false; + } + public void BindTcpClient(TcpClient client) { m_TcpClient = client; @@ -45,6 +57,19 @@ namespace MultiplayerToolkit.Backend.Wrap ReceiveUdpAsync(); } + public void Disconnect() + { + m_IsUdpBind = false; + m_IsTcpBind = false; + + m_TcpStream.Close(); + m_TcpClient.Close(); + + m_TcpStream = null; + m_TcpClient = null; + m_UdpEndPoint = null; + } + public bool IsBindWith(TcpClient client) { return m_TcpClient == client; @@ -146,8 +171,9 @@ namespace MultiplayerToolkit.Backend.Wrap private TcpListener m_TcpListener; private UdpClient m_UdpClient; + public UdpClient udpClient { get { return m_UdpClient; } } + public event Action<TcpClient> onNewTcpClient; - public event Action<UdpReceiveResult> onNewUdpClient; public event Action<UdpReceiveResult> onUdpReceiveData; private bool m_IsRunning; @@ -198,10 +224,6 @@ namespace MultiplayerToolkit.Backend.Wrap while (m_IsRunning) { UdpReceiveResult result = await m_UdpClient.ReceiveAsync(); - if (onNewUdpClient != null) - { - onNewUdpClient(result); - } if (onUdpReceiveData != null) { onUdpReceiveData(result); diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/NetObject.cs b/MultiplayerToolkit/Assets/MultiplayerToolkit/NetObject.cs new file mode 100644 index 0000000..0734b64 --- /dev/null +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/NetObject.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace MultiplayerToolkit +{ + + public enum ESendOption + { + Reliable, + Unreliable, + } + + /// <summary> + /// 需要网络同步的挂这个脚本 + /// </summary> + public abstract class NetObject : MonoBehaviour, IComparable<NetObject> + { + + public uint netId; // 这个物体的唯一网络编号 + + public bool amOwner; + + public uint dirtyBits; + + public ESendOption sendOption; + + public int ownerId; // 如果是玩家本身,表明这个物体对应的玩家 + + + public abstract void HandleRpc(byte callId, MessageReader reader); + + public abstract bool Serialize(MessageWriter writer, bool initialState); + + public abstract void Deserialize(MessageReader reader, bool initialState); + + + + public int CompareTo(NetObject other) + { + if (this.netId > other.netId) + { + return 1; + } + if (this.netId < other.netId) + { + return -1; + } + return 0; + } + + protected void SetDirtyBit(uint val) + { + this.dirtyBits |= val; + } + + } + +} diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/NetObject.cs.meta b/MultiplayerToolkit/Assets/MultiplayerToolkit/NetObject.cs.meta new file mode 100644 index 0000000..9db9f5b --- /dev/null +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/NetObject.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: cc4344a6b21418744bead268ab592141 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/Packet/Packet.cs b/MultiplayerToolkit/Assets/MultiplayerToolkit/Packet/Packet.cs index 20a2167..eb07feb 100644 --- a/MultiplayerToolkit/Assets/MultiplayerToolkit/Packet/Packet.cs +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Packet/Packet.cs @@ -58,7 +58,7 @@ namespace MultiplayerToolkit if (readable.mode != EPacketMode.Read) return null; Packet packet = GetWritablePacket(); - packet.Write(readable.readableBuffer); + packet.WriteBytes(readable.readableBuffer); return packet; } @@ -155,62 +155,66 @@ namespace MultiplayerToolkit return this.writableBuffer.Count; } - public void Write(byte _value) + //public bool IsComplete() + //{ + //} + + public void WriteByte(byte _value) { this.writableBuffer.Add(_value); } - public void Write(byte[] _value) + public void WriteBytes(byte[] _value) { this.writableBuffer.AddRange(_value); } - public void Write(short _value) + public void WriteShort(short _value) { this.writableBuffer.AddRange(BitConverter.GetBytes(_value)); } - public void Write(int _value) + public void WriteInt(int _value) { this.writableBuffer.AddRange(BitConverter.GetBytes(_value)); } - public void Write(long _value) + public void WriteLong(long _value) { this.writableBuffer.AddRange(BitConverter.GetBytes(_value)); } - public void Write(float _value) + public void WriteFloat(float _value) { this.writableBuffer.AddRange(BitConverter.GetBytes(_value)); } - public void Write(bool _value) + public void WriteBool(bool _value) { this.writableBuffer.AddRange(BitConverter.GetBytes(_value)); } - public void Write(string _value) + public void WriteString(string _value) { - this.Write((int)_value.Length); + this.WriteInt((int)_value.Length); this.writableBuffer.AddRange(Encoding.ASCII.GetBytes(_value)); } #if UNITY_5_3_OR_NEWER - public void Write(Vector3 _value) + public void WriteVector3(Vector3 _value) { - this.Write(_value.x); - this.Write(_value.y); - this.Write(_value.z); + this.WriteFloat(_value.x); + this.WriteFloat(_value.y); + this.WriteFloat(_value.z); } - public void Write(Quaternion _value) + public void WriteQuaternion(Quaternion _value) { - this.Write(_value.x); - this.Write(_value.y); - this.Write(_value.z); - this.Write(_value.w); + this.WriteFloat(_value.x); + this.WriteFloat(_value.y); + this.WriteFloat(_value.z); + this.WriteFloat(_value.w); } #endif diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/Test_TcpClient.cs b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/Test_TcpClient.cs index 31e2b24..4874737 100644 --- a/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/Test_TcpClient.cs +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestTcp/Test_TcpClient.cs @@ -26,8 +26,8 @@ public class Test_TcpClient : MonoBehaviour client.Connect("127.0.1.1", 42046); NetworkStream stream = client.GetStream(); Packet packet = Packet.GetWritablePacket(); - packet.Write((int)123); - packet.Write("hello"); + packet.WriteInt((int)123); + packet.WriteString("hello"); stream.Write(packet.ToArray()); } diff --git a/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestWrap/TestWrapClient.cs b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestWrap/TestWrapClient.cs index dfcdaba..4e31a68 100644 --- a/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestWrap/TestWrapClient.cs +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestWrap/TestWrapClient.cs @@ -25,19 +25,43 @@ public class TestWrapClient : MonoBehaviour setting.remoteUdpPort = 34545; client = new NetClient(); - TestConnect(setting); + client.onReceiveData += OnRecvData; + + ConnectServer(setting); } - private async void TestConnect(NetClientSetting setting) + private async void ConnectServer(NetClientSetting setting) { await client.ConnectToServer(setting); Packet packet = Packet.GetWritablePacket(); - packet.Write("hello, world"); - packet.Write(Quaternion.identity); + packet.WriteString("hello, world"); + packet.WriteQuaternion(Quaternion.identity); + + client.SendData(packet.ToArray(), 0, packet.GetWritableBufferLength(), ESendOption.Reliable); + } + + private void OnRecvData(byte[] data, int len, ESendOption sendOption) + { + Packet packet = Packet.GetReadablePacket(data, 0, len); + if(sendOption == ESendOption.Reliable) + { + if(packet.GetUnreadLength() >= 4) + { + int value = packet.ReadInt(); + if(value == 51825) // hello + { + int id = packet.ReadInt(); + Packet echo = Packet.GetWritablePacket(); + echo.WriteInt((int)51825); + echo.WriteInt(id); + echo.WriteString("UDP haloha"); + client.SendData(echo.ToArray(), 0, echo.GetWritableBufferLength(), ESendOption.Unreliable); + return; + } + } + } - 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 44a1fe7..c44f5b3 100644 --- a/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestWrap/TestWrapServer.cs +++ b/MultiplayerToolkit/Assets/MultiplayerToolkit/Test/TestWrap/TestWrapServer.cs @@ -7,12 +7,29 @@ using System.Net; using System.Net.Sockets; using Steamworks; using System; +using System.Threading; public class TestWrapServer : MonoBehaviour { - NetServer server; + public class Player + { + private static int IdCount = 1; + + public int id; + + public NetMember member; + + public Player() + { + member = new NetMember(); + this.id = Interlocked.Increment(ref IdCount); + } + } + + private List<Player> playerList = new List<Player>(); + void Start() { NetServerSetting setting = new NetServerSetting(); @@ -23,35 +40,31 @@ public class TestWrapServer : MonoBehaviour server = new NetServer(); server.onNewTcpClient += OnNewTcpClient; - server.onNewUdpClient += OnNewUdpClient; server.onUdpReceiveData += OnUdpReceiveData; server.Start(setting); - } private void OnNewTcpClient(TcpClient client) { - TestReceive(client); + //TestReceive(client); + Player player = new Player(); + player.member.BindTcpClient(client); + player.member.onReceiveData += OnRecvData; + playerList.Add(player); + // hello + Packet hello = Packet.GetWritablePacket(); + hello.WriteInt((int)51825); // magic number s + hello.WriteInt((int)player.id); + player.member.SendData(hello.ToArray(), 0, hello.GetWritableBufferLength(), ESendOption.Reliable); } - async void TestReceive(TcpClient client) + private void OnRecvData(byte[] data, int len) { - while(true) - { - var buffer = new byte[4096]; - var stream = client.GetStream(); - int len = await stream.ReadAsync(buffer, 0, 4096); - using Packet packet = Packet.GetReadablePacket(buffer, 0, len); - string str = packet.ReadString(); - Debug.Log(str); - } - } - - private void OnNewUdpClient(UdpReceiveResult client) - { - + Packet packet = Packet.GetReadablePacket(data); + string str = packet.ReadString(); + Debug.Log(str); } private void OnUdpReceiveData(UdpReceiveResult result) @@ -59,10 +72,23 @@ public class TestWrapServer : MonoBehaviour 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); + + if(packet.GetUnreadLength() >= 4) + { + int value =packet.ReadInt(); + if(value == 51825) + { + int id = packet.ReadInt(); + string content = packet.ReadString(); + Debug.Log(id); + Debug.Log(content); + var player = playerList.Find(x => x.id == id); + if (player != null) { + player.member.BindUdpClient(server.udpClient, result.RemoteEndPoint); + } + } + } + } }
\ No newline at end of file |