Debugging networking code



  • I spent about an hour debugging my networking code. It seemed to connect correctly but then mystically disconnect right away. I tried everything from moving server's io_service to another thread to running it on a different computer. Then I noticed my Connection::send method.

    	void Connection::send(PacketType type, Buffer* content)
    	{
    		socket.close();
    
    		Buffer* header = new Buffer();
    		header->putInt<uint16_t>((uint16_t) type);
    		header->putInt<uint32_t>(content->size());
    
    		std::vector<ba::const_buffer> buffers;
    		buffers.push_back(ba::buffer(header->pointer(), header->size()));
    		buffers.push_back(ba::buffer(content->pointer(), content->size()));
    
    		auto handler = bind(&Connection::sent, this, ba::placeholders::error, ba::placeholders::bytes_transferred, header, content);
    
    		ba::async_write(socket, buffers, handler);
    	}

    Just one thought came into my mind: What the fuck I was thinking when I wrote that line of code!?



  • @Scintillo said:

    What the fuck I was thinking when I wrote that line of code!?

    "Why did I make my socket class a singleton? I'll show me!"



  • @Scintillo said:

    Just one thought came into my mind: "Why the fuck am I working with such a boring technology"

    FTFY



  • auto handler

    TRWTF is C++11.



  • @Maciejasjmj said:

    auto handler

    TRWTF is C++

    FTFY.



  • Maybe socket should err-out or throw an exception if you try to write to a closed socket. There's no technical reason to do this.



  • @Maciejasjmj said:

    auto handler

    TRWTF is C++11.

    IIRC auto is useful for writing templates.

    also

    auto foo = []](type1 var1, type2 var2) -> int {...};
    instead of
    std::function<int (type1 var1, type2 var2)>  foo = []](type1 var1, type2 var2) -> int {...}; 
    n.b. i think i got that std::function syntax right


  • @Scintillo said:

    Just one thought came into my mind: What the fuck I was thinking when I wrote that line of code!?

    Since this is C++, maybe "I can't believe I didn't use Hungarian notation."  I mean if you called the variable m_socket (or maybe even m_hSocket) this bug would have been so much easier to spot.

     



  • @esoterik said:

    also

    auto foo = [](type1 var1, type2 var2) -> int {...};
    instead of
    std::function<int (type1 var1, type2 var2)>  foo = [](type1 var1, type2 var2) -> int {...}; 
    n.b. i think i got that std::function syntax right

    You've got the std::function syntax right, but not the equivalence. The statements are very different. The first keeps the function pointer encoded in the type, so the resulting call will be to direct operand and the compiler can possibly even inline the closure. The later will wrap the closure in an object with virtual method to dispatch to the function in a totally unoptimizable way.

    No, there is no equivalent for the former without using auto (unless you are getting the closure as argument, in which case you can have compiler infer template parameter from argument and use that).


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.