您的位置:首页 > 理论基础 > 计算机网络

【小松教你手游开发】【unity系统模块开发】unity网络层读写

2018-03-10 20:36 489 查看
在unity做强联网游戏的时候一般会选择用tcp来做通信(据说有一种udp的传输具有更高的效率),而接收信息的方法上一般会选择新建一个线程循环读取。

今天在我们项目上看到另外的一种方法。这里记录一下。

首先建立tcp连接

#using System.Net.Sockets;

TcpClient tcpClient = new TcpClient();
tcpClient .BeginConnect(address,port,new AsyncCallback(this.OnConnect),result);

可以看到这里用的是异步通信回调函数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)
{
}
}

上面是连接成功后的函数,连接成功后就可以断开连接并开始接受表头;同样是在异步通信回调函数内使用

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)
{
//上面就是读取信息逻辑,数据在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);
}
}

}
}

可以看到,这种方式也就是一直通过调用异步加载函数AsyncCallback
来实现一直读取信息
而上面用的的BeginRead()函数的最后一个参数item是自己定义的一个数据类,函数的这个参数是用来下次异步回调的时候把上次的item传给下个回调

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;

}
}

而写数据呢,是在游戏的Update里发送,加一条发送信息就在队列里加一个,在Update里检测如果队列里有需要发送的数据就写数据

public void UpdateSend()
{
//填写数据
try
{
NetworkStream stream = tcpCLient.getStream();
if(stream.CanWrite)
{
//pMsg数据Byte[]
stream.BeginWtrite(pMsg,0,pMsg.Length,new AsycCallback(this.OnWrite),tcpCLient);
}

}
catch(SocketException e)
{
}
}

在发送完了以后会跑到上面的异步回掉OnWrite里。在里面把流关闭写入

private void OnWrite(IAsyncResult ar)
{
TcpClient client = (TcpClient)ar.AsyncState;
try
{
client.GetStream().EndWrite(ar);
}
catch(SocketException e)
{
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  手游开发
相关文章推荐