Reputation:
#include<iostream>
#include<boost/thread.hpp>
#include<vector>
#include<boost/asio.hpp>
#include <ctime>
#include <string>
#include <boost/serialization/vector.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <sstream>
using boost::asio::ip::tcp;
std::string make_daytime_string()
{
using namespace std; // For time_t, time and ctime;
time_t now = time(0);
return ctime(&now);
}
class maintain_overlay{
public:
maintain_overlay():thread_(boost::bind(&maintain_overlay::member_list_server, this))
{
thread_.join();
}
void member_list_server(){
boost::asio::io_service io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 13));
for (;;)
{
tcp::socket socket(io_service);
acceptor.accept(socket);
member_list.push_back(socket.remote_endpoint());
std::string message = make_daytime_string();
std::stringstream ss;
boost::archive::text_oarchive oa(ss);
oa<<member_list;/////////////error comes because of this code
boost::system::error_code ignored_error;
boost::asio::write(socket, boost::asio::buffer(message),
boost::asio::transfer_all(), ignored_error);
}
}
private:
std::vector<tcp::endpoint> member_list;
boost::thread thread_;
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & member_list;
}
};
I get the Error
error C2039: 'serialize' : is not a member of 'boost::asio::ip::basic_endpoint<InternetProtocol>'
----edit------
I think a mistake I am making is that i am trying to seriliase the member data instead of a whole object instance...How and where should I declare the seriliase method if I want to seriliase the meber_list vector?
Upvotes: 1
Views: 1383
Reputation: 10698
The problem is that you're trying to serialize a vector<tcp::endpoint>
but tcp::endpoint
is not serializable.
If you want to send only the IP address and port of the connection, then you would have to extract them from the tcp::endpoint
or write your own serialization/deserialization method.
You can overload boost's serialization function in a non-intrusive way like this :
namespace boost {
namespace serialization {
template<class Archive, class Protocol>
void save(Archive & ar, asio::ip::basic_endpoint<Protocol> & e, unsigned int version)
{
string ip = e.address().to_string();
short port = e.port();
ar & ip;
ar & port;
}
template<class Archive, class Protocol>
void load(Archive & ar, asio::ip::basic_endpoint<Protocol> & e, unsigned int version)
{
string ip;
short port;
ar & ip;
ar & port;
e = asio::ip::basic_endpoint<Protocol>(ip, port);
}
template<class Archive, class Protocol>
inline void serialize(Archive & ar, asio::ip::basic_endpoint<Protocol> & e, const unsigned int file_version) {
split_free(ar, e, file_version);
}
}
}
Upvotes: 7