-
Notifications
You must be signed in to change notification settings - Fork 940
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
郑树新
committed
Sep 28, 2024
1 parent
037f55f
commit 4963936
Showing
19 changed files
with
1,093 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.