public bool Send(WazSession s, object data) { Packable packAttr = (Packable)Attribute.GetCustomAttribute(data.GetType(), typeof(Packable)); if (packAttr == null) { return(false); } int sizePackOffsetLen = sizeof(PackageOffsetToEnd); try { //序列化 FieldInfo[] members = data.GetType().GetFields(); mMemSendStream.Position = 0; mMemSendStream.SetLength(0); mBinWriter.Write(mPackHead); long posPackLen = mMemSendStream.Position; //留空位给等下回来写包长度 mBinWriter.Seek(sizePackOffsetLen, SeekOrigin.Current); mBinWriter.Write(packAttr.ID); foreach (FieldInfo f in members) { object o = f.GetValue(data); WazSerializerHelper.Write(mBinWriter, o); } PackageOffsetToEnd offsetToEnd = (PackageOffsetToEnd)(mMemSendStream.Position - posPackLen - sizePackOffsetLen); mBinWriter.Write(mPackEnd); mBinWriter.Seek((int)posPackLen, SeekOrigin.Begin); mBinWriter.Write(offsetToEnd); mNetwork.Send(s, mMemSendStream.ToArray()); return(true); } catch { return(false); } }
void Handle_RecivePackage(WazSession s, byte[] data, int size) { PackRecHelper recHelper; if (!mSessionRecHelpers.TryGetValue(s.Handle, out recHelper)) { return; } //将data 放入读取stream末尾 recHelper.Stream.Write(data, 0, size); recHelper.Stream.Seek(recHelper.PreReadStreamOffset, SeekOrigin.Begin); bool havePackageCut = false; //开始读取. while (recHelper.Stream.Position < recHelper.Stream.Length - 1) { long posStartRead = recHelper.Stream.Position; object objPack; Type typePack; try { //读包头 byte head = recHelper.Reader.ReadByte(); if (head != mPackHead) { continue; } //读长度(实际为到包尾offset) PackageOffsetToEnd offsetToEnd = recHelper.Reader.ReadUInt16(); //判断超出当前流(一个包在两段流中) if (recHelper.Stream.Position + offsetToEnd + 1 > recHelper.Stream.Length) { recHelper.PreReadStreamOffset = posStartRead; havePackageCut = true; break; } //判断包尾是否正确 long posTmp = recHelper.Stream.Position; recHelper.Stream.Seek(offsetToEnd, SeekOrigin.Current); byte end = recHelper.Reader.ReadByte(); recHelper.Stream.Position = posTmp;//还原pos到读位置之后 if (end != mPackEnd) { continue; } //读命令id PackageID pID = recHelper.Reader.ReadUInt16(); if (!mPackageMap.TryGetValue(pID, out typePack)) { Debug.WriteLine("[erro]解析包时出现未注册包ID"); continue; } //正式开始数据 objPack = Activator.CreateInstance(typePack); FieldInfo[] packFields = typePack.GetFields(); foreach (FieldInfo f in packFields) { f.SetValue(objPack, WazSerializerHelper.Read(recHelper.Reader, f.FieldType)); } //跳过包尾 recHelper.Stream.Seek(1, SeekOrigin.Current); } catch (Exception e) { // recHelper.PreReadStreamOffset = posStartRead; // havePackageCut = true; Debug.WriteLine("[erro]接收数据包异常.可能出现错误包 \r\n" + e.ToString()); break; } if (objPack != null) { if (EvtRecevicePack != null) { EvtRecevicePack(s, objPack); } //触发pack绑定处理函数 Action <WazSession, object> packBindHanlders; if (mPackDispathToAllSessionMap.TryGetValue(typePack, out packBindHanlders)) { packBindHanlders(s, objPack); } //触发session,pack绑定处理函数 Dictionary <Type, Action <object> > typePackMap; if (mPackDispatchMap.TryGetValue(s.Handle, out typePackMap)) { Action <object> handler; if (typePackMap.TryGetValue(typePack, out handler)) { handler(objPack); } } } } //证明完成读包,重置缓冲区 if (!havePackageCut) { recHelper.PreReadStreamOffset = 0; recHelper.Stream.Position = 0; recHelper.Stream.SetLength(0); } }