When Unity does strong networking games, it usually chooses to use TCP to communicate (it is said to have a UDP transmission with higher efficiency), and the method of receiving information generally chooses to create a new thread to iterate.
Today we see another way of doing this on our project. Here's a note.
First establish a TCP connection
#using System.Net.Sockets; TcpClient tcpClient = new TcpClient(); tcpClient .BeginConnect(address,port,new AsyncCallback(this.OnConnect),result);
You can see that the asynchronous communication callback function is used here 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) { } }
Above is a successful connection function, the connection is successful after the connection can be disconnected and start accepting the header, also in the asynchronous communication callback function used within
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) { Above is the reading of information logic, data in the ITEM.BUFS, their own according to demand analysis//The following calculation whether to read the Baotou, the next time should be read packet or Baotou 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) {//On The face is reading the information logic//should read Baotou Item.client.GetStram () next time. BeginRead (Item.bufs,0,bufssize,new AsyncCallback (this. OnRead), item); } } } }
As you can see, this is always done by calling the asynchronous load function asynccallback
To implement reading information all the time.
The last parameter of the BeginRead () function used above is a data class of its own definition, and this parameter of the function is used to pass the last item to the next callback at the time of the next asynchronous callback.
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; } }
and write data, is sent in the game update, add a send message in the queue to add one, in the update to detect if the queue has to send data to write data
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) { }
After sending it out, it will run asynchronously back to the onwrite. Write the stream off in the inside
private void OnWrite(IAsyncResult ar) { TcpClient client = (TcpClient)ar.AsyncState; try { client.GetStream().EndWrite(ar); } catch(SocketException e) { } }
"Komatsu teaches you to swim and develop" Unity system module Development "Unity Network layer Read and write