NetworkWriter WriteBytes:缓冲区太大

时间:2017-10-18 03:28:06

标签: c# unity3d networking buffer

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

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");
    }
}

1 个答案:

答案 0 :(得分:1)

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

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