Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Unity Multiplayer Use transport layer ,client side stop response

Discussion in 'Multiplayer' started by lifeFo, Apr 21, 2017.

  1. lifeFo

    lifeFo

    Joined:
    Apr 14, 2017
    Posts:
    15
    below is server side performance
    Code (CSharp):
    1. public  byte[] collectData()
    2.     {
    3.             byte[] syncbytes;
    4.         //if (isSever && isPlaying)
    5.         {
    6.             players = GameObject.FindGameObjectsWithTag("Player");
    7.             enemys = GameObject.FindGameObjectsWithTag("enemy");
    8.            // weapon= GameObject.FindGameObjectsWithTag("weapon");
    9.             tmpContainer = new GameObject[][] { players, enemys };
    10.             int activeGobjnum = players.Length + enemys.Length;
    11.             activeObj =new GameObject[activeGobjnum];
    12.            // SyncPosition = new Vector3[activeGobjnum];
    13.             players.CopyTo(activeObj, 0);
    14.             enemys.CopyTo(activeObj, players.Length);
    15.             int id;
    16.        
    17.             BaseContol objectstate;
    18.             SynchronizationMessage.Clear();
    19.             for(int i=0;i<tmpContainer.Length;i++)
    20.             {
    21.            
    22.                 GameObject[] g = tmpContainer[i];
    23.                 foreach (GameObject g1 in g)
    24.                 {
    25.                     objectstate = g1.GetComponent<BaseContol>();
    26.                     id = objectstate.NetworkId;
    27.                     byte FireState = objectstate.bullect;
    28.                     if (id == -1)
    29.                     {
    30.                         enemycounter += 1;
    31.                         objectstate.NetworkId = enemycounter;
    32.                         SynchronizationMessage.Add(g1.name+ enemycounter, new Gamobjectsinfo(objectstate.NetworkId, new byte[] { FireState }, g1.transform.position, g1.transform.rotation));
    33.                         Debug.Log(g1.name+"-counter=" + enemycounter);
    34.                     }
    35.                     else
    36.                     {
    37.                         SynchronizationMessage.Add(g1.name+id, new Gamobjectsinfo(id, new byte[] { (byte)FireState }, g1.transform.position, g1.transform.rotation));
    38.                     }
    39.  
    40.                     objectstate.bullect = 0;
    41.  
    42.                 }
    43.             }
    44.  
    45.        }
    below is both side initialize setting
    Code (CSharp):
    1.  void init()
    2.     {
    3.         if (hostId != -1)
    4.             return;
    5.         NetworkTransport.Init();
    6.         config = new ConnectionConfig();
    7.         channelid = config.AddChannel(QosType.ReliableFragmented);
    8.         HostTopology topology = new HostTopology(config, 15);
    9.         // hostId = NetworkTransport.AddHost(topology);
    10.         Debug.Log("hostId1=" + hostId);
    11.         hostId = NetworkTransport.AddHost(topology, broadcastPort);
    12.     }
    receive code is
    Code (CSharp):
    1.     int outHostId;
    2.         int outConnectionId;
    3.         int outChannelId;
    4.         string outConnIp;
    5.         int outPort;
    6.         NetworkID outID;
    7.         NodeID outNode;
    8.         int receivedSize;
    9.         byte error;
    10.         int quesize;
    11.         NetworkEventType evt;
    12.         int dog = 0;
    13.         do
    14.         {
    15.             dog++;
    16.             buffer = new byte[1024];
    17.             evt =
    18. NetworkTransport.Receive(out outHostId, out outConnectionId, out outChannelId, buffer, buffer.Length, out receivedSize, out error);
    19.             quesize = NetworkTransport.GetIncomingMessageQueueSize(outHostId, out error);
    20.             if (error != 0)
    21.                 Debug.LogError("Receive error=" + error + "quesize=" + quesize);
    22.             switch (evt)
    23.             {
    24.                 case NetworkEventType.ConnectEvent:
    25.                     {
    26.                         NetworkTransport.GetConnectionInfo(outHostId, outConnectionId, out outConnIp, out outPort, out outID, out outNode, out error);
    27.                         connectionClient.Add(outConnectionId, new StrTxt(Instantiate(text), outConnIp, true));
    28.                         OnConnect(outHostId, outConnectionId, (NetworkError)error);
    29.                         break;
    30.                     }
    31.                 case NetworkEventType.DisconnectEvent:
    32.                     {
    33.                         OnDisconnect(outHostId, outConnectionId, (NetworkError)error);
    34.                         break;
    35.                     }
    36.                 case NetworkEventType.DataEvent:
    37.                     {
    38.                      
    39.                         OnData(outHostId, outConnectionId, outChannelId, buffer, receivedSize, (NetworkError)error);
    40.                         break;
    41.                     }
    42.                 case NetworkEventType.BroadcastEvent:
    43.                     {
    44.                         OnBroadcast(outHostId, buffer, receivedSize, (NetworkError)error);
    45.                         break;
    46.                     }
    47.                 case NetworkEventType.Nothing:
    48.                     break;
    49.  
    50.                 default:
    51.                     Debug.LogError("Unknown network message type received: " + evt);
    52.                     break;
    53.             }
    54.  
    55.  
    56.         }
    57.         while (dog<30&&quesize!=0);
    I test this on wifi.Use a windows device and another is android device.
    When I send message over 1024 bytes , receive device's application will on response
    even in editor.Therefore I have to use task manager close unity.
    Is anyone know how to solve this?
    This cause me use QosType.ReliableSequenced channel.I split large (over 1024 bytes) to chunks.
    Combine that at client.But when client receive some times message.It's no response.
    My unity versions is 5.60f3. I use VisualStudio2015,windows10 Os.
    Any help here would be greatly appreciated.
     
    Last edited: Apr 21, 2017
  2. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    @lifeFo I do not see any error in your code. Please generate bug (with repro steps and description) and ping me with it's number. I will take a look on the next week
     
  3. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    @lifeFo oop I know whats happened - your buffer size is 1024, check please two parameters from Receive out receivedSize, out error. error should be MessageTooLong and receivedSize will return the actual message size. Probably you will need to increase your buffer
     
  4. lifeFo

    lifeFo

    Joined:
    Apr 14, 2017
    Posts:
    15
    @aabramychev Is QosType.ReliableFragmented fragmented message automatically? As i said before when send bytes over 1024.Receive side will lost control immediately and send side every thing is fine(error=0).So I can't see what happened from LOG.It's also happened when I use reliable channel(I split large message to chunks which not over 1024 byte) which after receive several times.
     
  5. aabramychev

    aabramychev

    Unity Technologies

    Joined:
    Jul 17, 2012
    Posts:
    574
    @lifeInfo, yes it is. +it assembles them automatically too. For ex

    You send 4k - 1k 1k 1k 1k - (on receiver) 4 k... Other words if you send 4k, you should receiev 4k too, if your buffer is 1k i will never receive 4k. Check please error and received size
     
  6. lifeFo

    lifeFo

    Joined:
    Apr 14, 2017
    Posts:
    15
    @aabramychev As you said,I adjust my buffer size.My send side code is
    Code (CSharp):
    1.  
    2.  public void sendmessage(int ID, byte[] msg, int Type)
    3.     {
    4. byte error = new byte();
    5.         string form = Type + "," + msg.Length+","+"HEAD";// + "," + "HEAD";
    6.         Debug.Log("msgHEAD = " + form+'|');
    7.         byte[] msgInfo = GetBytes(form);
    8.  
    9.         NetworkTransport.Send(hostId, ID, channelid, msgInfo, msgInfo.Length, out error);
    10.         Debug.Log("connectionIdHead  " + connectionId + " error= " + error.ToString()+" channelid="+channelid);
    11.         NetworkTransport.Send(hostId, ID, channelid, msg, msg.Length, out error);
    12. }
    receive side is
    Code (CSharp):
    1. int receiveMsgType;
    2.     bool isHaed = true;
    3.     int  GetSize;
    4.     int conuter = 0;
    5. byte[] buffer=new buffer[1024];//receive buffer
    6.     private void OnData(int outHostId, int outConnectionId, int outChannelId, byte[] buffer, int receivedSize, NetworkError error)
    7.     {
    8.         Debug.Log("Entry OnData receive size="+buffer.Length+ "receivedSize"+ receivedSize);
    9.  
    10.         if (isHaed)
    11.         {
    12.             byte[] reallyBytes = new byte[receivedSize];
    13.             Array.Copy(buffer, 0, reallyBytes, 0, receivedSize);
    14.             Debug.Log("!= HEAD?"+GetString(reallyBytes)+ "!= HEAD?");
    15.             string[] s = splitString(reallyBytes);
    16.           //  Debug.Log("s[2]=" + s[2] +"!= HEAD?");
    17.          //   if (s[2] != "HEAD")
    18.          //       return;
    19.             isHaed = false;
    20.             GetSize = int.Parse(s[1]);
    21.             this.buffer = new byte[GetSize];//adjust buffer size to hold data
    22.             receiveMsgType = int.Parse(s[0]);
    23.             Debug.Log("receiveMsgType"+receiveMsgType);
    24.             Debug.Log("GetSize=" + GetSize);
    25.             return;
    26.         }
    27.         Debug.Log("Entry Receiver");
    28.         if (receiveMsgType == msgType.testMessage)
    29.         {
    30.             Debug.Log("Receive test message=" + NetWorkAdapter.GetString(buffer));
    31.             isHaed = true;
    32.             return;
    33.         }
    When I send message low frequently ,basically fine(although sometimes with wrong sequence and duplicate).
    But if I send info like this
    Code (CSharp):
    1.  
    2.               byte[] b= new byte[1390];
    3.               while (e++ < 15)
    4.                 {
    5.                     sendmessage(id, b, msgType.testMessage);
    6.                 }
    or this
    Code (CSharp):
    1.  tempTime=0;
    2. void Update()
    3.     {
    4.         if (GaneStart)
    5.         {
    6.             tempTime += Time.deltaTime;
    7.             if (tempTime > 0.1)
    8.             {
    9.  
    10.                 tempTime = 0;
    11.                 syncData();//send object data
    12.                 //    Debug.Log("datasize=" +collectData().Length);
    13.             }
    14.         }
    15.    
    16.     }
    Receive will no response immediately and simultaneously I can't see any log info.
    But at send side error=0.
    I use above code synchronization game data.Any suggestion? And thanks for your reply.:)
     
    Last edited: Apr 25, 2017