Beispiel #1
0
    /// <summary>
    /// 如果数据满了,且缓冲区内的数据是空的则返回需要发送的数据
    /// 调用Enqueue(...)后调用Dequeue(...),不能直接调用Dequeue(...)
    /// </summary>
    /// <param name="byteBuffer"></param>
    /// <param name="iOffset"></param>
    /// <param name="iLength"></param>
    /// <returns></returns>
    public void Enqueue(byte[] byteBuffer, long iOffset, long iLength)
    {
        if (byteBuffer == null)
        {
            throw new Exception("SendQueue.Enqueue(...) - byteBuffer == null error!");
        }

        if (iOffset < 0 || iOffset >= byteBuffer.Length)
        {
            throw new Exception("SendQueue.Enqueue(...) - iOffset < 0 || iOffset >= byteBuffer.Length error!");
        }

        if (iLength < 0 || iLength > byteBuffer.Length) // 如果iLength == 0就返回空,如果iLength == 0就跳过
        {
            throw new Exception("SendQueue.Enqueue(...) - iLength < 0 || iLength > byteBuffer.Length error!");
        }

        if ((byteBuffer.Length - iOffset) < iLength)
        {
            throw new Exception("SendQueue.Enqueue(...) - ( byteBuffer.Length - iOffset ) < iLength error!");
        }

        m_LockFlushAndPending.Enter();
        {
            do
            {
                if (m_FlushBuffer.IsNull == true)
                {
                    // nothing yet buffered
                    m_FlushBuffer = SendBuffer.Instance();

                    if (m_FlushBuffer.IsNull == true)
                    {
                        throw new Exception("SendQueue.Enqueue(...) - m_FlushBuffer.IsNull == true error!");
                    }
                }

                // 当前已经写入的字节
                long iBytesWritten = m_FlushBuffer.Write(byteBuffer, iOffset, iLength);

                iOffset += iBytesWritten;
                iLength -= iBytesWritten;

                // 写入需要发送的数据的大小
                m_WaitSendSize += iBytesWritten;

                // 如果数据没有满,且数据写入完毕则退出,返回空,不添加到集合内
                if (m_FlushBuffer.IsFull == true)
                {
                    // 如果满了添加到集合内的尾处
                    m_PendingBuffer.Enqueue(m_FlushBuffer);
                    m_FlushBuffer = SendBuffer.NullBuffer; // 置空再次请求缓存
                }
            } while (iLength > 0);
        }
        m_LockFlushAndPending.Exit();
    }
Beispiel #2
0
 public void SendMessage(string msg)
 {
     lock (SendBuffer)
     {
         SendBuffer.Clear();
         SendBuffer.Write(msg);
         tcpClient.Client.Send(SendBuffer.Buffer, 0, SendBuffer.DataCount, SocketFlags.None);
     }
 }
Beispiel #3
0
 public void SendMessage(DataBase db)
 {
     lock (SendBuffer)
     {
         SendBuffer.Clear();
         SendBuffer.Write(db);
         tcpClient.Client.Send(SendBuffer.Buffer, 0, SendBuffer.DataCount, SocketFlags.None);
     }
 }
Beispiel #4
0
 /// <summary>
 /// Send bytes to server
 /// </summary>
 /// <param name="bytes"></param>
 public void Send(byte[] bytes)
 {
     lock (locker)
     {
         if (!IsConnected)
         {
             throw new Exception("From send : disconnected");
         }
         SendBuffer.Write(bytes);
     }
 }
Beispiel #5
0
        public void WriteTest()
        {
            SendBuffer target = new SendBuffer(); // TODO: 初始化为适当的值

            byte[] byteBuffer = null;             // TODO: 初始化为适当的值
            long   lOffset    = 0;                // TODO: 初始化为适当的值
            long   lLength    = 0;                // TODO: 初始化为适当的值
            long   expected   = 0;                // TODO: 初始化为适当的值
            long   actual;

            actual = target.Write(byteBuffer, lOffset, lLength);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("验证此测试方法的正确性。");
        }
Beispiel #6
0
 public void Send(byte[] bytes, int index, int length)
 {
     lock (SendBuffer)
     {
         AssertThat.IsTrue(IsConnected, "From send : disconnected");
         SendBuffer.Write(bytes, index, length);
         try
         {
             Socket.BeginSend(bytes, index, length, SocketFlags.None, EndSend, Socket);
         }
         catch (Exception e)
         {
             throw new Exception(e.ToString());
         }
     }
 }
Beispiel #7
0
 /// <summary>
 /// Send bytes to server
 /// </summary>
 /// <param name="bytes"></param>
 public void Send(byte[] bytes)
 {
     lock (locker)
     {
         if (!IsConnected)
         {
             throw new Exception("From send : disconnected");
         }
         SendBuffer.Write(bytes);//use for geting havent send successfull data
         try
         {
             Socket.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, EndSend, Socket);
         }
         catch (Exception e)
         {
             throw new Exception(e.ToString());
         }
     }
 }
Beispiel #8
0
 public void SendSocketData(SocketData sockData)
 {
     _sendBuf.Write(sockData);
 }