Socket 字节缓冲区第二种方式
来源:互联网 发布:c语言输出double类型 编辑:程序博客网 时间:2024/06/05 03:17
转载连接: http://blog.csdn.net/wyd1520/article/details/44153543
上一篇说到Socket 的字节缓冲区里的 有包头、包长度、包尾,有些数据包只有包头与包尾而里面没有包长度,且这个包头包尾是多个字节组成的。这篇文章也是应CSDN某位网友的需求对这样的数据字节缓冲区进行改造一下,不多说了上代码
-
-
-
- public class ByteQueue
- {
- private List<byte> m_buffer = new List<byte>();
- private byte[] headBuffer = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xCA, 0xCB, 0xCC, 0xCD };
- private byte[] tailBuffer = new byte[] { 0xEA, 0xEB, 0xEC, 0xED };
-
- private int IndexOf(List<byte> source, byte[] seach, int startIndex)
- {
- string baseStr = BitConverter.ToString(source.ToArray());
- string searchStr=BitConverter.ToString(seach);
- int index= baseStr.IndexOf(searchStr, startIndex);
- return index==-1?-1 : index / 3;
- }
-
-
- public bool Find()
- {
- if (m_buffer.Count == 0)
- return false;
- int HeadIndex = IndexOf(m_buffer, headBuffer,0);
- if (HeadIndex == -1)
- {
- m_buffer.Clear();
- return false;
- }
- else if (HeadIndex != 0)
- {
- if (HeadIndex > 1)
- {
- m_buffer.RemoveRange(0, HeadIndex);
- HeadIndex = 0;
- }
- }
-
-
-
- int TailIndex = IndexOf(m_buffer, tailBuffer, headBuffer.Length);
-
- if (TailIndex == -1)
- {
-
- int head = IndexOf(m_buffer, headBuffer, 0);
- if (head > -1)
- {
- m_buffer.RemoveRange(0, head);
- }
- return false;
- }
- int packLength = TailIndex - HeadIndex;
- if (packLength<0)
- {
- m_buffer.RemoveRange(0, TailIndex + tailBuffer.Length);
- return false;
- }
- return true;
- }
-
-
-
-
-
-
-
-
-
-
- private int GetLength()
- {
- int len = IndexOf(m_buffer, tailBuffer, headBuffer.Length) + tailBuffer.Length;
- return len;
- }
-
-
-
- public byte[] Dequeue()
- {
- int length = GetLength();
- List<byte> result = m_buffer.Take(length).ToList();
- result.RemoveRange(0, headBuffer.Length);
- result.RemoveRange(result.Count - tailBuffer.Length, tailBuffer.Length);
- m_buffer.RemoveRange(0, length);
- return result.ToArray();
-
- }
-
-
-
-
-
- public void Enqueue(byte[] buffer)
- {
- m_buffer.AddRange(buffer);
- }
- }
调用例子:
- public partial class MainForm : Form
- {
- public MainForm()
- {
- InitializeComponent();
- }
- ByteQueue queue = new ByteQueue();
-
- private void button1_Click(object sender, EventArgs e)
- {
-
- byte[] buffer = System.Text.Encoding.UTF8.GetBytes(textBox1.Text);
- queue.Enqueue(CreatePack(buffer));
-
-
- }
- private void button2_Click(object sender, EventArgs e)
- {
- queue.Enqueue(CreatePack(buffer));
- while (queue.Find())
- {
-
- byte[] buffer = queue.Dequeue();
- textBox2.Text += System.Text.Encoding.UTF8.GetString(buffer) + "\r\n";
-
- }
- }
-
- private byte[] CreatePack(byte[] body)
- {
- byte[] headbuff = new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xCA, 0xCB, 0xCC, 0xCD };
- byte[] tailbuff = new byte[] { 0xEA, 0xEB, 0xEC, 0xED };
- byte[] buffer = new byte[headbuff.Length + tailbuff.Length + body.Length];
-
- Buffer.BlockCopy(headbuff, 0, buffer, 0, headbuff.Length);
- Buffer.BlockCopy(body, 0, buffer, headbuff.Length, body.Length);
- Buffer.BlockCopy(tailbuff, 0, buffer, headbuff.Length + body.Length, tailbuff.Length);
- return buffer;
- }
-
-
- }
放到串口或Socke的例子:
- private ByteQueue queue = new ByteQueue();
- private void socket_DataReceived(object sender, SerialDataReceivedEventArgs e)
- {
- int len = serialPort1.BytesToRead;
- if (len > 0)
- {
- byte[] temp = new byte[len];
- serialPort1.Read(temp, 0, len);
- queue.Enqueue(temp);
- while (queue.Find())
- {
- byte[] readBuffer = queue.Dequeue();
- OnReceiveData(readBuffer);
- }
-
- }
-
- }