【问题标题】:NetworkWriter WriteBytes: buffer is too largeNetworkWriter WriteBytes:缓冲区太大
【发布时间】:2017-10-18 03:28:06
【问题描述】:

我正在尝试为我正在开发的涂色书类型应用程序创建网络原型,该应用程序允许用户创建图像,然后将该图像发送到服务器,然后获取图像并将其包裹在实例化的网格周围.我在这次迭代中的最初问题是缓冲区大小。我收到错误:

NetworkWriter WriteBytes: buffer is too large (699064) bytes. The maximum buffer size is 64K bytes.
UnityEngine.Networking.NetworkWriter:WriteBytesFull(Byte[])
TextureMessage:Serialize(NetworkWriter)
UnityEngine.Networking.NetworkServer:SendToAll(Int16, MessageBase)
Server:SendTexture(Texture2D, String) (at Assets/Scripts/Server.cs:32)
Server:SendOnButtonPress() (at Assets/Scripts/Server.cs:19)
UnityEngine.EventSystems.EventSystem:Update()

如何增加缓冲区大小?我的图像将在 1.5-2mb 范围内。到目前为止我的代码如下:

public class MyMsgType
{
    public static short texture = MsgType.Highest + 1;
}

public class TextureMessage : MessageBase
{
    public byte[] textureBytes;
    public string message; //Optional
}

服务器端代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class Server : MonoBehaviour {

    public Texture2D textureToSend;
    string messageToSend = "Test Message";

    // Use this for initialization
    void Start () {
        NetworkManager.singleton.StartHost();
        Debug.Log("Server Started.");
    }

    public void SendOnButtonPress()
    {
        SendTexture(textureToSend, messageToSend);
    }

    //Call to send the Texture and a simple string message
    public void SendTexture(Texture2D texture, string message)
    {
        TextureMessage msg = new TextureMessage();

        //Convert Texture2D to byte array

        msg.textureBytes = texture.GetRawTextureData();
        msg.message = message;

        NetworkServer.SendToAll(MyMsgType.texture, msg);
    }
}

客户端代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class Client : MonoBehaviour
{
    NetworkClient myClient;

    // Use this for initialization
    void Start () {
        NetworkManager.singleton.StartClient();
        Debug.Log("Client Started.");

        setupClient();
    }

    // Create a client and connect to the server port
    public void setupClient()
    {
        //Create new client
        myClient = new NetworkClient();
        //Register to connect event
        myClient.RegisterHandler(MsgType.Connect, OnConnected);
        //Register to texture receive event
        myClient.RegisterHandler(MyMsgType.texture, OnTextureReceive);

        //Connect to server
        myClient.Connect("127.0.0.1", 4444);
    }

    //Called when texture is received
    public void OnTextureReceive(NetworkMessage netMsg)
    {
        TextureMessage msg = netMsg.ReadMessage<TextureMessage>();

        //Your Received message
        string message = msg.message;
        Debug.Log("Texture Messsage " + message);

        //Your Received Texture2D
        Texture2D receivedtexture = new Texture2D(4, 4);
        receivedtexture.LoadRawTextureData(msg.textureBytes);
        receivedtexture.Apply();
    }

    public void OnConnected(NetworkMessage netMsg)
    {
        Debug.Log("Connected to server");
    }
}

【问题讨论】:

    标签: c# unity3d networking buffer


    【解决方案1】:

    您需要通过将NetworkManager.connectionConfig 设置为您已设置为具有更大数据包大小的ConnectionConfig 对象来设置自定义配置。示例代码应该可以帮助您入门:

    void Start()
    {
        ConnectionConfig myConfig = new ConnectionConfig();
        myConfig.AddChannel(QosType.Unreliable);
        myConfig.AddChannel(QosType.UnreliableFragmented);
        myConfig.PacketSize = 1440;
        NetworkManager.connectionConfig = myConfig;
    }
    

    【讨论】:

    • 我在哪里说当我实际发送消息时要使用哪种渠道类型?
    • connectionConfig 是一个只读属性。目前这似乎不起作用。
    • @greyBow 文档并没有说它是只读的。我从未使用过它,所以我只有文档。
    • 这是我得到的错误:Assets/Scripts/Server.cs(31,10): error CS0200: Property or indexer UnityEngine.Networking.NetworkManager.connectionConfig' 不能分配给(它是只读的)`
    • @greyBow 您使用的是哪个版本的 Unity?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 1970-01-01
    相关资源
    最近更新 更多