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

Commit a059f33

Browse files
committed
增加protobuf,简单封包
1 parent 09d2370 commit a059f33

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+5312
-9
lines changed

Assets/GameFramework/Network/KcpChannel.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,18 @@ public KcpChannel(int port,Action<byte[]> reveiveHandler)
6262

6363
_receiveMeesages = new Queue<byte[]>();
6464

65-
_udpClient.BeginReceive(ReceiveMessage, this);
65+
_udpClient.BeginReceive(UdpReceiveMessage, this);
6666

6767
Thread updataThread = new Thread(Update);
6868
updataThread.Start();
6969
updataThread.IsBackground = true;
7070
}
7171

72-
private void ReceiveMessage(IAsyncResult asyncCallback)
72+
private void UdpReceiveMessage(IAsyncResult asyncCallback)
7373
{
7474
byte[] datas = _udpClient.EndReceive(asyncCallback,ref _recEndPoint);
7575
_receiveMeesages.Enqueue(datas);
76-
_udpClient?.BeginReceive(ReceiveMessage,this);
76+
_udpClient?.BeginReceive(UdpReceiveMessage, this);
7777
}
7878

7979

@@ -97,8 +97,7 @@ public void Send(byte[] datas,bool toAll=false)
9797
_currentEndPoint = _targetEndPoinnt;
9898
_kcp?.Send(datas);
9999
}
100-
101-
100+
102101
private void Update()
103102
{
104103
while (true)
@@ -113,9 +112,9 @@ private void Update()
113112
for (var size = _kcp.PeekSize(); size > 0; size = _kcp.PeekSize())
114113
{
115114
var buffer = new byte[size];
116-
if (_kcp.Recv(buffer) > 0)
117-
{
118-
_reveiveHandler(buffer);
115+
if (_kcp.Recv(buffer)>0)
116+
{
117+
_reveiveHandler(buffer);
119118
}
120119
}
121120
}
@@ -125,6 +124,10 @@ private void Update()
125124
}
126125
}
127126

127+
//private void ReceiveMessage(byt)
128+
129+
130+
128131

129132
}
130133

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright>
3+
// Copyright (c) 2018 Zhang Yang. All rights reserved.
4+
// </copyright>
5+
// <describe> #Kcp 通信接口,简单封包+序列化# </describe>
6+
// <email> [email protected] </email>
7+
// <time> #2018年7月29日 21点05分# </time>
8+
//-----------------------------------------------------------------------
9+
using System;
10+
using System.Collections;
11+
using System.Collections.Generic;
12+
using System.IO;
13+
using Google.Protobuf;
14+
15+
namespace GameFramework.Taurus
16+
{
17+
public class KcpService
18+
{
19+
#region 属性
20+
private KcpChannel _kcpChannel;
21+
22+
private Queue<byte[]> _receiveDatas;
23+
24+
private List<byte> _lastReceiveDatas;
25+
26+
// private Action<ushort, byte[]> _receiveDataCallback;
27+
private Action<ushort, object> _receiveMessageCallback;
28+
29+
private ProtobufPacker _protobufPacker;
30+
#endregion
31+
32+
public KcpService(int port,Action<ushort, object> receiveMessageCallback)
33+
{
34+
_lastReceiveDatas = new List<byte>();
35+
_receiveDatas = new Queue<byte[]>();
36+
_kcpChannel = new KcpChannel(port, ReceiveData);
37+
_receiveMessageCallback = receiveMessageCallback;
38+
39+
_protobufPacker = new ProtobufPacker();
40+
}
41+
42+
public void SetTargetEndPoint(string ip, int port)
43+
{
44+
_kcpChannel?.Connect(ip, port);
45+
}
46+
47+
48+
public void Update()
49+
{
50+
while (_receiveDatas.Count > 0)
51+
{
52+
_lastReceiveDatas.AddRange(_receiveDatas.Dequeue());
53+
if (_lastReceiveDatas.Count > 4)
54+
{
55+
byte[] datas = _lastReceiveDatas.ToArray();
56+
int length = System.BitConverter.ToInt32(datas,0);
57+
if (_lastReceiveDatas.Count >=length+4)
58+
{
59+
ushort type =System.BitConverter.ToUInt16(datas, 2);
60+
byte[] messageData = _lastReceiveDatas.GetRange(6, length-2).ToArray();
61+
//_receiveDataCallback?.Invoke(type, messageData);
62+
_receiveMessageCallback?.Invoke(type, ReceiveMessage(type,messageData));
63+
_lastReceiveDatas.RemoveRange(0, length + 4);
64+
}
65+
}
66+
}
67+
}
68+
69+
public void SendMessage(object message)
70+
{
71+
byte[] messageData = _protobufPacker.ToBytes(message);
72+
MessageTypeCode typeCode = (MessageTypeCode)Enum.Parse(typeof(MessageTypeCode),message.GetType().ToString());
73+
byte[] typeData = BitConverter.GetBytes((ushort) typeCode);
74+
byte[] length = BitConverter.GetBytes(messageData.Length+2);
75+
byte[] result = new byte[messageData.Length+ 2 + 4];
76+
length.CopyTo(result, 0);
77+
typeData.CopyTo(result, 4);
78+
messageData.CopyTo(result, 6);
79+
_kcpChannel?.Send(result);
80+
}
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+
89+
90+
private void ReceiveData(byte[] data)
91+
{
92+
_receiveDatas.Enqueue(data);
93+
}
94+
95+
96+
97+
}
98+
}

