.NET開源高性能Socket通信中間件Helios介紹及演示

来源:http://www.cnblogs.com/kklldog/archive/2016/01/14/helios_chat_room.html
-Advertisement-
Play Games

一:Helios是什麼 Helios是一套高性能的Socket通信中間件,使用C#編寫。Helios的開發受到Netty的啟發,使用非阻塞的事件驅動模型架構來實現高併發高吞吐量。Helios為我們大大的簡化了Socket編程,它已經為我們處理好了高併發情況下的解包,粘包,buffer管理等等。 .....


一:Helios是什麼

  Helios是一套高性能的Socket通信中間件,使用C#編寫。Helios的開發受到Netty的啟發,使用非阻塞的事件驅動模型架構來實現高併發高吞吐量。Helios為我們大大的簡化了Socket編程,它已經為我們處理好了高併發情況下的解包,粘包,buffer管理等等。

  GitHub:https://github.com/helios-io/helios/

  為避免誤會特別提示:helios不是本人作品,小弟還在努力的路上。

二:Helios的特點

  1.Powerful APIs

    Takes the complexity out of socket programming with intelligent I/O, concurrency, buffer management, and pipelining APIs.

    使用socket編程不再複雜。提供智能的I/O,併發,buffer管理,管道形式的API。

  2.Event-Driven

    Helios is Reactive - it uses a event-driven architecture to simplify development and build responsive systems that scale.

    Helios是反應式的,它使用事件驅動的架構來簡化開發和構建易伸縮的系統。

  3.Performant

    Performance is a cross-cutting concern we factor in at every level in the design of the framework in order to eliminate overhead for your apps and clients.

    這個系統在開發和設計的時候都充分考慮到了性能,構建你的app和client的時候請消除這方面的顧慮。

  4.Battle-Tested

    Helios powers the clustering and remoting capbilities built into Akka.NET and more.

    Akka.net的集群,遠程功能構建在Helios之上。

三:一個基於Helios的聊天室示例

  要用來演示Socket通信那麼最好的示例無非就是聊天程式了。

  整個解決方案包含3個項目:

  

  1.HeliosChat.Common

  這個項目里是一些公共的類型,新建完之後使用nuget添加helios的庫

  

  Message 類:所有發送的消息都是通過Message包裝的,每一個消息都有一個Command跟Content來構成。

 public class Message
    {
        public Command Command { get; set; }

        public string Content { get; set; }
    }

  Command枚舉:用來描述消息的命令

