One: What is Helios?
Helios is a high-performance socket communication middleware, written in C #. The development of Helios is inspired by Netty and uses a non-blocking event-driven model architecture to achieve high concurrency and high throughput. Helios for us greatly simplifies the socket programming, it has been for us to deal with high concurrency cases of unpacking, sticky packet, buffer management and so on.
github:https://github.com/helios-io/helios/
In order to avoid misunderstanding special tip: Helios is not my work, the younger brother is still on the road.
II: Features of Helios 1.Powerful APIs
Takes the complexity out of sockets programming with intelligent I/O, concurrency, buffer management, and pipelining APIs.
Programming with sockets is no longer complex. Provides intelligent I/O, concurrency, buffer management, and pipeline forms of APIs.
2.event-driven
Helios is reactive-it uses a Event-driven architecture to simplify development and build responsive systems.
Helios is reactive and uses an event-driven architecture to simplify development and build scalable systems.
3.Performant
Performance is a cross-cutting concern we factor on every level in the design of the framework in order to eliminate OV Erhead for your apps and clients.
This system is developed and designed to take full account of performance, when building your app and client, please eliminate this concern.
4.battle-tested
Helios powers the clustering and remoting capbilities built into akka.net and more.
Akka.net cluster, the remote function is built on the Helios.
Three: an example of a Helios-based chat room
The best example to use to demonstrate socket communication is the chat program.
The entire solution consists of 3 projects:
1.helioschat.common
There are some common types in this project, and using NuGet to add Helios libraries after the new
Message class: All messages sent are wrapped in a message, each with a command and content.
public class Message {public command command {get; set;} public string Content {get; set;} }
Command enumeration: Commands used to describe a message
Public enum Command { Join, Send, }
Messageconverter Static Class: This class is used to convert the message object to byte[], or to convert byte[] to a message object. The message object needs to be transferred to byte[] when it is transmitted via Helios, so we need to define the format of the package ourselves. We use the first four bits of byte[] to store command,content into byte and start from 5th place.
public class Messageconverter {public static Message Tomessage (Networkdata data) {try {var commanddata = data. Buffer.take (4). ToArray (); var contentdata = data. Buffer.skip (4). Take (data. BUFFER.LENGTH-4). ToArray (); var command = Bitconverter.toint32 (commanddata,0); var content = Encoding.UTF8.GetString (contentdata); return new Message () {command = (command) command, content = Content }; } catch (Exception exc) {Console.WriteLine ("Cant convert Networkdata to Message: {0 } ", exc. Message); } return null; } public static byte[] tobytes (Message message) {try {var commandbyte s = bitconverter.getbytes ((int) message.command); var messagebytes = Encoding.UTF8.GetBytes (mesSage. Content); var bytes = new Byte[commandbytes.length + messagebytes.length]; Commandbytes.copyto (bytes, 0); Messagebytes.copyto (bytes, commandbytes.length); return bytes; } catch (Exception exc) {Console.WriteLine ("Cant convert message to bytes: {0}", ex C.message); } return null; } }
2.helioschat.server
Needless to say, this is the service side of the chat room, which is responsible for connecting users and forwarding messages.
Internal class Program {private static readonly concurrentdictionary<string, iconnection> clients = New concurrentdictionary<string, iconnection> (); private static void Main (string[] args) {var host = Ipaddress.any; var port = 9991; Console.title = "Server"; Console.WriteLine ("Starting server on {0}:{1}", host, Port); var serverfactory = new Serverbootstrap (). Settransport (TRANSPORTTYPE.TCP). Build (); var server = Serverfactory.newreactor (Nodebuilder.buildnode (). Host (host). Withport (port)); Server. OnConnection + = (address, connection) = {Console.WriteLine ("Connected: {0}", address); Connection. BeginReceive (Receive); }; Server. OnDisconnection + = (reason, address) = Console.WriteLine ("Disconnected: {0}; Reason: {1} ", address. ReMotehost, reason. Type); Server. Start (); Console.WriteLine ("Running, press any key to exit"); Console.readkey (); }///<summary>///processing received messages//</summary>//<param name= "Data" ></para m>//<param name= "channel" ></param> public static void Receive (Networkdata data, Iconnectio N Channel) {var message = messageconverter.tomessage (data); Switch (message.command) {case Command.Join:JoinGroup (message). Content, channel); Break Case Command.Send:Broadcast (message. Content); Break }} public static void Joingroup (String clientName, Iconnection channel) {if (clients.try ADD (ClientName, channel)) {Broadcast (string. Format ("{0} Join group successful.", ClientName)); } else {var errmsg = new Message () {Command = Com Mand. Send, Content = "client name is used." }; SendMessage (channel, errmsg); }}///<summary>//Radio messages//</summary>//<param name= "Clientmessage "></param> public static void broadcast (String clientmessage) {Console.WriteLine (client Message); var clientName = clientmessage.split (': ') [0]; var message = new Message {Command = command.send, Content = Clientmessage }; foreach (var client in clients) {if (client. Key! = clientName) {SendMessage (client. Value, message); }}} public static void SendMessage (Iconnection connection,Message message) {var messagebytes = messageconverter.tobytes (message); Connection. Send (new Networkdata {Buffer = messagebytes, Length = messagebytes.length}); } }
3.helioschat.client
Client for Chat Service
Internal class Program {public static iconnection Client; public static string ClientName; private static void Main (string[] args) {var host = Ipaddress.loopback; var port = 9991; var connectionfactory = new Clientbootstrap (). Settransport (TRANSPORTTYPE.TCP). Build (); New A client client = Connectionfactory.newconnection (Node.empty (), Nodebuilder.buildnode (). Host (host). Withport (port)); Client.onconnection + = (address, connection) = {Console.WriteLine ("Connect Server Successfu L. "); Connection. BeginReceive (Received); }; Client.ondisconnection + = (address, reason) = Console.WriteLine ("Disconnected."); Console.WriteLine ("Input ClientName"); ClientName = Console.ReadLine (); Console.title = string. Format ("Client {0}", ClientName); Establish a connection Client.open (); Join the chat Group join (); Wait for input waitinput (); public static void Waitinput () {while (true) {var input = CONSOLE.R Eadline (); if (!string. IsNullOrEmpty (Input)) {var message = Makesendmessage (input); SendMessage (Client, message); }}}///<summary>//Jion chat Group///</summary> Public St atic void Join () {var message = Makejoinmessage (); SendMessage (Client,message); }///<summary>///processing received messages//</summary>//<param name= "Data" ></para m>//<param name= "Responsechannel" ></param> public static void Received (Networkdata data, I Connection responsechannel) {var message = messageconverter.tomessage (data); if (Message.command = = Command.send) {Console.WriteLine (message. Content); }}///<summary>//Structuring Chat messages///</summary>//<param name= "input" >& lt;/param>//<returns></returns> public static Message makesendmessage (String input) {return new Message {Command = command.send, Content = string. Format ("{0}:{1}", ClientName, Input)}; }///<summary>//construct a message to join the group//</summary>//<returns></returns> public static Message Makejoinmessage () {var message = new Message (); Message.command = Command.join; Message. Content = ClientName; return message; public static void SendMessage (iconnection connection, message message) {var messagebytes =Messageconverter.tobytes (message); Connection. Send (new Networkdata {Buffer = messagebytes, Length = messagebytes.length}); } }
4. Running Results
Such a simple chat room program is complete.
Four: Helios 2.0
The asynchronous programming model of Helios 1.0 is based on APM, and is changed from Helios 2.0 to SocketAsyncEventArgs mode for asynchronous implementation. The SocketAsyncEventArgs low-level package IOCP,IOCP is the highest performance technology on Windows Server for socket communication, with IOCP Helios 2.0 bound to have higher performance, so for Helios 2.0 is still very much looking forward to.
Sample Download: http://files.cnblogs.com/files/kklldog/HeliosChat.7z
. NET open source high performance socket communication middleware Helios Introduction and demonstration