开发者

Merge splitted bytes from socket for further usage?

开发者 https://www.devze.com 2023-03-13 04:02 出处:网络
I have a TCP socket application where I have to read several types of replies being the max size of a buffer 8192 some replies are splitted in more packets.

I have a TCP socket application where I have to read several types of replies being the max size of a buffer 8192 some replies are splitted in more packets.

Currently I receive a list of members at reply 44, so the first idea I had to be able to deal with splitted packets was to define a stream out side of it to stored the incoming data until it is complete with a bool and current size variable.

Once it hits the reply 44 it will check if extraList is true or false, if false it means it is an initial request to incoming members list.

If the 4 initial bytes of the packet is bigger then bytes.Legth which is 8192 it will trigger the extraList to true and fill the initial data to the buffer I had previously set with the with the total packet size as it is size.

Since extraList has been trigged and turned into true, the packet reading will fall into it until the data is complete, which will then set it back to false and trigger the MemberList function with the complete list.

Would like some advices, suggestions, etc to improve this code.

int storedCurSize = 0;
MemoryStream stored = null;
bool extraList = false;

while (roomSocket.Connected)
{
    byte[] bytes = new byte[roomSocket.ReceiveBufferSize];
    roomSocket.Receive(bytes);

    MemoryStream bufferReceived = new MemoryStream(bytes, 0, bytes.Length);
    using (var reader = new BinaryReader(bufferReceived))
    {
        int packetSize = (int)reader.ReadInt32() + 9;
        int reply = (int)reader.ReadByte();
        if (reply == 44 || extraList)
        {
            if (!extraList && packetSize <= bytes.Length)
            {
                MemberList(bytes);
            }
            else
            {
                if (!extraList)
                {
                    stored = new MemoryStream(new byte[packetSize], 0, packetSize);
                    stored.Write(bytes, 0, bytes.Length);
                    storedCurSize = bytes.Length;
                    extraList = true;
                }
                else
                {
                    if (storedCurSize < stored.Length)
                    {
                        int storedLeftSize = (int)stored.Length - storedCurSize;
                        stored.Write(bytes, 0, (storedLeftSzie < bytes.Length) ? storedLeftSize : bytes.Length);
                        storedCurSize += (storedLeftSize < bytes.Length) ? storedLeftSize : bytes.Length;
                        if (storedCurSize >= stored.Length)
                        {
                            extraList = false;
                            MemberList(stored.ToArray开发者_开发技巧());
                            stored.Close();
                        }
                    }
                }
            }
        }
    }
}


While reading code briefly what is flaring is magic numbers (9, 44) and very deep nesting. Replace numbers with good-named constants and move out some parts of code as methods.

In case they are tightly twisted by used local variables - probably all this method worth moving out to worker class with single responsibility - to read the message. Thus local variables becomes class fields and methods wouldn't be so inflexible to refactoring.

Also MemberList(...) is poor name for a method as for me. Make it a verb that will describe what method is doing.


To merge bytes that aren't together, you can use Buffer.BlockCopy().

byte[] buf1;
byte[] buf2;
byte[] concatenated = new byte[buf1.Length + buf2.Length];

Buffer.BlockCopy(buf1, 0, concatenated, 0, buf1.Length);
Buffer.BlockCopy(buf2, 0, concatenated, buf1.ength, buf2.Length);
0

精彩评论

暂无评论...
验证码 换一张
取 消