如何應用C#實現UDP的分包組包

來源:互聯網
上載者:User

情境介紹
如果需要使用UDP傳輸較大資料,例如傳輸10M的圖片,這突破了UDP的設計原則。UDP的設計是基於"datagram",也就是它假設你發送的每個資料包都能包含在單一的包內。並且設定UDP資料包的最大長度受基礎網路通訊協定的限制。

UDP資料包的理論最大長度限制是 65535 bytes,這包含 8 bytes 資料包頭和 65527 bytes 資料。但如果基於IPv4網路傳輸,則還需減去 20 bytes 的IP資料包頭。
則單一的UDP資料包可傳輸的資料最大長度為:

則單一的UDP資料包可傳輸的資料最大長度為:

MaxUdpDataLength = 65535 - 8 - 20 = 65507 bytes

這就需要實現UDP包的分包傳輸和接收組包功能。

分包功能

複製代碼 代碼如下:/// <summary>
/// UDP資料包分割器
/// </summary>
public static class UdpPacketSplitter
{
/// <summary>
/// 分割UDP資料包
/// </summary>
/// <param name="sequence">UDP資料包所持有的序號</param>
/// <param name="datagram">被分割的UDP資料包</param>
/// <param name="chunkLength">分割塊的長度</param>
/// <returns>
/// 分割後的UDP資料包列表
/// </returns>
public static ICollection<UdpPacket> Split(long sequence, byte[] datagram, int chunkLength)
{
if (datagram == null)
throw new ArgumentNullException("datagram");

List<UdpPacket> packets = new List<UdpPacket>();

int chunks = datagram.Length / chunkLength;
int remainder = datagram.Length % chunkLength;
int total = chunks;
if (remainder > 0) total++;

for (int i = 1; i <= chunks; i++)
{
byte[] chunk = new byte[chunkLength];
Buffer.BlockCopy(datagram, (i - 1) * chunkLength, chunk, 0, chunkLength);
packets.Add(new UdpPacket(sequence, total, i, chunk, chunkLength));
}
if (remainder > 0)
{
int length = datagram.Length - (chunkLength * chunks);
byte[] chunk = new byte[length];
Buffer.BlockCopy(datagram, chunkLength * chunks, chunk, 0, length);
packets.Add(new UdpPacket(sequence, total, total, chunk, length));
}

return packets;
}
}

發送分包
複製代碼 代碼如下:private void WorkThread()
{
while (IsRunning)
{
waiter.WaitOne();
waiter.Reset();

while (queue.Count > 0)
{
StreamPacket packet = null;
if (queue.TryDequeue(out packet))
{
RtpPacket rtpPacket = RtpPacket.FromImage(
RtpPayloadType.JPEG,
packet.SequenceNumber,
(long)Epoch.GetDateTimeTotalMillisecondsByYesterday(packet.Timestamp),
packet.Frame);

// max UDP packet length limited to 65,535 bytes
byte[] datagram = rtpPacket.ToArray();
packet.Frame.Dispose();

// split udp packet to many packets
// to reduce the size to 65507 limit by underlying IPv4 protocol
ICollection<UdpPacket> udpPackets
= UdpPacketSplitter.Split(
packet.SequenceNumber,
datagram,
65507 - UdpPacket.HeaderSize);
foreach (var udpPacket in udpPackets)
{
byte[] udpPacketDatagram = udpPacket.ToArray();
// async sending
udpClient.BeginSend(
udpPacketDatagram, udpPacketDatagram.Length,
packet.Destination.Address,
packet.Destination.Port,
SendCompleted, udpClient);
}
}
}
}
}

接收組包功能
複製代碼 代碼如下:private void OnDatagramReceived(object sender, UdpDatagramReceivedEventArgs<byte[]> e)
{
try
{
UdpPacket udpPacket = UdpPacket.FromArray(e.Datagram);

if (udpPacket.Total == 1)
{
RtpPacket packet = new RtpPacket(udpPacket.Payload, udpPacket.PayloadSize);
Bitmap bitmap = packet.ToBitmap();
RaiseNewFrameEvent(
bitmap, Epoch.GetDateTimeByYesterdayTotalMilliseconds(packet.Timestamp));
}
else
{
// rearrange packets to one packet
if (packetCache.ContainsKey(udpPacket.Sequence))
{
List<UdpPacket> udpPackets = null;
if (packetCache.TryGetValue(udpPacket.Sequence, out udpPackets))
{
udpPackets.Add(udpPacket);

if (udpPackets.Count == udpPacket.Total)
{
packetCache.TryRemove(udpPacket.Sequence, out udpPackets);

udpPackets = udpPackets.OrderBy(u => u.Order).ToList();
int rtpPacketLength = udpPackets.Sum(u => u.PayloadSize);
int maxPacketLength = udpPackets.Select(u => u.PayloadSize).Max();

byte[] rtpPacket = new byte[rtpPacketLength];
foreach (var item in udpPackets)
{
Buffer.BlockCopy(
item.Payload, 0, rtpPacket,
(item.Order - 1) * maxPacketLength, item.PayloadSize);
}

RtpPacket packet = new RtpPacket(rtpPacket, rtpPacket.Length);
Bitmap bitmap = packet.ToBitmap();
RaiseNewFrameEvent(
bitmap,
Epoch.GetDateTimeByYesterdayTotalMilliseconds(packet.Timestamp));

packetCache.Clear();
}
}
}
else
{
List<UdpPacket> udpPackets = new List<UdpPacket>();
udpPackets.Add(udpPacket);
packetCache.AddOrUpdate(
udpPacket.Sequence,
udpPackets, (k, v) => { return udpPackets; });
}
}
}
catch (Exception ex)
{
RaiseVideoSourceExceptionEvent(ex.Message);
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.