Beispiel #1
0
 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;
 }
        public void SendProcess()
        {
            SendBuffer.Clear(); //清除已发送的包
            int surplus = SendBuffer.Buffer.Length;

            FlushBuffer(ref surplus);
            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 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;
 }
Beispiel #4
0
 public void SendMessage(string msg)
 {
     lock (SendBuffer)
     {
         SendBuffer.Clear();
         SendBuffer.Write(msg);
         tcpClient.Client.Send(SendBuffer.Buffer, 0, SendBuffer.DataCount, SocketFlags.None);
     }
 }
Beispiel #5
0
 public void SendMessage(DataBase db)
 {
     lock (SendBuffer)
     {
         SendBuffer.Clear();
         SendBuffer.Write(db);
         tcpClient.Client.Send(SendBuffer.Buffer, 0, SendBuffer.DataCount, SocketFlags.None);
     }
 }
Beispiel #6
0
 /// <summary>
 /// Reset the object to init state.
 /// </summary>
 public void Reset()
 {
     Tag = null;
     // ClientSocket = null;
     // ClientID = default(long);
     IPEndPoint = null;
     ReceiveBuffer?.Clear();
     SendBuffer?.Clear();
     RecvSpeedController?.Reset();
     SendController?.Reset();
 }
 public void Clear()
 {
     SendBuffer.Clear();
     lock (clearLock)
     {
         isSend = false;
         if (sendDataQueue.Count > 0)
         {
             SpinWait spinWait = new SpinWait();
             byte[]   cmd;
             while (sendDataQueue.TryDequeue(out cmd))
             {
                 spinWait.SpinOnce();
             }
         }
     }
     noComplateBytes         = null;
     needReceivePacketLenght = 0;
 }
Beispiel #8
0
 public void SendComplate(object sender, SocketAsyncEventArgs sendEventArgs)
 {
     Session.ActiveDateTime = DateTime.Now;//发送数据视为活跃
     if (sendEventArgs.SocketError == SocketError.Success)
     {
         SendBuffer.Clear(); //清除已发送的包
         if (Session.ConnectSocket != null)
         {
             SendProcess();//继续发送
         }
     }
     else
     {
         lock (closeLock)
         {
             DisConnect();
         }
     }
 }
Beispiel #9
0
    public byte[] GetSendBytes()
    {
        List <byte> send = null;

        lock (SendBuffer)
        {
            send = new List <byte>();

            int    length  = SendBuffer.Count;
            byte[] lengthB = BitConverter.GetBytes(length);
            send.AddRange(lengthB);

            byte[] body = SendBuffer.ToArray();
            send.AddRange(body);
            //
            SendBuffer.Clear();
        }
        return(send.ToArray());
    }
Beispiel #10
0
 /// <summary>
 /// Reset the object to init state.
 /// </summary>
 public void Reset()
 {
     Tag          = null;
     ClientSocket = null;
     ClientID     = default(long);
     IPEndPoint   = null;
     ReceiveBuffer?.Clear();
     SendBuffer?.Clear();
     Groups = null;
     Status = ClientStatus.Closed;
     RecvSpeedController?.Reset();
     SendController?.Reset();
     RecvRawMessage.ClientID       = default(long);
     RecvRawMessage.MessageRawData = default(ArraySegment <byte>);
     if (SockAsyncArgs != null)
     {
         SockAsyncArgs.AcceptSocket = null;
         SockAsyncArgs.UserToken    = null;
     }
 }
Beispiel #11
0
 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;
 }
Beispiel #12
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 -= surplus;
                        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;
            }
        }