BOOL CConnectionMgr::CloseAllConnection() { CConnection *pConn = NULL; for(int i = 0; i < SVR_CONN_ID; i++) { pConn = m_StableConnList[i]; if(pConn != NULL) { pConn->Close(FALSE); } } for(std::set<CConnection*>::iterator itor = m_WaitConnList.begin(); itor != m_WaitConnList.end(); ++itor) { pConn = *itor; if(pConn != NULL) { pConn->Close(FALSE); } } for(stdext::hash_map<UINT64, CConnection*>::iterator itor = m_VarieableConnList.begin(); itor != m_VarieableConnList.end(); ++itor) { pConn = itor->second; if(pConn != NULL) { pConn->Close(FALSE); } } return TRUE; }
int ads_gm_import() { CProvidersCollection pc(true); StringPairs providersList = pc.ToStringPairs(); DlgProviders dlg(providersList, CWnd::FromHandle(adsw_acadMainWnd())); dlg.DoModal(); String providerName = dlg.GetProviderName(); if (providerName.length() == 0) { ads_retnil(); return( RSRSLT); } DlgProviderParam dlgProviders(pc.GetItem(providerName)); dlgProviders.DoModal(); CConnection * connection = dlgProviders.GetConnection(); if (connection != NULL) { FeatureClasses * featureClasses = CFeatureClass::GetFeatureClasses(connection); DlgLayers dlgLayers(featureClasses); dlgLayers.DoModal(); String featureClassName = dlgLayers.GetFeatureClassName(); if (featureClassName.length() == 0) { ads_retnil(); return( RSRSLT); } CFeatureClass * featureClass = featureClasses->operator [](featureClassName); if (featureClass == 0) { ads_retnil(); return( RSRSLT); } String extent = GetSelectedExtent(); connection->Open(); CFeatureReader featureReader = featureClass->SelectByExtent(extent); try { featureReader.DrawAll(); } catch (...) { } connection->Close(); delete featureClass; } delete connection; ads_retnil(); return( RSRSLT); }
BOOL CNetManager::WorkThread_ProcessEvent() { EventNode _EventNode; while(TRUE) { if(!m_DispatchEventList.Pop(_EventNode)) { continue; } CConnection *pConnection = (CConnection *)_EventNode.pPtr; if(pConnection == NULL) { if(_EventNode.dwEvent != 1) { CLog::GetInstancePtr()->AddLog("错误:取出一个空事件!"); } continue; } if(_EventNode.dwEvent == EVENT_READ) { if(!pConnection->HandleRecvEvent(0)) { //基本表明连接己断开,可以关闭连接了。 pConnection->Close(TRUE); CConnectionMgr::GetInstancePtr()->DeleteConnection(pConnection); } else { struct epoll_event EpollEvent; EpollEvent.data.ptr= pConnection; EpollEvent.events = EPOLLIN|EPOLLET; epoll_ctl(m_hCompletePort, EPOLL_CTL_MOD, pConnection->GetSocket(), &EpollEvent); } } else if(_EventNode.dwEvent == EVENT_WRITE) { } } return TRUE; }
CConnection* CNetManager::AssociateCompletePort( SOCKET hSocket ) { CConnection *pConnection = CConnectionMgr::GetInstancePtr()->CreateConnection(); pConnection->SetSocket(hSocket); pConnection->SetDataHandler(m_pBufferHandler); if(NULL == CreateIoCompletionPort((HANDLE)hSocket, m_hCompletePort, (ULONG_PTR)pConnection, 0)) { pConnection->Close(FALSE); return NULL; } return pConnection; }
BOOL CNetManager::WorkThread_Listen() { while(TRUE) { sockaddr_in Con_Addr; socklen_t nLen = sizeof(Con_Addr); memset(&Con_Addr, 0, sizeof(Con_Addr)); SOCKET hClientSocket = accept(m_hListenSocket, (sockaddr*)&Con_Addr, &nLen); if(hClientSocket == INVALID_SOCKET) { CLog::GetInstancePtr()->AddLog("accept 错误 原因:%s!", CommonSocket::GetLastErrorStr(CommonSocket::GetSocketLastError()).c_str()); break; } CommonSocket::SetSocketUnblock(hClientSocket); CConnection *pConnection = AssociateCompletePort(hClientSocket); if(pConnection != NULL) { CLog::GetInstancePtr()->AddLog("成功收到新连接,发送身份信息, 并提交数据请求!"); pConnection->SetConnectionOK(TRUE); SendIdentifyInfo(hClientSocket); #ifdef WIN32 if(!pConnection->DoReceive()) { pConnection->Close(FALSE); CConnectionMgr::GetInstancePtr()->DeleteConnection(pConnection); } #endif } else { CLog::GetInstancePtr()->AddLog("accept 错误 原因:%s!", CommonSocket::GetLastErrorStr(CommonSocket::GetSocketLastError()).c_str()); } } return TRUE; }
BOOL CNetManager::ConnectToOtherSvr( std::string strIpAddr, UINT16 sPort ) { SOCKET hSocket = CommonSocket::CreateSocket(AF_INET, SOCK_STREAM, 0); if(hSocket == INVALID_SOCKET) { ASSERT_FAIELD; CommonSocket::CloseSocket(hSocket); CLog::GetInstancePtr()->AddLog("创建套接字失败!!"); return FALSE; } CommonSocket::SetSocketBlock(hSocket); CommonSocket::SetSocketNoDelay(hSocket); if(!CommonSocket::ConnectSocket(hSocket, strIpAddr.c_str(), sPort)) { CommonSocket::CloseSocket(hSocket); return FALSE; } CConnection *pConnection = AssociateCompletePort(hSocket); if(pConnection == NULL) { ASSERT_FAIELD; CLog::GetInstancePtr()->AddLog("邦定套接字到完成端口失败!!"); return FALSE; } SendIdentifyInfo(hSocket); if(!pConnection->DoReceive()) { pConnection->Close(FALSE); CConnectionMgr::GetInstancePtr()->DeleteConnection(pConnection); } return TRUE; }
CConnection* CNetManager::AssociateCompletePort( SOCKET hSocket ) { CConnection *pConnection = CConnectionMgr::GetInstancePtr()->CreateConnection(); pConnection->SetSocket(hSocket); pConnection->SetDataHandler(m_pBufferHandler); struct epoll_event EpollEvent; EpollEvent.data.ptr= pConnection; EpollEvent.events = EPOLLIN|EPOLLOUT|EPOLLET; if(-1 == epoll_ctl(m_hCompletePort, EPOLL_CTL_ADD, hSocket,&EpollEvent)) { pConnection->Close(FALSE); CConnectionMgr::GetInstancePtr()->DeleteConnection(pConnection); return NULL; } return pConnection; }
BOOL CNetManager::WorkThread_SendData() { while(!m_bCloseSend) { SendDataNode _SendNode; if(!m_SendDataList.Pop(_SendNode)) { continue; } IDataBuffer *pDataBuffer = (IDataBuffer *)_SendNode.pPtr; if(pDataBuffer == NULL) { if(_SendNode.u64ConnID != 1) { CLog::GetInstancePtr()->AddLog("发送线程:空消息 ,准备退出发送线程!"); } continue; } SOCKET hSocket = INVALID_SOCKET; CConnection *pConnection = NULL; if(_SendNode.bIsConnID) { pConnection = CConnectionMgr::GetInstancePtr()->GetConnectionByConnID(_SendNode.u64ConnID); if(pConnection == NULL) { CLog::GetInstancePtr()->AddLog("发送线程:发送失败, 无效的连接ID!"); pDataBuffer->Release(); continue; } hSocket = pConnection->GetSocket(); } else { hSocket = _SendNode.u64ConnID; } if(hSocket == INVALID_SOCKET) { CLog::GetInstancePtr()->AddLog("发送线程:发送失败, 无效的套接字!"); pDataBuffer->Release(); continue; } INT32 nRet = send(hSocket, pDataBuffer->GetData(),pDataBuffer->GetDataLenth(), 0); if(nRet < 0) { int nErr = CommonSocket::GetSocketLastError(); CLog::GetInstancePtr()->AddLog("发送线程:发送失败, 原因:%s!", CommonSocket::GetLastErrorStr(nErr).c_str()); } else if(nRet < pDataBuffer->GetDataLenth()) { if(pConnection != NULL) { pConnection->Close(TRUE); CConnectionMgr::GetInstancePtr()->DeleteConnection(pConnection); } CLog::GetInstancePtr()->AddLog("发送线程:发送失败, 缓冲区满了!"); } pDataBuffer->Release(); } return TRUE; }
BOOL CNetManager::WorkThread_SendData() { while(!m_bCloseSend) { SendDataNode _SendNode; if(!m_SendDataList.Pop(_SendNode)) { continue; } IDataBuffer *pDataBuffer = (IDataBuffer *)_SendNode.pPtr; if(pDataBuffer == NULL) { if(_SendNode.u64ConnID != 0) { CLog::GetInstancePtr()->AddLog("发送线程:发送失败 ,一个空包!"); } continue; } SOCKET hSocket = INVALID_SOCKET; CConnection *pConnection = NULL; if(_SendNode.bIsConnID) { pConnection = CConnectionMgr::GetInstancePtr()->GetConnectionByConnID(_SendNode.u64ConnID); if(pConnection == NULL) { CLog::GetInstancePtr()->AddLog("发送线程:发送失败, 无效的连接ID!"); pDataBuffer->Release(); continue; } hSocket = pConnection->GetSocket(); } else { hSocket = (SOCKET)_SendNode.u64ConnID; } if(hSocket == INVALID_SOCKET) { CLog::GetInstancePtr()->AddLog("发送线程:发送失败, 无效的套接字!"); pDataBuffer->Release(); continue; } WSABUF DataBuf; DataBuf.len = pDataBuffer->GetDataLenth(); DataBuf.buf = pDataBuffer->GetData(); NetIoOperatorData *pOperatorData = (NetIoOperatorData *)pDataBuffer->GetBufferPos(pDataBuffer->GetDataLenth()+1); if(pOperatorData == NULL) { pDataBuffer->Release(); continue; } pOperatorData->Clear(); pOperatorData->dwCmdType = NET_CMD_SEND; pOperatorData->pDataBuffer = pDataBuffer; DWORD dwSendBytes; int nRet = WSASend(hSocket, &DataBuf, 1, &dwSendBytes, 0, (LPOVERLAPPED)pOperatorData, NULL); if(nRet == 0) { if(dwSendBytes == 0) { if(pConnection != NULL) { CLog::GetInstancePtr()->AddLog("发送线程:发送失败, 未能发送出数据,所以主动关闭连接!"); pConnection->Close(TRUE); CConnectionMgr::GetInstancePtr()->DeleteConnection(pConnection); pDataBuffer->Release(); } } } else if( nRet == -1 ) { UINT32 errCode = CommonSocket::GetSocketLastError(); if(errCode != ERROR_IO_PENDING) { if(pConnection != NULL) { pConnection->Close(TRUE); CConnectionMgr::GetInstancePtr()->DeleteConnection(pConnection); } pDataBuffer->Release(); CLog::GetInstancePtr()->AddLog("发送线程:发送失败, 连接关闭原因:%s!", CommonSocket::GetLastErrorStr(errCode).c_str()); } } } return TRUE; }
BOOL CNetManager::WorkThread_ProcessEvent() { if(m_hCompletePort == INVALID_HANDLE_VALUE) { CLog::GetInstancePtr()->AddLog("创建事件网络事件处理线程失败, 完成端口还未创建!"); ASSERT_FAIELD; return FALSE; } DWORD dwNumOfByte = 0; DWORD dwCompleteKey = 0; LPOVERLAPPED lpOverlapped = NULL; DWORD dwWaitTime = 1000; BOOL bRetValue = FALSE; while(!m_bCloseEvent) { bRetValue = GetQueuedCompletionStatus(m_hCompletePort, &dwNumOfByte, &dwCompleteKey, &lpOverlapped, dwWaitTime); if(!bRetValue) { if(lpOverlapped == NULL) { if(ERROR_ABANDONED_WAIT_0 == CommonSocket::GetSocketLastError()) { CLog::GetInstancePtr()->AddLog("完成端口被外部关闭!"); return FALSE; } if(CommonSocket::GetSocketLastError() == WAIT_TIMEOUT) { //CLog::GetInstancePtr()->AddLog("完成端口等待超时出错,严重!"); //return FALSE; continue; } } } NetIoOperatorData *pIoPeratorData = (NetIoOperatorData*)lpOverlapped; switch( pIoPeratorData->dwCmdType ) { case NET_CMD_RECV: { CConnection *pConnection = (CConnection *)dwCompleteKey; if(pConnection != NULL) { if(dwNumOfByte == 0) { //说明对方己经关闭 CLog::GetInstancePtr()->AddLog("完成端口收到数据为0, 对方己经关闭连接!"); pConnection->Close(TRUE); CConnectionMgr::GetInstancePtr()->DeleteConnection(pConnection); } else { if(!pConnection->HandleRecvEvent(dwNumOfByte)) { //收数据失败,基本就是连接己断开 pConnection->Close(TRUE); CConnectionMgr::GetInstancePtr()->DeleteConnection(pConnection); } } } } break; case NET_CMD_SEND: { pIoPeratorData->pDataBuffer->Release(); } break; case NET_CMD_CONNECT: { CConnection *pConnection = (CConnection *)dwCompleteKey; if(pConnection != NULL) { if(bRetValue) { CLog::GetInstancePtr()->AddLog("连接其它服务器成功!"); pConnection->SetConnectionOK(TRUE); if(SendIdentifyInfo(pConnection->GetSocket())) { if(!pConnection->DoReceive()) { pConnection->Close(FALSE); CConnectionMgr::GetInstancePtr()->DeleteConnection(pConnection); } } } else { CLog::GetInstancePtr()->AddLog("连接其它服务器失败!"); pConnection->Close(FALSE); pConnection->SetConnectionOK(FALSE); CConnectionMgr::GetInstancePtr()->DeleteConnection(pConnection); } } } break; default: { } break; } } return TRUE; }