首頁 後端開發 C#.Net教程 socket傳輸protobuf位元組流的實例介紹

socket傳輸protobuf位元組流的實例介紹

Jun 29, 2017 pm 02:31 PM
protobuf socket unity 傳輸 探索者

上一篇講到了資料的處理,這篇主要講使用多執行緒收發訊息

//创建消息数据模型  2
//正式项目中,消息的结构一般是消息长度+消息id+消息主体内容  3 
public class Message  4
 {     
 public IExtensible protobuf;
 
  public int messageId;  7 }  8   9 public class SocketClientTemp : MonoBehaviour 10 { 11     
  const int packageMaxLength = 1024; 12  13     Socket mSocket; 14     Thread threadSend; 15     
  Thread threadRecive; 16     Queue<Message> allMessages = new Queue<Message>(); 17     
  Queue<byte[]> sendQueue = new Queue<byte[]>();
  public bool Init() 20     
  { 21         //创建一个socket对象 22        
   mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 23         
   return SocketConnection("此处是ip", 1111); 24     } 25  26     void Update() 27     { 28        
   AnalysisMessage(); 29     } 30  31     /// <summary> 32     /// 建立服务器连接 33     /// </summary> 34     
   /// <param name="ip">服务器的ip地址</param> 35     /// <param name="port">端口</param> 36     
   bool SocketConnection(string ip, int port) 37     { 38         try 39         { 40             
   IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ip), port); 41            
   //同步连接服务器,实际使用时推荐使用异步连接,处理方式会在下一篇讲断线重连时讲到 42            
   mSocket.Connect(ipep); 43             //连接成功后,创建两个线程,分别用于发送和接收消息 44            
   threadSend = new Thread(new ThreadStart(SendMessage)); 45             threadSend.Start(); 46            
   threadRecive = new Thread(new ThreadStart(ReceiveMessage)); 47             threadRecive.Start(); 48            
   return true; 49         } 50         catch (Exception e) 51         { 52            
   Debug.Log(e.ToString()); 53             Close(); 54             return false; 55       
    }      }
         #region ...发送消息 59     
 /// <summary> 60     /// 添加数据到发送队列 61     
 /// </summary> 62     /// <param name="protobufModel"></param> 63     
 /// <param name="messageId"></param> 64    
 public void AddSendMessageQueue(IExtensible protobufModel, int messageId) 65     
{ 66         sendQueue.Enqueue(BuildPackage(protobufModel, messageId)); 67     } 
void SendMessage() 70     { 71         //循环获取发送队列中第一个数据,然后发送到服务器 72         
 while (true) 73         { 74             if (sendQueue.Count == 0) 75             { 76                 
 Thread.Sleep(100); 77                 continue; 78             } 79             
if (!mSocket.Connected) 80             { 81                 Close(); 82                 
break; 83             } 84             
else 85                 Send(sendQueue.Peek());
//发送队列中第一条数据 86         } 87     } 88  89    
 void Send(byte[] bytes) 90     { 91         try 92         
 { 93             mSocket.Send(bytes, SocketFlags.None); 94             
 //发送成功后,从发送队列中移除已发送的消息 95             sendQueue.Dequeue(); 96         
 } 97         catch (SocketException e) 98         { 99             
 //如果错误码为10035,说明服务器缓存区满了,所以等100毫秒再次发送100             
 if (e.NativeErrorCode == 10035)101             {102                
 Thread.Sleep(100);103                 Send(bytes);104             
 }105             else106                 
 Debug.Log(e.ToString());107         }108     }109     #endregion110 111     
 #region ...接收消息112     /// <summary>113     /// 解析收到的消息114     
 /// </summary>115     void AnalysisMessage()116     {117         
 while (allMessages.Count > 0)118         {119             
 int id = allMessages.Dequeue().messageId;120             
 switch (id)121             {122                
 //根据消息id做不同的处理123             }124         }125     }126 127     
 /// <summary>128     /// 接收数据129     /// </summary>130     
 void ReceiveMessage()131     {132         while (true)133         
 {134             if (!mSocket.Connected)135                 
 break;136             byte[] recvBytesHead = GetBytesReceive(4);137             
 int bodyLength = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(recvBytesHead, 0));138             
 byte[] recvBytesBody = GetBytesReceive(bodyLength);139 140             
 byte[] messageId = new byte[4];141             
 Array.Copy(recvBytesBody, 0, messageId, 0, 4);142             
 byte[] messageBody = new byte[bodyLength - 4];143             
 Array.Copy(recvBytesBody, 4, messageBody, 0, bodyLength - 4);144 145             
 if (BitConverter.IsLittleEndian)146                 
 Array.Reverse(messageId);147             
 FillAllPackages(BitConverter.ToInt32(messageId, 0), messageBody);148         
 }    
 }
  /// <summary>152    
   /// 填充接收消息队列153     /// </summary>154     
   /// <param name="messageId"></param>155     /// <param name="messageBody"></param>156     void FillAllPackages(int messageId, byte[] messageBody)157     {158         switch (messageId)159         {160             //根据消息id处理消息,并添加到接收消息队列161             case 1:162                 allMessages.Enqueue(new Message() 
163                 
{164                     
protobuf = ProtobufSerilizer.DeSerialize<TestTemp>(messageBody), 
165                     
messageId = messageId 
166                 });167                
 break;168         }169     }170 171     
