conn

package
v0.0.0-...-c7e9c76 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2020 License: MIT Imports: 6 Imported by: 1

Documentation

Overview

conn 封装过的连接 可以直接读出帧

帧分成三个部分 分别是len、opcode、payload

len是payload的长度 有baseLen和extendLen两种 如果len小于255 占一个byte 如果length的长度大于等于255 先1byte的255 然后用2byte来表示长度 最大2byte

opcode占1个byte 具体有哪些看下面的常量

不管payload是什么类型的数据 这里只处理读写数据

Index

Constants

View Source
const (
	// 第一个length的最大值
	BaseLenMax = 255
	// 上述变量的byte
	BaseLenMaxByte = byte(BaseLenMax)

	// length的最大值
	ExtendLenMax = 255 * 255

	// opcode
	// 注册服务
	OpSign = 1
	// 请求
	OpRequest = 2
	// 响应
	OpResponse = 3
	// 错误
	OpError = 4
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Conn

type Conn interface {
	ReadData() (opcode uint8, data []byte, err error)

	WriteSign(interface{}) error
	WriteRequest(interface{}) error
	WriteResponse(interface{}) error
	WriteError(interface{}) error
	Close() error
	Addr() string
	// contains filtered or unexported methods
}

Conn 封装的tcp连接的接口 现在实现的是一个普通的连接 后面或许会上连接池

func New

func New(conn net.Conn) (res Conn)

type DefaultConn

type DefaultConn struct {
	// contains filtered or unexported fields
}

DefaultConn 默认的不带连接池的连接

func (*DefaultConn) Addr

func (c *DefaultConn) Addr() (res string)

Addr 拿到连接的远程地址

func (*DefaultConn) Close

func (c *DefaultConn) Close() error

Close 关闭连接

func (*DefaultConn) ReadData

func (c *DefaultConn) ReadData() (opcode uint8, data []byte, err error)

ReadData 读一个包 返回opcode和json数据

func (*DefaultConn) WriteData

func (c *DefaultConn) WriteData(opcode int, in interface{}) (err error)

WriteData 写数据进去

func (*DefaultConn) WriteError

func (c *DefaultConn) WriteError(data interface{}) (err error)

WriteError 写一个错误帧

func (*DefaultConn) WriteRequest

func (c *DefaultConn) WriteRequest(data interface{}) (err error)

WriteRequest 写一个请求帧

func (*DefaultConn) WriteResponse

func (c *DefaultConn) WriteResponse(data interface{}) (err error)

WriteResponse 写一个响应帧

func (*DefaultConn) WriteSign

func (c *DefaultConn) WriteSign(data interface{}) (err error)

WriteSign 写一个注册帧

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL