Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit f653553

Browse files
committed
增加网络消息的处理类
1 parent 00d659f commit f653553

22 files changed

+467
-72
lines changed

Assets/Game/Scripts/Base/GameMode.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public class GameMode : MonoBehaviour
2525
public static AudioManager Audio;
2626
public static LocalizationManager Localization;
2727
public static SettingManager Setting;
28+
public static SystemManager System;
29+
public static NetworkManager Network;
2830

2931
/// <summary>
3032
/// 当前程序集
@@ -72,10 +74,12 @@ IEnumerator Start()
7274
Audio = GameFrameworkMode.GetModule<AudioManager>();
7375
Localization = GameFrameworkMode.GetModule<LocalizationManager>();
7476
Setting = GameFrameworkMode.GetModule<SettingManager>();
75-
#endregion
77+
System= GameFrameworkMode.GetModule<SystemManager>();
78+
Network= GameFrameworkMode.GetModule<NetworkManager>();
79+
#endregion
7680

77-
#region resource
78-
Resource.ResUpdateType = ResUpdateType;
81+
#region resource
82+
Resource.ResUpdateType = ResUpdateType;
7983
Resource.ResUpdatePath = ResUpdatePath;
8084
Resource.LocalPathType = LocalPathType;
8185

Assets/GameFramework/Base/UnityGameFrameworkException.cs renamed to Assets/GameFramework/Base/GamekException.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
namespace GameFramework.Taurus
1616
{
1717
[Serializable]
18-
public class UnityGameFrameworkException : Exception
18+
public class GamekException : Exception
1919
{
20-
public UnityGameFrameworkException()
20+
public GamekException()
2121
: base()
2222
{
2323

@@ -27,7 +27,7 @@ public UnityGameFrameworkException()
2727
/// 使用指定错误消息初始化游戏框架异常类的新实例。
2828
/// </summary>
2929
/// <param name="message">描述错误的消息</param>
30-
public UnityGameFrameworkException(string message)
30+
public GamekException(string message)
3131
: base(message)
3232
{
3333

@@ -38,7 +38,7 @@ public UnityGameFrameworkException(string message)
3838
/// </summary>
3939
/// <param name="message">解释异常原因的错误消息。</param>
4040
/// <param name="innerException">导致当前异常的异常。如果 innerException 参数不为空引用,则在处理内部异常的 catch 块中引发当前异常。</param>
41-
public UnityGameFrameworkException(string message, Exception innerException)
41+
public GamekException(string message, Exception innerException)
4242
: base(message, innerException)
4343
{
4444

@@ -49,7 +49,7 @@ public UnityGameFrameworkException(string message, Exception innerException)
4949
/// </summary>
5050
/// <param name="info">存有有关所引发异常的序列化的对象数据。</param>
5151
/// <param name="context">包含有关源或目标的上下文信息。</param>
52-
protected UnityGameFrameworkException(SerializationInfo info, StreamingContext context)
52+
protected GamekException(SerializationInfo info, StreamingContext context)
5353
: base(info, context)
5454
{
5555

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright>
3+
// Copyright (c) 2018 Zhang Yang. All rights reserved.
4+
// </copyright>
5+
// <describe> #系统管理类# </describe>
6+
// <email> [email protected] </email>
7+
// <time> #2018年8月4日 14点52分# </time>
8+
//-----------------------------------------------------------------------
9+
10+
using System;
11+
using System.Collections;
12+
using System.Collections.Generic;
13+
using System.Reflection;
14+
15+
namespace GameFramework.Taurus
16+
{
17+
public sealed class SystemManager:GameFrameworkModule
18+
{
19+
public Assembly GetAssembly { get; private set; }
20+
public Type[] GetTypes { get; private set; }
21+
22+
public SystemManager()
23+
{
24+
GetAssembly = typeof(SystemManager).Assembly;
25+
GetTypes = GetAssembly.GetTypes();
26+
}
27+
28+
public override void OnClose()
29+
{
30+
31+
}
32+
}
33+
}

Assets/GameFramework/Base/SystemManager.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright>
3+
// Copyright (c) 2018 Zhang Yang. All rights reserved.
4+
// </copyright>
5+
// <describe> #Message请求接口# </describe>
6+
// <email> [email protected] </email>
7+
// <time> #2018年8月4日 15点57分# </time>
8+
//-----------------------------------------------------------------------
9+
10+
using System.Collections;
11+
using System.Collections.Generic;
12+
13+
namespace GameFramework.Taurus
14+
{
15+
public interface IRequest
16+
{
17+
int RpcId { get; set; }
18+
}
19+
}

Assets/GameFramework/Network/IRequest.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright>
3+
// Copyright (c) 2018 Zhang Yang. All rights reserved.
4+
// </copyright>
5+
// <describe> #Message响应接口# </describe>
6+
// <email> [email protected] </email>
7+
// <time> #2018年8月4日 15点57分# </time>
8+
//-----------------------------------------------------------------------
9+
10+
using System.Collections;
11+
using System.Collections.Generic;
12+
13+
namespace GameFramework.Taurus
14+
{
15+
public interface IResponse
16+
{
17+
int RpcId { get; set; }
18+
}
19+
}

Assets/GameFramework/Network/IResponse.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/GameFramework/Network/KcpChannel.cs

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.Net;
1414
using System.Net.Sockets;
1515
using System.Threading;
16+
using System.Threading.Tasks;
1617

1718
namespace GameFramework.Taurus
1819
{
@@ -31,23 +32,17 @@ public static UInt32 iclock()
3132

3233
private UdpClient _udpClient;
3334

34-
private IPEndPoint _targetEndPoinnt;
35-
36-
private IPEndPoint _allEndPoint;
37-
38-
private IPEndPoint _currentEndPoint;
39-
4035
private IPEndPoint _recEndPoint;
4136

37+
private IPEndPoint _currentEndPoint;
38+
4239
private Queue<byte[]> _receiveMeesages;
4340

4441
private Action<byte[]> _reveiveHandler;
4542
#endregion
46-
47-
43+
4844
public KcpChannel(int port,Action<byte[]> reveiveHandler)
4945
{
50-
_allEndPoint = new IPEndPoint(IPAddress.Parse("255.255.255.255"), port);
5146
_recEndPoint = new IPEndPoint(IPAddress.Any, 0);
5247

5348
_reveiveHandler = reveiveHandler;
@@ -63,10 +58,8 @@ public KcpChannel(int port,Action<byte[]> reveiveHandler)
6358
_receiveMeesages = new Queue<byte[]>();
6459

6560
_udpClient.BeginReceive(UdpReceiveMessage, this);
66-
67-
Thread updataThread = new Thread(Update);
68-
updataThread.Start();
69-
updataThread.IsBackground = true;
61+
62+
new Task(Update).Start();
7063
}
7164

7265
private void UdpReceiveMessage(IAsyncResult asyncCallback)
@@ -75,26 +68,20 @@ private void UdpReceiveMessage(IAsyncResult asyncCallback)
7568
_receiveMeesages.Enqueue(datas);
7669
_udpClient?.BeginReceive(UdpReceiveMessage, this);
7770
}
78-
79-
80-
public void Connect(string ip, int port)
81-
{
82-
_targetEndPoinnt = new IPEndPoint(IPAddress.Parse(ip), port);
83-
_currentEndPoint = _targetEndPoinnt;
84-
}
71+
8572

8673
private void UdpSendData(byte[] datas, int length)
8774
{
8875
if(_currentEndPoint!=null)
8976
_udpClient?.Send(datas, length, _currentEndPoint);
9077
}
9178

92-
public void Send(byte[] datas,bool toAll=false)
79+
public void Send(byte[] datas,IPEndPoint endPoint)
9380
{
94-
if (toAll)
95-
_currentEndPoint = _allEndPoint;
96-
else
97-
_currentEndPoint = _targetEndPoinnt;
81+
if (endPoint == null)
82+
return;
83+
_currentEndPoint = endPoint;
84+
9885
_kcp?.Send(datas);
9986
}
10087

@@ -123,11 +110,7 @@ private void Update()
123110
Thread.Sleep(10);
124111
}
125112
}
126-
127-
//private void ReceiveMessage(byt)
128-
129-
130-
113+
131114

132115
}
133116

Assets/GameFramework/Network/KcpService.cs

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Collections;
1111
using System.Collections.Generic;
1212
using System.IO;
13+
using System.Net;
1314
using Google.Protobuf;
1415

1516
namespace GameFramework.Taurus
@@ -23,27 +24,18 @@ public class KcpService
2324

2425
private List<byte> _lastReceiveDatas;
2526

26-
// private Action<ushort, byte[]> _receiveDataCallback;
27-
private Action<ushort, object> _receiveMessageCallback;
27+
private Action<ushort, byte[]> _receiveDataCallback;
2828

29-
private ProtobufPacker _protobufPacker;
3029
#endregion
3130

32-
public KcpService(int port,Action<ushort, object> receiveMessageCallback)
31+
public KcpService(int port,Action<ushort, byte[]> receiveDataCallback)
3332
{
3433
_lastReceiveDatas = new List<byte>();
3534
_receiveDatas = new Queue<byte[]>();
3635
_kcpChannel = new KcpChannel(port, ReceiveData);
37-
_receiveMessageCallback = receiveMessageCallback;
38-
39-
_protobufPacker = new ProtobufPacker();
36+
_receiveDataCallback = receiveDataCallback;
4037
}
41-
42-
public void SetTargetEndPoint(string ip, int port)
43-
{
44-
_kcpChannel?.Connect(ip, port);
45-
}
46-
38+
4739

4840
public void Update()
4941
{
@@ -56,36 +48,26 @@ public void Update()
5648
int length = System.BitConverter.ToInt32(datas,0);
5749
if (_lastReceiveDatas.Count >=length+4)
5850
{
59-
ushort type =System.BitConverter.ToUInt16(datas, 2);
51+
ushort type =System.BitConverter.ToUInt16(datas, 4);
6052
byte[] messageData = _lastReceiveDatas.GetRange(6, length-2).ToArray();
61-
//_receiveDataCallback?.Invoke(type, messageData);
62-
_receiveMessageCallback?.Invoke(type, ReceiveMessage(type,messageData));
53+
_receiveDataCallback?.Invoke(type, messageData);
6354
_lastReceiveDatas.RemoveRange(0, length + 4);
6455
}
6556
}
6657
}
6758
}
6859

69-
public void SendMessage(object message)
60+
public void SendMessage(ushort typeCode, byte[] messageData,IPEndPoint endPoint)
7061
{
71-
byte[] messageData = _protobufPacker.ToBytes(message);
72-
MessageTypeCode typeCode = (MessageTypeCode)Enum.Parse(typeof(MessageTypeCode),message.GetType().ToString());
73-
byte[] typeData = BitConverter.GetBytes((ushort) typeCode);
62+
byte[] typeData = BitConverter.GetBytes(typeCode);
7463
byte[] length = BitConverter.GetBytes(messageData.Length+2);
7564
byte[] result = new byte[messageData.Length+ 2 + 4];
7665
length.CopyTo(result, 0);
7766
typeData.CopyTo(result, 4);
7867
messageData.CopyTo(result, 6);
79-
_kcpChannel?.Send(result);
68+
_kcpChannel?.Send(result, endPoint);
8069
}
81-
82-
private object ReceiveMessage(ushort typeCode,byte[] data)
83-
{
84-
MessageTypeCode code = (MessageTypeCode) typeCode;
85-
86-
return _protobufPacker.ToMessage(Type.GetType(code.ToString()),data);
87-
}
88-
70+
8971

9072
private void ReceiveData(byte[] data)
9173
{
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright>
3+
// Copyright (c) 2018 Zhang Yang. All rights reserved.
4+
// </copyright>
5+
// <describe> #消息发送类标记# </describe>
6+
// <email> [email protected] </email>
7+
// <time> #2018年8月4日 14点42分# </time>
8+
//-----------------------------------------------------------------------
9+
10+
using System.Collections;
11+
using System.Collections.Generic;
12+
using System;
13+
14+
namespace GameFramework.Taurus
15+
{
16+
public class MessageAttribute : Attribute
17+
{
18+
public ushort TypeCode { get; private set; }
19+
20+
public MessageAttribute(ushort typeCode)
21+
{
22+
TypeCode = typeCode;
23+
}
24+
}
25+
26+
}

Assets/GameFramework/Network/MessageAttribute.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)