写tracker服务器的时候:
typedef boost::ptr_map<SOCKET, Cconnection> t_mapconnections;
/** 用map存储tcp连接
*/
t_mapconnections m_mapconnection;
if (connection->s() != INVALID_SOCKET)
{
connection->SetIocpContent(pClient);
if (strstr(pBuffer->buff, "\r\n\r\n"))
{
connection->OnRecv(pBuffer->buff, dwTrans);
iSend = TRUE;
}
//iSend = FALSE;
::EnterCriticalSection(&g_cs);
m_mapconnection[pBuffer->sClient] = connection.release();
::LeaveCriticalSection(&g_cs);
}
m_mapconnection[pBuffer->sClient] = connection.release(); m_mapconnection[pBuffer->sClient] 返回的是引用,不能直接给引用复制(就是不能用别的值赋给引用)。智能改变引用所指向的值的内容
所以得修改成以下内容
typedef boost::unordered_map<SOCKET, boost::shared_ptr<Cconnection> > t_mapconnections;
/** 用map存储tcp连接
*/
t_mapconnections m_mapconnection;
boost::shared_ptr<Cconnection> connection(new Cconnection(m_pServer, pBuffer->sClient, *((sockaddr_in*)pRemoteAddr) , this));
if (connection->s() != INVALID_SOCKET)
{
connection->SetIocpContent(pClient);
connection->OnRecv(pBuffer->buff, dwTrans);
iSend = FALSE;
::EnterCriticalSection(&g_cs);
m_mapconnection[pBuffer->sClient] = connection;
::LeaveCriticalSection(&g_cs);
}