从零开始手写mmo游戏从框架到爆炸(七)— 消息封装

        上一篇,我们初步把消息handler 注册到了服务中,在进行后续工作之前我们需要再做一些准备工作。

        第一:把之前自己管理的bean放到spring中去管理,后面大部分的bean都通过spring来管理。

        第二:为了方便路由消费,我们要创建一个消息体方便byte字节数组传输。

Spring        

先把spring上下文变量工具整好

SpringContextHelper.java

package com.loveprogrammer.base.factory;

import org.springframework.context.ApplicationContext;

public class SpringContextHelper {

	private static ApplicationContext ac;

	public static void setApplicationContext(ApplicationContext ac) {

		SpringContextHelper.ac = ac;
	}

	public static ApplicationContext getContext() {

		return ac;
	}

	public static Object getBean(String name) {

		return ac.getBean(name);
	}

	public static Object getBean(Class clazz) {

		return ac.getBean(clazz);
	}

}

CommonBootConfig.java

import com.loveprogrammer.base.factory.SpringContextHelper;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CommonBootConfig implements ApplicationContextAware {

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

		SpringContextHelper.setApplicationContext(applicationContext);
	}
}

修改 NetworkListener.java

@Component
public class NetworkListener implements INetworkEventListener {

 修改 TcpMessageStringHandler.java

@Component
public class TcpMessageStringHandler extends SimpleChannelInboundHandler<String> {
    private static final Logger logger = LoggerFactory.getLogger(TcpMessageStringHandler.class);

    @Autowired
    private INetworkEventListener listener;

//    public TcpMessageStringHandler(INetworkEventListener listener) {
//        this.listener = listener;
//    }

修改TcpServerStringInitializer.java

public class TcpServerStringInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast("framer",new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
        pipeline.addLast("decoder", new StringDecoder());
        pipeline.addLast("encoder", new StringEncoder());
        TcpMessageStringHandler handler = (TcpMessageStringHandler) SpringContextHelper.getBean(TcpMessageStringHandler.class);
        pipeline.addLast(handler);
    }

}

 消息封装

创建一个类 StringMessage

package com.loveprogrammer.pojo;

import com.alibaba.fastjson2.JSON;

/**
 * @ClassName StringMessage
 * @Description string类型的请求的请求体
 * @Author admin
 * @Date 2024/1/31 10:35
 * @Version 1.0
 */
public class StringMessage {

    /***
     *         topicId 路由主键对应class
     *         tagId 路由副健,对应method
     *         statusC内容的长ode主要是返回内容是告诉客户端消息的状态,成功为1,其他不同的错误使用不同的错误码
     *         length是度,内容的长度是不可控制的,所以使用一个长度进行定义
     *         body是具体的内容
     */

    private int topicId;

    private int tagId;

    private int statusCode;

    private int length;

    private String body;

    public StringMessage() {
    }

//    public StringMessage(short messageId) {
//        this.messageId = messageId;
//    }

    public static StringMessage create(int topicId,int tagId) {
        StringMessage stringMessage = new StringMessage();
        stringMessage.setTopicId(topicId);
        stringMessage.setTagId(tagId);
        return stringMessage;
    }

    public static StringMessage create(String origin) {
        StringMessage stringMessage = JSON.parseObject(origin, StringMessage.class);
        return stringMessage;
    }

    public static StringMessage create(int length, int topicId,int tagId , int statusCode, String content) {
        return new StringMessage(length, topicId, tagId, statusCode, content);
    }

    private StringMessage(int length, int topicId,int tagId, int statusCode, String body) {
        this.length = length;
        this.topicId = topicId;
        this.tagId = tagId;
        this.statusCode = statusCode;
        this.body = body;
    }

    public int getTopicId() {
        return topicId;
    }

    public void setTopicId(int topicId) {
        this.topicId = topicId;
    }

    public int getTagId() {
        return tagId;
    }