Assets/GameFramework/Network/KcpService.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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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年7月29日 21点29分# </time>
8+
//-----------------------------------------------------------------------
9+
10+
using System;
11+
12+
namespace GameFramework.Taurus
13+
{
14+
/// <summary>
15+
/// 列举通信类型的类型
16+
/// 因为枚举不支持带.的字符串,所以建议继承Google.Protobuf.IMagess的类型不要加命名空间
17+
/// 暂时简化操作,后面可以再在通信类型上添加标记
18+
/// </summary>
19+
public enum MessageTypeCode: ushort
20+
{
21+
/// <summary>
22+
/// proto测试协议
23+
/// </summary>
24+
//ProtoTest=0,
25+
26+
}
27+
28+
}

Assets/GameFramework/Network/MessageTypeCode.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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright>
3+
// Copyright (c) 2018 Zhang Yang. All rights reserved.
4+
// </copyright>
5+
// <describe> #Protobuf的序列化的实现,后期再提接口# </describe>
6+
// <email> [email protected] </email>
7+
// <time> #2018年7月29日 21点28分# </time>
8+
//-----------------------------------------------------------------------
9+
10+
using System;
11+
using System.Collections;
12+
using System.Collections.Generic;
13+
using System.IO;
14+
using Google.Protobuf;
15+
16+
namespace GameFramework.Taurus
17+
{
18+
public class ProtobufPacker
19+
{
20+
21+
public byte[] ToBytes(object message)
22+
{
23+
IMessage msg = message as IMessage;
24+
return msg.ToByteArray();
25+
}
26+
27+
public object ToMessage(Type type, byte[] datas)
28+
{
29+
IMessage result = (IMessage) Activator.CreateInstance(type);
30+
result.MergeFrom(datas, 0, datas.Length);
31+
return result;
32+
}
33+
34+
}
35+
}

Assets/GameFramework/Network/ProtobufPacker.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/ThirdParty.meta

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

Assets/ThirdParty/Google.Protobuf.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#region Copyright notice and license
2+
// Protocol Buffers - Google's data interchange format
3+
// Copyright 2008 Google Inc. All rights reserved.
4+
// https://developers.google.com/protocol-buffers/
5+
//
6+
// Redistribution and use in source and binary forms, with or without
7+
// modification, are permitted provided that the following conditions are
8+
// met:
9+
//
10+
// * Redistributions of source code must retain the above copyright
11+
// notice, this list of conditions and the following disclaimer.
12+
// * Redistributions in binary form must reproduce the above
13+
// copyright notice, this list of conditions and the following disclaimer
14+
// in the documentation and/or other materials provided with the
15+
// distribution.
16+
// * Neither the name of Google Inc. nor the names of its
17+
// contributors may be used to endorse or promote products derived from
18+
// this software without specific prior written permission.
19+
//
20+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
#endregion
32+
33+
using System;
34+
35+
namespace Google.Protobuf
36+
{
37+
/// <summary>
38+
/// Provides a utility routine to copy small arrays much more quickly than Buffer.BlockCopy
39+
/// </summary>
40+
internal static class ByteArray
41+
{
42+
/// <summary>
43+
/// The threshold above which you should use Buffer.BlockCopy rather than ByteArray.Copy
44+
/// </summary>
45+
private const int CopyThreshold = 12;
46+
47+
/// <summary>
48+
/// Determines which copy routine to use based on the number of bytes to be copied.
49+
/// </summary>
50+
internal static void Copy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int count)
51+
{
52+
if (count > CopyThreshold)
53+
{
54+
Buffer.BlockCopy(src, srcOffset, dst, dstOffset, count);
55+
}
56+
else
57+
{
58+
int stop = srcOffset + count;
59+
for (int i = srcOffset; i < stop; i++)
60+
{
61+
dst[dstOffset++] = src[i];
62+
}
63+
}
64+
}
65+
66+
/// <summary>
67+
/// Reverses the order of bytes in the array
68+
/// </summary>
69+
internal static void Reverse(byte[] bytes)
70+
{
71+
for (int first = 0, last = bytes.Length - 1; first < last; first++, last--)
72+
{
73+
byte temp = bytes[first];
74+
bytes[first] = bytes[last];
75+
bytes[last] = temp;
76+
}
77+
}
78+
}
79+
}

Assets/ThirdParty/Google.Protobuf/ByteArray.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)