您好,登錄后才能下訂單哦!
在unity做強聯(lián)網(wǎng)游戲的時候一般會選擇用tcp來做通信(據(jù)說有一種udp的傳輸具有更高的效率),而接收信息的方法上一般會選擇新建一個線程循環(huán)讀取。
今天在我們項目上看到另外的一種方法。這里記錄一下。
首先建立tcp連接
#using System.Net.Sockets;
TcpClient tcpClient = new TcpClient();
tcpClient .BeginConnect(address,port,new AsyncCallback(this.OnConnect),result);
可以看到這里用的是異步通信回調(diào)函數(shù)AsyncCallback
private void OnConnect(IAsyncResult ar)
{
ConnectResult result = ar.AsyncState as ConnectRsult;
try
{
result.client.EndConnect(ar);
int size = HeaderLength;//定好的表頭大小
byte[] readBuf = new byte[size];
result.client.GetStream().BeginRead(readBuf,0,size,new AsyncCallback(this.OnRead),new RecvIremObject(result.client,readBuf,size));
}
catch(System.Net.Sockets.SocketException e)
{
}
}
上面是連接成功后的函數(shù),連接成功后就可以斷開連接并開始接受表頭;同樣是在異步通信回調(diào)函數(shù)內(nèi)使用
private void OnRead(IAsyncResult ar)
{
RecvItemObject item = (RecvItemObject)ar.AsyncState;
try
{
Stream stram = item.client.GetStram();
int readsize = stream.EndRead(ar);
item.current =+= readsize;
TotalReadSize += (uint)readsize;
if(item.current < item.total)
{
item.client.GetStram().BeginRead(ite.bufs,item.current,item.total - item.current,new AsyncCallback(OnRead),item);
}
else
{
if(item.state == RecvItemObject.EndReadState.ReadHearder)
{
//上面就是讀取信息邏輯,數(shù)據(jù)在item.bufs里,自己按需求解析
//下面計算是否讀完包頭,下次應該讀包還是包頭
if(true)
{
item.client.GetStram().BeginRead(item.bufs,0,bufsSize,new AsyncCallback(this.OnRead),item);
}
else
{
item.client.GetStram().BeginRead(item.bufs,0,dataLength,new AsyncCallback(this.OnRead),item);
}
}
else(item.state == RecvItemObject.EndReadState.ReadData)
{
//上面就是讀取信息邏輯
//下次應該讀包頭
item.client.GetStram().BeginRead(item.bufs,0,bufsSize,new AsyncCallback(this.OnRead),item);
}
}
}
}
可以看到,這種方式也就是一直通過調(diào)用異步加載函數(shù)AsyncCallback
來實現(xiàn)一直讀取信息
而上面用的的BeginRead()函數(shù)的最后一個參數(shù)item是自己定義的一個數(shù)據(jù)類,函數(shù)的這個參數(shù)是用來下次異步回調(diào)的時候把上次的item傳給下個回調(diào)
private class RecvItemObject
{
public enum EReadState
{
ReadData,
ReadHeader,
}
public byte[] bufs;
public int total;
public int current;
public EReadState state;
public TcpClient client;
public NetworkStram networkStream;
public RecvItemObject(TcpClient client, byte[] bufs,int total)
{
this.client = client;
this.bufs = bufs;
this.total = total;
current =0;
state = EReadState.ReadHeader;
}
}
而寫數(shù)據(jù)呢,是在游戲的Update里發(fā)送,加一條發(fā)送信息就在隊列里加一個,在Update里檢測如果隊列里有需要發(fā)送的數(shù)據(jù)就寫數(shù)據(jù)
public void UpdateSend()
{
//填寫數(shù)據(jù)
try
{
NetworkStream stream = tcpCLient.getStream();
if(stream.CanWrite)
{
//pMsg數(shù)據(jù)Byte[]
stream.BeginWtrite(pMsg,0,pMsg.Length,new AsycCallback(this.OnWrite),tcpCLient);
}
}
catch(SocketException e)
{
}
}
在發(fā)送完了以后會跑到上面的異步回掉OnWrite里。在里面把流關(guān)閉寫入
private void OnWrite(IAsyncResult ar)
{
TcpClient client = (TcpClient)ar.AsyncState;
try
{
client.GetStream().EndWrite(ar);
}
catch(SocketException e)
{
}
}
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。