Using beetle. Express to easily build high-throughput TCP & UDP applications

Source: Internet
Author: User

In beetle. in express1.2, UDP support is added, and the overall design structure is also adjusted. the TCP and UDP applications with high spof can be implemented through simple configuration. because the component reference UDP Service also supports session Status, TCP or UDP is completely transparent to users. users only need to care about the following interfaces: iServer (Service Description Interface), ichannel (Channel Session), idata (send data description), ireceivedata (data receipt description) and iserverhandler (service processing interface ). when using components, you only need to implementIserverhandler.

Iser V Erhandler

This interface is used to describe the work of related services, including connection access, connection release, data receiving, data sending completion, and error handling.

/// <Summary> /// Service Processing description interface /// </Summary> Public interface iserverhandler {/// <summary> // connection creation process /// </Summary> /// <Param name = "server"> corresponding TCP & UDP Service object </param>
/// <Param name = "E"> connection details </param> void connect (iServer server, channelconnecteventargs E ); /// <summary> /// process of disconnection /// </Summary> /// <Param name = "server"> corresponding TCP & UDP Service object </ param>
/// <Param name = "E"> connection details </param> void disposed (iServer server, channeleventargs E ); /// <summary> /// error handling process /// </Summary> /// <Param name = "server"> corresponding TCP & UDP Service object </Param >/// <Param name = "E"> detailed error message </param> void error (iServer server, erroreventargs E ); /// <summary> /// data receiving and processing process /// </Summary> /// <Param name = "server"> corresponding TCP & UDP Service object </ param> // <Param name = "E"> receive data details </param> void receive (iServer server, channelreceiveeventargs E ); /// <summary> /// processing process of data sending status /// </Summary> /// <Param name = "server"> corresponding TCP & UDP Service object </param>
/// <Param name = "E"> data sending status information </param> void sendcompleted (iServer server, channelsendeventargs E );}

Use beetle. Express to build TCP or UDP services, and only implement the preceding interfaces. No additional work is required.

Class program: iserverhandler {public void connect (iServer server, channelconnecteventargs e) {console. writeline ("{0} connected @ {1}", E. channel. endpoint, server. name);} public void disposed (iServer server, channeleventargs e) {console. writeline ("{0} disposed", E. channel. endpoint);} public void error (iServer server, erroreventargs e) {console. writeline ("{0} error: {1}", E. channel. endpoint, E. error. message);} public void receive (iServer server, channelreceiveeventargs e) {string command = E. data. tostring (encoding. utf8); console. writeline ("receive: {0} \ t @ {1}", command, server. name); data Data = new data (64); data. write (server. name, encoding. utf8); server. send (data, E. channel);} public void sendcompleted (iServer server, channelsendeventargs e ){}}

The preceding figure shows how to implement a network processing service.CodeIt can be either a TCP Service or a UDP Service.Iserverhandler manages service processing in a unified manner. Therefore, this implementation depends on the TCP or UDP Service.Configuration Information loaded by serverfactory. In fact, n tcp services and UDP services can be bound to the handler at the same time.

Bind Service

A component is a service description by configuration. Multiple TCP and UDP services can be described at the same time.Start TCP and UDP services in the same amount on iserverhandler.

<? XML version = "1.0"?> <Configuration> <configsections> <section name = "serversection" type = "beetle. express. serversection, beetle. express "/> </configsections> <serversection xmlns =" urn: beetle. express "> <listens> <Add name =" test_tcp "type =" TCP "Port =" 8088 "handler =" beetle. express. sample. program, beetle. express. sample "/> <Add name =" test_udp "type =" UDP "Port =" 8089 "handler =" beetle. express. sample. program, beetle. express. sample "/> </listens> </serversection> <startup> <supportedruntime version =" v4.0 "SKU = ". netframework, version = v4.0 "/> </startup> </configuration>

The preceding configuration enables TCP Service on port 8088 and UDP Service on port 8089 on the same handler respectively.Load serverfactory.

Static serverfactory mfactory; static void main (string [] ARGs) {mfactory = new serverfactory ("serversection"); foreach (iServer item in mfactory. servers) {console. writeline ("{0} start @ {1}", item. name, item. port);} system. threading. thread. sleep (-1 );}

Through beetle. express can easily build TCP and UDP services. During the purchase process, you do not need to care about socket-related details, the connection processing and data sending and receiving details are completely transparent to users. the components also provide LRU-basedAlgorithmTo more easily control the connection resources.

Download Sample

Download beetle. Express and documentation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.