Skip to content

Commit

Permalink
Test GUI fiber
Browse files Browse the repository at this point in the history
  • Loading branch information
郑树新 committed Jan 21, 2024
1 parent 454a78d commit 15a4ce7
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 10 deletions.
12 changes: 9 additions & 3 deletions lib_fiber/samples-gui/HttpGet/HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ CHttpClient::CHttpClient(acl::fiber_tbox<CHttpMsg>& box, const CString& url)

CHttpClient::~CHttpClient() {}

void CHttpClient::run(BOOL usePost)
void CHttpClient::run(BOOL usePost, const char *filePath)
{
acl::http_url hu;
if (!hu.parse(m_url.GetString())) {
Expand All @@ -44,15 +44,21 @@ void CHttpClient::run(BOOL usePost)
header.set_url(url.GetString()).accept_gzip(true).set_host(domain);

acl::string head;
acl::string body;
if (usePost) {
header.set_method(acl::HTTP_METHOD_POST);
header.set_content_length(0);

if (filePath != NULL) {
acl::ifstream::load(filePath, body);
}

header.set_content_length(body.length());
}

header.build_request(head);
SetRequestHead(head.c_str());

if (!request.request(NULL, 0)) {
if (!request.request(body.empty() ? NULL : body.c_str(), body.size())) {
SetError("Send request to %s error: %s",
addr.GetString(), acl::last_serror());
SetEnd();
Expand Down
2 changes: 1 addition & 1 deletion lib_fiber/samples-gui/HttpGet/HttpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CHttpClient
CHttpClient(acl::fiber_tbox<CHttpMsg>& mBox, const CString& url);
~CHttpClient();

void run(BOOL usePost = TRUE);
void run(BOOL usePost = TRUE, const char *filePath = NULL);

private:
CHttpGetDlg* m_hWin;
Expand Down
Binary file modified lib_fiber/samples-gui/HttpGet/HttpGet.rc
Binary file not shown.
2 changes: 1 addition & 1 deletion lib_fiber/samples-gui/HttpGet/HttpGet.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32;_WINDOWS;_DEBUG;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<AdditionalIncludeDirectories>..\..\..\lib_acl_cpp\include;..\..\..\lib_acl\include;..\..\..\lib_fiber\c\include;..\..\..\lib_fiber\cpp\include</AdditionalIncludeDirectories>
<AdditionalOptions>/Zc:__cplusplus %(AdditionalOptions)</AdditionalOptions>
Expand Down
55 changes: 53 additions & 2 deletions lib_fiber/samples-gui/HttpGet/HttpGetDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,24 @@ END_MESSAGE_MAP()

CHttpGetDlg::CHttpGetDlg(CWnd* pParent /*=nullptr*/)
: CDialogEx(IDD_HTTPGET_DIALOG, pParent)
, m_dosFp(NULL)
, m_url("http://www.baidu.com/")
, m_length(-1)
, m_lastPos(0)
, m_usePost(FALSE)
, m_downType(0)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
acl::log::stdout_open(true);
}

CHttpGetDlg::~CHttpGetDlg()
{
if (m_dosFp) {
fclose(m_dosFp);
m_dosFp = NULL;
FreeConsole();
}
}

void CHttpGetDlg::DoDataExchange(CDataExchange* pDX)
Expand All @@ -81,6 +92,8 @@ BEGIN_MESSAGE_MAP(CHttpGetDlg, CDialogEx)
ON_BN_CLICKED(IDC_RADIO_FIBER, &CHttpGetDlg::OnBnClickedRadio)
ON_BN_CLICKED(IDC_RADIO_THREAD, &CHttpGetDlg::OnBnClickedRadio)
ON_BN_CLICKED(IDC_CHECK_POST, &CHttpGetDlg::OnBnClickedCheckPost)
ON_BN_CLICKED(IDC_BROWSER, &CHttpGetDlg::OnBnClickedBrowser)
ON_BN_CLICKED(IDC_DOS_OPEN, &CHttpGetDlg::OnBnClickedDosOpen)
END_MESSAGE_MAP()


Expand Down Expand Up @@ -198,22 +211,25 @@ void CHttpGetDlg::OnBnClickedStartGet()
return;
}

CString filePath;
GetDlgItem(IDC_FILE)->GetWindowText(filePath);

m_progress.SetPos(0);
m_lastPos = 0;

if (m_downType == HTTP_DOWNLOAD_FIBER) {
go[=] {
CHttpClient client(*this, *url);
delete url;
client.run(m_usePost);
client.run(m_usePost, filePath.IsEmpty() ? NULL : filePath.GetString());
};
} else if (m_downType == HTTP_DOWNLOAD_THREAD) {
go[=] {
acl::fiber_tbox<CHttpMsg> box;
std::thread thread([&] {
CHttpClient client(box, *url);
delete url;
client.run();
client.run(m_usePost, filePath.IsEmpty() ? NULL : filePath.GetString());
});

thread.detach();
Expand Down Expand Up @@ -319,3 +335,38 @@ void CHttpGetDlg::OnBnClickedCheckPost()
// TODO: 在此添加控件通知处理程序代码
UpdateData(TRUE);
}


void CHttpGetDlg::OnBnClickedBrowser()
{
// TODO: 在此添加控件通知处理程序代码
CFileDialog file(TRUE,"文件","",OFN_HIDEREADONLY,"FILE(*.*)|*.*||",NULL);
if(file.DoModal()==IDOK)
{
CString pathname;

pathname=file.GetPathName();
GetDlgItem(IDC_FILE)->SetWindowText(pathname);
UpdateData(TRUE);
}
}


void CHttpGetDlg::OnBnClickedDosOpen()
{
// TODO: 在此添加控件通知处理程序代码
if (m_dosFp == NULL) {
//GetDlgItem(IDC_OPEN_DOS)->EnableWindow(FALSE);
UpdateData();
AllocConsole();
m_dosFp = freopen("CONOUT$","w+t",stdout);
CString info(_T("Close DOS"));
GetDlgItem(IDC_DOS_OPEN)->SetWindowText(info);
} else {
fclose(m_dosFp);
m_dosFp = NULL;
FreeConsole();
CString info(_T("Open DOS"));
GetDlgItem(IDC_DOS_OPEN)->SetWindowText(info);
}
}
7 changes: 5 additions & 2 deletions lib_fiber/samples-gui/HttpGet/HttpGetDlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ class CHttpGetDlg : public CDialogEx
// 构造
public:
CHttpGetDlg(CWnd* pParent = nullptr); // 标准构造函数
~CHttpGetDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_HTTPGET_DIALOG };
#endif

protected:
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持


// 实现
protected:
HICON m_hIcon;
Expand All @@ -46,8 +46,11 @@ class CHttpGetDlg : public CDialogEx
afx_msg void OnBnClickedReset();
afx_msg void OnBnClickedRadio();
afx_msg void OnBnClickedCheckPost();
afx_msg void OnBnClickedBrowser();
afx_msg void OnBnClickedDosOpen();

private:
FILE* m_dosFp;
CProgressCtrl m_progress;
CString m_url;
int m_downType;
Expand Down
6 changes: 5 additions & 1 deletion lib_fiber/samples-gui/HttpGet/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,18 @@
#define IDC_RADIO2 1010
#define IDC_RADIO_THREAD 1010
#define IDC_CHECK_POST 1011
#define IDC_FILE 1012
#define IDC_BROWSER 1013
#define IDC_BUTTON3 1014
#define IDC_DOS_OPEN 1014

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 130
#define _APS_NEXT_COMMAND_VALUE 32771
#define _APS_NEXT_CONTROL_VALUE 1012
#define _APS_NEXT_CONTROL_VALUE 1015
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif

0 comments on commit 15a4ce7

Please sign in to comment.