    public void setTagId(int tagId) {
        this.tagId = tagId;
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    @Override
    public String toString() {
        return "StringMessage{" + "messageId=" + topicId + ", statusCode=" + statusCode + ", length=" + length
                + ", body='" + body + '\'' + '}';
    }
}

  创建两个编解码类 

  MessageDecoder.java

package com.loveprogrammer.codec;

import com.loveprogrammer.constants.ConstantValue;
import com.loveprogrammer.pojo.StringMessage;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;

/**
 * @ClassName MessageDecoder
 * @Description 消息解码器
 * @Author admin
 * @Date 2024/1/31 10:43
 * @Version 1.0
 */
public class MessageDecoder extends LengthFieldBasedFrameDecoder {

    //判断传送客户端传送过来的数据是否按照协议传输,头部信息的大小应该是 int+int+int = 4+4+4 = 12
    private static final int HEADER_SIZE = 12;

    private int topicId;
    private int tagId;
    private int statusCode;
    private int length;

    private String body;

    /***
     *
     * @param maxFrameLength 解码时,处理每个帧数据的最大长度
     * @param lengthFieldOffset 该帧数据中,存放该帧数据的长度的数据的起始位置
     * @param lengthFieldLength 记录该帧数据长度的字段本身的长度
     * @param lengthAdjustment 修改帧数据长度字段中定义的值,可以为负数
     * @param initialBytesToStrip 解析的时候需要跳过的字节数
     * @param failFast 为true,当frame长度超过maxFrameLength时立即报TooLongFrameException异常,为false,读取完整个帧再报异常
     */
    public MessageDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip, boolean failFast) {
        super(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip, failFast);
    }

    @Override
    protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
        if(in == null){
            return null;
        }
        if(in.readableBytes() < HEADER_SIZE) {
            throw new Exception("可读信息段比头部信息都小");
        }

        // 注意在读的过程中,readIndex的指针也在移动
        topicId = in.readInt();
        tagId = in.readInt();
        statusCode = in.readInt();
        length = in.readInt();

        if(in.readableBytes() < length) {
            throw new Exception("body获取长度" + length + ",实际长度没有达到");
        }
        ByteBuf buf = in.readBytes(length);
        byte[] req = new byte[buf.readableBytes()];
        buf.readBytes(req);
        body = new String(req, ConstantValue.PROJECT_CHARSET);

        StringMessage stringMessage = StringMessage.create(length, topicId, tagId, statusCode, body);
        return stringMessage;

    }
}

MessageEncoder.java

package com.loveprogrammer.codec;

import com.loveprogrammer.constants.ConstantValue;
import com.loveprogrammer.pojo.StringMessage;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

import java.nio.charset.Charset;

/**
 * @ClassName MessageEncoder
 * @Description 消息编码器
 * @Author admin
 * @Date 2024/1/31 10:40
 * @Version 1.0
 */
public class MessageEncoder extends MessageToByteEncoder<StringMessage> {
    @Override
    protected void encode(ChannelHandlerContext ctx, StringMessage msg, ByteBuf out) throws Exception {
        if(null == msg) {
            throw new Exception("msg is null");
        }
        String body = msg.getBody();
        byte[] bodyBytes = body.getBytes(Charset.forName(ConstantValue.PROJECT_CHARSET));
        out.writeInt(msg.getTopicId());
        out.writeInt(msg.getTagId());
        out.writeInt(msg.getStatusCode());
        out.writeInt(bodyBytes.length);
        out.writeBytes(bodyBytes);
    }
}

 下一章我们来实现byte数组传输数据

上一篇:从零开始手写mmo游戏从框架到爆炸(六)— 消息处理工厂-CSDN博客

全部源码详见:

gitee : eternity-online: 多人在线mmo游戏 - Gitee.com

分支:step-06

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/373769.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

C语言:内存函数

