1 Star 0 Fork 0

starlove/kbengine_unity3d_plugins

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Bundle.cs 4.10 KB
一键复制 编辑 原始数据 按行查看 历史
kbengine 提交于 2017-01-18 17:20 . up
namespace KBEngine
{
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
/*
这个模块将多个数据包打捆在一起
由于每个数据包都有最大上限, 向Bundle中写入大量数据将会在内部产生多个MemoryStream
在send时会全部发送出去
*/
public class Bundle : ObjectPool<Bundle>
{
public MemoryStream stream = new MemoryStream();
public List<MemoryStream> streamList = new List<MemoryStream>();
public int numMessage = 0;
public int messageLength = 0;
public Message msgtype = null;
private int _curMsgStreamIndex = 0;
public Bundle()
{
}
public void clear()
{
stream = MemoryStream.createObject();
streamList = new List<MemoryStream>();
numMessage = 0;
messageLength = 0;
msgtype = null;
_curMsgStreamIndex = 0;
}
/// <summary>
/// 把自己放回缓冲池
/// </summary>
public void reclaimObject()
{
clear();
reclaimObject(this);
}
public void newMessage(Message mt)
{
fini(false);
msgtype = mt;
numMessage += 1;
writeUint16(msgtype.id);
if(msgtype.msglen == -1)
{
writeUint16(0);
messageLength = 0;
}
_curMsgStreamIndex = 0;
}
public void writeMsgLength()
{
if(msgtype.msglen != -1)
return;
MemoryStream stream = this.stream;
if(_curMsgStreamIndex > 0)
{
stream = streamList[streamList.Count - _curMsgStreamIndex];
}
stream.data()[2] = (Byte)(messageLength & 0xff);
stream.data()[3] = (Byte)(messageLength >> 8 & 0xff);
}
public void fini(bool issend)
{
if(numMessage > 0)
{
writeMsgLength();
streamList.Add(stream);
stream = MemoryStream.createObject();
}
if(issend)
{
numMessage = 0;
msgtype = null;
}
_curMsgStreamIndex = 0;
}
public void send(NetworkInterface networkInterface)
{
fini(true);
if(networkInterface.valid())
{
for(int i=0; i<streamList.Count; i++)
{
MemoryStream tempStream = streamList[i];
networkInterface.send(tempStream);
}
}
else
{
Dbg.ERROR_MSG("Bundle::send: networkInterface invalid!");
}
// 把不用的MemoryStream放回缓冲池,以减少垃圾回收的消耗
for (int i = 0; i < streamList.Count; ++i)
{
streamList[i].reclaimObject();
}
streamList.Clear();
stream.clear();
// 我们认为,发送完成,就视为这个bundle不再使用了,
// 所以我们会把它放回对象池,以减少垃圾回收带来的消耗,
// 如果需要继续使用,应该重新Bundle.createObject(),
// 如果外面不重新createObject()而直接使用,就可能会出现莫名的问题,
// 仅以此备注,警示使用者。
Bundle.reclaimObject(this);
}
public void checkStream(int v)
{
if(v > stream.space())
{
streamList.Add(stream);
stream = MemoryStream.createObject();
++ _curMsgStreamIndex;
}
messageLength += v;
}
//---------------------------------------------------------------------------------
public void writeInt8(SByte v)
{
checkStream(1);
stream.writeInt8(v);
}
public void writeInt16(Int16 v)
{
checkStream(2);
stream.writeInt16(v);
}
public void writeInt32(Int32 v)
{
checkStream(4);
stream.writeInt32(v);
}
public void writeInt64(Int64 v)
{
checkStream(8);
stream.writeInt64(v);
}
public void writeUint8(Byte v)
{
checkStream(1);
stream.writeUint8(v);
}
public void writeUint16(UInt16 v)
{
checkStream(2);
stream.writeUint16(v);
}
public void writeUint32(UInt32 v)
{
checkStream(4);
stream.writeUint32(v);
}
public void writeUint64(UInt64 v)
{
checkStream(8);
stream.writeUint64(v);
}
public void writeFloat(float v)
{
checkStream(4);
stream.writeFloat(v);
}
public void writeDouble(double v)
{
checkStream(8);
stream.writeDouble(v);
}
public void writeString(string v)
{
checkStream(v.Length + 1);
stream.writeString(v);
}
public void writeBlob(byte[] v)
{
checkStream(v.Length + 4);
stream.writeBlob(v);
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/starlove/kbengine_unity3d_plugins.git
git@gitee.com:starlove/kbengine_unity3d_plugins.git
starlove
kbengine_unity3d_plugins
kbengine_unity3d_plugins
master

搜索帮助