g_Marvin
February 11th, 2009, 11:52 AM
Hi,
I hope I will be able to explain my problem.... let's start :
I have a class, inherited from CAsyncSocket, called CDiplomat.
MyDiplomat is a member of the MainWindow class.
Since MyDiplomat is asynchronous, and threaded, I can't really check every seconds if MyDiplomat has new data, especially because of concurrency problems.
Moreover, the CDiplomat class will be used in various apps, which means I don't want to build my whole app around it, but instead make it someting "transportable".
Then, I'd like that, every time MyDiplomat receives the "OnReceive" message, it transmits the newData to its owner (the MainWindow), and then calls a message called "UpdateRemoteData()" for exemple for display purpose. Or I could also forget the data transmission and only call from MyDiplomat the MainWindow::UpdateRemoteData() which will access to MyDiplomat data on its own while the MyDiplomat will be waiting till it is finished before accepting new incoming data.
class MainWindow : public CWnd
{
...
CDiplomat * MyDiplomat;
...
CStaticText * IncomingDataDisplay;
...
void UpdateRemoteData();
...
}
...
Byte * data;
Long SizeOfData;
...
class CDiplomat : public CAsyncSocket
{
OnReceive()
{
...
receive(data, &SizeOfData);
...
}
}
}
A few ideas I tried to explore did not meet success or did not convince me.
Here they are :
- Using a timer thread and checking through a global "flag" if new data is available (and unlocked), and then retrieve them. Then, the flag would have to be a mutex to avoid concurrency.
- Trying to pass the "UpdateRemoteData" function as a pointer and store it into MyDiplomat... but I can't do that because can't pass a static pointer since I want to access the members of the MainWindow to update then with incoming data, and can't pass an instance function like in C.
- Trying to implement a "Delegation-like" functionnality, like the Objective-C one. It sounded a bit weird to me... didn't go very far in this way: just an idea for now.
- Trying to pass the MyDiplomat owner (the MainWindow instance) as an argument so that I can call the "UpdateRemoteData" through the pointer. But since I didn't know (and don't want it) the type of the owner, I cannot call a function on a (void *) pointer : or maybe it is possible but I didn't do it the correct way.
Then, I for now don't know how to do it... suggestions are then welcome!
Hope it was clear engouh to deserve your interest in it. Thanks for reading till the end, and thanks in advance for your suggestion about it.
I hope I will be able to explain my problem.... let's start :
I have a class, inherited from CAsyncSocket, called CDiplomat.
MyDiplomat is a member of the MainWindow class.
Since MyDiplomat is asynchronous, and threaded, I can't really check every seconds if MyDiplomat has new data, especially because of concurrency problems.
Moreover, the CDiplomat class will be used in various apps, which means I don't want to build my whole app around it, but instead make it someting "transportable".
Then, I'd like that, every time MyDiplomat receives the "OnReceive" message, it transmits the newData to its owner (the MainWindow), and then calls a message called "UpdateRemoteData()" for exemple for display purpose. Or I could also forget the data transmission and only call from MyDiplomat the MainWindow::UpdateRemoteData() which will access to MyDiplomat data on its own while the MyDiplomat will be waiting till it is finished before accepting new incoming data.
class MainWindow : public CWnd
{
...
CDiplomat * MyDiplomat;
...
CStaticText * IncomingDataDisplay;
...
void UpdateRemoteData();
...
}
...
Byte * data;
Long SizeOfData;
...
class CDiplomat : public CAsyncSocket
{
OnReceive()
{
...
receive(data, &SizeOfData);
...
}
}
}
A few ideas I tried to explore did not meet success or did not convince me.
Here they are :
- Using a timer thread and checking through a global "flag" if new data is available (and unlocked), and then retrieve them. Then, the flag would have to be a mutex to avoid concurrency.
- Trying to pass the "UpdateRemoteData" function as a pointer and store it into MyDiplomat... but I can't do that because can't pass a static pointer since I want to access the members of the MainWindow to update then with incoming data, and can't pass an instance function like in C.
- Trying to implement a "Delegation-like" functionnality, like the Objective-C one. It sounded a bit weird to me... didn't go very far in this way: just an idea for now.
- Trying to pass the MyDiplomat owner (the MainWindow instance) as an argument so that I can call the "UpdateRemoteData" through the pointer. But since I didn't know (and don't want it) the type of the owner, I cannot call a function on a (void *) pointer : or maybe it is possible but I didn't do it the correct way.
Then, I for now don't know how to do it... suggestions are then welcome!
Hope it was clear engouh to deserve your interest in it. Thanks for reading till the end, and thanks in advance for your suggestion about it.