Example #1
0
 public void SendAsync(EndPoint endPoint, SendData cmd)
 {
     var args = sendArgsPool.Pop();
     if (args == null)
     {
         while (args != null)
         {
             args= sendArgsPool.Pop();
         }
     }
     if (cmd.Data.Length + checkLenght > args.Buffer.Length)
         throw new Exception("发送的数据大于buffer最大长度");
     else
     {
         args.RemoteEndPoint = endPoint;
         byte[] length = BitConverter.GetBytes(cmd.Data.Length);
         System.Buffer.BlockCopy(length, 0, args.Buffer, 0, length.Length);
         System.Buffer.BlockCopy(cmd.Data, 0, args.Buffer,checkLenght, cmd.Data.Length);
         args.SetBuffer(0, cmd.Data.Length + checkLenght);
         if (!this.ListenerSocket.SendToAsync(args)) SendCompleted(null,args);
     }
 }
Example #2
0
 /// <summary>
 /// 发送指令
 /// </summary>
 /// <param name="data"></param>
 public void SendAsync(byte[] data)
 {
     SendData sendData = new SendData() { Data = data };
     PacketProtocol.SendAsync(sendData);
 }
 public void SendAsync(SendData cmd)
 {
     PacketProtocol.SendAsync(cmd);
 }
Example #4
0
 public void SendAsync(SendData cmd)
 {
     Server.SendAsync(RemoteEndPoint, cmd);
 }
 public void Clear()
 {
     SendBuffer.Clear();
     lock (clearLock)
     {
         isSend = false;
         if (sendDataQueue.Count > 0)
         {
             SendData cmd;
             while (sendDataQueue.TryDequeue(out cmd))
             {
             }
         }
         if (InterimPacketBuffer != null)
         {
             BufferPool.Push(InterimPacketBuffer);
             InterimPacketBuffer = null;
         }
         while (ReceiveBuffers.Count > 0)
         {
             var packetBuffer = ReceiveBuffers.Dequeue();
             BufferPool.Push(packetBuffer);
         }
     }
     NoComplateCmd = null;
     alreadyReceivePacketLength = 0;
     needReceivePacketLenght = 0;
 }
 public void SendProcess()
 {
     SendBuffer.Clear(); //清除已发送的包
     int surplus = SendBuffer.Buffer.Length;
     while (sendDataQueue.Count > 0)
     {
         if (NoComplateCmd != null)
         {
             int noComplateLength = NoComplateCmd.Data.Length - NoComplateCmd.Offset;
             if (noComplateLength <= surplus)
             {
                 SendBuffer.WriteBuffer(NoComplateCmd.Data, NoComplateCmd.Offset, noComplateLength);
                 surplus -= noComplateLength;
                 NoComplateCmd = null;
             }
             else
             {
                 SendBuffer.WriteBuffer(NoComplateCmd.Data, NoComplateCmd.Offset,surplus);
                 NoComplateCmd.Offset += surplus;
                 surplus = 0;
                 break;//跳出当前循环
             }
         }
         if (surplus >= intByteLength)
         {
             SendData data;
             if (sendDataQueue.TryDequeue(out data))
             {
                 var PacketAllLength = data.Data.Length + intByteLength;
                 if (PacketAllLength <= surplus)
                 {
                     SendBuffer.WriteInt(data.Data.Length, false); //写入总大小
                     SendBuffer.WriteBuffer(data.Data); //写入命令内容
                     surplus -= PacketAllLength;
                 }
                 else
                 {
                     SendBuffer.WriteInt(data.Data.Length, false); //写入总大小
                     surplus -= intByteLength;
                     if (surplus > 0)
                     {
                         SendBuffer.WriteBuffer(data.Data, data.Offset, surplus); //写入命令内容
                         data.Offset = surplus;
                     }
                     NoComplateCmd = data;//把未全部发送指令缓存
                     break;
                 }
             }
         }
         else
         {
             break;
         }
     }
     if (surplus < SendBuffer.Buffer.Length)
     {
         if (Session.ConnectSocket != null)
         {
             Session.SendEventArgs.SetBuffer(SendBuffer.Buffer, 0, SendBuffer.DataSize);
             bool willRaiseEvent = Session.ConnectSocket.SendAsync(Session.SendEventArgs);
             if (!willRaiseEvent)
             {
                 Session.SendComplate();
             }
         }
         else
         {
             isSend = false;
         }
     }
     else
     {
         isSend = false;
     }
 }
 public void SendAsync(SendData data)
 {
     sendDataQueue.Enqueue(data);
     if (!isSend)
     {
         lock (lockObj)
         {
             if (!isSend)
             {
                 isSend = true;
                 if (Session.ConnectSocket != null)
                 {
                     SendProcess();
                 }
             }
         }
     }
 }
 public void Clear()
 {
     lock (clearLock)
     {
         isSend = false;
         if (sendDataQueue.Count > 0)
         {
             SendData cmd;
             if (!sendDataQueue.TryDequeue(out cmd))
             {
                 SpinWait spinWait = new SpinWait();
                 while (sendDataQueue.TryDequeue(out cmd))
                 {
                     spinWait.SpinOnce();
                 }
             }
         }
         if (InterimPacketBuffer != null)
         {
             Session.Pool.FixedBufferPool.Push(InterimPacketBuffer);
             InterimPacketBuffer = null;
         }
         while (ReceiveBuffers.Count > 0)
         {
             var packetBuffer = ReceiveBuffers.Dequeue();
             Session.Pool.FixedBufferPool.Push(packetBuffer);
         }
     }
     SendBuffer.Clear();
     NoComplateCmd = null;
     alreadyReceivePacketLength = 0;
     needReceivePacketLenght = 0;
 }