Skip to content
This repository was archived by the owner on Apr 27, 2020. It is now read-only.

Commit 54f67c2

Browse files
committed
增加 WebSocket 支持
路由未完善
1 parent 5a3c8ab commit 54f67c2

File tree

10 files changed

+591
-16
lines changed

10 files changed

+591
-16
lines changed

CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ endif (MSVC)
4242
include_directories(
4343
.
4444
kProxy
45+
kProxy/kHttpd
4546
)
4647

4748

@@ -70,7 +71,14 @@ endif ()
7071

7172

7273
#aux_source_directory(kHttpd srcSource)
73-
FILE(GLOB_RECURSE srcSource "kProxy/*.c" "kProxy/*.cc" "kProxy/*.cpp" "kProxy/*.h" "kProxy/*.hpp")
74+
FILE(GLOB_RECURSE srcSource
75+
"kProxy/*.c"
76+
"kProxy/*.cc"
77+
"kProxy/*.cpp"
78+
"kProxy/*.h"
79+
"kProxy/*.hpp"
80+
"kProxy/*/*"
81+
)
7482

7583
add_library(kProxy STATIC ${srcSource})
7684

clangTools

kProxy/HttpResponseCode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class HttpResponseCode {
105105
return "Continue";
106106
// 切换协议。服务器根据客户端的请求切换协议。只能切换到更高级的协议,例如,切换到HTTP的新版本协议
107107
case SwitchingProtocols:
108-
return "SwitchingProtocols";
108+
return "Switching Protocols";
109109

110110
// 已创建。成功请求并创建了新的资源
111111
case Created:

kProxy/kHttpd.cpp renamed to kProxy/kHttpd/kHttpd.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#endif
1818

1919
#include <kHttpdClient.h>
20+
#include <kWebSocketClient.h>
2021
#include <socket.h>
2122

2223
using namespace std;
@@ -44,11 +45,6 @@ void kHttpd::Init() {
4445
(void)WSAStartup(wVersionRequested, &wsaData);
4546
#endif
4647

47-
#ifdef _WIN32
48-
evthread_use_windows_threads();
49-
#else
50-
evthread_use_pthreads();
51-
#endif
5248
}
5349

5450
kHttpd::kHttpd(int max_thread) {
@@ -128,7 +124,12 @@ int kHttpd::listen(int listen_count, short port, const char *ip) {
128124
) {
129125
threadPool->commit([this](int new_fd) -> void {
130126
// _logger->d(TAG, __LINE__, "======== client thread run ========");
131-
int _fd = kHttpdClient(this, new_fd).run();
127+
int _fd = -1;
128+
if (isWebSocket) {
129+
_fd = kWebSocketClient(this, new_fd).run();
130+
} else {
131+
_fd = kHttpdClient(this, new_fd).run();
132+
}
132133
if (_fd > 0) {
133134
shutdown(_fd, SHUT_RDWR);
134135
close(_fd);
@@ -167,3 +168,14 @@ bool
167168
kHttpd::check_host_path(class kHttpdClient *_kHttpdClient, std::string Host, std::string method, std::string url_path) {
168169
return false;
169170
}
171+
172+
bool
173+
kHttpd::check_host_path(class kWebSocketClient *_kWebSocketClient, std::string Host, std::string method,
174+
std::string url_path) {
175+
return false;
176+
}
177+
178+
bool
179+
kHttpd::check_host_path(class kWebSocketClient *_kWebSocketClient, int type, std::vector<unsigned char> data) {
180+
return _kWebSocketClient->send(data, type) >= 0;
181+
}

kProxy/kHttpd.h renamed to kProxy/kHttpd/kHttpd.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616
#endif
1717

1818
#include <logger.h>
19-
#include <event.h>
20-
#include <event2/event.h>
21-
#include <event2/bufferevent.h>
22-
#include <event2/thread.h>
23-
#include <event2/listener.h>
2419
#include <thread_pool.h>
2520

2621
class _kProxy_HEADER_Export kHttpd {
@@ -29,14 +24,18 @@ class _kProxy_HEADER_Export kHttpd {
2924
static const char *TAG;
3025
static logger *_logger;
3126

32-
3327
public:
3428
static void Init();
3529

3630
explicit kHttpd(int max_thread = 20);
3731

3832
explicit kHttpd(const char *web_root_path, int max_thread = 20);
3933

34+
/**
35+
* 是否是 WebSocket
36+
*/
37+
bool isWebSocket = false;
38+
4039
~kHttpd();
4140

4241
int listen(int listen_count = 20, short port = 8080, const char *ip = "0.0.0.0");
@@ -60,7 +59,14 @@ class _kProxy_HEADER_Export kHttpd {
6059

6160
bool check_host_path(class kHttpdClient *_kHttpdClient, std::string Host, std::string method, std::string url_path);
6261

62+
bool check_host_path(class kWebSocketClient *_kWebSocketClient, std::string Host, std::string method,
63+
std::string url_path);
64+
65+
bool check_host_path(class kWebSocketClient *_kWebSocketClient, int type, std::vector<unsigned char> data);
66+
6367
friend class kHttpdClient;
68+
69+
friend class kWebSocketClient;
6470
};
6571

6672

kProxy/kHttpdClient.cpp renamed to kProxy/kHttpd/kHttpdClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static map<string, string> kHttpdClient_HTTP_Content_Type = {
4444
#ifdef __FILENAME__
4545
const char *kHttpdClient::TAG = __FILENAME__;
4646
#else
47-
const char *kClient::TAG = "kClient";
47+
const char *kHttpdClient::TAG = "kHttpdClient";
4848
#endif
4949

5050
logger *kHttpdClient::_logger = logger::instance();
File renamed without changes.

0 commit comments

Comments
 (0)