public BlockInfo <BlockInfo> Pack(byte[] dat, byte type, UInt16 msgId, Int16 time) { int len = dat.Length; int p = len / FragmentSize; int r = len % FragmentSize; int all = p;//总计分卷数量 if (r > 0) { all++; } BlockInfo <BlockInfo> info = block1.RegNew(all); info.DataCount = all; int s = 0; unsafe { fixed(byte *bp = &tmpBuffer[0]) { for (int i = 0; i < p; i++) { MsgHead *head = (MsgHead *)bp; head->Type = type; head->MsgID = msgId; head->CurPart = (UInt16)i; head->AllPart = (UInt16)all; head->PartLen = (UInt16)FragmentSize; head->Lenth = (UInt32)len; int t = MsgHead.Size; for (int j = 0; j < FragmentSize; j++) { tmpBuffer[t] = dat[s]; t++; s++; } info.Addr[i] = PackInt(FragmentSize + MsgHead.Size); } if (r > 0) { MsgHead *head = (MsgHead *)bp; head->Type = type; head->MsgID = msgId; head->CurPart = (UInt16)p; head->AllPart = (UInt16)all; head->PartLen = (UInt16)r; head->Lenth = (UInt32)len; int t = MsgHead.Size; for (int j = 0; j < r; j++) { tmpBuffer[t] = dat[s]; t++; s++; } info.Addr[p] = PackInt(r + MsgHead.Size); } } } return(info); }
void PackAll(KcpData link, byte[] dat, byte type, UInt16 msgId, Int16 time) { int len = dat.Length; int p = len / FragmentSize; int r = len % FragmentSize; int all = p;//总计分卷数量 if (r > 0) { all++; } int s = 0; unsafe { fixed(byte *bp = &tmpBuffer[0]) { for (int i = 0; i < p; i++) { MsgHead *head = (MsgHead *)bp; head->Type = type; head->MsgID = msgId; head->CurPart = (UInt16)i; head->AllPart = (UInt16)all; head->PartLen = (UInt16)FragmentSize; head->Lenth = (UInt32)len; head->Time = time; byte *dp = bp + MsgHead.Size; for (int j = 0; j < FragmentSize; j++) { dp[j] = dat[s]; s++; } MsgInfo msg = new MsgInfo(); msg.data = PackInt(FragmentSize + MsgHead.Size); msg.MsgID = msgId; msg.CurPart = (UInt16)i; msg.CreateTime = time; SendMsg(link, ref msg, time); link.Msgs.Add(msg); } if (r > 0) { MsgHead *head = (MsgHead *)bp; head->Type = type; head->MsgID = msgId; head->CurPart = (UInt16)p; head->AllPart = (UInt16)all; head->PartLen = (UInt16)r; head->Lenth = (UInt32)len; head->Time = time; byte *dp = bp + MsgHead.Size; for (int j = 0; j < r; j++) { dp[j] = dat[s]; s++; } MsgInfo msg = new MsgInfo(); msg.data = PackInt(r + MsgHead.Size); msg.MsgID = msgId; msg.CurPart = (UInt16)p; msg.CreateTime = time; SendMsg(link, ref msg, time); link.Msgs.Add(msg); } } } }