using System; using System.Diagnostics; namespace Bro.Common.Link { /// /// 设备的连接基类 /// public abstract class LinkBase { // 默认保存数据缓存长度 public const int DFT_BUFF_LEN = 4096; // 连接对象接收缓存大小 public const int DFT_SEND_BUFF_LEN = 4096; // 连接对象发送缓存大小 public const int DFT_RECV_BUFF_LEN = 4096; // 连接描述 public string Desp { get;set;} // 初始化状态 public bool IsInit { get;set; } // 连接状态 public bool IsConn { get;set;} // 接收缓存长度 public int BufferLen { get;set;} // 接收缓存大小 public int SendBuffLen { get;set;} // 发送缓存大小 public int RecvBuffLen { get;set;} // 接收缓存 protected byte[] rcvBuf = null; // 连接状态 public LinkBase() { Desp = this.GetType().Name; IsInit = false; IsConn = false; SendBuffLen = DFT_SEND_BUFF_LEN; RecvBuffLen = DFT_RECV_BUFF_LEN; BufferLen = DFT_BUFF_LEN; } /// /// 连接初始化 /// /// public virtual bool Init() { if ( IsInit ) { return true; } try { rcvBuf = new byte[BufferLen]; return true; } catch(Exception ex) { Trace.TraceError("Link:{0} Init fail, ex:{1}-{2}", this.Desp, ex.Message, ex.StackTrace); } return false; } /// /// 打开连接 /// /// public abstract bool Open(); /// /// 关闭连接 /// public abstract void Close(); /// /// 销毁 /// public abstract void Fnit(); /// /// 接收数据 /// /// /// /// public abstract int Recv(byte[] rcvBuf, int offset); /// /// 发送数据 /// /// /// public abstract bool Send(byte[] sndBuf, int offset, int len); } }