Skip to content

Commit

Permalink
Add one windows GUI fiber demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
郑树新 committed Sep 28, 2024
1 parent 037f55f commit 4963936
Show file tree
Hide file tree
Showing 19 changed files with 1,093 additions and 0 deletions.
33 changes: 33 additions & 0 deletions acl_cpp_vc2019.sln
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "redis_builder", "app\redis_
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "https_request", "lib_acl_cpp\samples\ssl\https_request\https_request_vc2019.vcxproj", "{4D42F462-A2FE-4B4A-A680-93668FA128A6}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SimpleWin", "lib_fiber\samples-gui\SimpleWin\SimpleWin.vcxproj", "{4D524F5F-31D7-42AB-915B-312CB67B0C18}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Mixed Platforms = Debug|Mixed Platforms
Expand Down Expand Up @@ -925,6 +927,36 @@ Global
{4D42F462-A2FE-4B4A-A680-93668FA128A6}.Template|Win32.Build.0 = Debug|Win32
{4D42F462-A2FE-4B4A-A680-93668FA128A6}.Template|x64.ActiveCfg = Debug|x64
{4D42F462-A2FE-4B4A-A680-93668FA128A6}.Template|x64.Build.0 = Debug|x64
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Debug|Mixed Platforms.ActiveCfg = Debug|Win32
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Debug|Mixed Platforms.Build.0 = Debug|Win32
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Debug|Win32.ActiveCfg = Debug|Win32
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Debug|Win32.Build.0 = Debug|Win32
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Debug|x64.ActiveCfg = Debug|x64
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Debug|x64.Build.0 = Debug|x64
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.DebugDll|Mixed Platforms.ActiveCfg = DebugDll|Win32
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.DebugDll|Mixed Platforms.Build.0 = DebugDll|Win32
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.DebugDll|Win32.ActiveCfg = DebugDll|Win32
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.DebugDll|Win32.Build.0 = DebugDll|Win32
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.DebugDll|x64.ActiveCfg = DebugDll|x64
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.DebugDll|x64.Build.0 = DebugDll|x64
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Release|Mixed Platforms.ActiveCfg = Release|Win32
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Release|Mixed Platforms.Build.0 = Release|Win32
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Release|Win32.ActiveCfg = Release|Win32
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Release|Win32.Build.0 = Release|Win32
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Release|x64.ActiveCfg = Release|x64
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Release|x64.Build.0 = Release|x64
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Releasedll|Mixed Platforms.ActiveCfg = ReleaseDll|Win32
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Releasedll|Mixed Platforms.Build.0 = ReleaseDll|Win32
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Releasedll|Win32.ActiveCfg = ReleaseDll|Win32
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Releasedll|Win32.Build.0 = ReleaseDll|Win32
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Releasedll|x64.ActiveCfg = ReleaseDll|x64
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Releasedll|x64.Build.0 = ReleaseDll|x64
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Template|Mixed Platforms.ActiveCfg = Debug|Win32
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Template|Mixed Platforms.Build.0 = Debug|Win32
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Template|Win32.ActiveCfg = Debug|Win32
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Template|Win32.Build.0 = Debug|Win32
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Template|x64.ActiveCfg = Debug|x64
{4D524F5F-31D7-42AB-915B-312CB67B0C18}.Template|x64.Build.0 = Debug|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -964,6 +996,7 @@ Global
{45276293-D169-4D73-8930-F255C09DA976} = {30B69273-9D47-4AD6-B1C5-CF362F9C6D80}
{D90DCF51-E219-4BD8-A032-076335675F58} = {30B69273-9D47-4AD6-B1C5-CF362F9C6D80}
{4D42F462-A2FE-4B4A-A680-93668FA128A6} = {AB838366-C278-426E-ADDF-BE3A32BADD3E}
{4D524F5F-31D7-42AB-915B-312CB67B0C18} = {1CB30F2E-2587-43E7-A3CF-787783FB6924}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {40FA3A5B-123F-4EC0-9EEB-8A9C17BF573B}
Expand Down
29 changes: 29 additions & 0 deletions lib_fiber/samples-gui/SimpleWin/FiberClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "pch.h"
#include "FiberClient.h"

CFiberClient::CFiberClient(SOCKET s)
: m_sock(s)
{
}

CFiberClient::~CFiberClient(void)
{
closesocket(m_sock);
}

void CFiberClient::run(void)
{
char buf[8192];
while (true) {
int ret = recv(m_sock, buf, sizeof(buf), 0);
if (ret <= 0) {
break;
}

if (send(m_sock, buf, ret, 0) == -1) {
break;
}
}

delete this;
}
18 changes: 18 additions & 0 deletions lib_fiber/samples-gui/SimpleWin/FiberClient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <WinSock2.h>

