feat(network): add shared packet interfaces and auth request packet
This commit is contained in:
parent
b235ed3cf8
commit
afad3fb87e
22 changed files with 126 additions and 11 deletions
|
|
@ -19,6 +19,8 @@ namespace _Hub.Scripts.Network
|
|||
private NetPeer _netPeer;
|
||||
private NetDataWriter _netDataWriter;
|
||||
|
||||
public static System.Action OnServerConnected;
|
||||
|
||||
#endregion
|
||||
|
||||
private void Awake()
|
||||
|
|
@ -45,6 +47,7 @@ namespace _Hub.Scripts.Network
|
|||
|
||||
private void Init()
|
||||
{
|
||||
_netDataWriter = new NetDataWriter();
|
||||
_netManager = new NetManager(this)
|
||||
{
|
||||
DisconnectTimeout = 100000
|
||||
|
|
@ -57,16 +60,20 @@ namespace _Hub.Scripts.Network
|
|||
_netManager.Connect("localhost", 9050, "");
|
||||
}
|
||||
|
||||
public void SendServer(string data)
|
||||
public void SendServer<T>(T packet, DeliveryMethod deliveryMethod = DeliveryMethod.ReliableOrdered)
|
||||
where T : INetSerializable
|
||||
{
|
||||
var bytes = Encoding.UTF8.GetBytes(data);
|
||||
_netPeer.Send(bytes, DeliveryMethod.ReliableOrdered);
|
||||
if (_netPeer == null) return;
|
||||
_netDataWriter.Reset();
|
||||
packet.Serialize(_netDataWriter);
|
||||
_netPeer.Send(_netDataWriter, deliveryMethod);
|
||||
}
|
||||
|
||||
public void OnPeerConnected(NetPeer peer)
|
||||
{
|
||||
Debug.Log("We connect to server at " + peer.Address);
|
||||
_netPeer = peer;
|
||||
OnServerConnected?.Invoke();
|
||||
}
|
||||
|
||||
public void OnPeerDisconnected(NetPeer peer, DisconnectInfo disconnectInfo)
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ using System.Collections;
|
|||
using System.Text.RegularExpressions;
|
||||
using _Hub.Scripts.Network;
|
||||
using _Network.Auth;
|
||||
using _Network.Shared.Packets.ClientServer;
|
||||
using UnityEngine;
|
||||
|
||||
namespace _Hub.Scripts.UI
|
||||
|
|
@ -29,6 +30,16 @@ namespace _Hub.Scripts.UI
|
|||
|
||||
#region Unity
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
NetworkClient.OnServerConnected += OnServerConnected;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
NetworkClient.OnServerConnected -= OnServerConnected;
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
AddListeners();
|
||||
|
|
@ -43,7 +54,8 @@ namespace _Hub.Scripts.UI
|
|||
|
||||
private void OnLoginClick()
|
||||
{
|
||||
|
||||
StopCoroutine(nameof(LoginRoutine));
|
||||
StartCoroutine(nameof(LoginRoutine));
|
||||
}
|
||||
|
||||
private IEnumerator LoginRoutine()
|
||||
|
|
@ -57,8 +69,13 @@ namespace _Hub.Scripts.UI
|
|||
yield return null;
|
||||
}
|
||||
|
||||
Debug.Log("Connected to server");
|
||||
var authRequest = "authRequestObject";
|
||||
Debug.Log("Connected to server ehm");
|
||||
var authRequest = new NetAuthRequest()
|
||||
{
|
||||
Username = _username,
|
||||
Password = _password
|
||||
};
|
||||
|
||||
NetworkClient.Instance.SendServer(authRequest);
|
||||
}
|
||||
|
||||
|
|
@ -118,5 +135,14 @@ namespace _Hub.Scripts.UI
|
|||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Callbacks
|
||||
|
||||
private void OnServerConnected()
|
||||
{
|
||||
_isConnected = true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
8
Playvoi.Client/Assets/_Network/Shared/Enum.meta
Normal file
8
Playvoi.Client/Assets/_Network/Shared/Enum.meta
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: fbb8907d6f34c354aa4f0e75c161f93e
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -3,11 +3,13 @@
|
|||
public enum PacketType : byte
|
||||
{
|
||||
#region ClientServer
|
||||
|
||||
Invalid = 0,
|
||||
AuthRequest = 1,
|
||||
#endregion
|
||||
|
||||
#region ServerClient
|
||||
|
||||
OnAuth = 100,
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
8
Playvoi.Client/Assets/_Network/Shared/Interface.meta
Normal file
8
Playvoi.Client/Assets/_Network/Shared/Interface.meta
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ee61b43b2afff504abea4055e83605ff
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
using LiteNetLib.Utils;
|
||||
|
||||
namespace _Network.Shared.Interface
|
||||
{
|
||||
public interface INetPacket : INetSerializable
|
||||
{
|
||||
PacketType Type { get; }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: ab288e842f62469788a2b0811c294220
|
||||
timeCreated: 1768665506
|
||||
8
Playvoi.Client/Assets/_Network/Shared/Packets.meta
Normal file
8
Playvoi.Client/Assets/_Network/Shared/Packets.meta
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2b754838f9447ee4b9cc708148d658e2
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 89251cdbef042a34c999fc1bb6f5ff08
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
using _Network.Shared.Interface;
|
||||
using LiteNetLib.Utils;
|
||||
|
||||
namespace _Network.Shared.Packets.ClientServer
|
||||
{
|
||||
public class NetAuthRequest : INetPacket
|
||||
{
|
||||
public PacketType Type => PacketType.AuthRequest;
|
||||
public string Username { get; set; }
|
||||
public string Password { get; set; }
|
||||
|
||||
public void Serialize(NetDataWriter writer)
|
||||
{
|
||||
writer.Put((byte)Type);
|
||||
writer.Put(Username);
|
||||
writer.Put(Password);
|
||||
}
|
||||
|
||||
public void Deserialize(NetDataReader reader)
|
||||
{
|
||||
Username = reader.GetString();
|
||||
Password = reader.GetString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 140b105efcb547b9b2569a8397d1b5c3
|
||||
timeCreated: 1768665577
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: d0c882ddf9c547449a598cd104ab03e3
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Binary file not shown.
Binary file not shown.
|
|
@ -13,7 +13,7 @@ using System.Reflection;
|
|||
[assembly: System.Reflection.AssemblyCompanyAttribute("Playvoi.Server")]
|
||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Release")]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+ab729d1a2f9f4195576893a393b5d11c62f69313")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+b235ed3cf822640f9cb6664137db4bc06b729f93")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("Playvoi.Server")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("Playvoi.Server")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
98eff605502050ea012292e318729166e39cf9aa8ce58a266dabc44ef5c20548
|
||||
4bbd7f6de641cc43d39e9f91fda78ddd9b82cd18e65412f51bc2c09be924e288
|
||||
|
|
|
|||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
|
@ -1 +1 @@
|
|||
17686021595132556
|
||||
17686021599175449
|
||||
Loading…
Reference in a new issue