创作不易&#xff0c;友友们给个三连吧&#xff01;&#xff01; C语言标准库中有这样一些内存函数&#xff0c;让我们一起学习吧&#xff01;&#xff01; 一、memcpy函数的使用和模拟实现 void * memcpy ( void * destination, const void * source, size_t num ); 1.1 使…

分享65个节日PPT,总有一款适合您

分享65个节日PPT&#xff0c;总有一款适合您 65个节日PPT下载链接&#xff1a;https://pan.baidu.com/s/1hc1M5gfYK8eDxQVsK8O9xQ?pwd8888 提取码&#xff1a;8888 Python采集代码下载链接&#xff1a;采集代码.zip - 蓝奏云 学习知识费力气&#xff0c;收集整理更不易。知…

编译原理与技术(二)——词法分析(一)正则表达式

一、词法分析的概貌 一个程序&#xff0c;在我们看来往往是像下图这样的。 实际上&#xff0c;上面的程序本质上就是一个字符串&#xff0c;所以&#xff0c;它等价于下面这样的。 上面的字符串&#xff08;字符流&#xff09; &#xff0c;就是编译器接收到的程序的形式。 所…

centos安装inpanel

前置条件 安装python yum -y install python 安装 cd /usr/local git clone https://gitee.com/WangZhe168_admin/inpanel.git cd inpanel python install.py 安装过程需要设置账户 密码 端口号 我设置的是admin:admin 10050 使用 打开浏览器,输入 http://192.168.168.…

【人工智能】神奇的Embedding:文本变向量,大语言模型智慧密码解析(10)

什么是嵌入&#xff1f; OpenAI 的文本嵌入衡量文本字符串的相关性。嵌入通常用于&#xff1a; Search 搜索&#xff08;结果按与查询字符串的相关性排序&#xff09;Clustering 聚类&#xff08;文本字符串按相似性分组&#xff09;Recommendations 推荐&#xff08;推荐具有…

02.05

1.单链表 main #include "1list_head.h" int main(int argc, const char *argv[]) { //创建链表之前链表为空Linklist headNULL;int n;datatype element;printf("please enter n:");scanf("%d",&n);for(int i0;i<n;i){printf("ple…

22.仿简道云公式函数实战-数学函数-COT

1. COT函数 COT 函数可用于计算角度的余切值。 2. 函数用法 COT(弧度) 使用该函数时&#xff0c;需要将角度转化为弧度参与计算&#xff0c;可通过 RADIANS 函数 将角度转化为弧度。 3. 函数示例 如计算 COT(45) 的值&#xff0c;可设置公式为COT(RADIANS(45))&#xff0…

算法——二分查找算法

1. 二分算法是什么&#xff1f; 简单来说&#xff0c;"二分"指的是将查找的区间一分为二&#xff0c;通过比较目标值与中间元素的大小关系&#xff0c;确定目标值可能在哪一半区间内&#xff0c;从而缩小查找范围。这个过程不断重复&#xff0c;每次都将当前区间二分…

算法练习-四数之和(思路+流程图+代码)

难度参考 难度&#xff1a;中等 分类&#xff1a;数组 难度与分类由我所参与的培训课程提供&#xff0c;但需要注意的是&#xff0c;难度与分类仅供参考。且所在课程未提供测试平台&#xff0c;故实现代码主要为自行测试的那种&#xff0c;以下内容均为个人笔记&#xff0c;旨在…

配置git环境与项目创建

项目设计 名称&#xff1a;KOB 项目包含的模块 PK模块&#xff1a;匹配界面&#xff08;微服务&#xff09;、实况直播界面&#xff08;WebSocket协议&#xff09; 对局列表模块&#xff1a;对局列表界面、对局录像界面 排行榜模块&#xff1a;Bot排行榜界面 用户中心模块&…

【Qt】常见问题

