Пример #1
0
        private void processReByte(byte[] bytes, EndPoint endPoint)
        {
            PacketUtil packetUtil = new PacketUtil();
            int        id         = packetUtil.GetID(bytes);
            int        index      = packetUtil.GetIndex(bytes);

            byte[] infoBytes = sendOutPool[id].dic[index];

            Socket socket1 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            //socket1.Bind(new IPEndPoint(hostIPEndPoint.Address, 8080));
            socket1.SendTo(infoBytes, infoBytes.Length, SocketFlags.None, endPoint);
            //socket1.Dispose();
            //Thread.Sleep(1);


            /*Console.WriteLine("已发送请求");
             * Console.WriteLine(id);
             * Console.WriteLine(index);*/
        }
Пример #2
0
        //收到的网络数据包分为两种,一种是包含文件信息数据,另外一种就是包含请求的,所以应该分开处理
        /// <summary>
        /// 受到数据包处理方式
        /// </summary>
        /// <param name="TempData"></param>
        private void PacketProcess(object TempData)
        {
            int PackID;
            Dictionary <int, int> PackCheck;
            DataPool dataPool = null;


            //获取包装类中的数据
            byte[]   bytes    = ((ReceiveData)TempData).bytes;
            EndPoint endPoint = ((ReceiveData)TempData).endPoint;


            //调用工具类处理数据包数据
            // PacketUtil packetUtil = new PacketUtil();
            //获取数据包的ID,用作分类,获取数据池
            PackID = packetUtil.GetID(bytes);
            int index = packetUtil.GetIndex(bytes);



            //先判断总数据池中有没有相关ID的数据池
            lock (locker)
            {
                if (ResendBufferPool.ContainsKey(PackID))
                {
                    RemoveResendPool(PackID, index);
                }
                if (ReceivePool.ContainsKey(PackID))
                {
                    dataPool = ReceivePool[PackID];

                    dataPool.AddBytes(bytes);
                    dataPool.CountPlus();
                    dataPool.RefreshTime();
                }
                else
                {
                    ReceivePool.Add(PackID, new DataPool(PackID, new Dictionary <int, byte[]>(), endPoint, 30000));
                    dataPool = ReceivePool[PackID];
                    dataPool.AddBytes(bytes);
                    dataPool.CountPlus();
                    dataPool.RefreshTime();
                }
            }


            //Console.WriteLine(dataPool.Count);

            //检测是否可以进行拼包操作

            if (dataPool.Count == dataPool.TotalCount)
            {
                if (packetUtil.TotalCheckBool(dataPool.dic))
                {
                    FileStream f1 = File.Create(@"H:\test.pdf");


                    int count         = packetUtil.GetCount(dataPool.dic[0]);
                    int contextLength = packetUtil.GetContexLength(dataPool.dic[count - 1]);
                    int i             = 0;
                    while (i < count - 1)
                    {
                        f1.Write(dataPool.dic[i], packetUtil.HeadLength, packetUtil.MaxContextLength);
                        i++;
                        if (i % 100 == 0)
                        {
                            f1.Flush();
                        }
                    }
                    f1.Write(dataPool.dic[count - 1], packetUtil.HeadLength, contextLength);

                    f1.Close();
                    Console.WriteLine("finshed");


                    //完成后删除相关接收缓冲池
                    ReceivePool.Remove(PackID);
                }
                else
                {
                    Console.WriteLine("数据发现冗余");
                    PackCheck = packetUtil.TotalCheck(dataPool.dic);
                    ProcessLostPacket(PackCheck, dataPool.endPoint);
                }
            }
        }