【问题标题】:How can i fill a byte array with a string without creating a new array?如何在不创建新数组的情况下用字符串填充字节数组?
【发布时间】:2018-03-28 20:55:41
【问题描述】:

我正在尝试打开多个 websocket,我需要以某种方式在每个套接字使用相同的缓冲区或在发送/接收新消息之前清除它们。 接收方法很好,因为我可以传递字节数组的参数,它会在不创建新的字节数组实例的情况下填充该参数。

我可以用BitConverter.GetBytes 方法做什么?我是否需要开始使用不安全的上下文并使用带有指针参数的重载GetBytes?还有其他方法吗? 我需要它来填充我将在构造函数中定义的outBytes 变量。

public class Client:IDisposable
{
    //Fields
    public char[] innerData { get; private set; }

    private byte[] inBytes;
    private byte[] outBytes;

    private ArraySegment<byte> inSegment;
    private ArraySegment<byte> outSegment;


    private WebSocket webSocket;
    public WebSocket Socket => this.webSocket;

    public readonly string clientID;
    //Auxiliary
    private const int BufferSize = 1024;

    public static Client CreateClient(WebSocket socket, string id)
    {
        Client client = new Client(socket, id);

        return client;
    }

    public Client(WebSocket socket, string id)
    {
        this.inBytes = new byte[BufferSize];
        this.inSegment = new ArraySegment<byte>(inBytes);

        this.outBytes = new byte[BufferSize];
        this.outSegment = new ArraySegment<byte>(outBytes);


        this.webSocket = socket;
        this.clientID = id;
        this.innerData = new char[BufferSize];
    }
    public  async Task<WebSocketReceiveResult> ReceiveResult()
    {
        if(this.webSocket.State!=WebSocketState.Open)
        {
            return null;
        }

        WebSocketReceiveResult result = await this.webSocket.ReceiveAsync(this.inSegment, CancellationToken.None);
        Encoding.UTF8.GetChars(this.inSegment.Array, 0, BufferSize, this.innerData, 0);
        return result;
    }

    public async Task SendMessage(string message)
    {
        if(this.webSocket.State==WebSocketState.Open)
        {

            this.outBytes = Encoding.UTF8.GetBytes(message, 0, message.Length); //How can i fill the already existing outBytes?
            await this.webSocket.SendAsync(this.outSegment, WebSocketMessageType.Text, true, CancellationToken.None);
        }

    }

    public void Dispose()
    {
        if(this.webSocket.State!=WebSocketState.Closed)
        {
            this.webSocket.Dispose();
            this.webSocket = null;
        }
    }


}

当我转换将要发送的消息时,我需要以某种方式使用已经存在的outBytes。此时outBytes 的行为就像一个指针,并且每次SendMessage 方法的迭代GetBytes 都会产生一个新的字节数组.

【问题讨论】:

    标签: c# websocket .net-core dispose unsafe


    【解决方案1】:

    您显然对 GetBytes 的工作方式有一个错误的误解,它不会每次都生成一个新数组,这个重载:

    Encoding.GetBytes Method (String, Int32, Int32, Byte[], Int32)

    将指定字符串中的一组字符编码为指定字节数组(来自 MSDN)

    所以你的行应该是

    Encoding.UTF8.GetBytes(message, 0, message.Length, this.outBytes, 0);
    

    该函数将使用 UTF8 编码将此字符串转换为字节来填充您的数组... 并且您可以使用返回值(一个整数)来检查已写入数组的字节数。

    【讨论】:

    • 这正是我一直在寻找的,但在我的重载中我没有任何接受字节数组的方法。相反,它接受了一个字节指针。
    • 我可以发誓字节数组参数没有过载。msdn.microsoft.com/en-us/library/…。只有字节指针与 unsafe 一起使用。
    猜你喜欢
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多