public static RoleOperation_LogOnGameServerReturnProto GetProto(byte[] buffer) { RoleOperation_LogOnGameServerReturnProto proto = new RoleOperation_LogOnGameServerReturnProto(); MMO_MemoryStream ms = GameEntry.Socket.SocketReceiveMS; ms.SetLength(0); ms.Write(buffer, 0, buffer.Length); ms.Position = 0; proto.RoleCount = ms.ReadInt(); proto.RoleList = new List <RoleItem>(); for (int i = 0; i < proto.RoleCount; i++) { RoleItem _Role = new RoleItem(); _Role.RoleId = ms.ReadInt(); _Role.RoleNickName = ms.ReadUTF8String(); _Role.RoleJob = (byte)ms.ReadByte(); _Role.RoleLevel = ms.ReadInt(); proto.RoleList.Add(_Role); } return(proto); }
public static WorldMap_OtherRoleEnterProto GetProto(byte[] buffer, bool isChild = false) { WorldMap_OtherRoleEnterProto proto = new WorldMap_OtherRoleEnterProto(); MMO_MemoryStream ms = null; if (!isChild) { ms = GameEntry.Socket.SocketSendMS; } else { ms = GameEntry.Pool.DequeueClassObject <MMO_MemoryStream>(); } ms.SetLength(0); ms.Write(buffer, 0, buffer.Length); ms.Position = 0; proto.RoleId = ms.ReadInt(); proto.RoleNickName = ms.ReadUTF8String(); proto.RoleLevel = ms.ReadInt(); proto.RoleJobId = ms.ReadInt(); proto.RoleCurrMP = ms.ReadInt(); proto.RoleMaxMP = ms.ReadInt(); proto.RoleCurrHP = ms.ReadInt(); proto.RoleMaxHP = ms.ReadInt(); proto.RolePosX = ms.ReadFloat(); proto.RolePosY = ms.ReadFloat(); proto.RolePosZ = ms.ReadFloat(); proto.RoleYAngle = ms.ReadFloat(); if (isChild) { GameEntry.Pool.EnqueueClassObject(ms); } return(proto); }
public static RoleOperation_SelectRoleInfoReturnProto GetProto(byte[] buffer) { RoleOperation_SelectRoleInfoReturnProto proto = new RoleOperation_SelectRoleInfoReturnProto(); using (MMO_MemoryStream ms = new MMO_MemoryStream(buffer)) { proto.IsSucess = ms.ReadBool(); if (proto.IsSucess) { proto.RoldId = ms.ReadInt(); proto.RoleNickName = ms.ReadUTF8String(); proto.JobId = (byte)ms.ReadByte(); proto.Level = ms.ReadInt(); proto.Money = ms.ReadInt(); proto.Gold = ms.ReadInt(); proto.Exp = ms.ReadInt(); proto.MaxHP = ms.ReadInt(); proto.MaxMP = ms.ReadInt(); proto.CurrHP = ms.ReadInt(); proto.CurrMP = ms.ReadInt(); proto.Attack = ms.ReadInt(); proto.Defense = ms.ReadInt(); proto.Hit = ms.ReadInt(); proto.Dodge = ms.ReadInt(); proto.Cri = ms.ReadInt(); proto.Res = ms.ReadInt(); proto.Fighting = ms.ReadInt(); proto.LastInWorldMapId = ms.ReadInt(); proto.LastInWorldMapPos = ms.ReadUTF8String(); } else { proto.MessageId = ms.ReadShort(); } } return(proto); }
public static Backpack_SearchReturnProto GetProto(byte[] buffer, bool isChild = false) { Backpack_SearchReturnProto proto = new Backpack_SearchReturnProto(); MMO_MemoryStream ms = null; if (!isChild) { ms = GameEntry.Socket.SocketSendMS; } else { ms = GameEntry.Pool.DequeueClassObject <MMO_MemoryStream>(); } ms.SetLength(0); ms.Write(buffer, 0, buffer.Length); ms.Position = 0; proto.BackpackItemCount = ms.ReadInt(); proto.ItemList = new List <BackpackItem>(); for (int i = 0; i < proto.BackpackItemCount; i++) { BackpackItem _Item = new BackpackItem(); _Item.BackpackItemId = ms.ReadInt(); _Item.GoodsType = (byte)ms.ReadByte(); _Item.GoodsId = ms.ReadInt(); _Item.GoodsServerId = ms.ReadInt(); _Item.GoodsOverlayCount = ms.ReadInt(); proto.ItemList.Add(_Item); } if (isChild) { GameEntry.Pool.EnqueueClassObject(ms); } return(proto); }
public byte IsPutOn; //是否穿戴 public byte[] ToArray() { MMO_MemoryStream ms = GameEntry.Socket.SocketSendMS; ms.SetLength(0); ms.WriteUShort(ProtoCode); ms.WriteInt(EnchantLevel); ms.WriteByte(BaseAttr1Type); ms.WriteInt(BaseAttr1Value); ms.WriteByte(BaseAttr2Type); ms.WriteInt(BaseAttr2Value); ms.WriteInt(HP); ms.WriteInt(MP); ms.WriteInt(Attack); ms.WriteInt(Defense); ms.WriteInt(Hit); ms.WriteInt(Dodge); ms.WriteInt(Cri); ms.WriteInt(Res); ms.WriteByte(IsPutOn); return(ms.ToArray()); }
/// <summary> /// 加载列表 /// </summary> protected override void LoadList(MMO_MemoryStream ms) { int rows = ms.ReadInt(); int columns = ms.ReadInt(); for (int i = 0; i < rows; i++) { GameLevelMonsterEntity entity = new GameLevelMonsterEntity(); entity.Id = ms.ReadInt(); entity.GameLevelId = ms.ReadInt(); entity.Grade = ms.ReadInt(); entity.RegionId = ms.ReadInt(); entity.SpriteId = ms.ReadInt(); entity.SpriteCount = ms.ReadInt(); entity.Exp = ms.ReadInt(); entity.Gold = ms.ReadInt(); entity.DropEquip = ms.ReadUTF8String(); entity.DropItem = ms.ReadUTF8String(); entity.DropMaterial = ms.ReadUTF8String(); m_List.Add(entity); m_Dic[entity.Id] = entity; } }
public static GameServerOnePageResponseProto GetProto(byte[] buffer) { GameServerOnePageResponseProto proto = new GameServerOnePageResponseProto(); using (MMO_MemoryStream ms = new MMO_MemoryStream(buffer)) { proto.ItemCount = ms.ReadInt(); proto.ServerOnePageItemList = new List <GameServerOnePageItem>(); for (int i = 0; i < proto.ItemCount; i++) { GameServerOnePageItem _ServerOnePageItem = new GameServerOnePageItem(); _ServerOnePageItem.ServerId = ms.ReadInt(); _ServerOnePageItem.Status = ms.ReadInt(); _ServerOnePageItem.RunState = ms.ReadInt(); _ServerOnePageItem.IsCommand = ms.ReadBool(); _ServerOnePageItem.IsNew = ms.ReadBool(); _ServerOnePageItem.Name = ms.ReadUTF8String(); _ServerOnePageItem.Ip = ms.ReadUTF8String(); _ServerOnePageItem.Port = ms.ReadInt(); proto.ServerOnePageItemList.Add(_ServerOnePageItem); } } return(proto); }
public float RoleYAngle; //角色Y轴旋转 public byte[] ToArray(bool isChild = false) { MMO_MemoryStream ms = null; if (!isChild) { ms = GameEntry.Socket.SocketSendMS; ms.SetLength(0); ms.WriteUShort(ProtoCode); } else { ms = GameEntry.Pool.DequeueClassObject <MMO_MemoryStream>(); ms.SetLength(0); } ms.WriteInt(RoleId); ms.WriteUTF8String(RoleNickName); ms.WriteInt(RoleLevel); ms.WriteInt(RoleJobId); ms.WriteInt(RoleCurrMP); ms.WriteInt(RoleMaxMP); ms.WriteInt(RoleCurrHP); ms.WriteInt(RoleMaxHP); ms.WriteFloat(RolePosX); ms.WriteFloat(RolePosY); ms.WriteFloat(RolePosZ); ms.WriteFloat(RoleYAngle); byte[] retBuffer = ms.ToArray(); if (isChild) { GameEntry.Pool.EnqueueClassObject(ms); } return(retBuffer); }
public static Task_SearchTaskReturnProto GetProto(byte[] buffer, bool isChild = false) { Task_SearchTaskReturnProto proto = new Task_SearchTaskReturnProto(); MMO_MemoryStream ms = null; if (!isChild) { ms = GameEntry.Socket.SocketSendMS; } else { ms = GameEntry.Pool.DequeueClassObject <MMO_MemoryStream>(); } ms.SetLength(0); ms.Write(buffer, 0, buffer.Length); ms.Position = 0; proto.TaskCount = ms.ReadInt(); proto.CurrTaskItemList = new List <TaskItem>(); for (int i = 0; i < proto.TaskCount; i++) { TaskItem _CurrTaskItem = new TaskItem(); _CurrTaskItem.Id = ms.ReadInt(); _CurrTaskItem.Name = ms.ReadUTF8String(); _CurrTaskItem.Status = ms.ReadInt(); _CurrTaskItem.Content = ms.ReadUTF8String(); proto.CurrTaskItemList.Add(_CurrTaskItem); } if (isChild) { GameEntry.Pool.EnqueueClassObject(ms); } return(proto); }
public static RoleOperation_LogOnGameServerReturnProto GetProto(byte[] buffer, bool isChild = false) { RoleOperation_LogOnGameServerReturnProto proto = new RoleOperation_LogOnGameServerReturnProto(); MMO_MemoryStream ms = null; if (!isChild) { ms = GameEntry.Socket.SocketSendMS; } else { ms = GameEntry.Pool.DequeueClassObject <MMO_MemoryStream>(); } ms.SetLength(0); ms.Write(buffer, 0, buffer.Length); ms.Position = 0; proto.RoleCount = ms.ReadInt(); proto.RoleList = new List <RoleItem>(); for (int i = 0; i < proto.RoleCount; i++) { RoleItem _Role = new RoleItem(); _Role.RoleId = ms.ReadInt(); _Role.RoleNickName = ms.ReadUTF8String(); _Role.RoleJob = (byte)ms.ReadByte(); _Role.RoleLevel = ms.ReadInt(); proto.RoleList.Add(_Role); } if (isChild) { GameEntry.Pool.EnqueueClassObject(ms); } return(proto); }
public string LastInWorldMapPos; //最后进入的世界地图坐标 public byte[] ToArray() { using (MMO_MemoryStream ms = new MMO_MemoryStream()) { ms.WriteUShort(ProtoCode); ms.WriteBool(IsSucess); if (IsSucess) { ms.WriteInt(RoldId); ms.WriteUTF8String(RoleNickName); ms.WriteByte(JobId); ms.WriteInt(Level); ms.WriteInt(Money); ms.WriteInt(Gold); ms.WriteInt(Exp); ms.WriteInt(MaxHP); ms.WriteInt(MaxMP); ms.WriteInt(CurrHP); ms.WriteInt(CurrMP); ms.WriteInt(Attack); ms.WriteInt(Defense); ms.WriteInt(Hit); ms.WriteInt(Dodge); ms.WriteInt(Cri); ms.WriteInt(Res); ms.WriteInt(Fighting); ms.WriteInt(LastInWorldMapId); ms.WriteUTF8String(LastInWorldMapPos); } else { ms.WriteShort(MessageId); } return(ms.ToArray()); } }
public byte[] ToArray(bool isChild = false) { MMO_MemoryStream ms = null; if (!isChild) { ms = GameEntry.Socket.SocketSendMS; ms.SetLength(0); ms.WriteUShort(ProtoCode); } else { ms = GameEntry.Pool.DequeueClassObject <MMO_MemoryStream>(); ms.SetLength(0); } byte[] retBuffer = ms.ToArray(); if (isChild) { GameEntry.Pool.EnqueueClassObject(ms); } return(retBuffer); }
public static Backpack_GoodsChangeReturnProto GetProto(MMO_MemoryStream ms, byte[] buffer) { Backpack_GoodsChangeReturnProto proto = new Backpack_GoodsChangeReturnProto(); ms.SetLength(0); ms.Write(buffer, 0, buffer.Length); ms.Position = 0; proto.BackpackItemChangeCount = ms.ReadInt(); proto.ItemList = new List <ChangeItem>(); for (int i = 0; i < proto.BackpackItemChangeCount; i++) { ChangeItem _Item = new ChangeItem(); _Item.BackpackId = ms.ReadInt(); _Item.ChangeType = (byte)ms.ReadByte(); _Item.GoodsType = (byte)ms.ReadByte(); _Item.GoodsId = ms.ReadInt(); _Item.GoodsCount = ms.ReadInt(); _Item.GoodsServerId = ms.ReadInt(); proto.ItemList.Add(_Item); } return(proto); }
public static Backpack_SearchReturnProto GetProto(byte[] buffer) { Backpack_SearchReturnProto proto = new Backpack_SearchReturnProto(); MMO_MemoryStream ms = GameEntry.Socket.SocketReceiveMS; ms.SetLength(0); ms.Write(buffer, 0, buffer.Length); ms.Position = 0; proto.BackpackItemCount = ms.ReadInt(); proto.ItemList = new List <BackpackItem>(); for (int i = 0; i < proto.BackpackItemCount; i++) { BackpackItem _Item = new BackpackItem(); _Item.BackpackItemId = ms.ReadInt(); _Item.GoodsType = (byte)ms.ReadByte(); _Item.GoodsId = ms.ReadInt(); _Item.GoodsServerId = ms.ReadInt(); _Item.GoodsOverlayCount = ms.ReadInt(); proto.ItemList.Add(_Item); } return(proto); }
public byte[] ToArray(bool isChild = false) { MMO_MemoryStream ms = null; if (!isChild) { ms = GameEntry.Socket.SocketSendMS; ms.SetLength(0); ms.WriteUShort(ProtoCode); } else { ms = GameEntry.Pool.DequeueClassObject <MMO_MemoryStream>(); ms.SetLength(0); } ms.WriteInt(SkillId); ms.WriteInt(SkillLevel); ms.WriteFloat(RolePosX); ms.WriteFloat(RolePosY); ms.WriteFloat(RolePosZ); ms.WriteFloat(RoleYAngle); ms.WriteInt(BeAttackCount); for (int i = 0; i < BeAttackCount; i++) { var item = ItemList[i]; ms.WriteInt(item.BeAttackRoleId); } byte[] retBuffer = ms.ToArray(); if (isChild) { GameEntry.Pool.EnqueueClassObject(ms); } return(retBuffer); }
/// <summary> /// 加载列表 /// </summary> protected override void LoadList(MMO_MemoryStream ms) { int rows = ms.ReadInt(); int columns = ms.ReadInt(); for (int i = 0; i < rows; i++) { ItemEntity entity = new ItemEntity(); entity.Id = ms.ReadInt(); entity.Name = ms.ReadUTF8String(); entity.Type = ms.ReadInt(); entity.UsedLevel = ms.ReadInt(); entity.UsedMethod = ms.ReadUTF8String(); entity.SellMoney = ms.ReadInt(); entity.Quality = ms.ReadInt(); entity.Description = ms.ReadUTF8String(); entity.UsedItems = ms.ReadUTF8String(); entity.maxAmount = ms.ReadInt(); entity.packSort = ms.ReadInt(); m_List.Add(entity); m_Dic[entity.Id] = entity; } }
public byte[] ToArray() { using (MMO_MemoryStream ms = new MMO_MemoryStream()) { ms.WriteUShort(ProtoCode); ms.WriteInt(RoleCount); for (int i = 0; i < RoleCount; i++) { ms.WriteInt(ItemList[i].RoleId); ms.WriteUTF8String(ItemList[i].RoleNickName); ms.WriteInt(ItemList[i].RoleLevel); ms.WriteInt(ItemList[i].RoleMaxHP); ms.WriteInt(ItemList[i].RoleCurrHP); ms.WriteInt(ItemList[i].RoleMaxMP); ms.WriteInt(ItemList[i].RoleCurrMP); ms.WriteInt(ItemList[i].RoleJobId); ms.WriteFloat(ItemList[i].RolePosX); ms.WriteFloat(ItemList[i].RolePosY); ms.WriteFloat(ItemList[i].RolePosZ); ms.WriteFloat(ItemList[i].RoleYAngle); } return(ms.ToArray()); } }
public static Task_SearchTaskReturnProto GetProto(byte[] buffer) { Task_SearchTaskReturnProto proto = new Task_SearchTaskReturnProto(); //MMO_MemoryStream ms = GameEntry.Socket.SocketReceiveMS; MMO_MemoryStream ms = null; ms.SetLength(0); ms.Write(buffer, 0, buffer.Length); ms.Position = 0; proto.TaskCount = ms.ReadInt(); proto.CurrTaskItemList = new List <TaskItem>(); for (int i = 0; i < proto.TaskCount; i++) { TaskItem _CurrTaskItem = new TaskItem(); _CurrTaskItem.Id = ms.ReadInt(); _CurrTaskItem.Name = ms.ReadUTF8String(); _CurrTaskItem.Status = ms.ReadInt(); _CurrTaskItem.Content = ms.ReadUTF8String(); proto.CurrTaskItemList.Add(_CurrTaskItem); } return(proto); }
/// <summary> /// 加载列表 /// </summary> protected override void LoadList(MMO_MemoryStream ms) { int rows = ms.ReadInt(); int columns = ms.ReadInt(); for (int i = 0; i < rows; i++) { GameLevelEntity entity = new GameLevelEntity(); entity.Id = ms.ReadInt(); entity.ChapterID = ms.ReadInt(); entity.Name = ms.ReadUTF8String(); entity.SceneName = ms.ReadUTF8String(); entity.SmallMapImg = ms.ReadUTF8String(); entity.isBoss = ms.ReadInt(); entity.Ico = ms.ReadUTF8String(); entity.PosInMap = ms.ReadUTF8String(); entity.DlgPic = ms.ReadUTF8String(); entity.CameraRotation = ms.ReadUTF8String(); entity.Audio_BG = ms.ReadUTF8String(); m_List.Add(entity); m_Dic[entity.Id] = entity; } }
/// <summary> /// 接收数据回调 /// </summary> /// <param name="ar"></param> private void ReceiveCallBack(IAsyncResult ar) { try { int len = m_Socket.EndReceive(ar); if (len > 0) { //已经接收到数据 //把接收到的数据 写入缓冲数据流的尾部 m_ReceiveMs.Position = m_ReceiveMs.Length; //把指定长度的字节写入数据流 m_ReceiveMs.Write(m_ReceiveBuffer, 0, len); //byte[] buffer = m_ReceiveMs.ToArray(); //如果缓存数据流的长度 大于 2 说明至少有个不完整的包过来 //我们客户端封装数据包 用的ushort 长度就是2 if (m_ReceiveMs.Length > 2) { //循环拆分数据包 while (true) { //把数据流指针位置放在0处 m_ReceiveMs.Position = 0; //当前包体长度 int currMsgLen = m_ReceiveMs.ReadUShort(); //当前总包的长度 int currFullMsgLen = 2 + currMsgLen; //如果数据流长度大于或等于总包长度 说明至少收到一个完整包 if (m_ReceiveMs.Length >= currMsgLen) { //收到完整包 byte[] buffer = new byte[currMsgLen]; //把数据流指针放在包体位置 m_ReceiveMs.Position = 2; //把包体读取到byte数组中 m_ReceiveMs.Read(buffer, 0, currMsgLen); //============================================================ //异或之后的数组 byte[] bufferNew = new byte[buffer.Length - 3]; bool isCompress = false; ushort crc = 0; using (MMO_MemoryStream ms = new MMO_MemoryStream(buffer)) { isCompress = ms.ReadBool(); crc = ms.ReadUShort(); ms.Read(bufferNew, 0, bufferNew.Length); } //1、CRC 计算 ushort newCrc = Crc16.CalculateCrc16(bufferNew); if (newCrc == crc) { //异或得到原始数据 bufferNew = SecurityUtil.Xor(bufferNew); if (isCompress) { bufferNew = ZlibHelper.DeCompressBytes(bufferNew); } ushort protoCode = 0; byte[] protoContent = new byte[bufferNew.Length - 2]; //这里的buffer就是我们拆分的数据包 using (MMO_MemoryStream ms2 = new MMO_MemoryStream(bufferNew)) { protoCode = ms2.ReadUShort(); ms2.Read(protoContent, 0, protoContent.Length); } //将接收的数据协议进行分发 EventDispatcher.Instance.Dispatch(protoCode, m_Role, protoContent); } //处理剩余字节长度 int remainLen = (int)(m_ReceiveMs.Length - currFullMsgLen); if (remainLen > 0) { //把指针放在第一个包的尾部 m_ReceiveMs.Position = currFullMsgLen; //定义剩余字节数组 byte[] remainBuffer = new byte[remainLen]; //将数据流读取到剩余字节数组当中 m_ReceiveMs.Read(remainBuffer, 0, remainLen); //清空数据流 m_ReceiveMs.Position = 0; m_ReceiveMs.SetLength(0); //将剩余字节数组重新写入数据流 m_ReceiveMs.Write(remainBuffer, 0, remainBuffer.Length); remainBuffer = null; } else { //没有剩余字节 //清空数据流 m_ReceiveMs.Position = 0; m_ReceiveMs.SetLength(0); break; } } else { //还没有完整包 break; } } } //进行下一次接收数据包 m_Socket.BeginReceive(m_ReceiveBuffer, 0, m_ReceiveBuffer.Length, SocketFlags.None, ReceiveCallBack, m_Socket); } else { //说明客户端断开连接了 Console.WriteLine("客户端{0}断开连接了", m_Socket.RemoteEndPoint.ToString()); //将角色移除 RoleMgr.Instance.AllRole.Remove(m_Role); } } catch { //说明客户端断开连接了 Console.WriteLine("客户端{0}断开连接了", m_Socket.RemoteEndPoint.ToString()); //将角色移除 RoleMgr.Instance.AllRole.Remove(m_Role); } }
/// <summary> 接收数据回调 </summary> /// <param name="ar"></param> private void ReceiveCallBack(IAsyncResult ar) { try { int len = m_Scoket.EndReceive(ar); if (len > 0) { //把接收到的数据写入缓冲数据流的尾部 m_ReceiveMS.Position = m_ReceiveMS.Length; //把制定长度的字节写入数据流 m_ReceiveMS.Write(m_ReceiveBuffer, 0, len); //如果缓存数据流的长度>2,说明至少有一个不完整的包 //客户端封装数据包的时候用的ushort 长度为2 if (m_ReceiveMS.Length > 2) { //循环拆分数据包 while (true) { //把数据流指针位置放在0处 m_ReceiveMS.Position = 0; //currMsgLen = 包体的长度 int currMsgLen = m_ReceiveMS.ReadUShort(); int currFullMsgLen = 2 + currMsgLen;//整包的长度 //如果数据流长度>=整包的长度 说明至少收到了一个完整包 if (m_ReceiveMS.Length >= currFullMsgLen) { //进行拆包 byte[] buffer = new byte[currMsgLen]; //将数据指针设置到包体位置 m_ReceiveMS.Position = 2; //把包体读到byte[]数组 m_ReceiveMS.Read(buffer, 0, currMsgLen); //======================= //异或后的数据 byte[] bufferNew = new byte[buffer.Length - 3]; bool isCompress = false; ushort crc = 0; using (MMO_MemoryStream ms = new MMO_MemoryStream(buffer)) { isCompress = ms.ReadBool(); crc = ms.ReadUShort(); ms.Read(bufferNew, 0, bufferNew.Length); } //1.crc校验 int newCrc = Crc16.CalculateCrc16(bufferNew); if (newCrc == crc) { //2.异或得到原始数据 bufferNew = SecurityUtil.Xor(bufferNew); if (isCompress) { bufferNew = ZlibHelper.DeCompressBytes(bufferNew); } ushort protoCode = 0; byte[] protoContent = new byte[bufferNew.Length - 2]; using (MMO_MemoryStream ms = new MMO_MemoryStream(bufferNew)) { //协议编号 protoCode = ms.ReadUShort(); //Console.WriteLine(protoContent.Length + " "+protoCode); ms.Read(protoContent, 0, protoContent.Length); EventDispatcher.Instance.Dispatcher(protoCode, m_Role, protoContent); } } else { Console.WriteLine("校验码不匹配"); break; } //=============处理剩余字节数组============== int remainLen = (int)m_ReceiveMS.Length - currFullMsgLen; if (remainLen > 0) { //把指针放在第一个包的尾部 m_ReceiveMS.Position = currFullMsgLen; byte[] remainBuffer = new byte[remainLen]; m_ReceiveMS.Read(remainBuffer, 0, remainLen); //清空数据流 m_ReceiveMS.Position = 0; m_ReceiveMS.SetLength(0); //吧数据字节重新写入数据流 m_ReceiveMS.Write(remainBuffer, 0, remainBuffer.Length); remainBuffer = null; } else { //没有剩余字节 //清空数据流 m_ReceiveMS.Position = 0; m_ReceiveMS.SetLength(0); break; } } else { //还没收到完整包 break; } } } //进行下一次接收数据 ReceiveMsg(); } else { Console.WriteLine("客户端{0}断开连接", m_Scoket.RemoteEndPoint.ToString()); RoleMgr.Instance.AllRole.Remove(m_Role); } } catch { Console.WriteLine("客户端{0}断开连接......", m_Scoket.RemoteEndPoint.ToString()); RoleMgr.Instance.AllRole.Remove(m_Role); } }
/// <summary> /// 从ms获取指定的字节数据 /// </summary> /// <param name="ms"></param> /// <param name="len"></param> /// <returns></returns> public byte[] GetByteArray(MMO_MemoryStream ms, int len) { byte[] buffer = new byte[len]; ms.Read(buffer, 0, len); return(buffer); }
protected override void OnUpdate() { base.OnUpdate(); if (m_IsConnectedOk) { m_IsConnectedOk = false; if (OnConnectOK != null) { OnConnectOK(); } AppDebug.Log("连接成功"); } #region 从队列中获取数据 while (true) { if (m_ReceiveCount <= 5) { m_ReceiveCount++; lock (m_ReceiveQueue) { if (m_ReceiveQueue.Count > 0) { //得到队列中的数据包 byte[] buffer = m_ReceiveQueue.Dequeue(); //异或之后的数组 byte[] bufferNew = new byte[buffer.Length - 3]; bool isCompress = false; ushort crc = 0; using (MMO_MemoryStream ms = new MMO_MemoryStream(buffer)) { isCompress = ms.ReadBool(); crc = ms.ReadUShort(); ms.Read(bufferNew, 0, bufferNew.Length); } //先crc int newCrc = Crc16.CalculateCrc16(bufferNew); if (newCrc == crc) { //异或 得到原始数据 bufferNew = SecurityUtil.Xor(bufferNew); if (isCompress) { bufferNew = ZlibHelper.DeCompressBytes(bufferNew); } ushort protoCode = 0; byte[] protoContent = new byte[bufferNew.Length - 2]; using (MMO_MemoryStream ms = new MMO_MemoryStream(bufferNew)) { //协议编号 protoCode = ms.ReadUShort(); ms.Read(protoContent, 0, protoContent.Length); SocketDispatcher.Instance.Dispatch(protoCode, protoContent); } } else { break; } } else { break; } } } else { m_ReceiveCount = 0; break; } } #endregion }
/// <summary> /// 生成版本文件 /// </summary> private void OnCreateVersionFileCallBack() { string path = Application.dataPath + "/../AssetBundles/" + dal.GetVersion() + "/" + arrBuildTarget[buildTargetIndex]; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string strVersionFilePath = path + "/VersionFile.txt"; //版本文件路径 //如果版本文件存在 则删除 IOUtil.DeleteFile(strVersionFilePath); StringBuilder sbContent = new StringBuilder(); DirectoryInfo directory = new DirectoryInfo(path); //拿到文件夹下所有文件 FileInfo[] arrFiles = directory.GetFiles("*", SearchOption.AllDirectories); sbContent.AppendLine(dal.GetVersion()); for (int i = 0; i < arrFiles.Length; i++) { FileInfo file = arrFiles[i]; if (file.Extension == ".manifest") { continue; } string fullName = file.FullName; //全名 包含路径扩展名 //相对路径 string name = fullName.Substring(fullName.IndexOf(arrBuildTarget[buildTargetIndex]) + arrBuildTarget[buildTargetIndex].Length + 1); string md5 = EncryptUtil.GetFileMD5(fullName); //文件的MD5 if (md5 == null) { continue; } string size = file.Length.ToString(); //文件大小 bool isFirstData = false; //是否初始数据 bool isEncrypt = false; bool isBreak = false; for (int j = 0; j < m_List.Count; j++) { foreach (string xmlPath in m_List[j].PathList) { string tempPath = xmlPath; if (xmlPath.IndexOf(".") != -1) { tempPath = xmlPath.Substring(0, xmlPath.IndexOf(".")); } name = name.Replace("\\", "/"); if (name.IndexOf(tempPath, StringComparison.CurrentCultureIgnoreCase) != -1) { isFirstData = m_List[j].IsFirstData; isEncrypt = m_List[j].IsEncrypt; isBreak = true; break; } } if (isBreak) { break; } } string strLine = string.Format("{0}|{1}|{2}|{3}|{4}", name, md5, size, isFirstData ? 1 : 0, isEncrypt ? 1 : 0); sbContent.AppendLine(strLine); } IOUtil.CreateTextFile(strVersionFilePath, sbContent.ToString()); MMO_MemoryStream ms = new MMO_MemoryStream(); string str = sbContent.ToString().Trim(); string[] arr = str.Split('\n'); int len = arr.Length; ms.WriteInt(len); for (int i = 0; i < len; i++) { if (i == 0) { ms.WriteUTF8String(arr[i]); } else { string[] arrInner = arr[i].Split('|'); ms.WriteUTF8String(arrInner[0]); ms.WriteUTF8String(arrInner[1]); ms.WriteULong(ulong.Parse(arrInner[2])); ms.WriteByte(byte.Parse(arrInner[3])); ms.WriteByte(byte.Parse(arrInner[4])); } } string filePath = path + "/VersionFile.bytes"; //版本文件路径 byte[] buffer = ms.ToArray(); buffer = ZlibHelper.CompressBytes(buffer); FileStream fs = new FileStream(filePath, FileMode.Create); fs.Write(buffer, 0, buffer.Length); fs.Close(); fs.Dispose(); Debug.Log("创建版本文件成功"); }
/// <summary> /// 回池MMO_MemoryStream /// </summary> /// <param name="ms"></param> public void EnqueueMemoryStream(MMO_MemoryStream ms) { GameEntry.Pool.EnqueueClassObject(ms); }
/// <summary> /// 生成依赖关系文件 /// </summary> private void OnCreateDependenciesFile() { //第一次循环 把所有的Asset存储到一个列表里 //临时列表 List <AssetEntity> tempLst = new List <AssetEntity>(); //循环设置文件夹包括子文件里边的项 for (int i = 0; i < m_List.Count; i++) { AssetBundleEntity entity = m_List[i];//取到一个节点 string[] folderArr = new string[entity.PathList.Count]; for (int j = 0; j < entity.PathList.Count; j++) { string path = Application.dataPath + "/" + entity.PathList[j]; //Debug.LogError("path=" + path); CollectFileInfo(tempLst, path); } } // int len = tempLst.Count; //资源列表 List <AssetEntity> assetList = new List <AssetEntity>(); for (int i = 0; i < len; i++) { AssetEntity entity = tempLst[i]; AssetEntity newEntity = new AssetEntity(); newEntity.Category = entity.Category; newEntity.AssetName = entity.AssetFullName.Substring(entity.AssetFullName.LastIndexOf("/") + 1); newEntity.AssetName = newEntity.AssetName.Substring(0, newEntity.AssetName.LastIndexOf(".")); newEntity.AssetFullName = entity.AssetFullName; newEntity.AssetBundleName = entity.AssetBundleName; assetList.Add(newEntity); //场景不需要检查依赖项 if (entity.Category == AssetCategory.Scenes) { continue; } newEntity.DependsAssetList = new List <AssetDependsEntity>(); string[] arr = AssetDatabase.GetDependencies(entity.AssetFullName); foreach (string str in arr) { if (!str.Equals(newEntity.AssetFullName, StringComparison.CurrentCultureIgnoreCase) && GetIsAsset(tempLst, str)) { AssetDependsEntity assetDepends = new AssetDependsEntity(); assetDepends.Category = GetAssetCategory(str); assetDepends.AssetFullName = str; //把依赖资源 加入到依赖资源列表 newEntity.DependsAssetList.Add(assetDepends); } } } //生成一个Json文件 string targetPath = Application.dataPath + "/../AssetBundles/" + dal.GetVersion() + "/" + arrBuildTarget[buildTargetIndex]; if (!Directory.Exists(targetPath)) { Directory.CreateDirectory(targetPath); } string strJsonFilePath = targetPath + "/AssetInfo.json"; //版本文件路径 IOUtil.CreateTextFile(strJsonFilePath, LitJson.JsonMapper.ToJson(assetList)); Debug.Log("生成 AssetInfo.json 完毕"); MMO_MemoryStream ms = new MMO_MemoryStream(); //生成二进制文件 len = assetList.Count; ms.WriteInt(len); for (int i = 0; i < len; i++) { AssetEntity entity = assetList[i]; ms.WriteByte((byte)entity.Category); ms.WriteUTF8String(entity.AssetFullName); ms.WriteUTF8String(entity.AssetBundleName); if (entity.DependsAssetList != null) { //添加依赖资源 int depLen = entity.DependsAssetList.Count; ms.WriteInt(depLen); for (int j = 0; j < depLen; j++) { AssetDependsEntity assetDepends = entity.DependsAssetList[j]; ms.WriteByte((byte)assetDepends.Category); ms.WriteUTF8String(assetDepends.AssetFullName); } } else { ms.WriteInt(0); } } string filePath = targetPath + "/AssetInfo.bytes"; //版本文件路径 byte[] buffer = ms.ToArray(); buffer = ZlibHelper.CompressBytes(buffer); FileStream fs = new FileStream(filePath, FileMode.Create); fs.Write(buffer, 0, buffer.Length); fs.Close(); fs.Dispose(); Debug.Log("生成 AssetInfo.bytes 完毕"); }
protected override void OnStart() { base.OnStart(); LoadDataTableMS = new MMO_MemoryStream(); }
/// <summary> /// 接收数据回调 /// </summary> /// <param name="ar"></param> private void ReceiveCallBack(IAsyncResult ar) { try { int len = m_Socket.EndReceive(ar); if (len > 0) { //已经接收到数据 //把接收到数据 写入缓冲数据流的尾部 m_ReceiveMS.Position = m_ReceiveMS.Length; //把指定长度的字节 写入数据流 m_ReceiveMS.Write(m_ReceiveBuffer, 0, len); //如果缓存数据流的长度>2 说明至少有个不完整的包过来了 //为什么这里是2 因为我们客户端封装数据包 用的ushort 长度就是2 if (m_ReceiveMS.Length > 2) { //进行循环 拆分数据包 while (true) { //把数据流指针位置放在0处 m_ReceiveMS.Position = 0; //currMsgLen = 包体的长度 int currMsgLen = m_ReceiveMS.ReadUShort(); //currFullMsgLen 总包的长度=包头长度+包体长度 int currFullMsgLen = 2 + currMsgLen; //如果数据流的长度>=整包的长度 说明至少收到了一个完整包 if (m_ReceiveMS.Length >= currFullMsgLen) { //至少收到一个完整包 //定义包体的byte[]数组 byte[] buffer = new byte[currMsgLen]; //把数据流指针放到2的位置 也就是包体的位置 m_ReceiveMS.Position = 2; //把包体读到byte[]数组 m_ReceiveMS.Read(buffer, 0, currMsgLen); //---------------------------------------------------------- //这是异或之后的数组 byte[] bufferNew = new byte[buffer.Length - 3]; bool isCompress = false; ushort crc = 0; using (MMO_MemoryStream ms = new MMO_MemoryStream(buffer)) { isCompress = ms.ReadBool(); crc = ms.ReadUShort(); ms.Read(bufferNew, 0, bufferNew.Length); } //校验Crc int newCrc = Crc16.CalculateCrc16(bufferNew); if (newCrc == crc)//本地Crc与数据包的进行校验 { //异或 得到原始数据 bufferNew = SecurityUtil.Xor(bufferNew); if (isCompress)//如果压缩了 { //进行解压 bufferNew = ZlibHelper.DeCompressBytes(bufferNew); } //协议编号 ushort protoCode = 0; //协议内容 byte[] protoContent = new byte[buffer.Length - 2]; using (MMO_MemoryStream ms = new MMO_MemoryStream(bufferNew)) { protoCode = ms.ReadUShort(); ms.Read(protoContent, 0, protoContent.Length); } EventDispatcher.Instance.Dispath(protoCode, m_Role, protoContent); } //==============处理剩余字节数组=================== //剩余字节长度 int remainLen = (int)m_ReceiveMS.Length - currFullMsgLen; if (remainLen > 0) { //把指针放在第一个包的尾部 m_ReceiveMS.Position = currFullMsgLen; //定义剩余字节数组 byte[] remainBuffer = new byte[remainLen]; //把数据流读到剩余字节数组 m_ReceiveMS.Read(remainBuffer, 0, remainLen); //清空数据流 m_ReceiveMS.Position = 0; m_ReceiveMS.SetLength(0); //把剩余字节数组重新写入数据流 m_ReceiveMS.Write(remainBuffer, 0, remainBuffer.Length); remainBuffer = null; } else { //没有剩余字节 //清空数据流 m_ReceiveMS.Position = 0; m_ReceiveMS.SetLength(0); break; } } else { //还没有收到完整包 break; } } } //进行下一次接收数据包 ReceiveMsg(); } else { //客户端断开连接 Console.WriteLine("客户端{0}断开连接1", m_Socket.RemoteEndPoint.ToString()); RoleMgr.Instance.AllRole.Remove(m_Role); } } catch (Exception ex) { //客户端断开连接 Console.WriteLine("客户端{0}断开连接2 原因{1}", m_Socket.RemoteEndPoint.ToString(), ex.Message); RoleMgr.Instance.AllRole.Remove(m_Role); } }
public Role() { SocketSendMS = new MMO_MemoryStream(); SocketReceiveMS = new MMO_MemoryStream(); }
/// <summary> /// 加载数据列表 /// </summary> /// <param name="parse"></param> /// <returns></returns> protected abstract void LoadList(MMO_MemoryStream ms);