/// <summary>
        /// 启动接收
        /// </summary>
        public void StartRecvice()
        {
            isRecStop = false;
            EndPoint point  = new IPEndPoint(IPAddress.Any, 0);
            Thread   udprec = new Thread(() =>
            {
                int len = 0;
                while (isRun)
                {
                    RecviceBuffer buffer = BufferCache.Instance.GetBuffer();
                    try
                    {
                        len = socket.ReceiveFrom(buffer.Data, ref point);
                    }
                    catch (SocketException ex)
                    {
                        Debug.WriteLine(ex);
                    }
                    IPEndPoint iP = (IPEndPoint)point;
                    buffer.Point  = iP;
                    buffer.Len    = len;
                    recQueue.Enqueue(buffer);
                    if (isProcessRecStop)
                    {
                        isProcessRecStop = false;
                        ProcessRecvice();
                    }
                }
                isRecStop = true;
            });

            udprec.Name         = "udpRec";
            udprec.IsBackground = true;
            if (!udprec.IsAlive)
            {
                udprec.Start();
            }
        }
Exemple #2
0
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <returns></returns>
        public RecviceBuffer GetBuffer()
        {
            RecviceBuffer buffer = null;

            if (!buffers.TryDequeue(out buffer))
            {
                if (Interlocked.Decrement(ref Num) > 0)
                {
                    buffer = new RecviceBuffer(this)
                    {
                        Data = new byte[UDPPack.PackSize]
                    };
                }
                else
                {
                    buffer = new RecviceBuffer
                    {
                        Data = new byte[UDPPack.PackSize]
                    };
                }
            }
            return(buffer);
        }
        /// <summary>
        /// 处理接收的数据
        /// </summary>
        private void ProcessRecvice()
        {
            Task.Factory.StartNew(() =>
            {
                RecviceBuffer buffer = null;
                UDPPackage package;
                while (!recQueue.IsEmpty)
                {
                    if (recQueue.TryDequeue(out buffer))
                    {
                        var p = UDPPack.UnPack(buffer.Data, buffer.Len);
                        if (p.PackNum == 0)
                        {
                            continue;
                        }
                        if (p.DataType == 1)
                        {
                            if (sendQueue.TryGetValue(p.SessionId, out package))
                            {
                                package.Remove(p.SeqId);
                            }
                        }
                        else
                        {
                            rspQueue.Enqueue(new RspBuffer()
                            {
                                Point = buffer.Point, Package = p
                            });                                                                     //准备回执
                            if (recsession.AddData(buffer.Point.ToString(), p))
                            {
                                var buf = recsession.GetData(buffer.Point.ToString(), p.SessionId);
                                if (UDPPackProcess.Instance.dicFilter.ContainsKey(buffer.Point.ToString() + "," + p.SessionId))
                                {
                                    continue;
                                }
                                else
                                {
                                    UDPPackProcess.Instance.dicFilter[buffer.Point.ToString() + "," + p.SessionId] = DateTime.Now.Second;
                                }
                                if (UDPCall != null)
                                {
                                    UDPCall(this, buf, new SocketRsp()
                                    {
                                        Address = buffer.Point.Address.ToString(), Port = buffer.Point.Port
                                    });
                                }
                                else
                                {
                                    //只能复制
                                    var tmp = new RecviceBuffer
                                    {
                                        Data = new byte[buffer.Data.Length],
                                        Len  = buffer.Len
                                    };
                                    Array.Copy(buffer.Data, 0, tmp.Data, 0, tmp.Data.Length);


                                    block.Add(tmp);
                                }
                            }
                            if (isPspStop)
                            {
                                isPspStop = false;
                                ProcessRsp();
                            }
                        }
                        buffer.Return();
                    }
                }
                isProcessRecStop = true;
            });
        }
Exemple #4
0
 /// <summary>
 /// 返回
 /// </summary>
 /// <param name="recviceBuffer"></param>
 internal void Add(RecviceBuffer recviceBuffer)
 {
     buffers.Enqueue(recviceBuffer);
 }