/// <summary>172     /// 接收数据并处理173     
/// </summary>174     /// <param name="length"></param>175     
/// <returns></returns>176     byte[] GetBytesReceive(int length)177     
{178         byte[] recvBytes = new byte[length];179         while (length > 0)180         
{181             
byte[] receiveBytes = new byte[length < packageMaxLength ? length : packageMaxLength];182             
int iBytesBody = 0;183             if (length >= receiveBytes.Length)184                 
iBytesBody = mSocket.Receive(receiveBytes, receiveBytes.Length, 0);185             
else186                 iBytesBody = mSocket.Receive(receiveBytes, length, 0);187             
receiveBytes.CopyTo(recvBytes, recvBytes.Length - length);188             length -= iBytesBody;189         
}190         return recvBytes;191     }192     #endregion193 194     /// <summary>195     /// 构建消息数据包196     
/// </summary>197     /// <param name="protobufModel"></param>198     /// <param name="messageId"></param>199     
byte[] BuildPackage(IExtensible protobufModel, int messageId)200     {201         byte[] b;202         
if (protobufModel != null)203             b = ProtobufSerilizer.Serialize(protobufModel);204         
else205             b = new byte[0];206         //消息长度(int数据,长度4) + 消息id(int数据,长度4) + 消息主体内容207         
ByteBuffer buf = ByteBuffer.Allocate(b.Length + 4 + 4);208         //消息长度 = 消息主体内容长度 + 消息id长度209         
buf.WriteInt(b.Length + 4);210         buf.WriteInt(messageId);211 212         if (protobufModel != null)213            
 buf.WriteBytes(b);214         return buf.GetBytes();215     }216 217     void OnDestroy()218     {219         
 //停止运行后,如果不关闭socket多线程,再次运行时,unity会卡死220         Close();221     }222 223     
 /// <summary>224     /// 关闭socket,终止线程225     /// </summary>226     public void Close()227     
 {228         if (mSocket != null)229         {230             
 //微软官方推荐在关闭socket前先shutdown,但是经过测试,发现网络断开后,shutdown会无法执行231             
 if (mSocket.Connected)232                 mSocket.Shutdown(SocketShutdown.Both);233             
 mSocket.Close();234             mSocket = null;235         }236         //关闭线程237         
 if (threadSend != null)238             threadSend.Abort();239         if (threadRecive != null)240            
 threadRecive.Abort();241         threadSend = null;242         threadRecive = null;243     }244 }
登入後複製

到這裡,使用socket處理訊息的收發就基本上結束了,但是,某些專案為了增強體驗,可能還會增加斷線重連的功能,這個功能會在下一篇講到

以上是socket傳輸protobuf位元組流的實例介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

PHP+Socket系列之IO多路復用及實作web伺服器 PHP+Socket系列之IO多路復用及實作web伺服器 Feb 02, 2023 pm 01:43 PM

這篇文章為大家帶來了關於php+socket的相關知識,其中主要介紹了IO多路復用,以及php+socket如何實作web伺服器?有興趣的朋友下面一起來看一下,希望對大家有幫助。

Python的socket與socketserver怎麼使用 Python的socket與socketserver怎麼使用 May 28, 2023 pm 08:10 PM

