#include <vcl.h>
#include <mmsystem.h>
#pragma hdrstop
#include "App_Log.h"
#include "App_Client.h"
#include "App_Packet.h"
//---------------------------------------------------------------------------
// 패킷 처리
//---------------------------------------------------------------------------
void __fastcall TClient::Execute(void)
{
	int				Step	= 0;
    DWORD			Timer	= timeGetTime();
	SPacketHeader	stHeader;
    BYTE			Buffer[32767];

    TMemoryStream	*RecvStream	= new TMemoryStream;

	while(!Terminated)
	{
		Sleep(1);

        __try
        {
            if(!IsConnect())
                continue;

            RecvStream->Clear();
            Context->IOHandler->ReadStream(RecvStream);
            RecvStream->Position	= 0;

            while(RecvStream->Position < RecvStream->Size)
            {
                // 남아 있는 Stream 크기 측정
                if(RecvStream->Size - RecvStream->Position < sizeof(SPacketHeader))
                {
                    printf("TClient::Execute() 비정상적인 패킷 사이즈 #1. %I64d, %I64d\n", RecvStream->Position, RecvStream->Size);

                    break;
                }

                RecvStream->Read(&stHeader, sizeof(SPacketHeader));

                // 에러 검사
                if(stHeader.Size != stHeader.Check + 1)
                {
                    printf("TClient::Execute() 패킷 무결성 에러 - %x, %d - %I64d, %I64d, %d\n", stHeader.Cmd, stHeader.Check, stHeader.Size, RecvStream->Position, RecvStream->Size);

                    break;
                }

                // 남아 있는 Stream 크기 측정
                if(RecvStream->Size - RecvStream->Position < stHeader.Size)
                {
                    printf("TClient::Execute() 비정상적인 패킷 사이즈 #2. %I64d, %I64d\n", RecvStream->Position, RecvStream->Size);

                    break;
                }

                RecvStream->Read(Buffer, stHeader.Size);

                // 패킷 처리
                switch(stHeader.Cmd)
                {
                case 0xBF00:

                    break;
                default:
                    printf("TClient::Execute() 알 수 없는 패킷 : 0x%02x, Size : %d", stHeader.Cmd, stHeader.Size);

                    Context->Disconnect();
                    Context->IOHandler->InputBuffer->Clear();

                    break;
                }
            }
        }
        // 정상 종료
        catch(EIdClosedSocket& e)
        {
            Context->Disconnect();
            Context->IOHandler->InputBuffer->Clear();

            continue;
        }
        catch(EIdException& e)
        {
            if(e.Message.Pos("racefully") <= 0)
                printf("접속 해제 #1 %s\n", e.Message.t_str());

            Context->Disconnect();
            Context->IOHandler->InputBuffer->Clear();

            continue;
        }
        catch(Exception& e)
        {
            printf("접속 해제 #2 %s\n", e.Message.t_str());

            Context->Disconnect();
            Context->IOHandler->InputBuffer->Clear();

            continue;
        }
        catch(...)
        {
            printf("접속 해제 #3\n");

            Context->Disconnect();
            Context->IOHandler->InputBuffer->Clear();

            continue;
        }
	}

    delete RecvStream;
}

void TClient::SendTest(BYTE *pBuffer, int pSize)
{
    SendPacket(CMD_TEST, pBuffer, pSize);
}
