Improve WebSocket performance

This commit is contained in:
Li 2022-11-18 19:45:17 +13:00
parent eefc2b926d
commit 458748e6b0
8 changed files with 118 additions and 73 deletions

View file

@ -1,16 +1,19 @@
using HISP.Security;
using HISP.Util;
using System;
using System.Collections.Generic;
using System.Text;
namespace HISP.Server.Network
{
public class XmlSocket : Transport
{
private List<byte> currentPacket = new List<byte>();
private const byte XMLSOCKET_PACKET_TERMINATOR = 0x00;
public override void ProcessReceivedPackets(int available, byte[] buffer)
{
// In XmlSocket Packets are terminates by 0x00 so we have to read until we receive that terminator
for (int i = 0; i < available; i++)
{
if (buffer[i] == XMLSOCKET_PACKET_TERMINATOR) // Read until \0...
@ -38,10 +41,13 @@ namespace HISP.Server.Network
public override void Send(byte[] data)
{
int oldLength = data.Length;
// Resize the array to be 1 extra byte in size;
Array.Resize(ref data, oldLength + 1);
// add \0 to the end of the buffer
byte[] buffer = new byte[data.Length + 1];
Array.Copy(data, buffer, data.Length); // copy packet to buffer
buffer[buffer.Length - 1] = XMLSOCKET_PACKET_TERMINATOR;
data[oldLength] = XMLSOCKET_PACKET_TERMINATOR;
// send to the server
base.Send(data);