librespot-org / librespot-java

The most up-to-date open source Spotify client
Apache License 2.0
379 stars 93 forks source link

Translating Session creation / connect into C# failing. Stuck at reading int. #802

Closed maximilianosinski closed 7 months ago

christosk92 commented 7 months ago

I've been successful in making connection through TCP in C#. you can find the connection code here: https://github.com/christosk92/Wavee/blob/main/src/Wavee.Spfy/DefaultServices/DefaultTcpClient.cs

maximilianosinski commented 7 months ago

.

christosk92 commented 7 months ago

binaryreader and binarywriter are endian aware. All packets to Spotify are required to be in Big-Endian byte order.

Code running in JVM is already Big-Endian. But .NET most likely does stuff in Little-Endian.

Hence I advise against using BinaryReader/Writer and just writing bytes directly to the output stream.

Reference: https://learn.microsoft.com/en-us/dotnet/api/system.io.binaryreader.readuint16?view=net-8.0

`Reads a 2-byte unsigned integer from the current stream using little-endian encoding and advances the position of the stream by two bytes.

maximilianosinski commented 7 months ago

im using the stream only now, and im receiving a other error: System.IO.EndOfStreamException: Unable to read beyond the end of the stream.

         Span<byte> header = new byte[3];
        _conn.Stream.ReadExactly(header);

this in the receive function, any idea?

christosk92 commented 7 months ago

Spotify probably closed the stream because you sent it invalid data. Is your Receive/Send function thread safe? Make sure it cannot be hit more than once at a team (its a blocking function)

christosk92 commented 7 months ago

Also for both send and receive you are starting at sequence=1, it should always start at 0

maximilianosinski commented 7 months ago

okay, so im incrementing the sequences at the end of the functions, so the first send and receive will have 0, no success. my send and receive should be correct, it's 1:1 the same from your code, and as you said i maybe sent invalid data, this could be, im trying to authenticate with username and password, do you have a code example for that?

maximilianosinski commented 7 months ago

i actually had the wrong field numbers in my protobuf-files, and now it works!

other, question do you have the stream reporting in your code? (context changed, etc) i can't seem to find it.