主頁 > .NET開發 > LiteByte教程

LiteByte教程

2020-09-20 15:22:14 .NET開發

簡介

LiteByte是一種輕量級的二進制資料交換格式,
體積小巧、簡單易用是設計目標,主要用于解決前后臺資料傳輸量的問題,

作者:冰封百度(ZhangYu)
設計的靈感來源于C# struct記憶體對齊后的緊湊格式,暫時只實作了C#版本,

特點

1.緊湊的二進制資料格式,支持變長整型,資料量小,
2.用近似代碼定義類的方式定義物件結構,使用方便,

實作思路

把一個物件分為兩個部分:結構和值,

結構用組態檔定義,越方便越好,

值用于網路傳輸,越小越好,

前后臺依賴相同的結構組態檔,轉換時把物件的值拆出來傳輸,決議時把值還原成物件,

使用方法

1.創建自定義結構檔案CustomType.lbs (LiteByte Schema 文本檔案),
2.定義物件欄位(像寫類一樣寫結構、型別和名稱),
3.呼叫LBUtil.Serialize(object) 把物件序列化成二進制資料,
4.呼叫LBUtil.Deserilize(bytes) 把二進制資料反序列化成物件,

代碼樣例:

// 自定義物件結構:
// 寫結構配置和C#中寫struct一樣, 訪問修飾符可以不寫,讀組態檔的時候會被忽略

/// <summary> 玩家資訊測驗 | PlayerInfo test </summary>
public struct PlayerInfo {

    public uint id;
    public string nickname;
    public byte gender;
    public bool isVip;
    public int lv;
    public int hp;
    public int mp;
    public int exp;

}

// 命名空間
using LiteByte;

// 創建物件
PlayerInfo player = new PlayerInfo();
player.id = 100001;
player.nickname = "冰封百度";
player.gender = 1;
player.isVip = true;
player.lv = 999;
player.hp = 999999;
player.mp = 999999;
player.exp = 9999999;

// 序列化:
string typeName = "PlayerInfo";
byte[] bytes = LBUtil.Serialize(typeName, player); // 長度:31位元組

// 反序列化:
PlayerInfo info = LBUtil.Deserialize<PlayerInfo>(typeName, bytes);

轉換結果:

代碼說明:

1.序列化物件時用LBUtil.Serialize("name", obj)
2.反序列化物件時候用LBUtil.Deserilize("name", obj)
3.可以轉換自定義的struct和class 欄位需要是public的
4.提供了通用的轉換物件LBObject,可以Set和Get值,用于動態創建全新的自定義結構,由于序列化和反序列化自定義struct和class時用了反射,效率不高,用LBObject轉換效率會更高一些,但用起來會麻煩一些,按自己喜歡的方式使用就好,

支持的資料型別

資料型別介紹

1.基本的值型別:bool、byte、short、int、long
2.字串 string (支持UTF8、Unicode、ASCII三種編碼方式)
3.陣列 (型別+"[]"會被識別成陣列)
4.自定義型別 (復雜物件)

基本資料型別

位元型(7種)

 

型別 長度 值范圍
Bit1(Boolean) 1位 0 ~ 1
Bit2(Byte) 2位 0 ~ 3
Bit3(Byte) 3位 0 ~ 7
Bit4(Byte) 4位 0 ~ 15
Bit5(Byte) 5位 0 ~ 31
Bit6(Byte) 6位 0 ~ 63
Bit7(Byte) 7位 0 ~ 127

 

 

 

 

 

 

 

 

 

 

 

整型(16種)

型別 長度  值范圍
Int8(sbyte) 1位元組 -128 ~ 127
Int16(short) 2位元組 -32768 ~ -32767
Int24(int) 3位元組 -8388608 ~ 8388607
Int32(int) 4位元組 -2147483648 ~ 2147483647
Int40(long) 5位元組 -549755813888 ~ 549755813887
Int40(long) 6位元組 -140737488355328 ~ 140737488355327
Int40(long) 7位元組 -36028797018963968 ~ 36028797018963967
Int64(long) 8位元組 -9223372036854775808 ~ 9223372036854775807
UInt8(byte) 1位元組 0 ~ 255
UInt16(ushort) 1位元組 0 ~ 65535
UInt24(uint) 1位元組 0 ~ 16777215
UInt32(uint) 1位元組 0 ~ 4294967295
UInt40(ulong) 1位元組 0 ~ 1099511627775
UInt48(ulong) 1位元組 0 ~ 281474976710655
UInt56(ulong) 1位元組 0 ~ 72057594037927935
UInt64(ulong) 1位元組 0 ~ 18446744073709551615

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

