【问题标题】:Unity3d LLAPI Networking: custom networking class not being used (NetworkConnection.TransportRecieve)Unity3d LLAPI Networking:未使用自定义网络类(NetworkConnection.TransportRecieve)
【发布时间】:2023-04-04 22:35:01
【问题描述】:

我正在尝试使用覆盖虚拟功能 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 和 Server 的 NetworkConnection 使用这个 DummyConnection 类,它通过我自己的网络解决方案强制发送数据。这是一种实验。我“滥用”了 NetworkConnection.Initialize 中的参数,只是简单地放置了我的网络解决方案生成的我自己的自定义 connectionId,因为我没有将 NetworkConnection 用于其他任何事情,而是假装一个实际的连接。然后当它需要发送数据时,它应该在 DummyConnection 中调用我的覆盖函数 TransportSend。我这样做是因为我希望仍然能够将高级 API 与 [SyncVar] 和 [Command] 等功能一起使用。

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

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

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

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

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

【问题讨论】:

    标签: c# networking unity3d network-programming multiplayer


    【解决方案1】:

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

    Send()
    

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

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

    【讨论】:

    • 看2017.4的开源网络代码,现在所有的调用路径似乎最终都要经过TransportSend。所以也许这已经解决了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多