示例#1
0
        /// <summary>
        /// 开启线程发送
        /// 每次发送时调用
        /// </summary>
        private void TaskSend()
        {
            if (!semaphore.WaitOne(100))
            {
                return;
            }
            if (!queue.IsEmpty)
            {
                Task.Factory.StartNew(() =>
                {
                    AsyncUDPSendBuffer buffer = null;
                    do
                    {
                        if (queue.TryDequeue(out buffer))
                        {
                            socket.SendTo(buffer.Buffer, buffer.Offset, buffer.Length, SocketFlags.None, buffer.EndPoint);

                            buffer.FreeDataCache();
                            buffer.Free();
                            if (buffer.Token != null)
                            {
                                AsyncUdpUserToken token = buffer.Token as AsyncUdpUserToken;
                                if (null != token)
                                {
                                    //外部缓存也释放
                                    token.FreeCache();
                                }
                            }
                        }
                    }while (!queue.IsEmpty);
                    semaphore.Release();
                });
            }
        }
示例#2
0
 /// <summary>
 /// 置回实体
 /// </summary>
 /// <param name="token"></param>
 public void Push(AsyncUDPSendBuffer token)
 {
     if (removeNum > 0)
     {
         removeNum--;
         return;
     }
     stack.Push(token);
     Free();
 }
示例#3
0
        /// <summary>
        /// 取出实体
        /// </summary>
        /// <returns></returns>
        public AsyncUDPSendBuffer Pop()
        {
            AsyncUDPSendBuffer buffer = null;

            if (!stack.TryPop(out buffer))
            {
                buffer      = new AsyncUDPSendBuffer();
                buffer.ID   = Interlocked.Increment(ref tokenid);
                buffer.Pool = this;
                emptyTime   = DateTime.Now;
            }
            removeNum--;
            return(buffer);
        }
示例#4
0
        /// <summary>
        /// 数据直接发送
        /// </summary>
        /// <param name="content"></param>
        /// <param name="remoteEndPoint"></param>
        public void Send(byte[] content, EndPoint remoteEndPoint, int offset = 0, int len = 0)
        {
            if (len == 0)
            {
                len = content.Length;
            }
            AsyncUDPSendBuffer sendBuffer = uDPSendPool.Pop();

            //socket.SendTo(content, offset, len, SocketFlags.None, remoteEndPoint);
            sendBuffer.Buffer   = content;
            sendBuffer.Offset   = offset;
            sendBuffer.Length   = len;
            sendBuffer.EndPoint = remoteEndPoint;
            queue.Enqueue(sendBuffer);
        }
示例#5
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="token"></param>
        /// <param name="isCache"></param>
        public void SendPackage(AsyncUdpUserToken token, int isCache = 0)
        {
            /*
             * 说明,通信层的缓存不回收,其余的数据均回收
             * 0.socketArgs.UserToken=null,直接设置buffer=null(标记UserToken=null)
             * 1.本层缓存,不回收buffer;(不做任何处理,每次有多个)
             * 2.外部缓存,回收直接设置buffer=null,同时回收使用缓存(每次只会有一个,直接回收)
             */
            if (0 == isCache)
            {
                Send(token.Data, token.Remote, token.Offset, token.Length);
            }
            else if (1 == isCache)
            {
                //使用通信缓存分组发送;
                //只有这个分支会用到通信缓存
                int    index      = token.Offset;
                byte[] sendBuffer = null;
                int    Offset     = 0;
                int    len        = 0;
                do
                {
                    token.Socket   = socket;
                    token.UserInfo = "udpcache";

                    if (token.Length == 0)
                    {
                        token.Length = token.Data.Length;
                    }
                    //拷贝数据到本缓存
                    AsyncUDPSendBuffer buffer = uDPSendPool.Pop();
                    buffer.EndPoint = token.Remote;
                    if (IsFixCache)
                    {
                        cacheManager.SetBuffer(out sendBuffer, out Offset);
                    }
                    else
                    {
                        cacheManager.GetBuffer(out sendBuffer);
                    }
                    if (cacheManager.BufferSize + index >= token.Length)
                    {
                        Array.Copy(token.Data, token.Offset + index, sendBuffer, Offset, cacheManager.BufferSize);
                        index += cacheManager.BufferSize;
                        len    = cacheManager.BufferSize;
                        //
                        buffer.Offset      = Offset;
                        buffer.Length      = len;
                        buffer.BufferCache = cacheManager;
                        buffer.IsFixCache  = IsFixCache;
                    }
                    else
                    {
                        //不够缓存发送了
                        byte[] tmp = new byte[token.Length - index];
                        Array.Copy(token.Data, token.Offset + index, tmp, 0, tmp.Length);
                        index        += tmp.Length;
                        len           = tmp.Length;
                        buffer.Length = len;
                        buffer.Offset = 0;
                        buffer.Buffer = tmp;
                    }
                    //
                    //socket.SendTo(sendBuffer, Offset, len,SocketFlags.None,token.Remote);

                    queue.Enqueue(buffer);
                } while (index < token.Length);
                token.FreeCache();//用完外部的了;
            }
            else if (2 == isCache)
            {
                token.UserInfo = "outcache";
                if (token.Length == 0)
                {
                    token.Length = token.Data.Length;
                }

                AsyncUDPSendBuffer buffer = uDPSendPool.Pop();
                buffer.Buffer   = token.Data;
                buffer.Offset   = token.Offset;
                buffer.Length   = token.Length;
                buffer.EndPoint = token.Remote;
                buffer.Token    = token;//外部缓存发送完成释放
                queue.Enqueue(buffer);
                //持续使用外部缓存发送,发送后要释放
            }
            else
            {
                Console.WriteLine("isCache参数不正确");
            }
            TaskSend();
        }