Пример #1
0
        /// <summary>
                  ///  生成重发请求列表
                  /// </summary>
                  /// <param name="in_iPackKey"></param>
                  /// <returns></returns>
        private List <ResendInfo> FindLostPack(int in_iPackKey, EndPoint in_EndPoint)
        {
            // 返回变量
            List <ResendInfo> listRes = new List <ResendInfo>();

            // 得到总包数
            int iCount = ((DataPools)m_listInDataPools[in_iPackKey]).TotalPack;

            // 总包数布尔值
            bool[]        listAllTable = new bool[iCount];
            List <byte[]> listPools    = m_listInDataPools[in_iPackKey].PacketParking;

            // 调用包组件
            ByePacketUtil packetUtil = new ByePacketUtil();

            foreach (byte[] listItem in listPools)
            {
                int i = packetUtil.GetJointHeadIndex(listItem);
                listAllTable[i] = true;
            }


            for (int i = 0; i < iCount; i++)
            {
                if (listAllTable[i] == false)
                {
                    listRes.Add(new ResendInfo(in_iPackKey, in_EndPoint, i, m_ipSelfIP));
                }
            }

            return(listRes);
        }
Пример #2
0
        /// <summary>
                  /// 发送数据
                  /// </summary>
                  /// <param name="in_listToSendData"></param>
                  /// <param name="in_strDesIP"></param>
        public void Send(byte[] in_listToSendData, string in_strDesIP)
        {
            List <byte> listByte = new List <byte>();

            listByte.AddRange(in_listToSendData);
            // 设置目标IP,设置TCP端口号
            IPEndPoint ipEndPoint = StrToEndPoint(in_strDesIP);

            // 调用包组件
            ByePacketUtil stPacketUtil = new ByePacketUtil();

            // 声明包组变量
            List <byte[]> listToSendDatas = stPacketUtil.SplitPacket(listByte);

            // 添加到发送池
            m_listOutDataPools.Add(stPacketUtil.GetJointHeadID(listToSendDatas[0]), new DataPools(0, 0, null, 30000, listToSendDatas));

            foreach (var blistPackage in listToSendDatas)
            {
                //循环发送每个包
                byte[] bSends = new byte[2048];
                bSends = blistPackage.ToArray();

                // 发送
                m_SocketService.SendTo(bSends, bSends.Length, SocketFlags.None, ipEndPoint);

                // 缓解并发压力
                Thread.Sleep(m_iPush);
            }
        }
Пример #3
0
        private void SendByInfo(ResendInfo in_resendInfo)
        {
            int iKey   = in_resendInfo.Key;
            int iIndex = in_resendInfo.Index;

            // 调用包组件
            ByePacketUtil packetUtil = new ByePacketUtil();

            if (m_listOutDataPools.ContainsKey(iKey))
            {
                foreach (var bItem in m_listOutDataPools[iKey].PacketParking)
                {
                    // 得到包的Index
                    int iPacketIndex = packetUtil.GetJointHeadIndex(bItem);
                    if (iPacketIndex == iIndex)
                    {
                        // 发送
                        m_SocketService.SendTo(bItem, bItem.Length, SocketFlags.None, in_resendInfo.MyEndPoint);
                    }
                }
            }
        }
Пример #4
0
        /// <summary>
                  /// 分配收到的包
                  /// </summary>
                  /// <param name="in_iData"></param>
        private void AllotPack(object in_objCustomParm)
        {
            byte[]   listDataIn = ((CustomParm)in_objCustomParm).ParmBytes;
            EndPoint endPointIn = ((CustomParm)in_objCustomParm).ParmEndPoint;

            // 默认等待时间(毫秒)
            int iLeftDef = 30000;

            // 调用包组件
            ByePacketUtil packetUtil = new ByePacketUtil();

            // 得到包的ID
            int iPacketID = packetUtil.GetJointHeadID(listDataIn);

            // 得到包的Index
            int iPacketIndex = packetUtil.GetJointHeadIndex(listDataIn);

            // 得到包的份数
            int iPacketCount = packetUtil.GetJointHeadCount(listDataIn);

            m_mutexPick.WaitOne();
            Console.WriteLine("收到" + iPacketIndex + "/" + iPacketCount);
            // 操作接收池
            if (m_listInDataPools.ContainsKey(iPacketID))
            {
                // 存在就存一条

                m_listInDataPools[iPacketID].PacketParking.Add(listDataIn);
                m_listInDataPools[iPacketID].ExistPack++;
                m_listInDataPools[iPacketID].LeftTime = iLeftDef;
            }
            else
            {
                // 不存在就新建一条
                List <byte[]> listTempDatas = new List <byte[]>();
                listTempDatas.Add(listDataIn);
                m_listInDataPools.Add(iPacketID, new DataPools(1, iPacketCount, endPointIn, iLeftDef, listTempDatas));
            }

            // 检查接收此包后是否有完整大包
            if (m_listInDataPools[iPacketID].PacketParking.Count() == iPacketCount)
            {
                // 临时变量池
                List <byte> listTempPool;

                // 若大包完整执行封包
                listTempPool = packetUtil.JointPacket(m_listInDataPools[iPacketID].PacketParking);

                if (OnDataArrived != null)
                {
                    // 事件抛出数据
                    OnDataArrived(listTempPool.ToArray());
                }


                Console.WriteLine("开始组包");

                // 删除相关数据
                RemoveByKey(iPacketID);
            }

            m_mutexPick.ReleaseMutex();
        }