10-zinx基于Golang-链接属性配置

前言

  • 当我们在使⽤链接处理的时候,希望和链接绑定⼀些⽤户的数据,或者参数。那么我们现在可以把当前
    链接设定⼀些传递参数的接⼝或者⽅法

一、实现思路

在这里插入图片描述

二、链接添加链接配置接⼝

  • zinx/ziface/iconnection.go
package ziface

import "net"

//定义链接模块的抽象层
type IConneciton interface {
	//启动链接 让当前的链接准备开始工作
	Start()

	//停止链接 结束当前链接的工作
	Stop()

	//获取当前链接的绑定socket conn
	GetTCPConnection() *net.TCPConn

	//获取当前链接模块的链接ID
	GetConnID() uint32

	//获取远程客户端的 TCP状态 IP port
	RemoteAddr() net.Addr

	//发送数据, 将数据发送给远程的客户端
	SendMsg(msgId uint32, data []byte) error

	//设置链接属性
	SetProperty(key string, value interface{})

	//获取链接属性
	GetProperty(key string) (interface{}, error)

	//移除链接属性
	RemoveProperty(key string)
}

//定义一个处理链接业务的方法
type HandleFunc func(*net.TCPConn, []byte, int) error

三、链接实现配置接口

  • zinx/znet/connection.go
/*
  链接模块
*/
type Connection struct {
	//当前Conn隶属于哪个Server
	TcpServer ziface.IServer
	//当前链接的socket TCP套接字
	Conn *net.TCPConn

	//链接的ID
	ConnID uint32

	//当前的链接状态
	isClosed bool

	//告知当前链接已经退出的/停止 channel(由Reader告知Writer退出)
	ExitChan chan bool

	//无缓冲的管道,用于读、写Goroutine之间的消息通信
	msgChan chan []byte

	//消息的管理MsgID 和对应的处理业务API关系
	MsgHandler ziface.IMsgHandle

	//链接属性集合
	property map[string]interface{}

	//保护链接属性的锁
	propertyLock sync.RWMutex
}

//初始化链接模块的方法
func NewConnection(server ziface.IServer, conn *net.TCPConn, connID uint32, msgHandler ziface.IMsgHandle) *Connection {
	c := &Connection{
		TcpServer:  server,
		Conn:       conn,
		ConnID:     connID,
		MsgHandler: msgHandler,
		isClosed:   false,
		msgChan:    make(chan []byte),
		ExitChan:   make(chan bool, 1),
		property:   make(map[string]interface{}),
	}

	//将conn加入到ConnMananger中
	c.TcpServer.GetConnMgr().Add(c)

	return c
}

//设置链接属性
func (c *Connection) SetProperty(key string, value interface{}) {
	c.propertyLock.Lock()
	defer c.propertyLock.Unlock()

	//添加一个链接属性
	c.property[key] = value
}

//获取链接属性
func (c *Connection) GetProperty(key string) (interface{}, error) {
	c.propertyLock.RLock()
	defer c.propertyLock.RUnlock()

	//读取属性
	if value, ok := c.property[key]; ok {
		return value, nil
	} else {
		return nil, errors.New("no property found")
	}
}

//移除链接属性
func (c *Connection) RemoveProperty(key string) {
	c.propertyLock.Lock()
	defer c.propertyLock.Unlock()

	//删除属性
	delete(c.property, key)
}

四、测试链接属性配置

  • myDemo/zinxV1.0/Server.go:测试的时候在创建链接之后设置属性,在链接断开之前读取属性
//创建链接之后执行钩子函数
func DoConnectionBegin(conn ziface.IConneciton) {
	fmt.Println("===> DoConnectionBegin is Called ... ")
	if err := conn.SendMsg(202, []byte("DoConnection BEGIN")); err != nil {
		fmt.Println(err)
	}

	//给当前的链接设置一些属性
	fmt.Println("Set conn property....")
	conn.SetProperty("Name", "刘丹冰-Aceld-IT无崖子")
	conn.SetProperty("GitHub", "https://github.com/aceld")
	conn.SetProperty("Home", "https://legacy.gitbook.com/@aceld")
	conn.SetProperty("Blog", "https://www.jianshu.com/u/35261429b7f1")
}

//链接断开之前的需要执行的函数
func DoConnectionLost(conn ziface.IConneciton) {
	fmt.Println("===> DoConnectionLost is Called ...")
	fmt.Println("conn ID = ", conn.GetConnID(), " is Lost...")

	//获取链接属性
	if name, err := conn.GetProperty("Name"); err == nil {
		fmt.Println("Name = ", name)
	}
	if home, err := conn.GetProperty("Home"); err == nil {
		fmt.Println("Home = ", home)
	}
	if github, err := conn.GetProperty("GitHub"); err == nil {
		fmt.Println("GitHub = ", github)
	}
	if blog, err := conn.GetProperty("Blog"); err == nil {
		fmt.Println("Blog = ", blog)
	}
}

在这里插入图片描述

五、目录结构

六、完整源码

下载zinxV1.0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无休止符

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值