CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2005
    Posts
    478

    Class Instance as Timer ID - Failing

    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.

    Code:
    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;
    
    };
    Windows XP, Visual Studio 2008, SVN

  2. #2
    Join Date
    Aug 2005
    Posts
    478

    Re: Class Instance as Timer ID - Failing

    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.
    Windows XP, Visual Studio 2008, SVN

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured