This commit is contained in:
PC-20230316NUNE\Administrator
2024-09-29 20:30:29 +08:00
parent c5700ce655
commit a16d51d033
151 changed files with 69 additions and 63 deletions

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2a1ee7e98b494ccfadfd9ee103715117
timeCreated: 1723691610

View File

@@ -0,0 +1,11 @@
namespace AppGame.Systems.CServer
{
/// <summary>
/// 客户端角色
/// </summary>
public enum JNGClientRole : int
{
Player, //玩家
SlaveServer //从服务器
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 25def782de754dbfb15c0b04eaa50e66
timeCreated: 1724642638

View File

@@ -0,0 +1,101 @@
using System.Net;
using System.Threading.Tasks;
using AppGame.Systems.CServer;
using Cysharp.Threading.Tasks;
using DotRecast.Core.Collections;
using JNGame.Network;
using Plugins.JNGame.Network;
using Plugins.JNGame.Network.Action;
namespace AppGame.Systems
{
public class JNGClient : JNTCPClient
{
private string _point;
private int _clientId;
public int ClientId => _clientId;
/// <summary>
/// 区块服务器标识
/// </summary>
private string _tileServer;
public string TileServer => _tileServer;
public void BindID(int clientId)
{
_clientId = clientId;
}
public void SetPoint(string point)
{
_point = point;
}
public void SetTileServer(string tileServer)
{
_tileServer = tileServer;
}
protected override async UniTask<string> GetEndPoint()
{
await UniTask.NextFrame();
return _point;
}
public override async Task OnInit()
{
//监听服务端事件
AddListener((int)NActionEnum.NSyncStateDataUpdate,OnNSyncStateDataUpdate);
AddListener((int)NActionEnum.LocalClientConnect,OnClientConnect);
AddListener((int)NActionEnum.NSyncTileAllUpdateBack,OnNSyncTileAllUpdateBack);
//连接
await base.OnInit();
}
private void OnClientConnect(byte[] obj)
{
//向服务器发送玩家Id
Send((int)GActionEnum.BindClientID,new GBindClientID()
{
ClientId = ClientId
});
//向服务器绑定角色
Send((int)GActionEnum.BindClientRole,new GBindClientRole()
{
Role = (int)JNGClientRole.Player
});
//向服务器索要全量信息
Send((int)NActionEnum.NSyncTileAllUpdate);
}
private void OnNSyncStateDataUpdate(byte[] data)
{
var info = JNStateItemData.Parser.ParseFrom(data);
App.Game.SyncState(info,true,false);
}
/// <summary>
/// 状态同步全量回调
/// </summary>
/// <param name="obj"></param>
private void OnNSyncTileAllUpdateBack(byte[] data)
{
var allData = JNStateTileAllData.Parser.ParseFrom(data);
App.Game.ClearTileState(allData.TId);
//生效全局回调
allData.Data.Data.ForEach(child =>
{
App.Game.SyncState(child,true,false);
});
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: de07e6b56137456fb6464195f9e1941d
timeCreated: 1722426114

View File

@@ -0,0 +1,26 @@
using System.Threading.Tasks;
using Plugins.JNGame.Network.Group;
namespace AppGame.Systems
{
public class JNGClientGroup : JNClientGroup<JNGClient>
{
//玩家Id
private int clientId;
public int ClientID => clientId;
public override async Task OnInit()
{
clientId = (await App.GAPI.NSyncTileClientId).data;
await base.OnInit();
}
public override void AddClient(JNGClient client)
{
client.BindID(clientId);
base.AddClient(client);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: b74afb48ac594bbd8db4d8323f8c2b7f
timeCreated: 1723451509

View File

@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using JNGame.Network;
using Plugins.JNGame.Network;
namespace AppGame.Systems.CServer
{
public partial class JNGServer : JNTCPServer
{
//客户端绑定的Id
private Dictionary<string, int> ids = new();
//客户端角色
private Dictionary<string, JNGClientRole> _roles = new();
public Dictionary<string, JNGClientRole> Roles => _roles;
public void OnInit_Game()
{
AddListener((int)GActionEnum.BindClientID,OnBindClientID);
AddListener((int)GActionEnum.BindClientRole,OnBindClientRole);
}
/// <summary>
/// 绑定客户端Id
/// </summary>
/// <param name="obj"></param>
/// <exception cref="NotImplementedException"></exception>
private void OnBindClientID(JNServerParam args)
{
var message = GBindClientID.Parser.ParseFrom(args.Message);
ids[args.Client] = message.ClientId;
}
/// <summary>
/// 绑定客户端角色
/// </summary>
private void OnBindClientRole(JNServerParam args)
{
var message = GBindClientRole.Parser.ParseFrom(args.Message);
_roles[args.Client] = (JNGClientRole)message.Role;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 14c921a786654c3fb9bb74e0d0ba29bd
timeCreated: 1724642902

View File

@@ -0,0 +1,190 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using AppGame.Sync;
using Cysharp.Threading.Tasks;
using DotRecast.Core.Collections;
using Game.Logic.System;
using Google.Protobuf;
using JNGame.Network;
using JNGame.Sync.State.Tile;
using JNGame.Sync.System.Data;
using Plugins.JNGame.Network;
using Plugins.JNGame.Network.Action;
namespace AppGame.Systems.CServer
{
public partial class JNGServer : JNTCPServer
{
private int _index = 1;
private bool isInit = false;
public override async Task OnInit()
{
if (isInit) return;
isInit = true;
//监听服务端事件
AddListener((int)NActionEnum.NSyncFrameInput,OnNSyncFrameInput);
AddListener((int)NActionEnum.NSyncTileInput,OnNSyncTileInput);
AddListener((int)NActionEnum.NSyncTileAllUpdate,OnNSyncTileAllUpdate);
AddListener((int)NActionEnum.NSyncTileGetTileInfo,OnNSyncTileGetTileInfo);
AddListener((int)NActionEnum.LocalClientDisconnect,OnLocalClientDisconnect);
OnInit_Game();
//连接
await base.OnInit();
}
public override void OnClose()
{
isInit = false;
base.OnClose();
}
protected override async UniTask<int> GetPort()
{
return (await App.GAPI.NSyncTilePort).data;
}
/// <summary>
/// 接收帧数入
/// </summary>
/// <param name="args"></param>
private void OnNSyncFrameInput(JNServerParam args)
{
var inputs = JNFrameInputs.Parser.ParseFrom(args.Message);
var frame = new JNFrameInfo();
frame.Index = 0;
foreach (var input in inputs.Inputs)
{
frame.Messages.Add(input);
}
App.Game.AddInput(frame);
}
/// <summary>
/// 接收瓦片输入
/// </summary>
/// <param name="args"></param>
private void OnNSyncTileInput(JNServerParam args)
{
var inputs = JNStateTileInputs.Parser.ParseFrom(args.Message);
//只有绑定过ID 的客户端才可以执行操作
if (!ids.ContainsKey(args.Client)) return;
inputs.Message.Inputs.ForEach(child =>
{
child.ClientId = ids[args.Client];
});
App.Game.AddTileInput(inputs);
}
/// <summary>
/// 返回全量信息
/// </summary>
private void OnNSyncTileAllUpdate(JNServerParam args)
{
if (App.Game.Server is not JNSSTileServerService tileServer) return;
var allData = new JNStateTileAllData();
allData.TId = tileServer.TID;
allData.Data = new JNStateAllData();
//获取角色 根据角色 返回 全量数据
Roles.TryGetValue(args.Client, out var role);
tileServer.GetSystems<ISTileDataSystem>().ForEach(data =>
{
var item = new JNStateItemData();
allData.Data.Data.Add(item);
item.NetID = ((ISStateDataSystem)data).NetID;
Dictionary<ulong, byte[]> byteData;
switch (role)
{
case JNGClientRole.Player:
byteData = data.GetHostDataBytes();
break;
case JNGClientRole.SlaveServer:
byteData = data.GetHostDataBytesFilterSlave();
break;
default:
byteData = new ();
break;
}
byteData.ForEach(keyValue =>
{
item.Messages[keyValue.Key] = new JNStateData() { Data = ByteString.CopyFrom(keyValue.Value) };
});
});
Send(args.Client, (int)NActionEnum.NSyncTileAllUpdateBack,allData);
}
/// <summary>
/// 获取指定区块的全量信息
/// </summary>
private void OnNSyncTileGetTileInfo(JNServerParam args)
{
if (App.Game.Server is not JNGTileServerSystem tileServer) return;
var tileSpecify = NSyncTileGetTileInfoRequest.Parser.ParseFrom(args.Message);
var allData = new JNStateTileAllData();
allData.TId = tileServer.TID;
allData.Data = new JNStateAllData();
tileServer.GetSystems<ISTileDataSystem>().ForEach(data =>
{
var item = new JNStateItemData();
allData.Data.Data.Add(item);
item.NetID = ((ISStateDataSystem)data).NetID;
data.GetTileDataBytes(tileSpecify.TId).ForEach(keyValue =>
{
item.Messages[keyValue.Key] = new JNStateData() { Data = ByteString.CopyFrom(keyValue.Value) };
});
});
//返回消息
SendCallback(args.Client,args.MessageID,allData);
}
/// <summary>
/// 有客户端断开连接
/// </summary>
private void OnLocalClientDisconnect(JNServerParam args)
{
if (App.Game.Server is null) return;
//只有绑定过ID 的客户端才可以执行操作
if (!ids.ContainsKey(args.Client)) return;
App.Game.Server.GetSystems<DGBasisSystem>().ForEach(child =>
{
child.OnPlayerExitServer(ids[args.Client]);
});
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 4d29d51922a6446490e06c87636d2590
timeCreated: 1722426103

View File

@@ -0,0 +1,117 @@
using System.Threading.Tasks;
using AppGame.Systems.CServer;
using Cysharp.Threading.Tasks;
using DotRecast.Core.Collections;
using JNGame.Network;
using Plugins.JNGame.Network.Action;
namespace AppGame.Systems
{
public class JNGTileClient : JNTCPClient
{
private string _point;
/// <summary>
/// 区块服务器标识
/// </summary>
private string _tileServer;
public string TileServer => _tileServer;
private JNGClientRole _role;
public JNGClientRole Role => _role;
/// <summary>
/// 设置IP
/// </summary>
public void SetPoint(string point)
{
_point = point;
}
/// <summary>
/// 设置角色
/// </summary>
public void SetRole(JNGClientRole role)
{
_role = role;
}
public void SetTileServer(string tileServer)
{
_tileServer = tileServer;
}
protected override async UniTask<string> GetEndPoint()
{
await UniTask.NextFrame();
return _point;
}
public override async Task OnInit()
{
//监听服务端事件
AddListener((int)NActionEnum.NSyncStateDataUpdate,OnNSyncStateDataUpdate);
AddListener((int)NActionEnum.LocalClientConnect,OnClientConnect);
AddListener((int)NActionEnum.NSyncTileAllUpdateBack,OnNSyncTileAllUpdateBack);
//连接
await base.OnInit();
}
private void OnClientConnect(byte[] obj)
{
//向服务器绑定角色
Send((int)GActionEnum.BindClientRole,new GBindClientRole()
{
Role = (int)Role
});
//向服务器索要全量信息
Send((int)NActionEnum.NSyncTileAllUpdate);
}
private void OnNSyncStateDataUpdate(byte[] data)
{
var info = JNStateItemData.Parser.ParseFrom(data);
App.Game.SyncState(info,false,true);
}
/// <summary>
/// 状态同步全量回调
/// </summary>
/// <param name="obj"></param>
private void OnNSyncTileAllUpdateBack(byte[] data)
{
var allData = JNStateTileAllData.Parser.ParseFrom(data);
App.Game.ClearTileState(allData.TId);
//生效全局状态
allData.Data.Data.ForEach(child =>
{
App.Game.SyncState(child,false,true);
});
}
/// <summary>
/// 获取指定区块状态
/// </summary>
public async Task<JNStateTileAllData> NSyncTileGetTileInfo(int index)
{
var data = new NSyncTileGetTileInfoRequest()
{
TId = index
};
var result = await SendCallback((int)NActionEnum.NSyncTileGetTileInfo, data);
if (result is null) return null;
return JNStateTileAllData.Parser.ParseFrom(result);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 892255e4a0a4489784ba78fef9492936
timeCreated: 1723691588

View File

@@ -0,0 +1,142 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AppGame.Sync;
using JNGame.Sync.Frame;
using JNGame.Sync.State;
using JNGame.Sync.System.Data;
using JNGame.Sync.System.View;
using Plugins.JNGame.System;
namespace AppGame.Systems
{
public class JNGGame : SystemBase
{
private JNSyncDefaultService client;
public JNSyncDefaultService Client => client;
private JNSStateServerService server;
public JNSStateServerService Server => server;
public bool IsStartClient => client is not null && client.IsStartGame;
public bool IsStartServer => server is not null && server.IsStartGame;
public override async Task OnInit()
{
}
public override void OnClose()
{
base.OnClose();
client?.Dispose();
server?.Dispose();
}
/// <summary>
/// 运行同步类
/// </summary>
public T StartClient<T>() where T : JNSyncDefaultService,new()
{
client = new T();
client.Initialize();
client.TStartExecute();
return client as T;
}
public T StartServer<T>() where T : JNSStateServerService,new()
{
server = new T();
server.Initialize();
server.TStartExecute();
return server as T;
}
/// <summary>
/// 获取第一个客户端的输入类
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T GetInput<T>() where T : JNInputBase, new()
{
return Client.GetSystem<JNInputSystem>().Input<T>();
}
public T GetClient<T>() where T : JNSyncDefaultService
{
if (!IsStartClient) return null;
return client as T;
}
/// <summary>
/// 接收输入数据
/// </summary>
/// <param name="frame"></param>
public void AddInput(JNFrameInfo frame)
{
(client as JNGFrameSystem)?.AddFrame(frame);
(server as JNGStateServerSystem)?.AddInput(frame);
}
public void AddTileInput(JNStateTileInputs frame)
{
if (server is JNGTileServerSystem system1)
{
if (system1.TID == frame.TId || frame.TId == 0)
{
system1.AddInput(frame);
}
}
if (server is JNGTileSlaveServerSystem system2)
{
if (system2.TID == frame.TId || frame.TId == 0)
{
system2.AddInput(frame);
}
}
}
/// <summary>
/// 清除指定区域的状态
/// </summary>
public void ClearTileState(int index)
{
client?.GetSystems<ISTileDataSystem>().ForEach(child =>
{
child.ClearTileData(index);
});
server?.GetSystems<ISTileDataSystem>().ForEach(child =>
{
child.ClearTileData(index);
});
}
/// <summary>
/// 接收状态数据
/// </summary>
public void SyncState(JNStateItemData frame,bool isSyncClient,bool isSyncServer)
{
var message = new Dictionary<ulong, byte[]>();
foreach (var data in frame.Messages)
{
message.Add(data.Key,data.Value.Data.ToByteArray());
}
if (isSyncClient)
{
client?.GetSystems<ISStateDataSystem>().ForEach(child =>
{
if (child.NetID != frame.NetID) return;
child.OnInsertUBytes(message);
});
}
if (isSyncServer)
{
server?.GetSystems<ISStateDataSystem>().ForEach(child =>
{
if (child.NetID != frame.NetID) return;
child.OnInsertUBytes(message);
});
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: bf6b9c1fec19438fa9b636bc2e7e4df1
timeCreated: 1722495304

View File

@@ -0,0 +1,50 @@

using System.Threading.Tasks;
using AppGame;
using AppGame.Sync;
using Cysharp.Threading.Tasks;
using Plugins.JNGame.Network;
using Plugins.JNGame.Network.Action;
public class JNGSocket : JNSocket
{
public override async Task OnInit()
{
AddListener((int)NActionEnum.ServerClientDisconnect,OnServerClientDisconnect);
AddListener((int)NActionEnum.NAddTileServer,OnNAddTileServer);
await base.OnInit();
}
protected override async UniTask<string> GetUrl()
{
await UniTask.NextFrame();
return "ws://127.0.0.1:8080/websocket";
}
/// <summary>
/// 有客户端断开服务器连接
/// </summary>
/// <param name="obj"></param>
private void OnServerClientDisconnect(byte[] data)
{
var disconnect = JNClientDisconnect.Parser.ParseFrom(data);
//断开Tile服务器连接
(App.Game.Client as JNGTileClientSystem)?.RemoveSocket(disconnect.ClientId);
(App.Game.Server as JNGTileServerSystem)?.RemoveSocket(disconnect.ClientId);
(App.Game.Server as JNGTileSlaveServerSystem)?.RemoveSocket(disconnect.ClientId);
}
/// <summary>
/// 有新的Tile服务器
/// </summary>
/// <param name="obj"></param>
private void OnNAddTileServer(byte[] obj)
{
App.Event.Dispatch(GEvent.NetNewTileServer);
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: aa09b61b047f4c98bd0b5e3dc2fad6aa
timeCreated: 1721384427