znets

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2022 License: MIT Imports: 19 Imported by: 0

README

znets

基于Golang简单轻量级TCP并发服务器框架


Example

package main

import "github.com/zhlin160/znets"

func main() {
	//srv := znets.NewServer()
	srv := znets.NewServerWithOptions(&znets.Options{
		IP:       "127.0.0.1",
		Port:     9898,
		WorkPool: 15,
	})

	srv.SetEventHandle(&Event{})
	srv.SetProtoPack(NewMessagePack())

	srv.Run()
}

Documentation

Index

Constants

View Source
const (
	GRACEFUL_ENVIRON_KEY    = "IS_GRACEFUL"
	GRACEFUL_ENVIRON_STRING = GRACEFUL_ENVIRON_KEY + "=1"

	DEFAULT_READ_TIMEOUT  = 60 * time.Second
	DEFAULT_WRITE_TIMEOUT = DEFAULT_READ_TIMEOUT
)
View Source
const (
	RED = uint8(iota + 91)
	GREEN
	YELLOW
	BLUE
	MAGENTA

	INFO    = "[INFO]"
	TRAC    = "[TRAC]"
	ERROR   = "[ERROR]"
	WARN    = "[WARNING]"
	SUCCESS = "[SUCCESS]"
)
View Source
const (
	DEV        = "dev"
	PRODUCTION = "production"
)

Variables

This section is empty.

Functions

func AddressToClientId

func AddressToClientId(connection IConnection) string

连接端封装程clientId

func CloseClient

func CloseClient(request IRequest, clientId string, data []byte) error

踢掉一个连接并发送消息

func GbToUtf8 added in v1.0.2

func GbToUtf8(s []byte) ([]byte, error)

func IsOnLine

func IsOnLine(request IRequest, clientId string) bool

是否有连接记录,是否在线

func SendToClient

func SendToClient(request IRequest, clientId string, data []byte) error

给给定的客户端发送消息

func Utf8ToGb added in v1.0.2

func Utf8ToGb(s []byte) ([]byte, error)

Types

type Connection

type Connection struct {
	//连接的套接字
	Conn *net.TCPConn
	//连接的ID
	ConnID uint32

	//写通道退出状态的channel
	ExitChan chan bool
	//当前连接的处理方法
	Handles IHandler
	// contains filtered or unexported fields
}

func (*Connection) DelProperty

func (c *Connection) DelProperty(key string)

func (*Connection) GetConn

func (c *Connection) GetConn() *net.TCPConn

获取当前连接绑定的conn

func (*Connection) GetID

func (c *Connection) GetID() uint32

获取当前连接的id

func (*Connection) GetProperty

func (c *Connection) GetProperty(key string) (interface{}, error)

func (*Connection) GetServer

func (c *Connection) GetServer() IServer

获取主sever

func (*Connection) RemoteAddr

func (c *Connection) RemoteAddr() net.Addr

获取远程客户端信息

func (*Connection) Send

func (c *Connection) Send(data []byte) error

发送数据

func (*Connection) SetPackProto

func (c *Connection) SetPackProto(proto IPack)

func (*Connection) SetProperty

func (c *Connection) SetProperty(key string, val interface{})

添加当前连接额外kye-value

func (*Connection) SetProtoPack

func (c *Connection) SetProtoPack(proto IPack)

func (*Connection) Start

func (c *Connection) Start()

启动连接

func (*Connection) StartReader

func (c *Connection) StartReader()

收到数据处理

func (*Connection) StartWriter

func (c *Connection) StartWriter()

发送数据处理

func (*Connection) Stop

func (c *Connection) Stop()

关闭连接

type HLog

type HLog struct {
	// contains filtered or unexported fields
}
var Log *HLog

func NewLog

func NewLog() *HLog

func NewLogWithModel

func NewLogWithModel(model string) *HLog

func (*HLog) Error

func (l *HLog) Error(format string, args ...interface{})

func (*HLog) Info

func (l *HLog) Info(format string, args ...interface{})

func (*HLog) Success

func (l *HLog) Success(format string, args ...interface{})

func (*HLog) Trace

func (l *HLog) Trace(format string, args ...interface{})

func (*HLog) Warning

