Example #1
0
        public void SendAsync(EndPoint endPoint, SendCommond cmd)
        {
            var args = sendArgsPool.Pop();
            if (args == null)
            {
                while (args != null)
                {
                    args= sendArgsPool.Pop();
                }
            }
            if (cmd.Buffer.Length + checkLenght > args.Buffer.Length)
                throw new Exception("发送的数据大于buffer最大长度");
            else
            {
                args.RemoteEndPoint = endPoint;
                byte[] length = BitConverter.GetBytes(cmd.Buffer.Length);

                byte[] id = BitConverter.GetBytes(cmd.CommondId);
                System.Buffer.BlockCopy(length, 0, args.Buffer, 0, length.Length);
                System.Buffer.BlockCopy(id, 0, args.Buffer,length.Length, id.Length);
                System.Buffer.BlockCopy(cmd.Buffer, 0, args.Buffer,checkLenght, cmd.Buffer.Length);
                args.SetBuffer(0, cmd.Buffer.Length + checkLenght);
                if (!this.ListenerSocket.SendToAsync(args)) SendCompleted(null,args);
            }
        }
 public void Clear()
 {
     SendBuffer.Clear();
     lock (clearLock)
     {
         if (InterimPacketBuffer != null)
         {
             InterimPacketBuffer.Clear();
             BufferPool.Push(InterimPacketBuffer);
             InterimPacketBuffer = null;
         }
         while (ReceiveBuffers.Count > 0)
         {
             var packetBuffer = ReceiveBuffers.Dequeue();
             packetBuffer.Clear();
             BufferPool.Push(packetBuffer);
         }
     }
     NoComplateCmd = null;
     alreadyReceivePacketLength = 0;
     needReceivePacketLenght = 0;
 }
        private void SendProcess()
        {
            int surplus = SendBuffer.Buffer.Length;
            while (cmdQueue.Count > 0)
            {
                if (NoComplateCmd != null)
                {
                    int noComplateLength = NoComplateCmd.Buffer.Length - NoComplateCmd.Offset;
                    if (noComplateLength <= SendBuffer.Buffer.Length)
                    {
                        SendBuffer.WriteBuffer(NoComplateCmd.Buffer, NoComplateCmd.Offset, noComplateLength);
                        surplus -= noComplateLength;
                        NoComplateCmd = null;
                    }
                    else
                    {
                        SendBuffer.WriteBuffer(NoComplateCmd.Buffer, NoComplateCmd.Offset, SendBuffer.Buffer.Length);
                        NoComplateCmd.Offset += SendBuffer.Buffer.Length;
                        surplus -= SendBuffer.Buffer.Length;
                        break;//跳出当前循环
                    }
                }
                if (surplus >= intByteLength)
                {
                    var cmd = cmdQueue.Dequeue();
                    var cmdAllLength = cmd.Buffer.Length + checkandCmdLength;
                    if (cmdAllLength <= surplus)
                    {
                        SendBuffer.WriteInt(cmd.Buffer.Length+shortByteLength, false); //写入总大小
                        SendBuffer.WriteShort(cmd.CommondId, false); //写入命令编号
                        SendBuffer.WriteBuffer(cmd.Buffer); //写入命令内容
                        surplus -= cmdAllLength;
                    }
                    else
                    {
                        SendBuffer.WriteInt(cmd.Buffer.Length, false); //写入总大小

                        surplus -= cmd.Buffer.Length;

                        if (surplus >= shortByteLength)
                        {
                            SendBuffer.WriteShort(cmd.CommondId, false); //写入命令编号
                            surplus -= shortByteLength;
                        }
                        if (surplus > 0)
                        {
                            SendBuffer.WriteBuffer(cmd.Buffer, cmd.Offset, surplus); //写入命令内容
                            cmd.Offset = surplus;
                        }
                        NoComplateCmd = cmd;//把未全部发送指令缓存
                    }
                }
                else
                {
                    break;
                }
            }
            if (surplus < SendBuffer.Buffer.Length)
            {
                Session.SendEventArgs.SetBuffer(SendBuffer.Buffer, 0, SendBuffer.DataSize);
                if (Session.ConnectSocket != null)
                {
                    bool willRaiseEvent = Session.ConnectSocket.SendAsync(Session.SendEventArgs);
                    if (!willRaiseEvent)
                    {
                        SendComplate(null, Session.SendEventArgs);
                    }
                }
                else
                {
                    Interlocked.Decrement(ref isSend);
                }
            }
            else
            {
                Interlocked.Decrement(ref isSend);
            }
        }
 public bool SendAsync(SendCommond cmd)
 {
     cmdQueue.Enqueue(cmd);
     if (Interlocked.Increment(ref isSend) == 1)
     {
         if (Session.ConnectSocket != null)
         {
             Task.Run(() =>
             {
                 SendProcess();
             });
         }
         else
         {
             return false;
         }
     }
     else
     {
         Interlocked.Decrement(ref isSend);
     }
     return true;
 }
Example #5
0
 public bool SendAsync(SendCommond cmd)
 {
     cmdQueue.Enqueue(cmd);
     if (Interlocked.Increment(ref isSend) == 1)
         Task.Run(() =>
         {
             SendProcess();
         });
     else
     {
         Interlocked.Decrement(ref isSend);
     }
     return true;
 }
Example #6
0
 /// <summary>
 /// 发送指令
 /// </summary>
 /// <param name="cmd"></param>
 public void SendAsync(SendCommond cmd)
 {
     PacketProtocol.SendAsync(cmd);
 }