xukc commited on
Commit
fc28fd3
·
1 Parent(s): 1a0b523

[feat]增加引用spdlog

Browse files
Files changed (5) hide show
  1. .vscode/settings.json +2 -1
  2. .vscode/tasks.json +3 -1
  3. Dockerfile +33 -1
  4. main.cpp +20 -0
  5. tcp_inbound.cpp +7 -6
.vscode/settings.json CHANGED
@@ -70,6 +70,7 @@
70
  "vector": "cpp",
71
  "algorithm": "cpp",
72
  "shared_mutex": "cpp",
73
- "__memory": "cpp"
 
74
  }
75
  }
 
70
  "vector": "cpp",
71
  "algorithm": "cpp",
72
  "shared_mutex": "cpp",
73
+ "__memory": "cpp",
74
+ "thread": "cpp"
75
  }
76
  }
.vscode/tasks.json CHANGED
@@ -15,7 +15,9 @@
15
  "hv_utils.cpp",
16
  "tcp_inbound.cpp",
17
  "main.cpp",
18
- "-lhv"
 
 
19
  ],
20
  "group": {
21
  "kind": "build",
 
15
  "hv_utils.cpp",
16
  "tcp_inbound.cpp",
17
  "main.cpp",
18
+ "-lhv",
19
+ "-lspdlog"
20
+
21
  ],
