Пример #1
0
void ExecCmd(ClientConn *client_conn) {
    g_cmd_lock.Lock();
    if (g_cmd_string_vec.empty()) {
        g_cmd_lock.Unlock();
        return;
    }

    if (g_cmd_string_vec[0] == "login") {
        if (g_cmd_string_vec.size() == 3) {
            SendLoginRequest(client_conn, g_cmd_string_vec[1], g_cmd_string_vec[2]);
        } else {
            PrintHelp();
        }
    } else if (g_cmd_string_vec[0] == "logout") {
        SendLogoutRequest(client_conn);
    } else if (g_cmd_string_vec[0] == "send") {
        if (g_cmd_string_vec.size() == 3) {
            SendMessageRequest(client_conn, g_cmd_string_vec[1], g_cmd_string_vec[2]);
        } else {
            PrintHelp();
        }
    } else {
        PrintHelp();
    }
    g_cmd_string_vec.clear();
    g_cmd_lock.Unlock();
}
Пример #2
0
void SplitCmd(char *buf) {
    g_cmd_lock.Lock();
    if (!buf) {
        g_cmd_lock.Unlock();
        return;
    }
    int buf_len = strlen(buf);
    if (buf_len <= 0) {
        g_cmd_lock.Unlock();
        return;
    }
    g_cmd_string_vec.clear();
    string cmd;
    for (int i = 0; i < buf_len; ++i) {
        if (buf[i] != ' ' && buf[i] != '\t') {
            cmd.push_back(buf[i]);
        } else {
            if (!cmd.empty()) {
                g_cmd_string_vec.push_back(cmd);
                cmd.clear();
            }
        }
    }
    // push the last cmd
    if (!cmd.empty()) {
        g_cmd_string_vec.push_back(cmd);
    }
    g_cmd_lock.Unlock();
}
Пример #3
0
HttpManager* HttpManager::getInstance() {
    if (httpManager == NULL) {
        cLock.Lock();
        if (httpManager == NULL) {
            httpManager = new HttpManager();
        }
        cLock.Unlock();
    }
    return httpManager;
}
Пример #4
0
unsigned __stdcall ThreadDownLoad(void* pParam)
{
	struct threadInfo *pthreadInfo = (struct threadInfo *)pParam;
	if(pthreadInfo->bState)
		return 0;
	//BOOL bError = FALSE;

	// 计算临时文件中已成功下载部分大小.
	DWORD occupy_size = 0;

	DWORD dwFileSize = myfile.GetDownloadedFileSizeByName(pthreadInfo->csDesFileName, &occupy_size);
    DWORD dwSectSize = pthreadInfo->dwEnd - pthreadInfo->dwStart;

	// 此段已下载完毕.
	if( dwFileSize >= dwSectSize )
	{
		//InterlockedIncrement(&g_finishedThreadNumber);
		pthreadInfo->bState = true;
		return 0;
	}

	BYTE * pStore = NULL;
	while (!pStore)
	{
		pStore = (BYTE*)::VirtualAlloc(NULL, PAGE_SIZE, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); 
	}

    HANDLE hFile = myfile.GetFilePointer(pthreadInfo->csDesFileName, occupy_size);
	if(hFile == INVALID_HANDLE_VALUE) 
	{
		//SetErrorDuringDownload();
		::VirtualFree(pStore, 0, MEM_RELEASE);
		return 0;
	}

	SOCKET hSocket;
	if(!g_csProxy.IsEmpty())
	{	
		hSocket = CHttpGet::ConnectHttpProxy(g_csProxy,g_ProxyPort);
	}
	else
	{
		hSocket = CHttpGet::ConnectHttpNonProxy(pthreadInfo->csHost,80);
	}

	if(hSocket == INVALID_SOCKET)
	{
		SetErrorDuringDownload();
		CloseHandle(hFile);
		::VirtualFree(pStore, 0, MEM_RELEASE);
		return 0;
	}

    // 设置下载范围.
	DWORD dwFileLength = 0;
	if (!CHttpGet::SendHttpHeader(hSocket,pthreadInfo->csHost,pthreadInfo->csPath,
		pthreadInfo->csFileName,dwFileLength,pthreadInfo->dwStart+dwFileSize))
	{
		g_bErrorDuringDownload = true;
		closesocket(hSocket);
		CloseHandle(hFile);
		::VirtualFree(pStore, 0, MEM_RELEASE);
		return 0;
	}

	int nLen; 
	DWORD nSumLen=0; 
	char szBuffer[1024];
	BYTE* p = pStore;

	while(!g_bStopDownload && !g_bErrorDuringDownload)
	{
		if(nSumLen>=dwSectSize-dwFileSize) break;
		nLen = recv(hSocket,szBuffer,sizeof(szBuffer),0);
		
		if (nLen == SOCKET_ERROR)
		{
			int err_code = WSAGetLastError();
			if (WSAEWOULDBLOCK != err_code)
			{
				SetErrorDuringDownload();
				break;
			}
			else
			{
				Sleep(300);
				continue;
			}
		}
		else if(nLen==0) break;

		nSumLen +=nLen;
		if (nSumLen>=dwSectSize-dwFileSize)
		{
			g_lock.Lock();
			g_downloaded += nLen - (nSumLen - (dwSectSize-dwFileSize));
			g_lock.Unlock();
		}
		else
		{
			g_lock.Lock();
			g_downloaded += nLen;
			g_lock.Unlock();
		}

		if ( (p + nLen) >= (pStore + PAGE_SIZE) )
		{
			memcpy(p, szBuffer, pStore + PAGE_SIZE - p);
			// CRC value
			//unsigned long crc_value = crc32.Calculate(pStore, WRITE_SIZE);
			//memcpy(pStore + WRITE_SIZE, &crc_value, sizeof(unsigned long));
			DWORD dwWritten;
			if (0 == WriteFile(hFile, pStore, PAGE_SIZE, &dwWritten, NULL))
			{
				SetErrorDuringDownload();
				break;
			}
			memcpy(pStore, szBuffer + (pStore + PAGE_SIZE - p), (p + nLen) - (pStore + PAGE_SIZE));
			p = (p + nLen) - PAGE_SIZE; 
		}
		else
		{
			memcpy(p, szBuffer, nLen);
			p += nLen;
		}
	}

	if (!g_bStopDownload && !g_bErrorDuringDownload && p > pStore)
	{
		// CRC value
		//unsigned long crc_value = crc32.Calculate(pStore, p - pStore);
		//memcpy(p, &crc_value, sizeof(unsigned long));
		DWORD dwWritten;
		WriteFile(hFile, pStore, p - pStore, &dwWritten, NULL);

	}

	closesocket(hSocket); // 关闭套接字.
	CloseHandle(hFile);
	::VirtualFree(pStore, 0, MEM_RELEASE);
	//InterlockedIncrement(&g_finishedThreadNumber);
	pthreadInfo->bState = true;
	return 0;
}