Unity3d LLAPI 网络:自定义网络 class 未使用 (NetworkConnection.TransportRecieve)

Unity3d LLAPI Networking: custom networking class not being used (NetworkConnection.TransportRecieve)

我正在尝试使用覆盖虚拟功能 NetworkConnection.TransportSend 和 NetworkConnection.TransportRecieve (btw this is a typo in the API, it's spelled receive!) 的能力创建我自己的网络接口,这是我的代码:

 void CreateNetworkClient(ConnectionId _connectionId) { 
         Debug.Log("creating new network client with connectionId " + _connectionId);
         DummyConnection newDummyConnection = new DummyConnection ();
         newDummyConnection.Initialize ( _connectionId.ToString (), 0,  _connectionId.id, new HostTopology(connectionConfig, 100)); //create a new dummy connection, which we'll assign to the high level API networkClient from its constructor

         networkClient = new NetworkClient(newDummyConnection);
         networkClient.SetNetworkConnectionClass<DummyConnection> (); //not sure how to use this, because it's supposed to apply this class to /new/ connections
         networkClient.connection.logNetworkMessages = true;
         Debug.Log ("networkClient class: " + networkClient.networkConnectionClass.Name); //shows DummyConnection
         Debug.Log ("networkClient connection Id: " + networkClient.connection.connectionId);
         networkManager.client = networkClient;

         ClientScene.Ready (networkClient.connection); //tell server we're ready to spawn
         if (!networkClient.connection.isConnected)
             Debug.LogError ("network client failed to connect");


     }

     public class DummyConnection : NetworkConnection {
     //override the sending function called every time a networkClient or networkServer wants to send data. 
     public override bool TransportSend(byte[] bytes, int numBytes, int channelId, out byte error) {
         Debug.Log ("using dummy connection to send data to " + connectionId);
         error = 0;
         if (channelId == Channels.DefaultReliable)
             SendData (bytes, (short)connectionId, true); 
         else if (channelId == Channels.DefaultUnreliable)
             SendData (bytes, (short)connectionId, false);
         else
             Debug.LogError ("error in DummyConnection: unknown channel! Try using default reliable or unreliable or adding support");


         return true; //probably not good. This might trick it into thinking there's no errors, ToDo: fix

     }
     //just a debug function
     public override void TransportRecieve(byte[] bytes, int numBytes, int channelId)
     {
         StringBuilder msg = new StringBuilder();
         for (int i = 0; i < numBytes; i++)
         {
             var s = String.Format("{0:X2}", bytes[i]);
             msg.Append(s);
             if (i > 50) break;
         }
         Debug.Log("TransportRecieve h:" + hostId + " con:" + connectionId + " bytes:" + numBytes + " " + msg);
         HandleBytes(bytes, numBytes, channelId);
     }
 }

基本上,我试图让 networkClient 和服务器的 NetworkConnection 使用这个 class DummyConnection,它通过我自己的网络解决方案强制发送数据。这是一种实验。我 "abuse" NetworkConnection.Initialize 中的参数只是将我自己的网络解决方案生成的自定义 connectionId 放入其中,因为我没有将 NetworkConnection 用于其他任何用途,只是为了伪装一个实际连接。然后当它需要发送数据时,它应该调用我在 DummyConnection 中的覆盖函数 TransportSend。我这样做是因为我希望仍然能够使用具有 [SyncVar] 和 [Command] 等功能的高级 API。

所以我连接了我自己的网络解决方案,然后添加了一个虚拟连接,我试图让 networkClient 和 networkServer 使用这个 DummyConnection,但是双方都没有显示应该通过 DummyConnection 发送的调试日志.

调试显示 networkClient、其关联的 networkConnection 和 networkManager 都处于活动状态并已连接。

到目前为止,我的自定义网络解决方案无关紧要,但连接正常。 将连接设置为使用 NetworkConnection.logNetworkMessages 确实表明客户端正在发送数据,在控制台中记录第一条消息 (ClientScene.Ready()).

所以我的问题是,我的设置是否正确?因为当我强制数据通过它时,我什至无法通过 DummyConnection 记录错误。 以下信息会有所帮助:

  1. 确切的 unity3d 网络堆栈是什么?例如:
  2. NetworkServer和NetworkClient是否一定要使用TransportSend()TransportRecieve()
  3. NetworkClient 是否必须使用 networkClient.Connect() 来尝试向其连接发送数据?

花了 DAYS 才得出这个解决方案,糟糕的文档几乎完全是罪魁祸首: 它实际上并没有像文档所暗示的那样使用 TransportSend()TransportRecieve() 将所有数据发送和接收到应用程序层,或者至少是其中的大部分内容。它实际上使用简单

Send()

在注意到此函数也是 NetworkConnection 中的虚函数后,我怀疑了这一点。配置完全没有问题-

在 DummyConnection 中添加一些覆盖函数 - 一个用于 Send() SendUnreliable(),其余的都解决了问题,现在当我的代码运行时 ClientScene.Ready() 它成功地使用了我的自定义覆盖功能!