{"id":550,"date":"2016-12-02T09:41:50","date_gmt":"2016-12-02T09:41:50","guid":{"rendered":"01f78be6f7cad02658508fe4616098a9"},"modified":"2016-12-02T09:41:50","modified_gmt":"2016-12-02T09:41:50","slug":"","status":"publish","type":"post","link":"https:\/\/www.xiaobo.li\/notes\/archives\/550","title":{"rendered":"C# UDP \u5927\u6570\u636e\u5206\u5305\u4f20\u8f93"},"content":{"rendered":"<p><span style=\"font-size:16px;\">\u5982\u679c\u9700\u8981\u4f7f\u7528UDP\u4f20\u8f93\u8f83\u5927\u6570\u636e\uff0c\u4f8b\u5982\u4f20\u8f9310M\u7684\u56fe\u7247\uff0c\u8fd9\u7a81\u7834\u4e86UDP\u7684\u8bbe\u8ba1\u539f\u5219\u3002UDP\u7684\u8bbe\u8ba1\u662f\u57fa\u4e8e\"datagram\"\uff0c\u4e5f\u5c31\u662f\u5b83\u5047\u8bbe\u4f60\u53d1\u9001\u7684\u6bcf\u4e2a\u6570\u636e\u5305\u90fd\u80fd\u5305\u542b\u5728\u5355\u4e00\u7684\u5305\u5185\u3002\u5e76\u4e14\u8bbe\u5b9aUDP\u6570\u636e\u5305\u7684\u6700\u5927\u957f\u5ea6\u53d7\u57fa\u7840\u7f51\u7edc\u534f\u8bae\u7684\u9650\u5236\u3002<\/span><\/p>\n<p><span style=\"font-size:16px;\">       <\/span><\/p>\n<p align=\"center\"><a target=\"_blank\" href=\"\/notes\/content\/uploadfile\/201612\/a9bf1480642974.png\" id=\"ematt:476\"><img decoding=\"async\" src=\"\/notes\/content\/uploadfile\/201612\/a9bf1480642974.png\" alt=\"\u70b9\u51fb\u67e5\u770b\u539f\u56fe\" border=\"0\" width=\"480\" \/><\/a><\/p>\n<p><span style=\"font-size:16px;\"> <\/span><\/p>\n<p><span style=\"font-size:16px;\">UDP\u6570\u636e\u5305\u7684\u7406\u8bba<\/span><strong><span style=\"font-size:16px;\">\u6700\u5927\u957f\u5ea6\u9650\u5236\u662f 65535 bytes<\/span><\/strong><span style=\"font-size:16px;\">\uff0c\u8fd9\u5305\u542b 8 bytes \u6570\u636e\u5305\u5934\u548c 65527 bytes \u6570\u636e\u3002\u4f46\u5982\u679c\u57fa\u4e8eIPv4\u7f51\u7edc\u4f20\u8f93\uff0c\u5219\u8fd8\u9700\u51cf\u53bb 20 bytes \u7684IP\u6570\u636e\u5305\u5934\u3002<\/span><br \/>\n<span style=\"font-size:16px;\"> \u5219\u5355\u4e00\u7684UDP\u6570\u636e\u5305\u53ef\u4f20\u8f93\u7684\u6570\u636e\u6700\u5927\u957f\u5ea6\u4e3a\uff1a<\/span><\/p>\n<p><span style=\"font-size:16px;\"> <\/span><\/p>\n<p><span style=\"font-size:16px;\">MaxUdpDataLength = 65535 - 8 - 20 = 65507 bytes<\/span><\/p>\n<p><span style=\"font-size:16px;\"> <\/span><\/p>\n<p><span style=\"font-size:16px;\">\u8fd9\u5c31\u9700\u8981\u5b9e\u73b0UDP\u5305\u7684\u5206\u5305\u4f20\u8f93\u548c\u63a5\u6536\u7ec4\u5305\u529f\u80fd\u3002<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u5206\u5305\u529f\u80fd<\/strong><\/p>\n<\/p>\n<pre>\/\/\/ &lt;summary&gt;\r\n\/\/\/ UDP\u6570\u636e\u5305\u5206\u5272\u5668\r\n\/\/\/ &lt;\/summary&gt;\r\npublic static class UdpPacketSplitter\r\n{\r\n  \/\/\/ &lt;summary&gt;\r\n  \/\/\/ \u5206\u5272UDP\u6570\u636e\u5305\r\n  \/\/\/ &lt;\/summary&gt;\r\n  \/\/\/ &lt;param name=\"sequence\"&gt;UDP\u6570\u636e\u5305\u6240\u6301\u6709\u7684\u5e8f\u53f7&lt;\/param&gt;\r\n  \/\/\/ &lt;param name=\"datagram\"&gt;\u88ab\u5206\u5272\u7684UDP\u6570\u636e\u5305&lt;\/param&gt;\r\n  \/\/\/ &lt;param name=\"chunkLength\"&gt;\u5206\u5272\u5757\u7684\u957f\u5ea6&lt;\/param&gt;\r\n  \/\/\/ &lt;returns&gt;\r\n  \/\/\/ \u5206\u5272\u540e\u7684UDP\u6570\u636e\u5305\u5217\u8868\r\n  \/\/\/ &lt;\/returns&gt;\r\n  public static ICollection&lt;UdpPacket&gt; Split(long sequence, byte[] datagram, int chunkLength)\r\n  {\r\n    if (datagram == null)\r\n      throw new ArgumentNullException(\"datagram\");\r\n\r\n    List&lt;UdpPacket&gt; packets = new List&lt;UdpPacket&gt;();\r\n\r\n    int chunks = datagram.Length \/ chunkLength;\r\n    int remainder = datagram.Length % chunkLength;\r\n    int total = chunks;\r\n    if (remainder &gt; 0) total++;\r\n\r\n    for (int i = 1; i &lt;= chunks; i++)\r\n    {\r\n      byte[] chunk = new byte[chunkLength];\r\n      Buffer.BlockCopy(datagram, (i - 1) * chunkLength, chunk, 0, chunkLength);\r\n      packets.Add(new UdpPacket(sequence, total, i, chunk, chunkLength));\r\n    }\r\n    if (remainder &gt; 0)\r\n    {\r\n      int length = datagram.Length - (chunkLength * chunks);\r\n      byte[] chunk = new byte[length];\r\n      Buffer.BlockCopy(datagram, chunkLength * chunks, chunk, 0, length);\r\n      packets.Add(new UdpPacket(sequence, total, total, chunk, length));\r\n    }\r\n\r\n    return packets;\r\n  }\r\n}<\/pre>\n<p>..............................<\/p>\n<p><strong>\u53d1\u9001\u5206\u5305<\/strong><\/p>\n<p>..............................<\/p>\n<pre>private void WorkThread()\r\n{\r\n  while (IsRunning)\r\n  {\r\n    waiter.WaitOne();\r\n    waiter.Reset();\r\n\r\n    while (queue.Count &gt; 0)\r\n    {\r\n      StreamPacket packet = null;\r\n      if (queue.TryDequeue(out packet))\r\n      {\r\n        RtpPacket rtpPacket = RtpPacket.FromImage(\r\n          RtpPayloadType.JPEG, \r\n          packet.SequenceNumber, \r\n          (long)Epoch.GetDateTimeTotalMillisecondsByYesterday(packet.Timestamp),\r\n          packet.Frame);\r\n\r\n        \/\/ max UDP packet length limited to 65,535 bytes\r\n        byte[] datagram = rtpPacket.ToArray(); \r\n        packet.Frame.Dispose();\r\n\r\n        \/\/ split udp packet to many packets \r\n        \/\/ to reduce the size to 65507 limit by underlying IPv4 protocol\r\n        ICollection&lt;UdpPacket&gt; udpPackets \r\n          = UdpPacketSplitter.Split(\r\n            packet.SequenceNumber, \r\n            datagram, \r\n            65507 - UdpPacket.HeaderSize);\r\n        foreach (var udpPacket in udpPackets)\r\n        {\r\n          byte[] udpPacketDatagram = udpPacket.ToArray();\r\n          \/\/ async sending\r\n          udpClient.BeginSend(\r\n            udpPacketDatagram, udpPacketDatagram.Length,\r\n            packet.Destination.Address,\r\n            packet.Destination.Port,\r\n            SendCompleted, udpClient);\r\n        }\r\n      }\r\n    }\r\n  }\r\n}<\/pre>\n<p><strong>\u63a5\u6536\u7ec4\u5305\u529f\u80fd<\/strong><\/p>\n<p>..............................<\/p>\n<pre>private void OnDatagramReceived(object sender, UdpDatagramReceivedEventArgs&lt;byte[]&gt; e)\r\n{\r\n  try\r\n  {\r\n    UdpPacket udpPacket = UdpPacket.FromArray(e.Datagram);\r\n\r\n    if (udpPacket.Total == 1)\r\n    {\r\n      RtpPacket packet = new RtpPacket(udpPacket.Payload, udpPacket.PayloadSize);\r\n      Bitmap bitmap = packet.ToBitmap();\r\n      RaiseNewFrameEvent(\r\n        bitmap, Epoch.GetDateTimeByYesterdayTotalMilliseconds(packet.Timestamp));\r\n    }\r\n    else\r\n    {\r\n      \/\/ rearrange packets to one packet\r\n      if (packetCache.ContainsKey(udpPacket.Sequence))\r\n      {\r\n        List&lt;UdpPacket&gt; udpPackets = null;\r\n        if (packetCache.TryGetValue(udpPacket.Sequence, out udpPackets))\r\n        {\r\n          udpPackets.Add(udpPacket);\r\n\r\n          if (udpPackets.Count == udpPacket.Total)\r\n          {\r\n            packetCache.TryRemove(udpPacket.Sequence, out udpPackets);\r\n\r\n            udpPackets = udpPackets.OrderBy(u =&gt; u.Order).ToList();\r\n            int rtpPacketLength = udpPackets.Sum(u =&gt; u.PayloadSize);\r\n            int maxPacketLength = udpPackets.Select(u =&gt; u.PayloadSize).Max();\r\n\r\n            byte[] rtpPacket = new byte[rtpPacketLength];\r\n            foreach (var item in udpPackets)\r\n            {\r\n              Buffer.BlockCopy(\r\n                item.Payload, 0, rtpPacket, \r\n                (item.Order - 1) * maxPacketLength, item.PayloadSize);\r\n            }\r\n\r\n            RtpPacket packet = new RtpPacket(rtpPacket, rtpPacket.Length);\r\n            Bitmap bitmap = packet.ToBitmap();\r\n            RaiseNewFrameEvent(\r\n              bitmap, \r\n              Epoch.GetDateTimeByYesterdayTotalMilliseconds(packet.Timestamp));\r\n\r\n            packetCache.Clear();\r\n          }\r\n        }\r\n      }\r\n      else\r\n      {\r\n        List&lt;UdpPacket&gt; udpPackets = new List&lt;UdpPacket&gt;();\r\n        udpPackets.Add(udpPacket);\r\n        packetCache.AddOrUpdate(\r\n          udpPacket.Sequence, \r\n          udpPackets, (k, v) =&gt; { return udpPackets; });\r\n      }\r\n    }\r\n  }\r\n  catch (Exception ex)\r\n  {\r\n    RaiseVideoSourceExceptionEvent(ex.Message);\r\n  }\r\n}<\/pre>\n<p>.......................<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>...<\/p>\n","protected":false},"excerpt":{"rendered":"<p><span style=\"font-size:16px;\">\u5982\u679c\u9700\u8981\u4f7f\u7528UDP\u4f20\u8f93\u8f83\u5927\u6570\u636e\uff0c\u4f8b\u5982\u4f20\u8f9310M\u7684\u56fe\u7247\uff0c\u8fd9\u7a81\u7834\u4e86UDP\u7684\u8bbe\u8ba1\u539f\u5219\u3002UDP\u7684\u8bbe\u8ba1\u662f\u57fa\u4e8e\"datagram\"\uff0c\u4e5f\u5c31\u662f\u5b83\u5047\u8bbe\u4f60\u53d1\u9001\u7684\u6bcf\u4e2a\u6570\u636e\u5305\u90fd\u80fd\u5305\u542b\u5728\u5355\u4e00\u7684\u5305\u5185\u3002\u5e76\u4e14\u8bbe\u5b9aUDP\u6570\u636e\u5305\u7684\u6700\u5927\u957f\u5ea6\u53d7\u57fa\u7840\u7f51\u7edc\u534f\u8bae\u7684\u9650\u5236\u3002<\/span><\/p>\n<p align=\"center\"><a target=\"_blank\" href=\"\/notes\/content\/uploadfile\/201612\/a9bf1480642974.png\" id=\"ematt:476\"><img loading=\"lazy\" decoding=\"async\" src=\"\/notes\/content\/uploadfile\/201612\/a9bf1480642974.png\" alt=\"\u70b9\u51fb\u67e5\u770b\u539f\u56fe\" height=\"99\" border=\"0\" width=\"568\" \/><\/a><\/p>\n<p><span style=\"font-size:16px;\">UDP\u6570\u636e\u5305\u7684\u7406\u8bba<\/span><strong><span style=\"font-size:16px;\">\u6700\u5927\u957f\u5ea6\u9650\u5236\u662f 65535 bytes<\/span><\/strong><span style=\"font-size:16px;\">\uff0c\u8fd9\u5305\u542b 8 bytes \u6570\u636e\u5305\u5934\u548c 65527 bytes \u6570\u636e\u3002\u4f46\u5982\u679c\u57fa\u4e8eIPv4\u7f51\u7edc\u4f20\u8f93\uff0c\u5219\u8fd8\u9700\u51cf\u53bb 20 bytes \u7684IP\u6570\u636e\u5305\u5934\u3002<\/span><br \/>\n<span style=\"font-size:16px;\"> \u5219\u5355\u4e00\u7684UDP\u6570\u636e\u5305\u53ef\u4f20\u8f93\u7684\u6570\u636e\u6700\u5927\u957f\u5ea6\u4e3a\uff1a<\/span><\/p>\n<p><span style=\"font-size:16px;\">MaxUdpDataLength = 65535 - 8 - 20 = 65507 bytes<\/span><\/p>\n<p><span style=\"font-size:16px;\">\u8fd9\u5c31\u9700\u8981\u5b9e\u73b0UDP\u5305\u7684\u5206\u5305\u4f20\u8f93\u548c\u63a5\u6536\u7ec4\u5305\u529f\u80fd\u3002<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u5206\u5305\u529f\u80fd<\/strong><\/p>\n<\/p>\n<pre class=\"brush:c#; toolbar: true; auto-links: true;\">\/\/\/ &lt;summary&gt;\r\n\/\/\/ UDP\u6570\u636e\u5305\u5206\u5272\u5668\r\n\/\/\/ &lt;\/summary&gt;\r\npublic static class UdpPacketSplitter\r\n{\r\n  \/\/\/ &lt;summary&gt;\r\n  \/\/\/ \u5206\u5272UDP\u6570\u636e\u5305\r\n  \/\/\/ &lt;\/summary&gt;\r\n  \/\/\/ &lt;param name=\"sequence\"&gt;UDP\u6570\u636e\u5305\u6240\u6301\u6709\u7684\u5e8f\u53f7&lt;\/param&gt;\r\n  \/\/\/ &lt;param name=\"datagram\"&gt;\u88ab\u5206\u5272\u7684UDP\u6570\u636e\u5305&lt;\/para...<\/pre>\n<\/p>\n<p> <a href=\"https:\/\/www.xiaobo.li\/notes\/archives\/550\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[279],"tags":[],"class_list":["post-550","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/550","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/comments?post=550"}],"version-history":[{"count":0,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/550\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/media?parent=550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/categories?post=550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/tags?post=550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}