22
  "group": {
23
  "kind": "build",
Dockerfile CHANGED
@@ -10,6 +10,10 @@ RUN apt-get update
10
  # Install build tools
11
  RUN apt-get install -y g++ make cmake autoconf automake libtool
12
 
 
 
 
 
13
  # Download and build libhv from source
14
  RUN apt-get install -y git
15
  RUN git clone https://github.com/ithewei/libhv.git /usr/src/libhv
@@ -20,6 +24,28 @@ RUN make install
20
  # Add the libhv library path
21
  RUN ldconfig
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  # Set the working directory to /app
24
  WORKDIR /app
25
 
@@ -27,7 +53,13 @@ WORKDIR /app
27
  COPY . .
28
 
29
  # Compile the C++ program
30
- RUN g++ -std=c++14 -g -o proxyServer hv_utils.cpp tcp_inbound.cpp main.cpp -I include -I include/bolt -lhv
 
 
 
 
 
 
31
 
32
  EXPOSE 8080
33
  # Run my_program when the container launches
 
10
  # Install build tools
11
  RUN apt-get install -y g++ make cmake autoconf automake libtool
12
 
13
+ # 安装 spdlog 依赖
14
+ RUN apt-get install -y \
15
+ libfmt-dev
16
+
17
  # Download and build libhv from source
18
  RUN apt-get install -y git
19
  RUN git clone https://github.com/ithewei/libhv.git /usr/src/libhv
 
24
  # Add the libhv library path
25
  RUN ldconfig
26
 
27
+ # 克隆 spdlog 仓库
28
+ RUN git clone https://github.com/gabime/spdlog.git /usr/src/spdlog
29
+
30
+ WORKDIR /usr/src/spdlog
31
+
32
+ # 创建 build 目录并进入
33
+ RUN mkdir build && cd build
34
+
35
+ # 运行 CMake 配置
36
+ RUN cmake ..
37
+
38
+ # 运行构建
39
+ RUN make
40
+
41
+ # (可选)运行测试
42
+ RUN make test
43
+
44
+ # 运行安装
45
+ RUN make install
46
+
47
+ RUN ldconfig
48
+
49
  # Set the working directory to /app
50
  WORKDIR /app
51
 
 
53
  COPY . .
54
 
55
  # Compile the C++ program
56
+ RUN g++ -std=c++14 -g -o proxyServer hv_utils.cpp tcp_inbound.cpp main.cpp -I include -I include/bolt -lhv - lspdlog
57
+
58
+ RUN apt-get update && \
59
+ apt-get install -y tzdata
60
+
61
+ # 设置时区为 Asia/Shanghai
62
+ ENV TZ=Asia/Shanghai
63
 
64
  EXPOSE 8080
65
  # Run my_program when the container launches
main.cpp CHANGED
@@ -132,6 +132,10 @@
132
  #include "include/hv_utils.h"
133
  #include "include/tcp_inbound.h"
134
 
 
 
 
 
135
  static const char* host = "0.0.0.0";
136
  static int port = 8080;
137
  static int thread_num = 4;
@@ -179,9 +183,25 @@ int main(int argc, char** argv) {
179
  // return -10;
180
  // }
181
  // port = atoi(argv[1]);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
182
 
183
  init_loop(thread_num, worker_thread);
184
 
 
 
185
  accept_loop = hloop_new(HLOOP_FLAG_AUTO_FREE);
186
  accept_thread(accept_loop);
187
 
 
132
  #include "include/hv_utils.h"
133
  #include "include/tcp_inbound.h"
134
 
135
+ #include "spdlog/spdlog.h"
136
+ #include "spdlog/async.h"
137
+ #include "spdlog/sinks/stdout_color_sinks.h"
138
+
139
  static const char* host = "0.0.0.0";
140
  static int port = 8080;
141
  static int thread_num = 4;
 
183
  // return -10;
184
  // }
185
  // port = atoi(argv[1]);
186
+ int cores = std::thread::hardware_concurrency();
187
+ if (cores > 0) {
188
+ thread_num = cores;
189
+ }
190
+
191
+ auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
192
+ auto logger = std::make_shared<spdlog::logger>("my_logger", console_sink);
193
+
194
+ // 设置异步模式,设置线程数(0 表示使用 CPU 核心数)
195
+ spdlog::init_thread_pool(8192, thread_num);
196
+
197
+ // 设置异步日志器
198
+ spdlog::set_default_logger(std::make_shared<spdlog::async_logger>(
199
+ "ProxyServer", console_sink, spdlog::thread_pool(), spdlog::async_overflow_policy::block));
200
 
201
  init_loop(thread_num, worker_thread);
202
 
203
+ spdlog::info("ProxyServer start: threadNum:%d", thread_num);
204
+
205
  accept_loop = hloop_new(HLOOP_FLAG_AUTO_FREE);
206
  accept_thread(accept_loop);
207
 
tcp_inbound.cpp CHANGED
@@ -4,6 +4,7 @@
4
  #include "hv/hsocket.h"
5
  #include "hv/hthread.h"
6
  #include "hv/TcpClient.h"
 
7
  #include <string>
8
 
9
  class TcpClientShell {
@@ -17,20 +18,20 @@ class TcpClientShell {
17
  cli.onConnection = [this](const hv::SocketChannelPtr& channel) {
18
  std::string peeraddr = channel->peeraddr();
19
  if (channel->isConnected()) {
20
- printf("connected to %s! connfd=%d\n", peeraddr.c_str(), channel->fd());
21
  if (wait_send_buf.getDataSize() > 0)
22
  {
23
  cli.send(wait_send_buf.getData(), wait_send_buf.getDataSize());
24
  }
25
  } else {
26
- printf("disconnected to %s! connfd=%d\n", peeraddr.c_str(), channel->fd());
27
  hio_close(io);
28
  }
29
  };
30
 
31
  cli.onMessage = [this](const hv::SocketChannelPtr& channel, hv::Buffer* buf) {
32
  hio_write(io, buf->data(), buf->size());
33
- printf("< %.*s\n", (int)buf->size(), (char*)buf->data());
34
  };
35
 
36
  cli.onWriteComplete = [this](const hv::SocketChannelPtr& channel, hv::Buffer* buf) {
@@ -60,12 +61,12 @@ class TcpClientShell {
60
  };
61
 
62
  static void tcp_on_close(hio_t* io) {
63
- printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
64
  ConnMap<hio_t*, TcpClientShell>::getInstance().remove(io);
65
  }
66
 
67
  static void tcp_on_recv(hio_t* io, void* buf, int readbytes) {
68
- // echo
69
  hio_write(io, buf, readbytes);
70
  auto cli = ConnMap<hio_t*, TcpClientShell>::getInstance().get(io);
71
  if(cli) {
@@ -78,7 +79,7 @@ void tcp_on_accept(hio_t* io, hevent_t* ev) {
78
 
79
  char localaddrstr[SOCKADDR_STRLEN] = {0};
80
  char peeraddrstr[SOCKADDR_STRLEN] = {0};
81
- printf("tid=%ld connfd=%d [%s] <= [%s]\n",
82
  (long)hv_gettid(),
83
  (int)hio_fd(io),
84
  SOCKADDR_STR(hio_localaddr(io), localaddrstr),
 
4
  #include "hv/hsocket.h"
5
  #include "hv/hthread.h"
6
  #include "hv/TcpClient.h"
7
+ #include "spdlog/spdlog.h"
8
  #include <string>
9
 
10
  class TcpClientShell {
 
18
  cli.onConnection = [this](const hv::SocketChannelPtr& channel) {
19
  std::string peeraddr = channel->peeraddr();
20
  if (channel->isConnected()) {
21
+ spdlog::info("connected to %s! connfd=%d\n", peeraddr.c_str(), channel->fd());
22
  if (wait_send_buf.getDataSize() > 0)
23
  {
24
  cli.send(wait_send_buf.getData(), wait_send_buf.getDataSize());
25
  }
26
  } else {
27
+ spdlog::info("disconnected to %s! connfd=%d\n", peeraddr.c_str(), channel->fd());
28
  hio_close(io);
29
  }
30
  };
31
 
32
  cli.onMessage = [this](const hv::SocketChannelPtr& channel, hv::Buffer* buf) {
33
  hio_write(io, buf->data(), buf->size());
34
+ spdlog::info("< %.*s\n", (int)buf->size(), (char*)buf->data());
35
  };
36
 
37
  cli.onWriteComplete = [this](const hv::SocketChannelPtr& channel, hv::Buffer* buf) {
 
61
  };
62
 
63
  static void tcp_on_close(hio_t* io) {
64
+ spdlog::info("tcp_on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
65
  ConnMap<hio_t*, TcpClientShell>::getInstance().remove(io);
66
  }
67
 
68
  static void tcp_on_recv(hio_t* io, void* buf, int readbytes) {
69
+ spdlog::info("tcp_on_recv fd=%d buf(%d)=%s\n", hio_fd(io), readbytes, buf);
70
  hio_write(io, buf, readbytes);
71
  auto cli = ConnMap<hio_t*, TcpClientShell>::getInstance().get(io);
72
  if(cli) {
 
79
 
80
  char localaddrstr[SOCKADDR_STRLEN] = {0};
81
  char peeraddrstr[SOCKADDR_STRLEN] = {0};
82
+ spdlog::info("tcp_on_accept tid=%ld connfd=%d [%s] <= [%s]\n",
83
  (long)hv_gettid(),
84
  (int)hio_fd(io),
85
  SOCKADDR_STR(hio_localaddr(io), localaddrstr),