Beetle.Express是Beetle相應的免費版Tcp通訊組件,Beetle.Express提供基礎的TCP通訊封裝,雖然只具備了基礎功能,但在TCP吞吐上有着和Beetle一樣出色的性能.組件是通過隊列的方式處理控制TCP數據的發送和接收,在發送數據的時候你只關心操作即可,不必關心不停地調用發送方法會不會導致緩存區益出的情況.組件還提供了用於管理連接接入,斷開和數據接收等事件,使開發人員對於連接的管理更方便.
使用配置
組件在使用之前需要進行一個簡單的配置,主要是描述服務端的一個監聽等信息
<configSections> <section name="serverSection" type="Beetle.Express.ServerSection, Beetle.Express"/> </configSections> <serverSection host="" port="8088" receiveBufferSize="4096" receiveDataPoolSize="1000" sendBufferSize="4096" handler="HelloWorld.Program,HelloWorld"/>以簡配置在所有IP的8088端口打開監聽服務
服務代碼
配置完成后,只需要針對相關配置打開TCP服務即可
class Program : IServerHandler { static void Main(string[] args) { TcpServer server = new TcpServer(); server.Open("serverSection"); Console.WriteLine("Start {0}@{1}", server.Host, server.Port); System.Threading.Thread.Sleep(-1); } public void Connect(TcpServer server, ChannelConnectEventArgs e) { Console.WriteLine("{0} connected", e.Channel.EndPoint); } public void Disposed(TcpServer server, ChannelEventArgs e) { Console.WriteLine("{0} disposed", e.Channel.EndPoint); } public void Error(TcpServer server, ErrorEventArgs e) { Console.WriteLine("{0} error:{1}", e.Channel.EndPoint, e.Error.Message); } public void Receive(TcpServer server, ChannelReceiveEventArgs e) { Data data = new Data(); byte[] hw = Encoding.ASCII.GetBytes("Hello World"); data.SetBuffer(hw, hw.Length); server.Send(data, e.Channel); } public void SendCompleted(TcpServer server, ChannelSendEventArgs e) { } }
這樣一個簡單的Hello Word程序就完成,你只需要簡單地telnet到這個端口並發收信息就會收到一個Hello Word的回復
組件性能
在網絡吞吐能力方面組件繼承了beetle出色的處理能力.在一台E1230V的PC上5000個長連接,每秒處理5W的請求應答其CPU使用率不到10%.
- 1000連接測試情況
- 5000連接測試情況