func (l *HLog) Warning(format string, args ...interface{})

type HandleFunc

type HandleFunc func(*net.TCPConn, []byte, int) error

type Handler

type Handler struct {
	Middlewares []HandlerFunc //中间件集合
	// contains filtered or unexported fields
}

func NewHandler

func NewHandler() *Handler

func (*Handler) Abort

func (h *Handler) Abort()

终断请求

func (*Handler) After

func (h *Handler) After(rf HandlerFunc)

设置后置处理钩子

func (*Handler) Before

func (h *Handler) Before(rf HandlerFunc)

设置前置处理钩子

func (*Handler) RunHandler

func (h *Handler) RunHandler(request IRequest)

开始处理请求

func (*Handler) RunWorkPool

func (h *Handler) RunWorkPool()

启动工作池

func (*Handler) SendToTasks

func (h *Handler) SendToTasks(rq IRequest)

轮询获取工作池处理任务

func (*Handler) SetEventHandle

func (h *Handler) SetEventHandle(event IEvent)

设置事件处理类

func (*Handler) SetWorkPoolSize

func (h *Handler) SetWorkPoolSize(size uint32)

设置工作池数量

func (*Handler) Use

func (h *Handler) Use(rf HandlerFunc)

设置中间件

type HandlerFunc

type HandlerFunc func(request IRequest)

type IConnection

type IConnection interface {
	Start()
	Stop()
	GetConn() *net.TCPConn
	GetID() uint32
	RemoteAddr() net.Addr
	Send(data []byte) error
	SetProperty(key string, val interface{})
	GetProperty(key string) (interface{}, error)
	DelProperty(key string)

	SetProtoPack(IPack)
	GetServer() IServer
}

func NewConnection

func NewConnection(server IServer, conn *net.TCPConn, id uint32, handler IHandler, wg *sync.WaitGroup) IConnection

type IEvent

type IEvent interface {
	OnMessage(IRequest)
	OnConnect(IConnection, string) //连接对象, clientId
	OnClose(IConnection, string)
	OnWorkerStart()
}

type IHandler

type IHandler interface {
	RunHandler(request IRequest)
	Before(HandlerFunc)
	After(HandlerFunc)
	Use(HandlerFunc)
	Abort()
	RunWorkPool()
	SendToTasks(rq IRequest)
	SetWorkPoolSize(size uint32)

	SetEventHandle(IEvent)
}

type IManager

type IManager interface {
	Add(con IConnection)
	Del(con IConnection)
	Get(id uint32) (IConnection, error)
	Num() int
	Clear()
}

func NewManager

func NewManager() IManager

type IMessage

type IMessage interface {
	GetData() []byte
	GetId() uint32

	SetData([]byte)
	SetId(uint32)

	SetLen(uint32)
	GetLen() uint32
}

type IPack

type IPack interface {
	Input(string) int
	Pack([]byte) []byte
	UnPack([]byte) []byte
}

type IRequest

type IRequest interface {
	GetConnection() IConnection
	GetData() []byte

	GetID() uint32
	GetLen() uint32

	SetWorkId(uint32)

	GetWorkId() uint32

	//获取客户端连接id,封装的地址及连接信息字符串
	GetClientId() string
	// contains filtered or unexported methods
}

func NewRequest

func NewRequest(con IConnection, msg IMessage, rid *uint32, clientId string) IRequest

实例化,rid:全局请求id

type IServer

type IServer interface {
	Run()

	Stop()

	Before(HandlerFunc)

	After(HandlerFunc)
	Use(HandlerFunc)
	Abort()

	SetWorkPoolSize(uint32)
	GetRid() *uint32

	GetManager() IManager
	OverLoad(overloadHandler)
	SetMaxCon(uint32)

	OnStart(hookHandler)

	OnStop(hookHandler)

	SetEventHandle(IEvent)
	// contains filtered or unexported methods
}

type Listener added in v1.0.2

type Listener struct {
	*net.TCPListener
	// contains filtered or unexported fields
}

func ListenTCP added in v1.0.2

func ListenTCP(nett string, laddr *net.TCPAddr, server *Server) (*Listener, error)

func NewListener added in v1.0.2

