//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "servermain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

#define BUF_SIZE    1024
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::TCPServerExecute(TIdPeerThread *AThread)
{
/*
    if( AThread->Stopped || AThread->Terminated ) return;
    TIdTCPServerConnection * con = AThread->Connection;
    AnsiString Message;
    try {
        Application->ProcessMessages();
        Message = con->ReadLn();
        MessageHandler(Message.c_str(), AThread);
    }
    catch(...) {
//		ListBox1->Items->Add("Error...");
        con->Disconnect();
    }
*/

    if( AThread->Stopped || AThread->Terminated ) return;
	TIdTCPServerConnection * con = AThread->Connection;
    char Message[BUF_SIZE];
	try
	{
        Application->ProcessMessages();
        con->ReadBuffer(Message, BUF_SIZE);
//        con->ClearWriteBuffer();
        MessageHandler(Message, AThread);
	}

	catch(...)
	{
//		ListBox1->Items->Add("Error...");
		con->Disconnect();
	}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::MessageHandler(char *Message, TIdPeerThread *AThread)
{
    //data handle
    ListBox1->Items->Add((AnsiString)Message);

    //send data
/*    char sBuf[BUF_SIZE];
    sprintf(sBuf, "Reseive Success!");
    AThread->Connection->WriteBuffer(sBuf, BUF_SIZE, true);
*/
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
    TCPServer->Active = True;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
    TCPServer->Active = false;
}
//---------------------------------------------------------------------------