浮點型(5種)

型別 長度 有效數字 值范圍
Float8(float) 1位元組 7位 0/255 ~ 255/255
Float16(float) 2位元組 3位 ±6.55E +4
Float24(float) 3位元組 5位 ±1.8447E +19
Float32(float) 4位元組 7位 ±3.402823E +38
Float64(double) 8位元組 15位 ±1.7976931348623157E +308

 

 

 

 

 

 

 

 

 

變長整型(7種)

型別 長度 值范圍
VarInt16(short) 1位 + 1~2位元組 同Int16
VarInt32(int) 2位 + 1~4位元組 同Int32
VarInt64(long) 3位 + 1~8位元組 同Int64
VarUInt16(ushort) 1位 + 1~2位元組 同UInt16
VarUInt32(uint)  2位 + 1~4位元組 同UInt32
VarUInt64(ulong) 3位 + 1~8位元組 同UInt64
VarLength(int)  3位 + 1~8位元組 -1 ~ (Int32.MaxValue/2 - 1)

 

 

 

 

 

 

 

 

 

 

 

字串(3種編碼)

型別 單個字符長度 總長度范圍
UTF8(string) 1~4位元組 頭(1~4)位元組+體(0 ~ 1073741822)位元組
Unicode(string) 2位元組 頭(1~4)位元組+體(0 ~ 1073741822)x2位元組
ASCII(string) 1位元組 頭(1~4)位元組+體(0 ~ 1073741822)位元組

 

 

 

 

 

 

復雜資料型別(2種)

型別 運算式
陣列(Array) 型別名稱[]
字典(未實作) Dictionary<基本型別, 型別名稱>
自定義型別 只要不和基本型別和陣列重名 即被當作自定義型別

 

 

 

 

 

 

自定義型別結構配置(LiteByte Schema)樣例

以下樣例中 基本型別默認應用以下簡稱配置
Bit1 = bool
Int8 = sbyte
UInt8 = byte
VarInt32 = int
VarUnt32 = uint
VarInt64 = long
VarUInt64 = ulong
UTF8 = string

基本資料型別 結構:

 1 struct BaseTypeST {
 2 
 3     // 位元型
 4     bool boolValue;
 5 
 6     // 有符號整型
 7     sbyte sbyteValue;
 8     short shortValue;
 9     int intValue;
10     long longValue;
11 
12     // 無符號整型
13     byte byteValue;
14     ushort ushortValue;
15     uint uintValue;
16     ulong ulongValue;
17 
18     // 有符號浮點型
19     float floatValue;
20     double doubleValue;
21 
22     // 字符型(UTF8)
23     string stringValue;
24 
25 }

陣列 結構:

1 struct ArrayST {
2     int[] ids;
3     string[] names;
4 }

用戶資訊 結構:

 1 struct UserInfoST {
 2 
 3     uint id;
 4     string username;
 5     string nickname;
 6     int hp;
 7     int mp;
 8     long exp;
 9     long gold;
10     byte age;
11     bool isVip;
12 
13 }

各語言型別對照表

型別 長度 C# Java C++ Go
Bit1 1位 bool boolean char bool
Bit2 2位 byte byte char uint8
Bit3 3位 byte byte char uint8
Bit4 4位 byte byte char uint8
Bit5 5位 byte byte char uint8
Bit6 6位 byte byte char uint8
Bit7 7位 byte byte char uint8
Int8 1位元組 sbyte sbyte char int8
Int16 2位元組 short short short int16
Int24 3位元組 int int int int32
Int32 4位元組 int int int int32
Int40 5位元組 long long long long int64
Int48 6位元組 long long long long int64
Int56 7位元組 long long long long int64
Int64 8位元組 long long long long int64
UInt8 1位元組 byte byte unsigned char uint8
UInt16 2位元組 ushort ushort unsigned short uint16
UInt24 3位元組 uint uint unsigned int uint32
UInt32 4位元組 uint uint unsigned int uint32
UInt40 5位元組 ulong ulong unsigned long long uint64
UInt48 6位元組 ulong ulong unsigned long long uint64
UInt56 7位元組 ulong ulong unsigned long long uint64
UInt64 8位元組 ulong ulong unsigned long long uint64
Float8 1位元組 float float float float32
Float16 2位元組 float float float float32
Float24 3位元組 float float float float32
Float32 4位元組 float float float float32
Float64 8位元組 double double double float64
VarInt16 1位+1~2位元組 short short short int16
VarInt32 2位+1~4位元組 int int int int32
VarInt64 3位+1~8位元組 long long long long int64
VarUInt16 1位+1~2位元組 ushort ushort unsigned short uint16
VarUInt32 2位+1~4位元組 uint uint unsigned int uint32
VarUInt64 3位+1~8位元組 ulong ulong unsigned long long uint64
VarLength 2位+1~4位元組 int int int int32
UTF8 1~4位元組 string string string string
Unicode 2位元組 string string string string
ASCII 1位元組 string string string string

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

