Example #1
0
        //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        public static void ProcessReceive()
        {
            if ((socket == null) || (socket.Connected == false))
                return;

            if (socket.Available != 0)
            {
                bytesRec = socket.Receive(socketTempBuffer.data);

                Array.Copy(socketTempBuffer.data, 0, socketBuffer.data, socketBuffer.length, bytesRec);
                socketBuffer.length += bytesRec;

                //Client.Log (DateTime.Now + " socketTempBuffer. Size " + bytesRec + " socketBuffer.length " + socketBuffer.length + " : "+ BitConverter.ToString (socketTempBuffer.data, 0, Math.Min (50, bytesRec)));
            }


            if (packetAvalable)
                return;

            if (!packetStarted)
            {
                if (socketBuffer.length < (int)HeaderOffsets.FirstEnd)
                    return;

                packetSize = socketBuffer.GetInt();
                packetCheckSum = socketBuffer.GetUShort();
                packetStarted = true;
            }

            if (packetStarted)
            {
                if ((socketBuffer.length - socketBuffer.index) >= packetSize)
                {
                    if (packetCheckSum != socketBuffer.GetCRC16(packetSize))
                    {
                        Client.Log("Ошибка приема пакета. Контрольные суммы не совпали: " + packetCheckSum + " / " + socketBuffer.GetCRC16(packetSize), LogTypes.CriticalError);
                        return;
                    }

                    byte getPacketNum = socketBuffer.GetByte();
                    if (getPacketNum != nextGetPacketNum)
                    {
                        Client.Log("getPacketNum: " + getPacketNum + ".  nextGetPacketNum: " + nextGetPacketNum, LogTypes.CriticalError);
                        nextGetPacketNum = getPacketNum;
                    }
                    nextGetPacketNum++;

                    Array.Copy(socketBuffer.data, socketBuffer.index, packetBuffer.data, 0, packetSize - 1);
                    packetBuffer.index = 0;
                    packetBuffer.length = packetSize - 1;
                    packetAvalable = true;
                    packetStarted = false;

                    socketBuffer.index += packetSize - 1;
                    socketBuffer.Shift(socketBuffer.index);

                    //Client.Log (DateTime.Now.TimeOfDay + " Packet. Size " + packetSize + " : "+ BitConverter.ToString (packetBuffer.data, 0, packetSize) + "socketBuffer.length: " + socketBuffer.length);
                }


            }

        }