C++负载均衡远程调用学习之消息队列与线程池

目录

1.昨日回顾

2.单线程的多路IO服务器模型和多线程模型区别

3.服务器的集中并发模式

4.LARSV0.8-task_msg消息队列任务数据类型

5.LARSV0.8--thread_queue消息队列的发送和接收流

6.LARSV0.8-thread_pool线程池的实现

7.LARSV0.8-thread_pool线程池的实现

8.LARSV0.8-处理新链接任务的函数业务实现

9.LARSV0.8-线程池继承到tcp_server中

10.LARSV0.8-启用线程池测试

11.LARSV0.8-限制链接数量的溢出bug问题


1.昨日回顾

2.单线程的多路IO服务器模型和多线程模型区别

1 server(TCP服务)

API-1 创建一个服务器

```c++

tcp_server::tcp_server(event_loop *loop, const char *ip, uint16_t port)

```

`loop`: 事件监听机制堆

`ip`: 服务器监听地址

`port`: 服务器监听端口

**example**

```c++

#include "tcp_server.h"

int main()

{

event_loop loop;

tcp_server server(&loop, "127.0.0.1", 7777);

loop.event_process();

return 0;

}

```

3.服务器的集中并发模式

API-2 添加路由业务

**业务回调函数方法原型**

```c++

typedef void msg_callback(const char *data, uint32_t len, int msgid, net_connection* conn, void *user_data);

```

`data`:需要处理的业务数据

`len`:需要处理的业务数据长度

`msgid`:消息ID

`conn`:需要处理数据的当前链接

`user_data`:开发者额外的自定义参数

4.LARSV0.8-task_msg消息队列任务数据类型

**注册路由业务**

tcp_server对象的方法。

```c++

void tcp_server::add_msg_router(int msgid, msg_callback *cb, void *user_data = NULL);

```

`msgid`:消息ID

`cb`:开发自定义的路由回调业务

`user_data`:开发者额外的自定义参数

5.LARSV0.8--thread_queue消息队列的发送和接收流

**example**

```c++

#include "tcp_server.h"

//定义一个回显的业务

void callback_busi(const char *data, uint32_t len, int msgid, net_connection *conn, void *user_data)

{

printf("callback busi is called!!!!!\n");

//直接将数据发回去

conn->send_message(data, len, msgid);

}

//定义一个打印业务

void print_busi(const char *data, uint32_t len, int msgid, net_connection *conn, void *user_data)

{

printf("print busi is called!\n");

printf("recv from client : %s\n", data);

printf("msgid = %d\n", msgid);

printf("len = %d\n", len);

}

int main()

{

event_loop loop;

tcp_server server(&loop, "127.0.0.1", 7777);

//注册一些回调方法

server.add_msg_router(1, callback_busi);

server.add_msg_router(2, print_busi);

loop.event_process();

return 0;

}

```

6.LARSV0.8-thread_pool线程池的实现

API-3 注册链接创建/销毁成功业务

**回调业务Hook函数原型**

```c++

typedef void (*conn_callback)(net_connection *conn, void *args);

```

`conn`:需要处理数据的当前链接

`args`:开发者额外的自定义参数

**新建连接成功之后Hook注册**

```c++

static void set_conn_start(conn_callback cb, void *args = NULL);

```

`cb`:回调业务Hook函数

`args`:开发者额外的自定义参数

7.LARSV0.8-thread_pool线程池的实现

**销毁连接之前Hookl注册**

```c++

static void set_conn_close(conn_callback cb, void *args = NULL);

```

`cb`:回调业务Hook函数

`args`:开发者额外的自定义参数

**example**

```c++

#include "tcp_server.h"

#include "string.h"

//打印业务

void print_busi(const char *data, uint32_t len, int msgid, net_connection *conn, void *user_data)

{

printf("recv from client : %s\n", data);

printf("msgid = %d\n", msgid);

printf("len = %d\n", len);

}

//新客户端创建成功之后的回调

void on_client_build(net_connection *conn, void *args)

{

printf("===> on_client_build is called!\n");

}

//客户端断开之前的回调

void on_client_lost(net_connection *conn, void *args)

{

printf("===> on_client_lost is called!\n");

}

int main()

{

event_loop loop;

tcp_server server(&loop, "127.0.0.1", 7777);

//注册一些回调方法

server.add_msg_router(2, print_busi);

//注册链接hook函数

server.set_conn_start(on_client_build);

server.set_conn_close(on_client_lost);

loop.event_process();

return 0;

}

```

8.LARSV0.8-处理新链接任务的函数业务实现

2 connection链接

API-1 发送数据

```c++

int send_message(const char *data, int msglen, int msgid)

```

`data`:需要处理的业务数据

`msglen`:需要处理的业务数据长度

`msgid`:消息ID

**example**

```c++

#include "tcp_server.h"

//定义一个回显的业务

void callback_busi(const char *data, uint32_t len, int msgid, net_connection *conn, void *user_data)

{

//直接将数据发回去

conn->send_message(data, len, msgid);

}

int main()

{

event_loop loop;

tcp_server server(&loop, "127.0.0.1", 7777);

//注册一些回调方法

server.add_msg_router(1, callback_busi);

loop.event_process();

return 0;

}

```

