rust学习基于tokio_actor聊天服务器实战(一 )

2024-02-01 08:28

本文主要是介绍rust学习基于tokio_actor聊天服务器实战(一 ),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言
tokio是Rust中使用最广泛的异步Runtime,它性能高、功能丰富、便于使用,是使用Rust实现高并发不可不学的一个框架
Actor 背后的基本思想是产生一个独立的任务,该任务独立于程序的其他部分执行某些工作。 通常,这些参与者通过使用消息传递信道与程序的其余部分进行通信。 由于每个 Actor 独立运行,因此使用它们设计的程序自然是并行的。 Actor 的一个常见用法是为 Actor 分配你要共享的某些资源的专有所有权,然后让其他任务通过与 Actor 通信来间接访问彼此的资源。 例如,如果要实现聊天服务器,则可以为每个连接生成一个任务,并在其他任务之间路由一个聊天消息的主任务。 十分有用,因为主任务可以避免必须处理网络IO,而连接任务可以专门处理网络IO;
为什么一定要用actor,这里只是仿照go项目里一部分,go 用的就是actor;

1:环境
rust1.75
ide rustrover64

2:设计及实现
这里使用类似单点登录模式,
useractor
先看go的
在这里插入图片描述
在这里插入图片描述
一共3个协程/future
接受网络消息 一个协程/future
发送网络消息 一个协程/future
逻辑处理 一个协程/future
协程/future间通信 直接用mpsc

world actor/accmgr 管理useractor 登录,踢人,广播等

一共1个协程/future 处理逻辑消息
在这里插入图片描述

rust 版
useractor
说明 receiver: mpsc::UnboundedReceiver, logic future 接受消息并处理
sendclient: mpsc::UnboundedSender 发送消息给 网络future 从而发送给前端
worldsender: mpsc::UnboundedSender, 跟world actor 通信接口