public enum Command
    {
        Join,
        Send,
    }

  MessageConverter靜態類:這個類用來轉換Message對象為Byte[],或者把Byte[]轉換成Message對象。Message對象在通過Helios傳輸的時候需要先轉成Byte[],所以我們需要自己定義包的格式。我們用Byte[]的前四位來存放Command,Content轉成Byte後從第5位開始存放。

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 commandBytes = 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}", exc.Message);
            }

            return null;
        }

    }

  2.HeliosChat.Server

  不用說也知道,這是聊天室的服務端,負責連接用戶及轉發消息。

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>
        /// 處理接受到的消息
        /// </summary>
        /// <param name="data"></param>
        /// <param name="channel"></param>
        public static void Receive(NetworkData data, IConnection 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.TryAdd(clientName, channel))
            {
                Broadcast(string.Format("{0} join group successful .", clientName));
            }
            else
            {
                var errMsg = new Message()
                {
                    Command = Command.Send,
                    Content = "client name is used."
                };
                SendMessage(channel, errMsg);
            }
        }

        /// <summary>
        /// 廣播消息
        /// </summary>
        /// <param name="clientMessage"></param>
        public static void Broadcast(string clientMessage)
        {
            Console.WriteLine(clientMessage);
            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

  聊天服務的客戶端

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一個Client
            Client = connectionFactory.NewConnection(Node.Empty(), NodeBuilder.BuildNode().Host(host).WithPort(port));
            Client.OnConnection += (address, connection) =>
            {
                Console.WriteLine("Connect server successful.");
                connection.BeginReceive(Received);
            };
            Client.OnDisconnection += (address, reason) => Console.WriteLine("Disconnected.");
            Console.WriteLine("Input ClientName ");
            ClientName = Console.ReadLine();
            Console.Title = string.Format("Client {0}", ClientName);
            //建立連接
            Client.Open();
            //加入聊天組
            Join();
            //等待輸入
            WaitInput();
        }

        public static void WaitInput()
        {
            while (true)
            {
                var input = Console.ReadLine();
                if (!string.IsNullOrEmpty(input))
                {
                    var message = MakeSendMessage(input);
                    SendMessage(Client, message);
                }
            }
        }

        /// <summary>
        /// Jion chat group
        /// </summary>
        public static void Join()
        {
            var message = MakeJoinMessage();
            SendMessage(Client,message);
        }

        /// <summary>
        /// 處理接受到的消息
        /// </summary>
        /// <param name="data"></param>
        /// <param name="responseChannel"></param>
        public static void Received(NetworkData data, IConnection responseChannel)
        {
            var message = MessageConverter.ToMessage(data);
            if (message.Command == Command.Send)
            {
                Console.WriteLine(message.Content);
            }
        }

        /// <summary>
        /// 構造聊天消息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static Message MakeSendMessage(string input)
        {
            return new Message
            {
                Command = Command.Send,
                Content = string.Format("{0}:{1}", ClientName, input)
            };
        }
        /// <summary>
        /// 構造加入組的消息
        /// </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.運行結果

  這樣一個簡單的聊天室程式就完成了。

四:Helios 2.0

  helios 1.0的非同步編程模型是基於APM的,從helios 2.0開始會改成SocketAsyncEventArgs方式來實現非同步。SocketAsyncEventArgs底層封裝了IOCP,IOCP是Windows server上Socket通訊性能最高的技術,使用了IOCP的helios 2.0勢必具有更高的性能,所以對於helios 2.0還是非常期待的。

  示例下載:http://files.cnblogs.com/files/kklldog/HeliosChat.7z


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • //自定義一個DatePicker.cshtml文件@helper Init(){ //日期字體顏色 }@helper Render(params string[] controlIds){ if (controlIds != null) { }...
  • Jquery方法$("img").one("error", function(e){ $(this).attr("src", "default.gif");});
  • public class CommonAuthorize : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase httpContext) { ...
  • 作者:[美]Adam Freeman 來源:《精通ASP.NET MVC 4》ASP.NET MVC 是微軟的一個 Web開發框架,它整合了“模型—視圖—控制器(MVC)”架構的高效與整潔、敏捷開發的最新的思想與技術以及當前ASP.NET 平臺的精華部分。ASP.NET MVC 可以完全替代傳統的....
  • 一. DescriptionAttribute的普通使用方式1.1 使用示例 DescriptionAttribute特性可以用到很多地方,比較常見的就是枚舉,通過獲取枚舉上定義的描述信息在UI上顯示,一個簡單的枚舉定義:public enum EnumGender { ...
  • 一:文件查找1:文件檢索有時候我們因為改bug的需要,必須要知道這個MD5函數在哪些文件中用到了,然而不像cs中我們可以用shift+f12來查找下函數引用,這時候我們就可以用 “文件查找” 解決這個問題。我們可以在 “查找結果” 中清楚的看到哪些文件和哪些行使用到了這個md5函數,然後我們繼續順藤...
  • 首先、導入命名空間:using System.Net.Mail;定義發送電子郵件的方法[網上很多不同的,可以對比著看一下,WinForm的也適用]:/// /// 發送電子郵件/// /// 發件人郵箱地址/// 收件人郵箱地址/// 郵件主題/// 郵件內容/// public bool Send...
  • 1、查找空節點//*[not(text())] 表示內容為空的節點//*[count(*)=0] 表示沒有子節點的節點"//*[count(*)=0 and not(text())]" 空節點,表示既沒有內容,也沒有子節點,但未排除包含屬性的節點
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...