|
|
@@ -7,6 +7,13 @@ using System.Threading.Tasks;
|
|
|
|
|
|
namespace ET
|
|
|
{
|
|
|
+ public enum NetworkProtocol
|
|
|
+ {
|
|
|
+ TCP,
|
|
|
+ KCP,
|
|
|
+ Websocket,
|
|
|
+ }
|
|
|
+
|
|
|
public enum NetOp: byte
|
|
|
{
|
|
|
AddService = 1,
|
|
|
@@ -14,7 +21,7 @@ namespace ET
|
|
|
OnAccept = 3,
|
|
|
OnRead = 4,
|
|
|
OnError = 5,
|
|
|
- GetOrCreate = 6,
|
|
|
+ CreateChannel = 6,
|
|
|
RemoveChannel = 7,
|
|
|
SendStream = 8,
|
|
|
SendMessage = 9,
|
|
|
@@ -33,21 +40,34 @@ namespace ET
|
|
|
|
|
|
public class NetServices: Singleton<NetServices>
|
|
|
{
|
|
|
- private readonly Dictionary<int, AService> services = new Dictionary<int, AService>();
|
|
|
-
|
|
|
- private readonly Queue<int> queue = new Queue<int>();
|
|
|
-
|
|
|
- // 初始化后不变,所以主线程,网络线程都可以读
|
|
|
- private readonly DoubleMap<Type, ushort> typeOpcode = new DoubleMap<Type, ushort>();
|
|
|
-
|
|
|
private readonly ConcurrentQueue<NetOperator> netThreadOperators = new ConcurrentQueue<NetOperator>();
|
|
|
private readonly ConcurrentQueue<NetOperator> mainThreadOperators = new ConcurrentQueue<NetOperator>();
|
|
|
|
|
|
- private readonly Dictionary<int, Action<long, IPEndPoint>> acceptCallback = new Dictionary<int, Action<long, IPEndPoint>>();
|
|
|
- private readonly Dictionary<int, Action<long, long, object>> readCallback = new Dictionary<int, Action<long, long, object>>();
|
|
|
- private readonly Dictionary<int, Action<long, int>> errorCallback = new Dictionary<int, Action<long, int>>();
|
|
|
+ public NetServices()
|
|
|
+ {
|
|
|
+ HashSet<Type> types = EventSystem.Instance.GetTypes(typeof (MessageAttribute));
|
|
|
+ foreach (Type type in types)
|
|
|
+ {
|
|
|
+ object[] attrs = type.GetCustomAttributes(typeof (MessageAttribute), false);
|
|
|
+ if (attrs.Length == 0)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ MessageAttribute messageAttribute = attrs[0] as MessageAttribute;
|
|
|
+ if (messageAttribute == null)
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ this.typeOpcode.Add(type, messageAttribute.Opcode);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
#region 线程安全
|
|
|
+
|
|
|
+ // 初始化后不变,所以主线程,网络线程都可以读
|
|
|
+ private readonly DoubleMap<Type, ushort> typeOpcode = new DoubleMap<Type, ushort>();
|
|
|
|
|
|
public ushort GetOpcode(Type type)
|
|
|
{
|
|
|
@@ -59,9 +79,10 @@ namespace ET
|
|
|
return this.typeOpcode.GetKeyByValue(opcode);
|
|
|
}
|
|
|
|
|
|
+ // 防止与内网进程号的ChannelId冲突,所以设置为一个大的随机数
|
|
|
public uint CreateRandomLocalConn()
|
|
|
{
|
|
|
- return (1u << 30) | RandomGenerator.RandUInt32();
|
|
|
+ return (1u << 31) | RandomGenerator.RandUInt32();
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
@@ -70,33 +91,18 @@ namespace ET
|
|
|
|
|
|
#region 主线程
|
|
|
|
|
|
+ private readonly Dictionary<int, Action<long, IPEndPoint>> acceptCallback = new Dictionary<int, Action<long, IPEndPoint>>();
|
|
|
+ private readonly Dictionary<int, Action<long, long, object>> readCallback = new Dictionary<int, Action<long, long, object>>();
|
|
|
+ private readonly Dictionary<int, Action<long, int>> errorCallback = new Dictionary<int, Action<long, int>>();
|
|
|
+
|
|
|
+ private int serviceIdGenerator;
|
|
|
+
|
|
|
// localConn放在低32bit
|
|
|
private long connectIdGenerater = int.MaxValue;
|
|
|
public long CreateConnectChannelId(uint localConn)
|
|
|
{
|
|
|
return (--this.connectIdGenerater << 32) | localConn;
|
|
|
}
|
|
|
-
|
|
|
- public NetServices()
|
|
|
- {
|
|
|
- HashSet<Type> types = EventSystem.Instance.GetTypes(typeof (MessageAttribute));
|
|
|
- foreach (Type type in types)
|
|
|
- {
|
|
|
- object[] attrs = type.GetCustomAttributes(typeof (MessageAttribute), false);
|
|
|
- if (attrs.Length == 0)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- MessageAttribute messageAttribute = attrs[0] as MessageAttribute;
|
|
|
- if (messageAttribute == null)
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- this.typeOpcode.Add(type, messageAttribute.Opcode);
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
public async Task<(uint, uint)> GetKChannelConn(int serviceId, long channelId)
|
|
|
{
|
|
|
@@ -124,10 +130,12 @@ namespace ET
|
|
|
this.netThreadOperators.Enqueue(netOperator);
|
|
|
}
|
|
|
|
|
|
- public void AddService(AService aService)
|
|
|
+ public int AddService(AService aService)
|
|
|
{
|
|
|
+ aService.Id = ++this.serviceIdGenerator;
|
|
|
NetOperator netOperator = new NetOperator() { Op = NetOp.AddService, ServiceId = aService.Id, ChannelId = 0, Object = aService };
|
|
|
this.netThreadOperators.Enqueue(netOperator);
|
|
|
+ return aService.Id;
|
|
|
}
|
|
|
|
|
|
public void RemoveService(int serviceId)
|
|
|
@@ -142,9 +150,9 @@ namespace ET
|
|
|
this.netThreadOperators.Enqueue(netOperator);
|
|
|
}
|
|
|
|
|
|
- public void GetChannel(int serviceId, long channelId, IPEndPoint address)
|
|
|
+ public void CreateChannel(int serviceId, long channelId, IPEndPoint address)
|
|
|
{
|
|
|
- NetOperator netOperator = new NetOperator() { Op = NetOp.GetOrCreate, ServiceId = serviceId, ChannelId = channelId, Object = address};
|
|
|
+ NetOperator netOperator = new NetOperator() { Op = NetOp.CreateChannel, ServiceId = serviceId, ChannelId = channelId, Object = address};
|
|
|
this.netThreadOperators.Enqueue(netOperator);
|
|
|
}
|
|
|
|
|
|
@@ -219,6 +227,9 @@ namespace ET
|
|
|
|
|
|
#region 网络线程
|
|
|
|
|
|
+ private readonly Dictionary<int, AService> services = new Dictionary<int, AService>();
|
|
|
+ private readonly Queue<int> queue = new Queue<int>();
|
|
|
+
|
|
|
private void Add(AService aService)
|
|
|
{
|
|
|
this.services[aService.Id] = aService;
|
|
|
@@ -268,10 +279,10 @@ namespace ET
|
|
|
this.Remove(op.ServiceId);
|
|
|
break;
|
|
|
}
|
|
|
- case NetOp.GetOrCreate:
|
|
|
+ case NetOp.CreateChannel:
|
|
|
{
|
|
|
AService service = this.Get(op.ServiceId);
|
|
|
- service.Get(op.ChannelId, op.Object as IPEndPoint);
|
|
|
+ service.Create(op.ChannelId, op.Object as IPEndPoint);
|
|
|
break;
|
|
|
}
|
|
|
case NetOp.RemoveChannel:
|