. NET core High performance communication open Source component Beetlex

Source: Internet
Author: User
Tags readline
Beetlex

Beetlex is a lightweight, high-performance TCP communication component based on Dotnet core that is easy to use, efficient, and secure, and is the starting point for component design! Developers can quickly build a high-performance TCP Communication Service program with the support of the BEETLX component, and secure SSL services simply by setting up SSL information in order to securely communicate.

Project address: Github.com/ikende/beetlex Domestic Address: Gitee.com/ikende/beetlex

Ease of Use

Beetlex Network stream reading and writing is based on the stream standard, and the basic reading and writing based on stream is too cumbersome for the application, and the component is more convenient for network data processing. On the basis of stream, a series of read and write rules are extended: ReadLine, ReadInt, WriteLine, Writeint, and other easy-to-use methods that allow users to process data more easily with the support of these methods In order to better compatible with other platform protocols in network communication The above methods are compatible with Big-endian and Little-endian different ways. In order to make better use of the existing serialization components, the component uses the IPacket interface to standardize the message extension, by implementing different packet interpreters, which can implement object data transmission based on Protobuf,json and Msgpack.

High performance features

Beetlex's high performance is built on the inside of a data flow processing object Pipestream, which is built on the stream standard; NET built-in NetworkStream the biggest difference is that Pipestream's read-write is based on SocketAsyncEventArgs implementation, which is the model advocated in writing high-performance network data processing. Pipestream not only has the superiority of performance in the network data processing mode, but also has the very big difference in the memory reading and writing and MemoryStream, because the Pipestream memory block is based on a linked list of SocketAsyncEventArgs Buffer , so Pipestream does not have the problem of memory expansion and replication in the case of writing big data; There is also an advantage of SocketAsyncEventArgs buffer as a base block of memory where there is no block copy of the protocol data and the network cache read-write. If the pipestream corresponding BinaryReader and Ibinarywriter read-write specifications are used in the application, most of the data processing basically does not have a memory copy process, which makes the data processing performance more efficient.

The following is the structure of the Pipestream:

Performance

Beetlex performance in the end, the following simple and dotnetty a network data exchange performance test, respectively, 1k,5k and 10K connection data request concurrency test

Dotnetty Test Code
    Public Override void Object message)        {            var as ibytebuffer;            Context. WriteAsync (message);        }
Beetlex Test Code
         Public Override void sessionreceive (IServer server, Sessionreceiveeventargs e)        {            server. Send (E.stream.topipestream (). Getreadbuffers (), e.session);             Base . Sessionreceive (server, E);        }
Test result 1K connections

5K connections

10K connections

Building a TCP Server
    classProgram:serverhandlerbase {Private StaticIServer server;  Public Static voidMain (string[] args) {netconfig config=NewNetconfig (); //SSL//CONFIG.            SSL = true; //CONFIG.            Certificatefile = @ "c:\ssltest.pfx"; //CONFIG. Certificatepassword = "123456";Server = socketfactory.createtcpserver<program>(config); Server.            Open ();            Console.Write (server);        Console.read (); }         Public Override voidsessionreceive (IServer server, Sessionreceiveeventargs e) {stringName =E.stream.topipestream ().            ReadLine ();            Console.WriteLine (name); E.session.stream.topipestream (). WriteLine ("Hello"+name);            E.session.stream.flush (); Base.        Sessionreceive (server, E); }    }
Building the TCP Client
    classProgram {Static voidMain (string[] args) {TcpClient Client= Socketfactory.createclient<tcpclient> ("127.0.0.1",9090); //SSL//TcpClient client = socketfactory.createsslclient<tcpclient> ("127.0.0.1", 9090, "localhost");             while(true) {Console.Write ("Enter Name:"); varline =Console.ReadLine (); Client. Stream.topipestream ().                WriteLine (line); Client.                Stream.flush (); varReader =client.                Read (); Line=Reader. Topipestream ().                ReadLine ();            Console.WriteLine (line); } Console.WriteLine ("Hello world!"); }    }
Async Client
    classProgram {Static voidMain (string[] args) {asynctcpclient Client= Socketfactory.createclient<asynctcpclient> ("127.0.0.1",9090); //SSL//asynctcpclient client = socketfactory.createsslclient<asynctcpclient> ("127.0.0.1", 9090, "ServiceName");Client. Clienterror = (o, e) = ={Console.WriteLine ("client Error {0}@{1}", E.message, E.error);            }; Client. Receive= (o, e) = ={Console.WriteLine (E.stream.topipestream ().            ReadLine ());            }; varPipestream =client.            Stream.topipestream (); Pipestream. WriteLine ("Hello, Henry ."); Client.            Stream.flush ();        Console.read (); }    }
Implement a Protobuf object interpreter
     Public classPacket:fixedheaderpacket { PublicPacket () {Typeheader=NewTypehandler (); }        PrivatePacketdecodecompletedeventargs Mcompletedeventargs =NewPacketdecodecompletedeventargs ();  Public voidRegister (paramsassembly[] Assemblies)        {Typeheader.register (assemblies); }         PublicImessagetypeheader Typeheader {Get;Set; }  Public OverrideIPacket Clone () {Packet result=NewPacket (); Result. Typeheader=Typeheader; returnresult; }        protected Override ObjectOnreader (isession session, Pipestream Reader) {type type=Typeheader.readtype (reader); intBodysize =Reader.            ReadInt32 (); returnReader.        Stream.deserialize (bodysize, type); }        protected Override voidOnwrite (ISession session,Objectdata, Pipestream writer)            {Typeheader.writetype (data, writer); Memoryblockcollection bodysize= writer. Allocate (4); intBodystartlegnth = (int) writer.            Cachelength; ProtoBuf.Meta.RuntimeTypeModel.Default.Serialize (writer.            Stream, data); Bodysize. Full ((int) writer. Cachelength-bodystartlegnth); }    }

Beetlex's introduction is here temporarily, if you need to know about the project can be downloaded to GitHub code and samples, if there is doubt can be gibhub on the relevant issue.

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.