read TCP stream issue

Hi, Maybe you will be able to give me some tips 😅 I am writting a tool using already created API. The creator of the api doesn't know c# so he isn't able to help me with that issue (API is written in C++ but it should work fine with c# too). In short - tool sends json messages using TCP socket. Messages are terminated with '\1' character or 0x01 byte (so one "packet" can in fact be stored in 2 <or more> messages received from a bot). and this is the part I have no idea how to handle. I wrote something like that:
//function to receive messages from tcpserver
while (true)
{
byte[] numArray = new byte[TCPClient.ReceiveBufferSize + 1];
TCPClientStream.Read(numArray, 0, TCPClient.ReceiveBufferSize);
string[] msg = Strings.Split(Encoding.UTF7.GetString(numArray), "\u0001");
...
later json deserialize etc
}
//function to receive messages from tcpserver
while (true)
{
byte[] numArray = new byte[TCPClient.ReceiveBufferSize + 1];
TCPClientStream.Read(numArray, 0, TCPClient.ReceiveBufferSize);
string[] msg = Strings.Split(Encoding.UTF7.GetString(numArray), "\u0001");
...
later json deserialize etc
}
but it's not reading packets which are stored in more than one tcp message. do you have any ideas how can I fix that? I made a tool with QT and c++..and ok. it works fine. but I personally hate c++ and "upgrading" this tool is a horror for me. With C# it seems to be not reading part of the messages, which should be handled. I checked that and seems to be missing about 30% of the packets. There is no official c# api...but this kind of api should be possible to work with any language which supports JSON and TCP sockets. thanks for any tip in advance and have a nice day!
5 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3y ago
see $pipeline
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX3y ago
(Contains example for Socket TCP Echo) https://devblogs.microsoft.com/dotnet/system-io-pipelines-high-performance-io-in-net/ https://docs.microsoft.com/en-us/dotnet/standard/io/pipelines if you're handling data from/to a stream, and dealing with intermediate buffer / parser / writer, you probably should consider using System.IO.Pipelines It's handling internally * temporary buffer * memory pool to re-use memory and avoid allocation * accumulate buffer until you decide you have enough data to do something (deserialize ?) There's dedicated extensions for Stream for specific use case like : (Contains a sample to read file / dump to console) Reading (eg: to a file):
using var stream = File.OpenRead("lorem-ipsum.txt");
var reader = PipeReader.Create(stream);
using var stream = File.OpenRead("lorem-ipsum.txt");
var reader = PipeReader.Create(stream);
Writing (eg: to Console):
var writer = PipeWriter.Create(Console.OpenStandardOutput(), new StreamPipeWriterOptions(leaveOpen: true));
var writer = PipeWriter.Create(Console.OpenStandardOutput(), new StreamPipeWriterOptions(leaveOpen: true));
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server