共計38種

資料型別說明:

1.對bool型的支持最好,一個bool型只占1位(1/8個位元組),
2.支持變長整數(short、int、long、ushort、uint、ulong)
3.支持null值 (能空的型別string, array, object,都支持它們為空的情況)
4.建議在定義資料格式時,用盡量小的型別定義欄位,這樣序列化的資料體積會更小,如果懶得寫,可以考慮使用變長資料,
5.支持自定義資料型別名稱,因為相同的資料型別在不同的編程語言中名字不一樣,為了方便使用,我添加了一套內置資料型別名稱并添加了一個型別名稱映射的功能,可以自定義基本值型別的名稱,按照自己喜歡的風格命名就好,
6.因為在撰寫變長資料型別的程序中用到了一些不常見的資料格式,為了重用型別,索性就一起支持了,在對資料大小很嚴格的環境會有幫助,這些非常規的資料型別有:
Bit2~Bit7 占2~7位
Int24、Int40、Int48、Int56 占3、5、6、7位元組
UInt24、UInt40、UInt48、UInt56 占3、5、6、7位元組
VarLength 用于表示string和array的長度 值范圍-1~(int.MaxValue/2 - 1)

其他說明:

由于能力有限,暫時只實作了C#版本(在Unity中實作的,算半個.Net吧)
其他語言后續有時間再寫,雖然造了個輪子 不過感覺造輪子的程序中識訓遠大于付出,挺開心的,
建了個群,有需求的可加,
QQ群:715800513

專案GitHub:https://github.com/zhangyukof/litebyte

測驗Demo:
鏈接:https://pan.baidu.com/s/1yQVn6f4YAkNBDnD0g86xow
提取碼:lio4

轉載請標明原文地址:https://www.cnblogs.com/zhangyukof/p/12073041.html

 

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/89718.html

標籤:C#

上一篇:C#Protected和多型(虛方法)