func NewListener(listener *net.TCPListener, server *Server) *Listener

func (*Listener) Accept added in v1.0.2

func (l *Listener) Accept() (*net.TCPConn, error)

func (*Listener) GetFd added in v1.0.2

func (l *Listener) GetFd() (uintptr, error)

func (*Listener) GetWg added in v1.0.2

func (l *Listener) GetWg() *sync.WaitGroup

func (*Listener) Wait added in v1.0.2

func (l *Listener) Wait()

type Manager

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

func (*Manager) Add

func (m *Manager) Add(con IConnection)

添加连接

func (*Manager) Clear

func (m *Manager) Clear()

关闭所有连接

func (*Manager) Del

func (m *Manager) Del(con IConnection)

删除连接

func (*Manager) Get

func (m *Manager) Get(id uint32) (IConnection, error)

获取连接

func (*Manager) Num

func (m *Manager) Num() int

获取连接数量

type Message

type Message struct {
	Id     uint32
	Length uint32
	Data   []byte
}

func (*Message) GetData

func (msg *Message) GetData() []byte

func (*Message) GetId

func (msg *Message) GetId() uint32

func (*Message) GetLen

func (msg *Message) GetLen() uint32

func (*Message) SetData

func (msg *Message) SetData(data []byte)

func (*Message) SetId

func (msg *Message) SetId(id uint32)

func (*Message) SetLen

func (msg *Message) SetLen(len uint32)

type Options

type Options struct {
	IP             string
	Port           int
	MaxConnections uint32
	Model          string //运行模式 dev|production
	MaxConnNum     uint32
	WorkPool       uint32
	PidFilePath    string //pid保存路径
}

type Request

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

func (*Request) GetClientId

func (r *Request) GetClientId() string

获取客户端连接封装id

func (*Request) GetConnection

func (r *Request) GetConnection() IConnection

获取当前连接

func (*Request) GetData

func (r *Request) GetData() []byte

获取消息数据

func (*Request) GetID

func (r *Request) GetID() uint32

获取ID

func (*Request) GetLen

func (r *Request) GetLen() uint32

获取数据长度

func (*Request) GetWorkId

func (r *Request) GetWorkId() uint32

获取工作池workId

func (*Request) SetWorkId

func (r *Request) SetWorkId(workId uint32)

设置工作池workId

type Server

type Server struct {
	IP        string
	Port      int
	Conn      *Listener
	IPVersion string
	Handles   *Handler
	Version   string
	// contains filtered or unexported fields
}

func NewServer

func NewServer() *Server

通过配置文件构建默认server

func NewServerWithOptions

func NewServerWithOptions(options *Options) *Server

使用Options字段构建server

func (*Server) Abort

func (s *Server) Abort()

终断请求

func (*Server) After

func (s *Server) After(rf HandlerFunc)

设置后置处理钩子

func (*Server) Before

func (s *Server) Before(rf HandlerFunc)

设置前置处理钩子

func (Server) GetConfig

func (s Server) GetConfig() *viper.Viper

返回配置对象

func (*Server) GetManager

func (s *Server) GetManager() IManager

func (*Server) GetRid

func (s *Server) GetRid() *uint32

func (*Server) OnStart

func (s *Server) OnStart(hook hookHandler)

连接创建hook

func (*Server) OnStop

func (s *Server) OnStop(c hookHandler)

连接断开hook

func (*Server) OverLoad

func (s *Server) OverLoad(o overloadHandler)

func (*Server) Run

func (s *Server) Run()

运行服务器

func (*Server) SetConfig

func (s *Server) SetConfig(conf *viper.Viper)

func (*Server) SetEventHandle

func (s *Server) SetEventHandle(event IEvent)

func (*Server) SetMaxCon

func (s *Server) SetMaxCon(size uint32)

func (*Server) SetProtoPack

func (s *Server) SetProtoPack(proto IPack)

func (*Server) SetWorkPoolSize

func (s *Server) SetWorkPoolSize(size uint32)

设置工作池大小

func (*Server) Stop

func (s *Server) Stop()

停止服务器

func (*Server) Use

func (s *Server) Use(rf HandlerFunc)

添加全局中间件

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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