1.存在未解析的标识符 将build文件夹删掉重新编译。 2.左侧项目目录栏无法删除已添加项目 打开目标项目上一级的pro文件&#xff0c;将目标文件名字注释或者删除掉&#xff0c;最后保存&#xff0c;qt就会自动更新&#xff0c;将该项目隐藏掉。 3.在qt creator下添加槽函数…

大型装备制造企业案例分享——通过CRM系统管理全球业务

本期&#xff0c;小Z为大家带来的CRM管理系统客户案例是某大型装备制造企业运用Zoho CRM管理全球业务的过程分享。该企业是创业板上市公司&#xff0c;业务遍及100多个国家和地区&#xff0c;合作伙伴超百位&#xff0c;拥有覆盖全球的销售和服务网络。截止目前&#xff0c;相继…

油猴js 获取替换网页链接并重定向

场景 适用一些镜像网站进行重定向&#xff0c;比如Github。 代码 // UserScript // name New Userscript // namespace http://tampermonkey.net/ // version 2024-02-06 // description try to take over the world! // author You // match …

❤ React18 环境搭建项目与运行(地址已经放Gitee开源)

❤ React项目搭建与运行 环境介绍 node v20.11.0 react 18.2 react-dom 18.2.0一、React环境搭建 第一种普通cra搭建 1、检查本地环境 node版本 18.17.0 检查node和npm环境 node -v npm -v 2、安装yarn npm install -g yarn yarn --version 3、创建一个新的React项目…

OpenCV 图像处理六(傅里叶变换、模板匹配与霍夫变换)

文章目录 一、傅里叶变换1.1 NumPy实现和逆实现1.1.1 NumPy实现傅里叶变换Demo 1.1.2 NumPy实现逆傅里叶变换Demo 1.2 OpenCV实现和逆实现1.2.1 OpenCV实现傅里叶变换Demo 1.2.2 OpenCV实现逆傅里叶变换Demo 1.3 频域滤波1.3.1低频、高频1.3.2 高通滤波器构造高通滤波器Demo 1.…

jquery写表格,通过后端传值,并合并单元格

<!DOCTYPE html> <html> <head><title>Table Using jQuery</title><style>#tableWrapper {width: 100%;height: 200px; /* 设置表格容器的高度 */overflow: auto; /* 添加滚动条 */margin-top: -10px; /* 负的外边距值&#xff0c;根据实际…

Mac OS中创建适合网络备份的加密镜像文件:详细步骤与参数选择

这篇文章提供了在Mac OS中创建适合网络备份的加密镜像文件的详细步骤&#xff0c;同时探讨了在选择相关参数时的关键考虑因素&#xff0c;以确保用户能够安全、高效地存储和保护重要数据。 创建步骤 在Mac OS Monterey中&#xff0c;你可以使用“磁盘工具”&#xff08;Disk …

【制作100个unity游戏之23】实现类似七日杀、森林一样的生存游戏12(附项目源码)

本节最终效果演示 文章目录 本节最终效果演示系列目录前言斧头动画控制配置拿出 待机和攻击动画代码控制攻击动画 源码完结 系列目录 前言 欢迎来到【制作100个Unity游戏】系列&#xff01;本系列将引导您一步步学习如何使用Unity开发各种类型的游戏。在这第23篇中&#xff0…

LRU缓存

有人从网络读数据&#xff0c;有人从磁盘读数据&#xff0c;机智的人懂得合理利用缓存加速数据的读取效率&#xff0c;提升程序的性能&#xff0c;搏得上司的赏识&#xff0c;赢得白富美的青睐&#xff0c;进一步走向人生巅峰~ LRU假说 LRU缓存&#xff08;Least Recently Used…

SQL--函数

概念 函数 是指一段可以直接被另一段程序调用的程序或代码。 也就意味着&#xff0c;这一段程序或代码在MySQL中 已经给我们提供了&#xff0c;我们要做的就是在合适的业务场景调用对应的函数完成对应的业务需求即可。 那 么&#xff0c;函数到底在哪儿使用呢&#xff1f; 我…