I've recently decided to use boost::asio
for my sockets, but now I'm running into a problem: documentation seems to be lacking.
What I want to do is write a function that will send a message consisting of the following structure:
- 2 bytes of an unsigned integer (
uint16_t
) for an opcode all bytes - all bytes after that (a flexible amount) being any type of data (cast to
void*
). This data will be operated upon based on the opcode
For example, if the opcode is 1, perhaps defined as OPCODE_LOGIN, then the bytes following the opcode could contain a string containing login information, etc.
bool sendMessage(tcp::socket* sock, uint16_t opcode, void* data)
{
void* fullData = malloc(sizeof(uint16_t) + sizeof(data));
memcpy(fullData, (void*)opcode, sizeof(opcode));
memcpy(&fullData + sizeof(uint16_t), data, sizeof(data));
boost::asio::write(sock, boost::asio::buffer(fullData, sizeof(fullData)));
// by the way, at this point, is it safe to delete fullData to prevent memory leaks?
return true;
}
This does not compile, however. I get a cryptic compilation error regarding the call to write:
1>------ Build started: Project: client, Configuration: Debug Win32 ------
1> main.cpp
1>c:\boost\boost_1_47\boost\asio\impl\write.hpp(46): error C2228: left of '.write_some' must have class/struct/union
1> type is 'boost::asio::basic_stream_socket<Protocol> '
1> with
1> [
1> Protocol=boost::asio::ip::tcp
1> ]
1> did you intend to use '->' instead?
1> c:\boost\boost_1_47\boost\asio\impl\write.hpp(59) : see reference to function template instantiation 'size_t boost::asio::write<SyncWriteStream,ConstBufferSequence,boost::asio::detail::transfer_all_t>(SyncWriteStream &,const ConstBufferSequence &,CompletionCondition,boost::system::error_code &)' being compiled
1> with
1> [
1> SyncWriteStream=boost::asio::ip::tcp::socket *,
1> ConstBufferSequence=boost::asio::mutable_buffers_1,
1> CompletionCondition=boost::asio::detail::transfer_all_t
1> ]
1> c:\users\josh\documents\visual studio 2010\projects\client\client\main.cpp(53) : see reference to function template instantiation 'size_t boost::asio::write<boost::asio::ip::tcp::socket*,boost::asio::mutable_buffers_ 1>(SyncWriteStream &,const ConstBufferSequence &)' being compiled
1> with
1> [
1> SyncWriteStream=boost::asio::ip::tcp::socket *,
1> ConstBufferSequence=boost::asio::mutable_buffers_1
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
As you can see, the error message points directly into Boost's write.hpp
file, and not to any of my own code.. I believe I'm calling write()
incorrectly in some way, but after a solid hour of googling and researching references and examples (all of which either use another overloaded write()
or are using data with specifically defined sizes/structures), I haven't been able to determine what exactly I'm doing wrong here.
Can someone help me debug this compilation error?