GO EASY 框架 之 NET 05

目录

1、Overview

2、Agent接口源码

3、收发消息

4、AgentHandle接收函数

5、conns.Conn接口


1、Overview

名称:agent.Agent 网络链接 接口;

DESC:网络链接,服务端与客户端通信媒介;

封装:进一步封装了net.Conn,封装了通用,以及自定义;

2、Agent接口源码

type Agent interface {
    WriteMsg(msg any)
    LocalAddr() net.Addr
    RemoteAddr() net.Addr
    Close()
    Destroy()
    UserData() any
    SetUserData(data any)
    LoopRecv(handle AgentHandle)
}

3、收发消息

  • 发送消息

Agent.WriteMsg(msg any);

直接将消息通过Agent发送到另一端,由Agent完成内部自动完成Encode/Decode(可在路由自定义Encode格式);

  • 接收消息

Agent.LoopRecv(handle AgentHandle),此接口是内部自动调用;

Agent 通过路由的消息协议调用handle并传Decode的消息内容传递给此handle;

接收消息的是一个个自定义的AgentHandle类型的函数;

消息ID,消息体,handle需要在路由中注册;

4、AgentHandle接收函数

type AgentHandle func([]byte, Agent)

在easy中,Route路由器将 typehandle.HandleMessage 转化成agent.AgentHandle 交给Agent处理;

5、conns.Conn接口

type Conn interface {
    ReadMsg() ([]byte, error)
    WriteMsg(args []byte) error
    LocalAddr() net.Addr
    RemoteAddr() net.Addr
    Close()
    Destroy()
    Done() chan struct{}
    GetOption() *Option
}

Agent基于conns.Conn接口实现了网络链接;

Tcp,WebSocket等链接体都实现了conns.Conn;

因此使用TCP,以及WebSocket 等不同的网络协议,对外接口都是一样的agent.Agent的;

你可能感兴趣的:(Go游戏服务器框架EASY,go,easy,go游戏框架,go网络库)