一、基於TCP協定的socket套接字程式設計1、套接字工作流程先從伺服器端說起。伺服器端先初始化Socket,然後與連接埠綁定(bind),對連接埠進行監聽(listen),呼叫accept阻塞,等待客戶端連線。在這時如果有個客戶端初始化一個Socket,然後連接伺服器(connect),如果連線成功,這時客戶端與伺服器端的連線就建立了。客戶端發送資料請求,伺服器端接收請求並處理請求,然後把回應資料傳送給客戶端,客戶端讀取數據,最後關閉連接,一次互動結束,使用以下Python程式碼實作:importso

Unity 團結引擎開啟內測,針對微信小遊戲與車機 HMI 系統研發 Unity 團結引擎開啟內測,針對微信小遊戲與車機 HMI 系統研發 Oct 16, 2023 pm 09:13 PM

本站10月16日消息,Unity「團結引擎」的首款產品團結引擎創世版和團結引擎車機版正式開啟內測活動,這是「團結引擎」首次針對開發者進行小範圍內測。測試時間:2023年10月16日至2023年12月31日。本站注意到,團結引擎是Unity中國研發團隊推出的Unity中國版引擎,以Unity2022LTS為研發基礎,推出了微信小遊戲解決方案、團結引擎車機版。微信小遊戲解決方案團結引擎針對性地推出了一站式微信小遊戲解決方案,為了更好地支持微信小遊戲開發(環境適配、性能優化),新增了微信小遊戲專屬目標

怎麼使用Spring Boot+Vue實現Socket通知推播 怎麼使用Spring Boot+Vue實現Socket通知推播 May 27, 2023 am 08:47 AM

SpringBoot端第一步,引入依賴首先我們需要引入WebSocket所需的依賴,以及處理輸出格式的依賴com.alibabafastjson1.2.73org.springframework.bootspring-boot-starter-websocket第二步,創建WebSocket配置類importorg. springframework.context.annotation.Bean;importorg.springframework.context.annotation.Config

php socket無法連線怎麼辦 php socket無法連線怎麼辦 Nov 09, 2022 am 10:34 AM

php socket無法連線的解決方法:1、檢查php是否開啟socket擴充;2、開啟php.ini文件,檢查「php_sockets.dll」是否已載入;3、取消「php_sockets.dll」的註解狀態即可。

利用PHP和Socket實現即時文件傳輸技術研究 利用PHP和Socket實現即時文件傳輸技術研究 Jun 28, 2023 am 09:11 AM

隨著網路的發展,文件傳輸成為人們日常工作和娛樂中不可或缺的一部分。然而,傳統的文件傳輸方式如郵件附件或文件分享網站存在一定的限制,無法滿足即時性和安全性的需求。因此,利用PHP和Socket技術實現即時檔案傳輸成為了一種新的解決方案。本文將介紹利用PHP和Socket技術實現即時檔案傳輸的技術原理、優點和應用場景,並透過具體案例來展示該技術的實作方法。科技

C#常見的網路通訊與安全性問題及解決方法 C#常見的網路通訊與安全性問題及解決方法 Oct 09, 2023 pm 09:21 PM

C#中常見的網路通訊和安全性問題及解決方法在當今互聯網時代,網路通訊已成為了軟體開發中必不可少的一部分。在C#中,我們通常會遇到一些網路通訊的問題,例如資料傳輸的安全性、網路連線的穩定性等。本文將針對C#中常見的網路通訊和安全性問題進行詳細討論,並提供相應的解決方法和程式碼範例。一、網路通訊問題網路連線中斷:網路通訊過程中,可能會出現網路連線的中斷,這會導致

unity發佈出來的安卓apk該如何加密 unity發佈出來的安卓apk該如何加密 May 13, 2023 am 11:10 AM

Unity3D程式的安全問題程式碼安全問題Unity3D程式的核心組件檔案Assembly-CSharp.dll是標準的.NET檔案格式,附帶了方法名稱、類別名稱、類型定義等豐富的元資料訊息,使用DnSpy等工具可以輕易地將其反編譯和篡改,程式碼邏輯、類別名稱和方法名等一覽無餘。程式碼邏輯一但被反編譯,很容易滋生各種類型的外掛,破壞遊戲平衡,如果程式碼邏輯中存在漏洞,也容易被挖掘和利用,可能對開發商造成無法預料的損失。資源安全性問題Unity3D程式在編譯打包階段會透過Unity編輯器將資源打包成AssetBun

See all articles