/// <summary>
        /// 发送数据,按照以下格式发送数据:
        /// |数据长度n|需要发送的数据
        ///    4字节        n字节
        /// </summary>
        /// <param name="data">需要发送的数据</param>
        /// <param name="offset">需要发送数据的偏移量</param>
        /// <param name="size">需要发送数据的长度</param>
        public void Send(Byte[] data, int offset, int size)
        {
            if (!IsConnecting)
            {
                throw new Exception("没有连接,无法发送数据!");
            }

            lock (m_lockobject)
            {
                if (data == null || data.Length == 0)
                {
                    return;
                }
                //计算数据的长度,并转换成字节数组,作为本次发送的头部
                //byte[] length = BitConverter.GetBytes(size);
                //Byte[] buffer = new Byte[size + length.Length];

                //Array.Copy(length, 0, buffer, 0, length.Length);
                //Array.Copy(data, offset, buffer, length.Length, size);
                //设置发送Buffer
                SendEventArgs.SetBuffer(data, 0, data.Length);

                SendResetEvent.Reset();
                Client.SendAsync(SendEventArgs);
                //等待发送成功的信息,只有收到该信息,才退出lock锁,
                //这样,确保只有当前面得数据发送完后,才发送下一段数据
                SendResetEvent.WaitOne();
            }
        }
Exemple #2
0
        public bool SendAsync(byte[] bytes)
        {
            SendBuffer.StartPacket();
            // 写入包头
            SendBuffer.Buffer.WriteInt16(Packet.HEAD);
            // 写入包长
            SendBuffer.Buffer.WriteInt32(bytes.Length);
            // 写入数据
            SendBuffer.Buffer.WriteBytes(bytes, 0, bytes.Length);
            SendBuffer.EndPacket();
            bool result = true;

            if (!mSendAsync)
            {
                int count  = 0;
                int offset = 0;
                if (SendBuffer.GetFirstPacket(ref offset, ref count))
                {
                    mSendAsync = true;
                    SendEventArgs.SetBuffer(SendBuffer.Buffer.Bytes, offset, count);
                    result = AsyncNet.SendAsync(this);
                }
            }
            return(result);
        }
        /// <summary>
        /// 检查队列里是否有要发送的数据,如果有则进行发送处理
        /// </summary>
        private void PeekSend()
        {
            if (isClose)
            {
                return;
            }

#if !UNITY_IPHONE
            lock (m_PendingBuffer)
#endif
            {
                if (isSending || m_PendingBuffer.Count == 0)
                {
                    return;
                }

                //  TODO 这里要不要考虑进行并报发送处理
                isSending = true;

                DogBuffer buff;
                buff = m_PendingBuffer.Dequeue();
                SendEventArgs.UserToken = buff;
                SendEventArgs.SetBuffer(buff.Bytes, 0, buff.Length);
                Socket.SendAsync(SendEventArgs);
            }
        }
        void Send()
        {
            bool SendAsync = false;

            if (Interlocked.CompareExchange(ref _SendLocked, 1, 0) != 1)
            {
                while (_SendStream.DataAvailable)
                {
                    int bytes = _SendStream.Peek(_SendBuffer, 0, _SendBuffer.Length);
                    if (bytes <= 0)
                    {
                        break;
                    }
                    SendEventArgs.SetBuffer(_SendBuffer, 0, bytes);
                    if (!_ChannelSocket.SendAsync(SendEventArgs))
                    {//Send同步完成
                        _SendStream.Skip(SendEventArgs.BytesTransferred);
                    }
                    else
                    {
                        SendAsync = true;
                        break;
                    }
                }
            }
            if (!SendAsync)
            {
                Interlocked.Exchange(ref _SendLocked, 0);
            }
        }
Exemple #5
0
        /// <summary>
        /// 检查队列里是否有要发送的数据,如果有则进行发送处理
        /// </summary>
        public void PeekSend()
        {
            lock (m_PendingBuffer)
            {
                if (isSending || m_PendingBuffer.Count == 0)
                {
                    return;
                }

                //  TODO 这里要不要考虑进行并报发送处理
                isSending = true;

                if (m_PendingBuffer.Count > 1)
                {
                    //   2 个包以上,进行拼包后再发送
                    var buffs = m_PendingBuffer.ToArray();
                    m_PendingBuffer.Clear();

                    int offSet = 0;
                    foreach (var b in buffs)
                    {
                        offSet += b.Length;
                    }

                    DogBuffer sendBuff;

                    if (offSet < 4000)
                    {
                        sendBuff = DogBuffer.GetFromPool4K();
                    }
                    else
                    {
                        sendBuff = DogBuffer.GetFromPool32K();
                    }

                    foreach (var buff in buffs)
                    {
                        Buffer.BlockCopy(buff.Bytes, 0, sendBuff.Bytes, sendBuff.Length, buff.Length);
                        sendBuff.Length += buff.Length;
                        buff.Release();
                    }

                    SendEventArgs.UserToken = sendBuff;
                    SendEventArgs.SetBuffer(sendBuff.Bytes, 0, sendBuff.Length);
                    Socket.SendAsync(SendEventArgs);
                }
                else
                {
                    DogBuffer buff;
                    buff = m_PendingBuffer.Dequeue();
                    SendEventArgs.UserToken = buff;
                    SendEventArgs.SetBuffer(buff.Bytes, 0, buff.Length);
                    Socket.SendAsync(SendEventArgs);
                }
            }
        }
Exemple #6
0
        private bool SendData()
        {
            int count  = 0;
            int offset = 0;

            if (SendBuffer.GetFirstPacket(ref offset, ref count))
            {
                isSending = true;
                SendEventArgs.SetBuffer(SendBuffer.Buffer.Bytes, offset, count);
                return(AsyncSocket.SendAsync(this));
            }
            return(true);
        }
Exemple #7
0
        public bool SendAsyncCompleted()
        {
            ActiveDateTime = DateTime.UtcNow;
            mSendAsync     = false;
            SendBuffer.ClearFirstPacket(); //清除已发送的包
            int offset = 0;
            int count  = 0;

            if (SendBuffer.GetFirstPacket(ref offset, ref count))
            {
                mSendAsync = true;
                // 设置发送的数据包
                SendEventArgs.SetBuffer(SendBuffer.Buffer.Bytes, offset, count);
                return(AsyncNet.SendAsync(this));
            }
            else
            {
                return(true);
            }
        }
Exemple #8
0
        /// <summary>
        /// 비동기 전송을 시작한다.
        /// </summary>
        private void StartSend()
        {
            byte[] buffer;
            lock (_lockSendingQueue)
            {
                // 전송이 아직 완료된 상태가 아니므로 데이터만 가져오고 큐에서 제거하진 않는다.
                buffer = _sendingQueue.Peek();
            }

            // 이번에 보낼 패킷 사이즈 만큼 버퍼 크기를 설정하고
            // 패킷 내용을 SocketAsyncEventArgs버퍼에 복사한다.
            SendEventArgs.SetBuffer(SendEventArgs.Offset, buffer.Length);
            Array.Copy(buffer, 0, SendEventArgs.Buffer, SendEventArgs.Offset, buffer.Length);


            // 비동기 전송 시작.
            bool pending = Socket.SendAsync(SendEventArgs);

            if (pending == false)
            {
                ProcessSend(SendEventArgs);
            }
        }