Asp.Net教程,WinForm教程,Asp.Net MVC,vs2008教程,vs2010教程,Silverlight技术,源码下载,Asp.Net视频教程
全站热门标签
vs2010 Silverlight 存储过程 水晶报表 LINQ ADO.NET JavaScript DataGridView GridView AjaxPro DevExpress 面向对象 Extjs XML HTML教程 Oracle jQuery WPF MVC 分页 Office2010 GDI+ Visual C++2010 Dojo MySQL VB.NET WCF4.0 ASPxGridView WinForm textbox Sql2005 cookie Discuz!NT checkbox WCF SQL经典语句 T-SQL F# asp.net VS2008 SQL Ajax DropDownList VS2008新特性 TreeView Access Flex 页面执行时间 字符串 DataSet VB2005 回调 动画 C#时间 游戏 ASP.NET MVC
FrameworkC#技术 VB.NET VC.NET WCF WPF
当前位置: 主页 > WinForm教程 > C#技术 >

Socket开发之通讯协议及处理(2)

时间:2010-08-31 23:33来源:未知 作者:admin 点击:

public class MessageStream
  2    {
  3        private byte[] _buffer;
  4        private int _position;
  5        private int _length;
  6        private int _capacity;
  7
  8        public MessageStream()
  9        {
 10            _buffer = new byte[0];
 11            _position = 0;
 12            _length = 0;
 13            _capacity = 0;
 14        }
 15
 16        private byte ReadByte()
 17        {
 18            if (this._position >= this._length)
 19            {
 20                return 0;
 21            }
 22            return this._buffer[this._position++];
 23        }
 24
 25        private int ReadInt()
 26        {
 27            int num = this._position += 4;
 28            if (num > this._length)
 29            {
 30                this._position = this._length;
 31                return -1;
 32            }
 33            return (((this._buffer[num - 4] | (this._buffer[num - 3] << 8)) | (this._buffer[num - 2] << 0x10)) | (this._buffer[num - 1] << 0x18));
 34        }
 35
 36        private byte[] ReadBytes(int count)
 37        {
 38            int num = this._length - this._position;
 39            if (num > count)
 40            {
 41                num = count;
 42            }
 43            if (num <= 0)
 44            {
 45                return null;
 46            }
 47            byte[] buffer = new byte[num];
 48            if (num <= 8)
 49            {
 50                int num2 = num;
 51                while (--num2 >= 0)
 52                {
 53                    buffer[num2] = this._buffer[this._position + num2];
 54                }
 55            }
 56            else
 57            {
 58                Buffer.BlockCopy(this._buffer, this._position, buffer, 0, num);
 59            }
 60            this._position += num;
 61            return buffer;
 62        }
 63
 64        public bool Read(out Message message)
 65        {
 66            message = null;
 67            _position = 0;
 68            if (_length > 6)
 69            {
 70                message = new Message();
 71                message.Class = ReadByte();
 72                message.Flag = ReadByte();
 73                message.Size = ReadInt();
 74                if (message.Size <= 0 || message.Size <= _length - _position)
 75                {
 76                    if (message.Size > 0)
 77                    {
 78                        message.Content = ReadBytes(message.Size);
 79                    }
 80                    Remove(message.Size + 6);
 81                    return true;
 82                }
 83                else
 84                {
 85                    message = null;
 86                    return false;
 87                }
 88            }
 89            else
 90            {
 91                return false;
 92            }
 93        }
 94
 95        private void EnsureCapacity(int value)
 96        {
 97            if (value <= this._capacity)
 98                return;
 99            int num1 = value;
100            if (num1 < 0x100)
101                num1 = 0x100;
102            if (num1 < (this._capacity * 2))
103                num1 = this._capacity * 2;
104            byte[] buffer1 = new byte[num1];
105            if (this._length > 0)
106                Buffer.BlockCopy(this._buffer, 0, buffer1, 0, this._length);
107            this._buffer = buffer1;
108            this._capacity = num1;
109        }
110
111        public void Write(byte[] buffer, int offset, int count)
112        {
113            if (buffer.Length - offset < count)
114            {
115                count = buffer.Length - offset;
116            }
117            EnsureCapacity(buffer.Length + count);
118            Array.Clear(_buffer, _length, _capacity - _length);
119            Buffer.BlockCopy(buffer, offset, _buffer, _length, count);
120            _length += count;
121        }
122
123        private void Remove(int count)
124        {
125            if (_length >= count)
126            {
127                Buffer.BlockCopy(_buffer, count, _buffer, 0, _length - count);
128                _length -= count;
129                Array.Clear(_buffer, _length, _capacity - _length);
130            }
131            else
132            {
133                _length = 0;
134                Array.Clear(_buffer, 0, _capacity);
135            }
136        }
137    }
这个类的使用非常简单,你只要用Write(byte[] buffer, int offset, int count)将接收到的数据写入数据流中,并用bool Read(out Message message)将数据中的第一个数据包取出,如果函数返回True,就说明取回一个封包成功,如果返回False,则说明流中已经没有完整的封包,你需要继续接收后面的数据以组成一个完整的封包。

这们我们的数据分析就会变得非常简单。我们可以在ReceiveCallBack回调函数中将接收到的数据写入到流中并通知线程池中的工作者线程分析数据流并处理数据。我在前面的关于Socket异步操作的文章中的Analyzer函数就是用这两个类来分析处理数据的。这样的好处理就是,Socket工作线程只需要负责数据的接收,并将其写入流,其它的事情由其它的线程这处理,就不会因为处理的时间过长而导致接收操作被阻塞。从而影响Socket的性能。

Tags:Socket
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价