Beispiel #1
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);
            }
        }
Beispiel #2
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();
        }