class CFiberClient : public acl::fiber
{
public:
CFiberClient(SOCKET sock);

private:
~CFiberClient(void);

protected:
void run(void);

private:
SOCKET m_sock;
};
57 changes: 57 additions & 0 deletions lib_fiber/samples-gui/SimpleWin/FiberConnect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "pch.h"
#include "FiberConnect.h"

CFiberConnect::CFiberConnect(const char* ip, int port, int count)
: ip_(ip)
, port_(port)
, count_(count)
{
}

CFiberConnect::~CFiberConnect(void) {}

bool CFiberConnect::Start(void)
{
struct sockaddr_in in;
in.sin_family = AF_INET;
in.sin_port = htons(port_);
if (inet_pton(AF_INET, ip_.c_str(), &in.sin_addr) == -1) {
printf("invalid ip = % s\r\n", ip_.c_str());
return false;
}

SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET) {
return false;
}

if (connect(sock, (const sockaddr*)&in, sizeof(in)) == -1) {
closesocket(sock);
printf("connect %s:%d error\r\n", ip_.c_str(), port_);
return false;
}

printf(">>>connect %s:%d ok\r\n", ip_.c_str(), port_);

const char data[] = "hello world!\r\n";
char buf[256];
int i;
for (i = 0; i < count_; i++) {
int ret = send(sock, data, sizeof(data) - 1, 0);
if (ret == -1) {
break;
}
ret = recv(sock, buf, sizeof(buf), 0);
if (ret <= 0) {
break;
}
if (i % 1000 == 0) {
printf(">>>current io=%d, thread=%d, fiber=%d\r\n",
i, GetCurrentThreadId(), acl::fiber::self());
}
}

closesocket(sock);
printf(">>>All over count=%d, %d\r\n", count_, i);
return true;
}
16 changes: 16 additions & 0 deletions lib_fiber/samples-gui/SimpleWin/FiberConnect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once

class CFiberConnect
{
public:
CFiberConnect(const char* ip, int port, int count);
~CFiberConnect(void);

bool Start(void);

private:
std::string ip_;
int port_;
int count_;
};

61 changes: 61 additions & 0 deletions lib_fiber/samples-gui/SimpleWin/FiberServer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "pch.h"
#include "FiberClient.h"
#include "FiberServer.h"

CFiberServer::CFiberServer(bool autoDestroy /* = true */)
: m_sock(INVALID_SOCKET)
, m_autoDestroy(autoDestroy)
{
}

CFiberServer::~CFiberServer(void)
{
if (m_sock != INVALID_SOCKET) {
closesocket(m_sock);
}
}

bool CFiberServer::BindAndListen(int port, const std::string& addr)
{
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET) {
return false;
}

struct sockaddr_in in;
in.sin_family = AF_INET;
in.sin_port = htons(port);
if (inet_pton(AF_INET, addr.c_str(), &in.sin_addr) == -1) {
closesocket(sock);
return false;
}

if (bind(sock, (const sockaddr*)&in, sizeof(in)) == -1) {
closesocket(sock);
return false;
}

if (listen(sock, 128) == -1) {
return false;
}

m_sock = sock;
return true;
}

void CFiberServer::run(void)
{
while (true) {
SOCKET s = accept(m_sock, NULL, NULL);
if (s == INVALID_SOCKET) {
break;
}

acl::fiber* fb = new CFiberClient(s);
fb->start();
}

if (m_autoDestroy) {
delete this;
}
}
19 changes: 19 additions & 0 deletions lib_fiber/samples-gui/SimpleWin/FiberServer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

class CFiberServer : public acl::fiber
{
public:
CFiberServer(bool autoDestroy = true);
~CFiberServer(void);

bool BindAndListen(int port, const std::string& addr);

protected:
// @override
void run(void);

private:
SOCKET m_sock;
bool m_autoDestroy;
};

35 changes: 35 additions & 0 deletions lib_fiber/samples-gui/SimpleWin/Resource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ 生成的包含文件。
// 供 SimpleWin.rc 使用
//
#define IDC_MYICON 2
#define IDD_SIMPLEWIN_DIALOG 102
#define IDS_APP_TITLE 103
#define IDD_ABOUTBOX 103
#define IDM_ABOUT 104
#define IDM_EXIT 105
#define IDI_SIMPLEWIN 107
#define IDI_SMALL 108
#define IDC_SIMPLEWIN 109
#define IDR_MAINFRAME 128
#define ID_Menu 32771
#define IDM_START_LISTENER 32775
#define IDM_STOP_LISTENER 32776
#define IDM_OPEN_DOS 32777
#define IDM_CLOSE_DOS 32778
#define IDM_START_CONNECT 32779
#define IDM_RESOLVE_THREAD 32780
#define IDM_RESOLVE_FIBER 32781
#define IDC_STATIC -1

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NO_MFC 1
#define _APS_NEXT_RESOURCE_VALUE 129
#define _APS_NEXT_COMMAND_VALUE 32782
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 110
#endif
#endif
Loading

0 comments on commit 4963936

Please sign in to comment.