Click to See Complete Forum and Search --> : Class Instance as Timer ID - Failing


Calculator
February 11th, 2007, 03:27 PM
I have a few wierd things going on here, but basically, I am storing instances of these 'client' classes in a vector in a server program, and I keep a timer going that pings them occaisionally to check connectivity. The thing is, is that, the sending is failing, because it doesn't believe that the id is my class instance. The only thing I see that happens is I cast the 'this' pointer to a UINT_PTR, but nothing else. Here's is the raw version with just that 'ping' functionality.

struct client
{
client( SOCKET a, SOCKADDR b, HWND c )
: s(a), sa(b), r(c), count(0)
{
dout << "client(SOCKET, SOCKADDR, HWND)" << "\n";

UINT_PTR ret = ::SetTimer( r, ( UINT_PTR ) this, 1000, &tp );

if( ret == 0 )
{
dout << geterr( "SetTimer", ::GetLastError() );
}
else
{
clean = true;
}
}

client( SOCKET a, HWND b )
: s(a), r(b), clean(false), count(0)
{
dout << "client(SOCKET, HWND)" << "\n";
}

client( const client & other )

:

s( other.s ),
sa( other.sa ),
r( other.r ),
recv_buf( other.recv_buf ),
count( other.count )

{
dout << "client(const client &)" << "\n";

client & sss = const_cast< client & >( *this );
client & ooo = const_cast< client & >( other );

ooo.clean = false;
sss.clean = true;
}

~client( void )
{
dout << __FUNCTION__ << "\n";

if( clean )
{
BOOL ret = ::KillTimer( r, ( UINT_PTR ) this );

if( ret == 0 )
{
dout << geterr( "KillTimer", ::GetLastError() );
}
}
}

client & operator = ( const client & other )
{
dout << "client & operator = (const client &)" << "\n";

client & sss = const_cast< client & >( *this );
client & ooo = const_cast< client & >( other );

ooo.clean = false;
sss.clean = true;

s = other.s;
sa = other.sa;
r = other.r;
recv_buf = other.recv_buf;
count = other.count;

return *this;
}

static VOID CALLBACK tp
(
HWND hwnd,
UINT uMsg,
UINT_PTR idEvent,
DWORD dwTime
)
{
dout << __FUNCTION__ << "\n";
client * _this = ( client * ) idEvent;
int i = _this->count;

std::stringstream ss;
ss << _this->count;
dout << ss.str().c_str() << "\n";

int ret = ::send( _this->s, ss.str().c_str(),
ss.str().length() + 1, 0 );

if( ret == SOCKET_ERROR )
{
// Fails - operation on a non-socket
dout << geterr( "send", ::WSAGetLastError() );
}

_this->count++;
}

bool operator == ( const client & other ) const
{
dout << __FUNCTION__ << "\n";
return s == other.s;
}

SOCKET s;
SOCKADDR sa;
HWND r;

bool clean;
int count;

std::vector< char > recv_buf;

};

Calculator
February 11th, 2007, 03:42 PM
I should have been more perceptive, I of course knew the objects were being copied and took measures for making that work for the program, but I never thought that the instance that would have originally set the timer would be invalidated (which of course it was). Problem solved.