mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-27 10:46:17 +00:00
提交Unity 联机Pro
This commit is contained in:
111
JNFrame2/Assets/JNGame/Network/Netty/TCP/TcpClientHandler.cs
Normal file
111
JNFrame2/Assets/JNGame/Network/Netty/TCP/TcpClientHandler.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using System;
|
||||
using DotNetty.Buffers;
|
||||
using DotNetty.Transport.Channels;
|
||||
using JNGame.Util.Types;
|
||||
using Plugins.JNGame.Network;
|
||||
using Plugins.JNGame.Network.Action;
|
||||
using Plugins.JNGame.Network.Entity;
|
||||
using UnityEngine;
|
||||
|
||||
namespace JNGame.Network.Netty.TCP
|
||||
{
|
||||
public class TcpClientHandler : ChannelHandlerAdapter
|
||||
{
|
||||
|
||||
private JNClientBase root;
|
||||
|
||||
public TcpClientHandler(JNClientBase client)
|
||||
{
|
||||
root = client;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 逻辑处理器被添加
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public override void HandlerAdded(IChannelHandlerContext context)
|
||||
{
|
||||
base.HandlerAdded(context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 绑定到线程
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public override void ChannelRegistered(IChannelHandlerContext context)
|
||||
{
|
||||
base.ChannelRegistered(context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 准备就绪
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public override void ChannelActive(IChannelHandlerContext context)
|
||||
{
|
||||
|
||||
base.ChannelActive(context);
|
||||
Debug.Log($"[TcpClientHandler] 连接成功: {context.Channel.RemoteAddress}");
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 有数据可读
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="message"></param>
|
||||
public override void ChannelRead(IChannelHandlerContext context, object message)
|
||||
{
|
||||
|
||||
base.ChannelRead(context, message);
|
||||
|
||||
if (message is not JNetParam data) return;
|
||||
|
||||
root.Dispatch(data);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 某次数据读完
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public override void ChannelReadComplete(IChannelHandlerContext context)
|
||||
{
|
||||
base.ChannelReadComplete(context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 被关闭
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public override void ChannelInactive(IChannelHandlerContext context)
|
||||
{
|
||||
|
||||
base.ChannelInactive(context);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取消线程(NioEventLoop) 的绑定
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public override void ChannelUnregistered(IChannelHandlerContext context)
|
||||
{
|
||||
base.ChannelUnregistered(context);
|
||||
context.Channel.EventLoop.Schedule(() =>
|
||||
{
|
||||
Debug.Log($"重连接: {context.Channel.RemoteAddress}");
|
||||
root.StartConnect();
|
||||
}, new TimeSpan(1000));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 逻辑处理器被移除
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public override void HandlerRemoved(IChannelHandlerContext context)
|
||||
{
|
||||
base.HandlerRemoved(context);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8a6cf562019b4e4eaf5fae1b4bb2b19f
|
||||
timeCreated: 1723799944
|
@@ -0,0 +1,34 @@
|
||||
using DotNetty.Handlers.Logging;
|
||||
using DotNetty.Handlers.Timeout;
|
||||
using DotNetty.Transport.Channels;
|
||||
using DotNetty.Transport.Channels.Sockets;
|
||||
using JNGame.Network.Netty;
|
||||
using JNGame.Network.Netty.TCP;
|
||||
using Plugins.JNGame.Network;
|
||||
using TestNetty.Service.Handlers;
|
||||
|
||||
namespace TestNetty.Client.Initializers
|
||||
{
|
||||
public class TcpClientInitializer : ChannelInitializer<ISocketChannel>
|
||||
{
|
||||
|
||||
private JNClientBase root;
|
||||
private TcpClientHandler handler;
|
||||
|
||||
public TcpClientInitializer(JNClientBase server)
|
||||
{
|
||||
root = server;
|
||||
handler = new TcpClientHandler(root);
|
||||
}
|
||||
|
||||
protected override void InitChannel(ISocketChannel channel)
|
||||
{
|
||||
IChannelPipeline pipeline = channel.Pipeline;
|
||||
pipeline.AddLast(new IdleStateHandler(30, 30, 60 * 5));
|
||||
pipeline.AddLast(new HeartBeatHandler());
|
||||
pipeline.AddLast("encoder", new TcpEncoderHandler());
|
||||
pipeline.AddLast("decoder", new TcpDecoderHandler());
|
||||
pipeline.AddLast(handler);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 96db8fb3f65d44b798bc06f95a8246bb
|
||||
timeCreated: 1723799785
|
@@ -0,0 +1,36 @@
|
||||
using DotNetty.Buffers;
|
||||
using DotNetty.Codecs;
|
||||
using DotNetty.Transport.Channels;
|
||||
using System.Collections.Generic;
|
||||
using Plugins.JNGame.Network.Util;
|
||||
|
||||
namespace TestNetty.Service.Handlers
|
||||
{
|
||||
/// <summary>
|
||||
/// Decoder Packet
|
||||
/// </summary>
|
||||
public class TcpDecoderHandler : ByteToMessageDecoder
|
||||
{
|
||||
|
||||
//准备读取的消息长度
|
||||
private int? length;
|
||||
|
||||
protected override void Decode(IChannelHandlerContext context, IByteBuffer input, List<object> output)
|
||||
{
|
||||
|
||||
if (length is null && input.ReadableBytes >= 4)
|
||||
{
|
||||
length = input.ReadInt();
|
||||
}
|
||||
|
||||
if (length is not null && input.ReadableBytes >= length)
|
||||
{
|
||||
IByteBuffer result = input.ReadBytes(length.Value);
|
||||
output.Add(NDataUtil.Parse(result.Array));
|
||||
result.Clear();
|
||||
length = null;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9487ff44b5354bc4843ac7320680c10f
|
||||
timeCreated: 1723773916
|
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using DotNetty.Buffers;
|
||||
using DotNetty.Codecs;
|
||||
using DotNetty.Transport.Channels;
|
||||
using Plugins.JNGame.Network.Entity;
|
||||
using Plugins.JNGame.Network.Util;
|
||||
|
||||
namespace TestNetty.Service.Handlers
|
||||
{
|
||||
/// <summary>
|
||||
/// Encoder Packet
|
||||
/// </summary>
|
||||
public class TcpEncoderHandler : MessageToByteEncoder<JNetParam>
|
||||
{
|
||||
protected override void Encode(IChannelHandlerContext context, JNetParam message, IByteBuffer output)
|
||||
{
|
||||
var data = NDataUtil.Encrypt(message);
|
||||
output.WriteInt(data.Length);//4-8
|
||||
output.WriteBytes(data);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 11f81d1e8543432c8226b6c42b605010
|
||||
timeCreated: 1723773916
|
11
JNFrame2/Assets/JNGame/Network/Netty/TCP/TcpPacket.cs
Normal file
11
JNFrame2/Assets/JNGame/Network/Netty/TCP/TcpPacket.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using Plugins.JNGame.Network.Entity;
|
||||
|
||||
namespace JNGame.Network.Netty.TCP
|
||||
{
|
||||
public class TcpPacket
|
||||
{
|
||||
public int Checkbit;
|
||||
public int Length;
|
||||
public JNetParam Data;
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b5008afe7a554eafb1c684bef86362b0
|
||||
timeCreated: 1723774619
|
154
JNFrame2/Assets/JNGame/Network/Netty/TCP/TcpServerHandler.cs
Normal file
154
JNFrame2/Assets/JNGame/Network/Netty/TCP/TcpServerHandler.cs
Normal file
@@ -0,0 +1,154 @@
|
||||
using System;
|
||||
using DotNetty.Transport.Channels;
|
||||
using JNGame.Util.Types;
|
||||
using Plugins.JNGame.Network;
|
||||
using Plugins.JNGame.Network.Action;
|
||||
using Plugins.JNGame.Network.Entity;
|
||||
using UnityEngine;
|
||||
|
||||
namespace JNGame.Network.Netty.TCP
|
||||
{
|
||||
public class TcpServerHandler : ChannelHandlerAdapter
|
||||
{
|
||||
|
||||
private int _index = 0;
|
||||
public int Next()
|
||||
{
|
||||
return _index++;
|
||||
}
|
||||
|
||||
private JNServerBase root;
|
||||
|
||||
public readonly KeyValue<IChannelHandlerContext, int> ClientInts = new();
|
||||
|
||||
public TcpServerHandler(JNServerBase server)
|
||||
{
|
||||
root = server;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 逻辑处理器被添加
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public override void HandlerAdded(IChannelHandlerContext context)
|
||||
{
|
||||
base.HandlerAdded(context);
|
||||
ClientInts.Add(context,Next());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 绑定到线程
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public override void ChannelRegistered(IChannelHandlerContext context)
|
||||
{
|
||||
base.ChannelRegistered(context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 准备就绪
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public override void ChannelActive(IChannelHandlerContext context)
|
||||
{
|
||||
|
||||
base.ChannelActive(context);
|
||||
|
||||
if (!(ClientInts.TryGetValueByKey(context,out var id)))
|
||||
{
|
||||
context.CloseAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
Debug.Log($"[TcpServerHandler] 连接成功: {context.Channel.RemoteAddress}");
|
||||
|
||||
//客户端连接
|
||||
root.Dispatch((int)NActionEnum.ClientConnect,new JNServerParam()
|
||||
{
|
||||
Client = id,
|
||||
Message = Array.Empty<byte>()
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 有数据可读
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="message"></param>
|
||||
public override void ChannelRead(IChannelHandlerContext context, object message)
|
||||
{
|
||||
|
||||
base.ChannelRead(context, message);
|
||||
|
||||
if (message is not JNetParam data) return;
|
||||
|
||||
if (!(ClientInts.TryGetValueByKey(context,out var id)))
|
||||
{
|
||||
context.CloseAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
root.Dispatch(data.HId,new JNServerParam()
|
||||
{
|
||||
Client = id,
|
||||
Message = data.Bytes
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 某次数据读完
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public override void ChannelReadComplete(IChannelHandlerContext context)
|
||||
{
|
||||
base.ChannelReadComplete(context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 被关闭
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public override void ChannelInactive(IChannelHandlerContext context)
|
||||
{
|
||||
|
||||
base.ChannelInactive(context);
|
||||
|
||||
if (!(ClientInts.TryGetValueByKey(context,out var id)))
|
||||
{
|
||||
context.CloseAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
Debug.Log($"[TcpServerHandler] 断开连接: {context.Channel.RemoteAddress}");
|
||||
|
||||
//客户端断开
|
||||
root.Dispatch((int)NActionEnum.ClientDisconnect,new JNServerParam()
|
||||
{
|
||||
Client = id,
|
||||
Message = Array.Empty<byte>()
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取消线程(NioEventLoop) 的绑定
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public override void ChannelUnregistered(IChannelHandlerContext context)
|
||||
{
|
||||
base.ChannelUnregistered(context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 逻辑处理器被移除
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public override void HandlerRemoved(IChannelHandlerContext context)
|
||||
{
|
||||
base.HandlerRemoved(context);
|
||||
ClientInts.RemoveByKey(context);
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 70f2f7800eb14ae189df47eb04e775aa
|
||||
timeCreated: 1723776097
|
@@ -0,0 +1,42 @@
|
||||
using DotNetty.Handlers.Timeout;
|
||||
using DotNetty.Transport.Channels;
|
||||
using DotNetty.Transport.Channels.Sockets;
|
||||
using Plugins.JNGame.Network;
|
||||
using TestNetty.Service.Handlers;
|
||||
|
||||
namespace JNGame.Network.Netty.TCP
|
||||
{
|
||||
public class TcpServerInitializer : ChannelInitializer<ISocketChannel>
|
||||
{
|
||||
|
||||
private JNServerBase root;
|
||||
private TcpServerHandler handler;
|
||||
|
||||
public TcpServerInitializer(JNServerBase server)
|
||||
{
|
||||
root = server;
|
||||
handler = new TcpServerHandler(root);
|
||||
}
|
||||
|
||||
protected override void InitChannel(ISocketChannel channel)
|
||||
{
|
||||
IChannelPipeline pipeline = channel.Pipeline;
|
||||
pipeline.AddLast(new IdleStateHandler(30,30,60 * 5));//心跳
|
||||
pipeline.AddLast(new HeartBeatHandler());
|
||||
pipeline.AddLast("encoder", new TcpEncoderHandler());
|
||||
pipeline.AddLast("decoder", new TcpDecoderHandler());
|
||||
pipeline.AddLast(handler);
|
||||
}
|
||||
|
||||
public IChannelHandlerContext GetClient(int index)
|
||||
{
|
||||
return handler.ClientInts.Value2Key(index);
|
||||
}
|
||||
|
||||
public int[] GetClients()
|
||||
{
|
||||
return handler.ClientInts.Values;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5c734c16eb6b4ff98f41bb63524f3209
|
||||
timeCreated: 1723773874
|
Reference in New Issue
Block a user