I have this map
std::map<IPv4Address,std::vector<Agent>> report;
Where agent is defined as the following
typedef struct
{
IPv4Address Address;
AgentType Type;
std::map <IPv4Address, IPRoute> RoutingTable;
MetricType metric;
int UpdateReceived = 0;
}Agent;
I am sending this Agent struct through tcp sockets
and saving the values in the report std::map
int receive = recv(as.socket, (void *) &agent, sizeof(agent),0);
The routing table is initially empty. When the routing table size becomes >=1 the app crashes when adding to the map as seen below:
mutex.lock();
PrintInfo("Mutex locked");
if(report.find(as.ip) != report.end())
{
//f tells us if the agent was connected before to the router
bool f = false;
std::vector<Agent> tmpv =report[as.ip];
int tmp;
PrintInfo("Vector loop");
for(std::size_t i=0 ; i < tmpv.size() ; i++)
{
if(tmpv[i].Type == agent.Type)
{
f = true;
tmp = i;
break;
}
}
PrintInfo("Vector loop End");
if(f)
{
PrintInfo("Found -> Replacing");
--> This line crashes report[as.ip][tmp] = agent;
}
else
{
PrintInfo("Not Found -> Adding");
report[as.ip].push_back(agent);
}
PrintInfo("After add");
}
After serializing the struct using boost
library everything worked fine.
Here is an example of serialization :
Struct :
typedef struct
{
template<class Archive>
void serialize(Archive &ar,const unsigned int version)
{
ar & Address & Type & InterceptionDuration & RoutingTable & metric & UpdateReceived;
}
IPv4Address Address;
AgentType Type;
double InterceptionDuration;
std::map <IPv4Address, IPRoute> RoutingTable;
MetricType metric;
int UpdateReceived = 0;
}Agent;
Serializing :
std::string SerializeAgent(Agent a)
{
std::ostringstream archive_stream;
boost::archive::text_oarchive archive(archive_stream);
archive << a;
std::string s = archive_stream.str();
return s;
}
Deserializing :
Agent DeserializeAgent(std::string s)
{
Agent a;
std::string r(&s[0],s.size());
std::istringstream archive_stream(r);
boost::archive::text_iarchive archive(archive_stream);
archive >> a;
return a;
}
Sending through socket :
std::string s = SerializeAgent(agent);
send(reporterSendingSocket,s.c_str(),s.size(),0);
Receiving through socket :
std::vector<char> response(REPORT_MAX_BUFFER_SIZE);
int receive = recv(as.socket, (void *) &response[0], response.size(),0);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.