Re: [sigc] templates and my problem



Am 02.08.2012 07:51, schrieb Mohsen Pahlevanzadeh:
Thank you so mush, i wake up now,I wrote the following function as a
signal:
///////////////////////////////////////////////////
bool NetworkSocket::isDataReady()
{
void *buffer;
sockaddr_in from;
socklen_t fromLength = sizeof( from );

if (::recvfrom(this->socketFD,buffer,FRAMEBUFFER + 6, 0, (sockaddr *)&this->getSocketAddressStructureOfServer(), &fromLength ) == -1)
{
if (errno == EAGAIN || errno == EWOULDBLOCK)
return false;
}
return true;

}
/////////////////////////////////////////////////

I don't know where you see any signalling being done here. There are various things wrong with your code though, like 'buffer' not being a buffer but an unitialized pointer, unused variables, unindented code (this could be the fault of your email client), confusing control flow (what is this true or false thing?), C-style casts in C++ code.


When in blocking mode data is ready it can notify me that i can read
socket...
Then i wrote a function as slot:
///////////////////////////////////////////////////
int NetworkSocket::readDatagrams(unsigned char *buffer, string
&srcAddress, unsigned short int & srcPort)
{
unsigned int maximumPacketSize = FRAMEBUFFER + 6;
int returnValue ;
sockaddr_in from;
socklen_t fromLength = sizeof( from );
int receivedBytes;

fromLength = sizeof(this->getSocketAddressStructureOfServer());
receivedBytes = recvfrom( this->socketFD, buffer, maximumPacketSize, 0, (sockaddr *)&this->getSocketAddressStructureOfServer(), &fromLength );

returnValue = receivedBytes;
if ( receivedBytes <= 0 )
returnValue = -1;


srcAddress = inet_ntoa(this->getSocketAddressStructureOfServer().sin_addr);
srcPort = ntohs( ( unsigned short int)this->getSocketAddressStructureOfServer().sin_port );

return returnValue;
}
////////////////////////////////////////////////////////

...sizeof is not a function, you only need (brackets) when applying that operator to a type, not to a reference. Again, lots of unused variables, something which you could avoid systematically by always declaring _and_ initializing them in the same place.


Above functions work fine, Kjell helped and i debug my code that wrote
in asleep, i define the following 2 lines in class body:
/////////////////////////////
sigc::signal<bool> ready;
void run();
///////////////////////////
the i wrote following code in constructor:
//////////////////////////
this->ready.connect(sigc::mem_fun(*this,&NetworkSocket::createThread));
/////////////////////////////////////////
Question:
But i don't relate my signal (isDataReady()) to createThread

I'm not sure what your problem is here, if you want your function to be called, just do that directly or maybe indirectly by emitting the signal. Also, do you really want to create a thread when something is ready?


However, I think you are going about this the wrong way on a very basic conceptual level. Let me explain...

Example 1: The ready() signal
If there is data ready, you probably want to call it with 'true' as parameter. If no data is ready, do you really want to call it with false as parameter? My approach would have been to emit a signal with the received data whenever I receive the data and not emit anything when no data is received.

Example 2: The readDatagrams() slot
Here, you receive data which is stored in a buffer provided by the caller. What happens if multiple slots are connected to the same signal? Or if none are connected? In general, a slot/signal framework is IMHO most "natural" when you have one sender and multiple receivers and when the receiver doesn't expect any response.


I think that the approach that best suits your problem is that you start a thread that receives datagrams and then emits a signal with the received data.

{Removed dozens of lines of quoted text without any relation to the question.}


Good luck!

Uli


**************************************************************************************
Domino Laser GmbH, Fangdieckstraße 75a, 22547 Hamburg, Deutschland
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
**************************************************************************************
Visit our website at http://www.dominolaser.com
**************************************************************************************
Diese E-Mail einschließlich sämtlicher Anhänge ist nur für den Adressaten bestimmt und kann vertrauliche Informationen enthalten. Bitte benachrichtigen Sie den Absender umgehend, falls Sie nicht der beabsichtigte Empfänger sein sollten. Die E-Mail ist in diesem Fall zu löschen und darf weder gelesen, weitergeleitet, veröffentlicht oder anderweitig benutzt werden.
E-Mails können durch Dritte gelesen werden und Viren sowie nichtautorisierte Änderungen enthalten. Domino Laser GmbH ist für diese Folgen nicht verantwortlich.
**************************************************************************************



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]