1 Star 0 Fork 45

code2758/GameServerCsharp

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ServerBundle.cs 2.47 KB
一键复制 编辑 原始数据 按行查看 历史
liyonghelpme 提交于 2016-03-27 13:09 +08:00 . adjust NameSpace
using System.Collections;
using Google.ProtocolBuffers;
using System.Collections.Generic;
using System;
namespace MyLib
{
public class ServerBundle
{
static System.UInt32 serverPushFlowId = 0;
KBEngine.MemoryStream stream = new KBEngine.MemoryStream();
public int messageLength = 0;
public KBEngine.Message msgtype = null;
public int moduleId;
public int msgId;
public System.UInt32 flowId;
void newMessage(System.Type type) {
Debug.Log ("ServerBundle:: 开始发送消息 Message is " + type.Name);
var pa = Util.GetMsgID (type.Name);
if(pa == null) {
Debug.LogError("GetMessage Id Error, please Update NameMap.json "+type.Name);
}
moduleId = pa.moduleId;
msgId = pa.messageId;
msgtype = null;
}
uint writePB(byte[] v, int errorCode=0) {
int bodyLength = 4 + 1 + 2+ 4 + 2 + v.Length;
int totalLength = 1 + 4 + bodyLength;
//checkStream (totalLength);
Debug.Log ("ServerBundle::writePB pack data is "+bodyLength+" pb length "+v.Length+" totalLength "+totalLength);
Debug.Log ("ServerBundle::writePB module Id msgId " + moduleId+" "+msgId);
stream.writeUint8 (Convert.ToByte(0xcc));
stream.writeUint32 (Convert.ToUInt32(bodyLength));
stream.writeUint32 (Convert.ToUInt32(flowId));
stream.writeUint8 (Convert.ToByte(moduleId));
stream.writeUint16 (Convert.ToUInt16(msgId));
stream.writeUint32 (Convert.ToUInt32 (123));//response time
stream.writeUint16 (Convert.ToUInt16 (errorCode)); // no error reponse flag
stream.writePB (v);
return flowId;
}
uint writePB(IMessageLite pbMsg, int errorCode=0) {
Debug.Log ("WritePB: "+pbMsg);
byte[] bytes;
using (System.IO.MemoryStream stream = new System.IO.MemoryStream()) {
pbMsg.WriteTo (stream);
bytes = stream.ToArray ();
}
return writePB (bytes, errorCode);
}
public static byte[] sendImmediateError(IBuilderLite build, uint flowId, int errorCode) {
var data = build.WeakBuild ();
var bundle = new ServerBundle ();
bundle.newMessage (data.GetType());
bundle.flowId = flowId;
bundle.writePB (data, errorCode);
return bundle.stream.getbuffer();
}
public static byte[] MakePacket(IBuilderLite build, uint flowId) {
var data = build.WeakBuild ();
var bundle = new ServerBundle ();
bundle.newMessage (data.GetType());
bundle.flowId = flowId;
bundle.writePB (data);
return bundle.stream.getbuffer();
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/code2758/GameServerCsharp.git
git@gitee.com:code2758/GameServerCsharp.git
code2758
GameServerCsharp
GameServerCsharp
master

搜索帮助