9.LARSV0.8-线程池继承到tcp_server中

3 客户端(C/C++)

API-1创建一个客户端

```c++

tcp_client::tcp_client(event_loop *loop, const char *ip, unsigned short port);

```

`loop`: 事件监听机制堆

`ip`: 服务器监听地址

`port`: 服务器监听端口

**example**

```c++

#include "tcp_client.h"

//注册一个客户端处理服务器返回数据的回调业务

void busi(const char *data, uint32_t len, int msgid, net_connection *client, void *user_data)

{

printf("recv from server\n");

printf("msgid = %d\n", msgid);

printf("len = %d\n", len);

printf("data = %s\n", data);

conn->send_message(data, len, msgid);

}

int main()

{

event_loop loop;

tcp_client *client = new tcp_client(&loop, "127.0.0.1", 7777);

//注册一个回调业务

client->add_msg_router(1, busi);

loop.event_process();

}

```

API-2 清除一个客户端

```c++

tcp_client::clean_conn();

```

10.LARSV0.8-启用线程池测试

API-3 注册链接创建/销毁成功业务

**回调业务Hook函数原型**

```c++

typedef void (*conn_callback)(net_connection *conn, void *args);

```

`conn`:需要处理数据的当前链接

`args`:开发者额外的自定义参数

**新建连接成功之后Hook注册**

```c++

static void set_conn_start(conn_callback cb, void *args = NULL);

```

`cb`:回调业务Hook函数

`args`:开发者额外的自定义参数

**销毁连接之前Hookl注册**

```c++

static void set_conn_close(conn_callback cb, void *args = NULL);

```

`cb`:回调业务Hook函数

`args`:开发者额外的自定义参数

**example**

```c++

#include "tcp_client.h"

#include <string.h>

//注册一个客户端处理服务器返回数据的回调业务

void callback_busi(const char *data, uint32_t len, int msgid, net_connection* conn, void *user_data)

{

//将数据写回去

conn->send_message(data, len, msgid);

}

//打印业务

void print_busi(const char *data, uint32_t len, int msgid, net_connection *conn, void *user_data)

{

printf("print busi is called!\n");

printf("recv from server : %s\n", data);

printf("msgid = %d\n", msgid);

printf("len = %d\n", len);

}

//客户端创建连接之后hook业务

void on_client_build(net_connection *conn, void *args)

{

printf("==> on_client build !\n");

//客户端一旦连接成功 就会主动server发送一个msgid = 1 的消息

int msgid = 1;

const char *msg = "hello Lars";

conn->send_message(msg, strlen(msg), msgid);

}

11.LARSV0.8-限制链接数量的溢出bug问题

//客户端销毁连接之前的hook业务

void on_client_lost(net_connection *conn, void *args)

{

printf("==> on_client lost !\n");

}

int main()

{

event_loop loop;

tcp_client *client = new tcp_client(&loop, "127.0.0.1", 7777);

client->add_msg_router(1, print_busi);

client->add_msg_router(2, callback_busi);

client->add_msg_router(200, print_busi);

//注册hook函数

client->set_conn_start(on_client_build);

client->set_conn_close(on_client_lost);

loop.event_process();

}

```

相关推荐
布莱克6052 小时前
C/C++ 中变量的声明与定义:区别、联系与常见误区
c语言·开发语言·c++·声明·定义
前进的李工4 小时前
Agent 学习笔记 05:系统提示词应该怎么写
笔记·学习·prompt·agent·个人开发·few-shot·ai应用开发
正在走向自律5 小时前
【3/5】人工智能训练师 三级/高级工 考证学习方向:从“独立执行”到“设计、归因与全链条负责”
人工智能·学习·人工智能训练师·注方案设计·数据质控负责人·模型评测与调优辅助·ai训练项目交付
沫璃染墨6 小时前
从零入门计算机网络系列(十二):HTTP长连接与会话管理详解——从Connection到Cookie与Session
linux·服务器·c++·tcp/ip·ubuntu·http·交互
CCIE-Yasuo7 小时前
网络安全-学习数据库-Mysql-2
数据库·学习·mysql
xiaoye-duck7 小时前
《C++ 项目:手写大模型接入 SDK》DeepSeek接口设计(下):流式响应实现和测试
开发语言·网络·c++·语言模型
La Pulga8 小时前
【FreeRTOS源码学习笔记】任务切换
单片机·mcu·学习·嵌入式·freertos·实时操作系统
li星野8 小时前
【学习记录】逻辑分析仪完全入门:从品牌方案到采样率、存储深度的核心概念解析
学习
旖旎夜光9 小时前
【C++ 基础入门】类和对象(上):封装、对象模型与 this 指针
开发语言·c++·笔记·学习
小超人诞生日记9 小时前
C++ 智能指针的使用及其原理详解
c++·算法