Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WaitForSingleObject returns to early with former string result

Status
Not open for further replies.

Irit

Programmer
Aug 21, 2001
9
0
0
US
Hello all,
We are using MFC in a small app that receives a name of a file from a target(remote board), trough FTP connection, then copies it to the host (PC app is working from).
We have used setEvent to tell the class CPlotParamsDlg when the name of the file is updated, but it seems that the class CPlotParamsDlg gets the file name before the signal is ariving.
Please see the code:

// This is the CPlotParamsDlg class, that receives the file name

class CPlotParamsDlg : public CDialog
{
// Construction
public:
~CPlotParamsDlg();
CPlotParamsDlg::CPlotParamsDlg( CWnd* pParent = NULL,
unsigned int ix = 0,
unsigned long ip = 0);
unsigned short sector, unsigned short channel);
// Received file name
char fileName[150];

// Implementation
private:
void SendFwRevLogFileMessage(int m_testType,int channel, int sector,
time_t startTime,time_t endTime);
unsigned int cardIx;
unsigned long cardIp;
int m_testType;
};

// constructor that initiates the gs_evGetDataReady handle.

CPlotParamsDlg::CPlotParamsDlg(CWnd* pParent /*=NULL*/,
unsigned int ix, unsigned long ip)
: CDialog(CPlotParamsDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPlotParamsDlg)
m_ChannelComboValue = _T("");
m_SectorComboValue = _T("");
//}}AFX_DATA_INIT
cardIx = ix;
cardIp = ip;
m_testType = 0;
gs_evGetDataReady = CreateEvent(NULL, TRUE, FALSE, /*EV_DO_TEST*/NULL);
}

// this function sends the request to get the information from the target.

void CPlotParamsDlg::SendFwRevLogFileMessage(int m_testType,int channel, int sector,
time_t startTime,time_t endTime)
{
char message[100] = {0};

sprintf(message,"%d,testType=%d,channels=%d,sectors=%d,startTime=%d,endTime=%d",GET_FW_REV_LOGS_CMD,m_testType,channel,sector,startTime,endTime);
g_udpMessanger.sendUdpMessage(message , strlen(message) , cardIp, cardIx );


}

void CPlotParamsDlg::OnOK()
{
int res , channel , sector;
UpdateData(TRUE);
char logsPath[150] = {0};
char logName[150] = {0};
IN_ADDR addr;
addr.s_addr = htonl(cardIp);

char ipStr[100];
strcpy(ipStr,inet_ntoa(addr));


SendFwRevLogFileMessage(m_testType , channel,sector,0,0);

res = WaitForSingleObject(gs_evGetDataReady, 5000) ;

if (res == WAIT_OBJECT_0)
{
if (!(g_udpMessanger.getUdpMessage( logName ) ))
return;
CCobraFtp myFtp(ipStr);
myFtp.setFtpDir("//ata0/Log");

if(!myFtp.GetTestRsltsLog(logName,logsPath))
{
AfxMessageBox("There're No Valid Test logs ");
return ;
}

m_CreateTestsPlot ( logsPath,channel, sector );
}
else if (res == WAIT_TIMEOUT)
{
AfxMessageBox("Error : Get Test Results Logs Time Out ");

}

}

/////////////////////////////////////////////////////////////////////

class UdpMessanger : public CSocket
{
public:
BOOL AudioDataFLag;
void handleMessage(char * buffer , unsigned long ip);
int sendUdpMessage(char * buffer, int len,unsigned long ip);
int sendUdpMessage(char *buffer, int len,unsigned long ip,unsigned short cardIx);
void closeSock();
int openSock();
int getUdpMessage(char * buffer);
UdpMessanger(unsigned short port = UDP_PORT);
virtual ~UdpMessanger();
protected:
virtual void OnReceive(int nErrorCode);
unsigned short udpPort;
char message_buffer[150];
};

// this function is the main handler of the UdpMessanger class.

void UdpMessanger::handleMessage(char * buffer , unsigned long ip)
{
extern HANDLE gs_evGetDataReady;
int cardIx;
if((cardIx = findCardIxfromIp(ip)) == -1)
return;

switch (message_type)
{
...
case GET_FW_REV_LOGS_CMD:
strcpy(message_buffer,&buffer[3]);
SetEvent(gs_evGetDataReady);
break;
case LIST_MAN_USERS_CMD:
if(m_gView)
{
char str[356];
sprintf(str , "Remote Users (IP Port): %s",&buffer[3]);
m_gView->printMessage(str , cardIx);
}

break;
default:
break;
}
}

// this function sends the udp message to the target.

int UdpMessanger::sendUdpMessage(char *buffer, int len,unsigned long ip)
{
SOCKADDR_IN Addr;
int numbytes;

Addr.sin_family = AF_INET;
Addr.sin_addr.s_addr = htonl(ip);
Addr.sin_port = htons(UDP_PORT);
memset(&(Addr.sin_zero),0,8);

if(numbytes = SendTo(buffer, len, (SOCKADDR*)&Addr, sizeof(Addr),0) != len)
{
my_printf("Send Error - send_udp_message(). Error is %d",
WSAGetLastError());
return 0;
}
return 1;
}

// this function copies the udp message to buffer.

int UdpMessanger::getUdpMessage(char * buf)
{
if ( buf != NULL )
{
strcpy(buf, message_buffer);
return 1;
}

return 0;
}

I hope this is not to much or too less.
What I mean is that when I take from OnOK() the logName after WaitForSingleObject returned, it gives me the former name (as it wasn't updated).

Sorry for the long message and thanks in advance for all of you.
Irit.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top