void SendMsg(KcpData link, ref MsgInfo msg, Int16 time) { if (msg.MsgID >= 60000) { kcpListener.UpdateBroadcastMsg(msg.MsgID); } int c = msg.data.DataCount; unsafe { byte *bp = msg.data.Addr; for (int i = 0; i < c; i++) { tmpBuffer[i] = bp[i]; } } msg.SendTime = time; msg.SendCount++; kcpListener.soc.SendTo(tmpBuffer, c, SocketFlags.None, link.endpPoint); }
public int SendMsg(KcpData link, Int16 time) { NetworkContent nc = new NetworkContent(); while (link.Get(ref nc)) { if (nc.dat == null) { if (nc.MsgID >= 60000)//这是一个广播消息 { var bm = kcpListener.FindBroadcastMsg(nc.MsgID); if (bm != null) { int c = bm.data.DataCount; for (int i = 0; i < c; i++) { MsgInfo info = new MsgInfo(); info.CurPart = (UInt16)i; unsafe { info.data = bm.data.Addr[i]; } info.MsgID = (UInt16)nc.MsgID; info.CreateTime = time; info.SendTime = time; link.Msgs.Add(info); } } } } else//这是一个私有消息 { link.MsgId++; if (link.MsgId >= MaxID) { link.MsgId = MinID; } PackAll(link, nc.dat, (byte)nc.MsgID, link.MsgId, time); } } ReSendTimeOut(link, time); return(link.Msgs.Count); }
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); } } } }