pub enum ActorMessage {synmsgwaitrep {//同步等待回复//需要发送到别处等到别处返回结果,类似于同步操作,只是异步执行的  //oneshot  spscrespond_to: crate::synMsgWaitRep, //同步消息},wtc_userchann {respond_to: crate::userChan_WTC, //},wtc_msg(sendMsgAndType),wtc_forwardmsg(sendMsgAndType), //直接转发 datactw_msg(sendMsgAndType),ctc_nettologic_msg(sendMsgAndType), //网络消息 to logicctc_logictonet_msg(sendMsgAndType), //logic to net  sendctc_signal_event(signalType),ctw_signal_event(signalType),wtc_signal_event(signalType),wtc_getChan_msg(userChannChann),
}
pub struct MyUserActor {connid: ConnectID,userid: UserID,username: String,guildid: GuildID,userstate: Arc<AtomicU8>,receiver: mpsc::UnboundedReceiver<ActorMessage>,sendclient: mpsc::UnboundedSender<VU8>,worldsender: mpsc::UnboundedSender<ActorMessage>,msgmask: u32,lasttime: [u32; ChatChannel_Num],
}

world actor
mpscrecv: mpsc::UnboundedReceiver, 接收ActorMessage logic future
chanchan: mpsc::UnboundedReceiver, 接受 ActorMessage2 logic future

pub enum ActorMessage2 {synmsgwaitrep {//同步等待回复//需要发送到别处等到别处返回结果,类似于同步操作,只是异步执行的  //oneshot  spscrespond_to: crate::synMsgWaitRep2, //同步消息},ctw_userhann {respond_to: crate::userChan_CTW, //同步消息},
}
pub struct userSendChanActorMessage {pub(crate) chanchan: Option<mpsc::UnboundedSender<ActorMessage>>,pub(crate) username: String,pub(crate) userguildid: GuildID,pub(crate) connectid: ConnectID,pub(crate) chanState: Arc<AtomicU8>, //user 状态
}pub struct worldActor {sharestate: Arc<AtomicU8>,mpscrecv: mpsc::UnboundedReceiver<ActorMessage>,chanchan: mpsc::UnboundedReceiver<ActorMessage2>,usermap: HashMap<UserID, userChan_world>,namemap: HashMap<String, UserID>,guildmap: HashMap<GuildID, HashSet<UserID>>,maxonlinerole: u32,
}async fn run(mut self) {// let logic_handle = self.handle_logic(recv);loop {tokio::select! {recvmsg= self.mpscrecv.recv()=> {if let Some(actmsg) = recvmsg {self.handle_logic(actmsg).await ;}}recvmsgchan= self.chanchan.recv()=>{if let Some(actmsg) = recvmsgchan {self.handle_logic2(actmsg).await ;}}_=tokio::time::sleep(Duration::from_millis(1000*8)) =>{}}} //end loop}

同步的方式的异步 go 很简单, rust go 上多一点点
go
在这里插入图片描述
rust
在这里插入图片描述

在这里插入图片描述

网络跟逻辑分开,这样 挤号,只需要把 logic future 里 sendclient mpsc 更新, 把网络 to logic mpsc 更新 及一些 状态重置下 即可,无需重新加载现有useractor 里的信息
类试单点登录 对于聊天服务器来说 ,只需要 角色进入后,由logic服 ase 对称加密(密钥及盐,logic 服 chat 服 共享/配置,共享方式自行决定)或 非对称(ECC) 等都可以,加密的token 由前端发送 给chat 服,chat 解密 得到 相应信息 并验证有效性 参考加解密验证用户的合法性

3:测试
前端简单用go 写了个

var origin = "http://192.168.1.32:8080"
var url = "wss://192.168.1.32:8080/websocket"
func GetProtoMsgID(data []byte) uint32 {var sMsgID uint16 = uint16(uint8(data[3] & 0x7f))if (uint8(data[3]) & 0x80) > 0 {sMsgID += (uint16(data[4]) & 0x7f) << 7}return uint32(sMsgID)
}func  sendMsg(ws *websocket.Conn,pb proto.Message) {if ws != nil {if data, err2 := proto.Marshal(pb); err2 != nil {log.Printf("SendMessage pb=%v err2=%v \n", pb, err2)} else {if err4 := websocket.Message.Send(ws, data); err4 != nil {log.Printf("send error =%v \n", err4)}}}
}func doLogicMsg(data []byte)  {msgId := GetProtoMsgID(data)fmt.Printf("msgid=%v",msgId)switch msgId {case uint32(chatproto.CHATMSG_CHC_Login_Rep):{loginReq := &chatproto.ChatMessageLoginRep{}if err := proto.Unmarshal(data, loginReq); err != nil {} else {fmt.Printf("CHATMSG_CHC_Login_Rep =%v \n",loginReq.Res)}}case uint32(chatproto.CHATMSG_CCH_Chat_Rep):{chatrep := &chatproto.ChatMessageChatRep{}if err := proto.Unmarshal(data, chatrep); err != nil {} else {fmt.Printf("CHATMSG_CCH_Chat_Rep =%v \n",chatrep.Res)}}case uint32(chatproto.CHATMSG_CHC_Notify_Chat):{chatmsg := &chatproto.ChatMessageNotifyChat{}if err := proto.Unmarshal(data, chatmsg); err != nil {} else {fmt.Printf("CHATMSG_CHC_Notify_Chat =%v fromuserid=%v text=%v \n",chatmsg.Chattype,chatmsg.Senderid,chatmsg.Strcontext)}}}}
func getTimestamp() uint32 {return  uint32(time.Now().UTC().Unix());
}func main(){//if os.Args[0]userid :=  getTimestamp()guildid := uint32(0)if len(os.Args) > 1 {if s,e := strconv.Atoi(os.Args[1]);e ==nil {userid = uint32(s)}}if len(os.Args) > 2 {if s,e := strconv.Atoi(os.Args[2]);e ==nil {guildid = uint32(s)}}ws, err := websocket.Dial(url, "", origin)if err != nil {log.Fatal(err)}fmt.Printf("userid=%v guild=%v \n",userid,guildid){msg := new(chatproto.ChatMessageLoginReq)msg.Msghead = &chatproto.ChatMessageHead{uint32(chatproto.CHATMSG_CCH_Login_Req), 1}msg.Userid = useridmsg.Username = "name_"+strconv.Itoa(int(userid))msg.Guildid = guildidmsg.Tokenmd5 = "md5"msg.Tokenstr = "Tokenstr"sendMsg(ws, msg)}disflag :=  false{go func() {for{buf := make([]byte, 1024*4)err := websocket.Message.Receive(ws, &buf)if err != nil {//log.Printf("websocket.Message.Receive err=%v  ---%s\n", err,self.getAccName())disflag = truereturn}if len(buf) >= 4 {doLogicMsg(buf)//self.msgQue.PostUserMessage(&ReceiveNetMsg{buf})} else {log.Printf("[error]recv data=%v \n", buf)return}}}()}time.Sleep(time.Second*3)//pub enum  ChatChannel{//	ChatChannel_NONE=0,//	ChatChannel_NORMAL,//	ChatChannel_GUILD,//	ChatChannel_WORLD,//	ChatChannel_ALL,//}{sendcount := uint32(1)num := uint32(0)msg := new(chatproto.ChatMessageChatReq)msg.Msghead = &chatproto.ChatMessageHead{uint32(chatproto.CHATMSG_CCH_Chat_Req), 1}msg.Chattype = 1msg.Context ="normal chat "+ strconv.Itoa(int(num))for {if disflag { //脏数据break}sendMsg(ws, msg)time.Sleep(time.Second*10)num++m := num % 3 +1msg.Chattype = uint32(m)msg.Context ="normal chat "+ strconv.Itoa(int(sendcount))fmt.Printf("[%v][%v] send chattype=%v \n",sendcount,getTimestamp(),msg.Chattype)sendcount++//if m == 3  {//	time.Sleep(time.Second*10)//}}}ws.Close()//关闭连接fmt.Printf("client exit\n")
}

相互挤号测试
在这里插入图片描述
4:DEMO工程 后续完善了如有需要再上传(当前只能说基本上跑起来)
如果觉得有用,麻烦点个赞,加个收藏

这篇关于rust学习基于tokio_actor聊天服务器实战(一 )的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/666670

相关文章

Prometheus+cpolar如何在手机上也能监控服务器状态?

《Prometheus+cpolar如何在手机上也能监控服务器状态?》本文强调了通过Cpolar这一内网穿透工具,轻松突破Prometheus仅限于局域网访问的限制,实现外网随时随地访问监控数据,教你... 目录前言1.安装prometheus2.安装cpolar实现随时随地开发3.配置公网地址4.保留固定

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

Java 队列Queue从原理到实战指南

《Java队列Queue从原理到实战指南》本文介绍了Java中队列(Queue)的底层实现、常见方法及其区别,通过LinkedList和ArrayDeque的实现,以及循环队列的概念,展示了如何高效... 目录一、队列的认识队列的底层与集合框架常见的队列方法插入元素方法对比(add和offer)移除元素方法

Spring Boot基于 JWT 优化 Spring Security 无状态登录实战指南

《SpringBoot基于JWT优化SpringSecurity无状态登录实战指南》本文介绍如何使用JWT优化SpringSecurity实现无状态登录,提高接口安全性,并通过实际操作步骤... 目录Spring Boot 实战:基于 JWT 优化 Spring Security 无状态登录一、先搞懂:为什

C++11中的包装器实战案例

《C++11中的包装器实战案例》本文给大家介绍C++11中的包装器实战案例,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录引言1.std::function1.1.什么是std::function1.2.核心用法1.2.1.包装普通函数1.2.

Nginx概念、架构、配置与虚拟主机实战操作指南

《Nginx概念、架构、配置与虚拟主机实战操作指南》Nginx是一个高性能的HTTP服务器、反向代理服务器、负载均衡器和IMAP/POP3/SMTP代理服务器,它支持高并发连接,资源占用低,功能全面且... 目录Nginx 深度解析:概念、架构、配置与虚拟主机实战一、Nginx 的概念二、Nginx 的特点

Spring IOC核心原理详解与运用实战教程

《SpringIOC核心原理详解与运用实战教程》本文详细解析了SpringIOC容器的核心原理,包括BeanFactory体系、依赖注入机制、循环依赖解决和三级缓存机制,同时,介绍了SpringBo... 目录1. Spring IOC核心原理深度解析1.1 BeanFactory体系与内部结构1.1.1

Redis 命令详解与实战案例

《Redis命令详解与实战案例》本文详细介绍了Redis的基础知识、核心数据结构与命令、高级功能与命令、最佳实践与性能优化,以及实战应用场景,通过实战案例,展示了如何使用Redis构建高性能应用系统... 目录Redis 命令详解与实战案例一、Redis 基础介绍二、Redis 核心数据结构与命令1. 字符

Linux服务器数据盘移除并重新挂载的全过程

《Linux服务器数据盘移除并重新挂载的全过程》:本文主要介绍在Linux服务器上移除并重新挂载数据盘的整个过程,分为三大步:卸载文件系统、分离磁盘和重新挂载,每一步都有详细的步骤和注意事项,确保... 目录引言第一步:卸载文件系统第二步:分离磁盘第三步:重新挂载引言在 linux 服务器上移除并重新挂p

在SpringBoot+MyBatis项目中实现MySQL读写分离的实战指南

《在SpringBoot+MyBatis项目中实现MySQL读写分离的实战指南》在SpringBoot和MyBatis项目中实现MySQL读写分离,主要有两种思路:一种是在应用层通过代码和配置手动控制... 目录如何选择实现方案核心实现:应用层手动分离实施中的关键问题与解决方案总结在Spring Boot和