void CEMSocket::InitProxySupport() { // Destroy all proxy layers (done inside Close()) Close(); // Proxy initialization const ProxySettings &settings = g_App.m_pPrefs->GetProxySettings(); m_bProxyConnectFailed = false; if (settings.m_bUseProxy && settings.m_nType != PROXYTYPE_NOPROXY) { m_pProxyLayer = new CAsyncProxySocketLayer; switch (settings.m_nType) { case PROXYTYPE_SOCKS4: case PROXYTYPE_SOCKS4A: m_pProxyLayer->SetProxy(settings.m_nType, settings.m_strName, settings.m_uPort); break; case PROXYTYPE_SOCKS5: case PROXYTYPE_HTTP11: if (settings.m_bEnablePassword) m_pProxyLayer->SetProxy(settings.m_nType, settings.m_strName, settings.m_uPort, settings.m_strUser, settings.m_strPassword); else m_pProxyLayer->SetProxy(settings.m_nType, settings.m_strName, settings.m_uPort); break; default: ASSERT(0); } AddLayer(m_pProxyLayer); } // Connection Initialization Create(0, SOCK_STREAM, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE, g_App.m_pPrefs->GetBindAddrA()); AsyncSelect(FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE); }
void CEMSocket::InitProxySupport() { m_bProxyConnectFailed = false; // ProxyInitialisation const ProxySettings& settings = thePrefs.GetProxySettings(); if (settings.UseProxy && settings.type != PROXYTYPE_NOPROXY) { Close(); m_pProxyLayer = new CAsyncProxySocketLayer; switch (settings.type) { case PROXYTYPE_SOCKS4: case PROXYTYPE_SOCKS4A: m_pProxyLayer->SetProxy(settings.type, settings.name, settings.port); break; case PROXYTYPE_SOCKS5: case PROXYTYPE_HTTP10: case PROXYTYPE_HTTP11: if (settings.EnablePassword) m_pProxyLayer->SetProxy(settings.type, settings.name, settings.port, settings.user, settings.password); else m_pProxyLayer->SetProxy(settings.type, settings.name, settings.port); break; default: ASSERT(0); } AddLayer(m_pProxyLayer); // Connection Initialisation Create(0, SOCK_STREAM, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE, thePrefs.GetBindAddrA()); AsyncSelect(FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE); } }
CEMSocket::~CEMSocket() { EMTrace("CEMSocket::~CEMSocket() on %d",(SOCKET)this); ClearQueues(); RemoveAllLayers(); AsyncSelect(0); }
CChatControl::~CChatControl() { // Save chat prefs m_pPrefs->SaveServers(m_pDoc->m_RunPath + "Data\\ChatServers.ini"); m_pPrefs->SaveChatAliases(m_pDoc->m_RunPath + "Data\\ChatAliases.ini"); m_pPrefs->SaveChatScripts(m_pDoc->m_RunPath + "Data\\ChatScripts.ini"); m_pPrefs->SaveChatHotlist(m_pDoc->m_RunPath + "Data\\ChatHotlist.ini"); delete m_pPrefs; m_pPrefs = NULL; if(m_hSocket != INVALID_SOCKET) AsyncSelect(0); TRACE0("*** CChatControl Deconstructing\n"); for(int i = 0; i < m_ServerList.size(); i++) { delete m_ServerList[i]; m_ServerList[i] = NULL; } while(m_IdentdList.size()) { delete m_IdentdList.back(); m_IdentdList.pop_back(); } m_ServerList.clear(); }
int WAsyncSocket::Close() { AsyncSelect(0); // turn off all async notifications m_wnd.DestroyWindow(); return WSocket::Close(); }
BOOL CAsyncSocket::Attach(SOCKET hSocket, long lEvent) { ASSERT(hSocket != INVALID_SOCKET); m_hSocket = hSocket; CAsyncSocket::AttachHandle(hSocket, this); return AsyncSelect(lEvent); }
BOOL CAsynSocket::Socket(int nSocketType, long lEvent, int nProtocolType, int nAddressFormat) { ASSERT(m_hSocket == INVALID_SOCKET); m_hSocket = socket(nAddressFormat,nSocketType,nProtocolType); if (m_hSocket != INVALID_SOCKET) return AsyncSelect(lEvent); return FALSE; }
SOCKET CAsyncSocket::Detach() { SOCKET hSocket = m_hSocket; if (AsyncSelect(0)) { CAsyncSocket::KillSocket(hSocket, this); m_hSocket = INVALID_SOCKET; return hSocket; } return INVALID_SOCKET; }
void CSocket::Close() { if (m_hSocket != INVALID_SOCKET) { CancelBlockingCall(); VERIFY(AsyncSelect(0)); CAsyncSocket::Close(); m_hSocket = INVALID_SOCKET; } }
void WAsyncSocket::Attach(SOCKET hSocket) { WSocket::Attach(hSocket); // Create message window so that it will accept messages posted to it m_wnd.CreateEx(0,AfxRegisterWndClass(0),NULL,0,0,0,0,0,0,NULL,NULL); m_wnd.InitSocket(this); m_hwnd=m_wnd.GetSafeHwnd(); // Do the asyncselect crap AsyncSelect(FD_CONNECT|FD_READ|FD_WRITE|FD_CLOSE); // was FD_ALL_EVENTS }
int WAsyncSocket2::Create(unsigned int port) { int ret=WSocket2::Create(port); if((ret!=INVALID_SOCKET)&&(ret!=SOCKET_ERROR)) { // Do the asyncselect crap ret=AsyncSelect(FD_ALL_EVENTS); } return ret; }
// Called when data is received. void CNDKClientSocket::OnReceive(int nErrorCode) { CString str; UINT ui; //remove read notifications VERIFY(AsyncSelect(/*FD_READ | */FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE)); CSocket::OnReceive(nErrorCode); ASSERT(m_pClient != NULL); if (m_pClient != NULL) m_pClient->ProcessPendingRead(nErrorCode); //reset notifications to default if(0 != GetSockName(str, ui)) { VERIFY(AsyncSelect()); } }
void ClientSocket::OnReceive(int nErrorCode) { // TODO: 在此添加专用代码和/或调用基类 char *pData = NULL; //pData = new char[10240]; //memset(pData, 0, sizeof(char)*10240); External_Mag_Head msgHead; int msgLen = sizeof(External_Mag_Head); UINT leng = 0; CString strDoc; pData = new char[msgLen]; memset(pData, 0, sizeof(char)*msgLen); SetTimeOut(3000); leng = Receive(pData, msgLen, 0); KillTimeOut(); AsyncSelect(FD_READ); memcpy(&msgHead, pData, msgLen); free(pData); pData = new char[msgHead.load_len + 1]; memset(pData, 0, sizeof(char)*msgHead.load_len + 1); SetTimeOut(3000); leng = Receive(pData, msgHead.load_len, 0); KillTimeOut(); AsyncSelect(FD_READ); //char *p_msgBody = (char*)malloc(sizeof(char)*msgHead.load_len); //memset(p_msgBody, 0, sizeof(char)*msgHead.load_len); //memcpy(p_msgBody, pData + msgLen, sizeof(char)*msgHead.load_len); strDoc = StringConvertor::Utf8ToWide(pData); m_xml.SetDoc(strDoc); ParseXml(m_xml); /*DWORD *pParams = new DWORD[2]; pParams[0] = (DWORD)this; pParams[1] = (DWORD)pData; m_hThread = ::CreateThread(NULL, NULL, ParseXml, (LPVOID*)(pParams), NULL, NULL);*/ delete pData; CSocket::OnReceive(nErrorCode); }
void CAsynSocket::_OnClose(int nErrorCode) { AsyncSelect(0); if(m_pSocketWnd) { m_pSocketWnd->DestroyWindow(); delete m_pSocketWnd; m_pSocketWnd = NULL; } OnClose(nErrorCode); }
void CClientSocket::OnSend(int nErrorCode) { // TODO: 在此添加专用代码和/或调用基类 my_nLength = WideCharToMultiByte(CP_ACP, 0, my_SendData, my_SendData.GetLength(), NULL, 0, NULL, NULL); WideCharToMultiByte(CP_ACP, 0, my_SendData, my_SendData.GetLength() + 1, my_szBuffer, my_nLength + 1, NULL, NULL); //转换为字节为单位 my_szBuffer[my_nLength + 1] = '/0'; Send(my_szBuffer, sizeof(my_szBuffer), 0); my_nLength = 0; memset(my_szBuffer, 0, sizeof(my_szBuffer)); AsyncSelect(FD_READ); CAsyncSocket::OnSend(nErrorCode); }
int MightyTCPAsyncSelectClient::afreshConnect() { if (m_ClientSock==INVALID_SOCKET) { m_ClientSock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); m_lEvent=FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE; AsyncSelect(m_lEvent); Connect(m_strAddress.c_str(),m_ConnectPort); return 0; } else { return 1; } }
int WAsyncSocket::Create(unsigned short int port,unsigned int ip) { // Create message window so that it will accept messages posted to it m_wnd.CreateEx(0,AfxRegisterWndClass(0),NULL,0,0,0,0,0,0,NULL,NULL); m_wnd.InitSocket(this); m_hwnd=m_wnd.GetSafeHwnd(); int ret=WSocket::Create(port,ip); if((ret!=INVALID_SOCKET)&&(ret!=SOCKET_ERROR)) { // Do the asyncselect crap ret=AsyncSelect(FD_ALL_EVENTS); } return ret; }
BOOL CAsynSocket::Attach(SOCKET hSocket, long lEvent) { ASSERT(hSocket != INVALID_SOCKET); m_hSocket = hSocket; m_pSocketWnd = new CSocketWnd(this); m_pSocketWnd->m_hWnd = NULL; if (!m_pSocketWnd->CreateEx(0, AfxRegisterWndClass(0), _T(""), WS_OVERLAPPED, 0, 0, 0, 0, NULL, NULL)) { TRACE0("Warning: unable to create socket notify window!\n"); return FALSE; } return AsyncSelect(lEvent); }
void CClientSocket::OnConnect(int nErrorCode) { // TODO: 在此添加专用代码和/或调用基类 if (nErrorCode == 0) { my_bConnected = TRUE; C客户端App *pApp = (C客户端App*)AfxGetApp(); C客户端Dlg *pDlg = (C客户端Dlg*)pApp->m_pMainWnd; CString temp = L"连接到:"; temp += pDlg->my_ServerAddr; pDlg->m_MsgR.InsertString(0, temp); pDlg->GetDlgItem(IDC_SEND)->EnableWindow(true); AsyncSelect(FD_READ); } CAsyncSocket::OnConnect(nErrorCode); }
CEMSocket::~CEMSocket(){ EMTrace("CEMSocket::~CEMSocket() on %d",(SOCKET)this); // need to be locked here to know that the other methods // won't be in the middle of things sendLocker.Lock(); byConnected = ES_DISCONNECTED; sendLocker.Unlock(); // now that we know no other method will keep adding to the queue // we can remove ourself from the queue theApp.uploadBandwidthThrottler->RemoveFromAllQueues(this); ClearQueues(); RemoveAllLayers(); // deadlake PROXYSUPPORT AsyncSelect(0); }
void CGnuUpdateSock::Close() { if(m_SocketData.hSocket != INVALID_SOCKET) { AsyncSelect(0); ShutDown(2); CAsyncSocketEx::Close(); } m_Status = TRANSFER_CLOSED; if(m_BytesCompleted) if(m_BytesCompleted == m_FileSize) m_Status = TRANSFER_COMPLETED; m_File.Abort(); }
WAsyncSocket::~WAsyncSocket() { AsyncSelect(0); // turn off all async notifications m_wnd.DestroyWindow(); }
// pach2: // written this overriden Receive to handle transparently FIN notifications coming from calls to recv() // This was maybe(??) the cause of a lot of socket error, notably after a brutal close from peer // also added trace so that we can debug after the fact ... int CEMSocket::Receive(void* lpBuf, int nBufLen, int nFlags) { // EMTrace("CEMSocket::Receive on %d, maxSize=%d",(SOCKET)this,nBufLen); int recvRetCode = CEncryptedStreamSocket::Receive(lpBuf,nBufLen,nFlags); // deadlake PROXYSUPPORT - changed to AsyncSocketEx switch (recvRetCode) { case 0: if (GetRealReceivedBytes() > 0) // we received data but it was for the underlying encryption layer - all fine return 0; //EMTrace("CEMSocket::##Received FIN on %d, maxSize=%d",(SOCKET)this,nBufLen); // FIN received on socket // Connection is being closed by peer //ASSERT (false); if ( 0 == AsyncSelect(FD_CLOSE|FD_WRITE) ) { // no more READ notifications ... //int waserr = GetLastError(); // oups, AsyncSelect failed !!! ASSERT(false); } return 0; case SOCKET_ERROR: switch (GetLastError()) { case WSANOTINITIALISED: ASSERT(false); EMTrace("CEMSocket::OnReceive:A successful AfxSocketInit must occur before using this API."); break; case WSAENETDOWN: ASSERT(true); EMTrace("CEMSocket::OnReceive:The socket %d received a net down error",(SOCKET)this); break; case WSAENOTCONN: // The socket is not connected. EMTrace("CEMSocket::OnReceive:The socket %d is not connected",(SOCKET)this); break; case WSAEINPROGRESS: // A blocking Windows Sockets operation is in progress. EMTrace("CEMSocket::OnReceive:The socket %d is blocked",(SOCKET)this); break; case WSAEWOULDBLOCK: // The socket is marked as nonblocking and the Receive operation would block. EMTrace("CEMSocket::OnReceive:The socket %d would block",(SOCKET)this); break; case WSAENOTSOCK: // The descriptor is not a socket. EMTrace("CEMSocket::OnReceive:The descriptor %d is not a socket (may have been closed or never created)",(SOCKET)this); break; case WSAEOPNOTSUPP: // MSG_OOB was specified, but the socket is not of type SOCK_STREAM. break; case WSAESHUTDOWN: // The socket has been shut down; it is not possible to call Receive on a socket after ShutDown has been invoked with nHow set to 0 or 2. EMTrace("CEMSocket::OnReceive:The socket %d has been shut down",(SOCKET)this); break; case WSAEMSGSIZE: // The datagram was too large to fit into the specified buffer and was truncated. EMTrace("CEMSocket::OnReceive:The datagram was too large to fit and was truncated (socket %d)",(SOCKET)this); break; case WSAEINVAL: // The socket has not been bound with Bind. EMTrace("CEMSocket::OnReceive:The socket %d has not been bound",(SOCKET)this); break; case WSAECONNABORTED: // The virtual circuit was aborted due to timeout or other failure. EMTrace("CEMSocket::OnReceive:The socket %d has not been bound",(SOCKET)this); break; case WSAECONNRESET: // The virtual circuit was reset by the remote side. EMTrace("CEMSocket::OnReceive:The socket %d has not been bound",(SOCKET)this); break; default: EMTrace("CEMSocket::OnReceive:Unexpected socket error %x on socket %d",GetLastError(),(SOCKET)this); break; } break; default: // EMTrace("CEMSocket::OnReceive on %d, receivedSize=%d",(SOCKET)this,recvRetCode); return recvRetCode; } return SOCKET_ERROR; }
CGnuLocal::~CGnuLocal() { if(m_hSocket != INVALID_SOCKET) AsyncSelect(0); }
int WAsyncSocket2::Close() { AsyncSelect(0); // turn off all async notifications return WSocket2::Close(); }
SOCKET WAsyncSocket::Detach() { AsyncSelect(0); // turn off all async notifications m_wnd.DestroyWindow(); return WSocket::Detach(); }