下一篇:[譯]C# 7系列,Part 8: in Parameters in引數

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • WebAPI簡介

    Web體系結構: 有三個核心:資源(resource),URL(統一資源識別符號)和表示 他們的關系是這樣的:一個資源由一個URL進行標識,HTTP客戶端使用URL定位資源,表示是從資源回傳資料,媒體型別是資源回傳的資料格式。 接下來我們說下HTTP. HTTP協議的系統是一種無狀態的方式,使用請求/ ......

    uj5u.com 2020-09-09 22:07:47 more
  • asp.net core 3.1 入口:Program.cs中的Main函式

    本文分析Program.cs 中Main()函式中代碼的運行順序分析asp.net core程式的啟動,重點不是剖析原始碼,而是理清程式開始時執行的順序。到呼叫了哪些實體,哪些法方。asp.net core 3.1 的程式入口在專案Program.cs檔案里,如下。ususing System; us ......

    uj5u.com 2020-09-09 22:07:49 more
  • asp.net網站作為websocket服務端的應用該如何寫

    最近被websocket的一個問題困擾了很久,有一個需求是在web網站中搭建websocket服務。客戶端通過網頁與服務器建立連接,然后服務器根據ip給客戶端網頁發送資訊。 其實,這個需求并不難,只是剛開始對websocket的內容不太了解。上網搜索了一下,有通過asp.net core 實作的、有 ......

    uj5u.com 2020-09-09 22:08:02 more
  • ASP.NET 開源匯入匯出庫Magicodes.IE Docker中使用

    Magicodes.IE在Docker中使用 更新歷史 2019.02.13 【Nuget】版本更新到2.0.2 【匯入】修復單列匯入的Bug,單元測驗“OneColumnImporter_Test”。問題見(https://github.com/dotnetcore/Magicodes.IE/is ......

    uj5u.com 2020-09-09 22:08:05 more
  • 在webform中使用ajax

    如果你用過Asp.net webform, 說明你也算是.NET 開發的老兵了。WEBform應該是2011 2013左右,當時還用visual studio 2005、 visual studio 2008。后來基本都用的是MVC。 如果是新開發的專案,估計沒人會用webform技術。但是有些舊版 ......

    uj5u.com 2020-09-09 22:08:50 more
  • iis添加asp.net網站,訪問提示:由于擴展配置問題而無法提供您請求的

    今天在iis服務器配置asp.net網站,遇到一個問題,記錄一下: 問題:由于擴展配置問題而無法提供您請求的頁面。如果該頁面是腳本,請添加處理程式。如果應下載檔案,請添加 MIME 映射。 WindowServer2012服務器,添加角色安裝完.netframework和iis之后,運行aspx頁面 ......

    uj5u.com 2020-09-09 22:10:00 more
  • WebAPI-處理架構

    帶著問題去思考,大家好! 問題1:HTTP請求和回傳相應的HTTP回應資訊之間發生了什么? 1:首先是最底層,托管層,位于WebAPI和底層HTTP堆疊之間 2:其次是 訊息處理程式管道層,這里比如日志和快取。OWIN的參考是將訊息處理程式管道的一些功能下移到堆疊下端的OWIN中間件了。 3:控制器處理 ......

    uj5u.com 2020-09-09 22:11:13 more
  • 微信門戶開發框架-使用指導說明書

    微信門戶應用管理系統,采用基于 MVC + Bootstrap + Ajax + Enterprise Library的技術路線,界面層采用Boostrap + Metronic組合的前端框架,資料訪問層支持Oracle、SQLServer、MySQL、PostgreSQL等資料庫。框架以MVC5,... ......

    uj5u.com 2020-09-09 22:15:18 more
  • WebAPI-HTTP編程模型

    帶著問題去思考,大家好!它是什么?它包含什么?它能干什么? 訊息 HTTP編程模型的核心就是訊息抽象,表示為:HttPRequestMessage,HttpResponseMessage.用于客戶端和服務端之間交換請求和回應訊息。 HttpMethod類包含了一組靜態屬性: private stat ......

    uj5u.com 2020-09-09 22:15:23 more
  • 部署WebApi隨筆

    一、跨域 NuGet參考Microsoft.AspNet.WebApi.Cors WebApiConfig.cs中配置: // Web API 配置和服務 config.EnableCors(new EnableCorsAttribute("*", "*", "*")); 二、清除默認回傳XML格式 ......

    uj5u.com 2020-09-09 22:15:48 more
最新发布
  • C#多執行緒學習(二) 如何操縱一個執行緒

    <a href="https://www.cnblogs.com/x-zhi/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2943582/20220801082530.png" alt="" /></...

    uj5u.com 2023-04-19 09:17:20 more
  • C#多執行緒學習(二) 如何操縱一個執行緒

    C#多執行緒學習(二) 如何操縱一個執行緒 執行緒學習第一篇:C#多執行緒學習(一) 多執行緒的相關概念 下面我們就動手來創建一個執行緒,使用Thread類創建執行緒時,只需提供執行緒入口即可。(執行緒入口使程式知道該讓這個執行緒干什么事) 在C#中,執行緒入口是通過ThreadStart代理(delegate)來提供的 ......

    uj5u.com 2023-04-19 09:16:49 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    <a href="https://www.cnblogs.com/huangxincheng/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/214741/20200614104537.png" alt="" /&g...

    uj5u.com 2023-04-18 08:39:04 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    一:背景 1. 講故事 前段時間協助訓練營里的一位朋友分析了一個程式卡死的問題,回過頭來看這個案例比較經典,這篇稍微整理一下供后來者少踩坑吧。 二:WinDbg 分析 1. 為什么會卡死 因為是表單程式,理所當然就是看主執行緒此時正在做什么? 可以用 ~0s ; k 看一下便知。 0:000> k # ......

    uj5u.com 2023-04-18 08:33:10 more
  • SignalR, No Connection with that ID,IIS

    <a href="https://www.cnblogs.com/smartstar/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/u36196.jpg" alt="" /></a>...

    uj5u.com 2023-03-30 17:21:52 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:15:33 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:13:31 more
  • C#遍歷指定檔案夾中所有檔案的3種方法

    <a href="https://www.cnblogs.com/xbhp/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/957602/20230310105611.png" alt="" /></a&...

    uj5u.com 2023-03-27 14:46:55 more
  • C#/VB.NET:如何將PDF轉為PDF/A

    <a href="https://www.cnblogs.com/Carina-baby/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2859233/20220427162558.png" alt="" />...

    uj5u.com 2023-03-27 14:46:35 more
  • 武裝你的WEBAPI-OData聚合查詢

    <a href="https://www.cnblogs.com/podolski/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/616093/20140323000327.png" alt="" /><...

    uj5u.com 2023-03-27 14:46:16 more