starGo

package module
v0.0.0-...-f33cab8 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2019 License: Apache-2.0 Imports: 31 Imported by: 0

README

starGo

starGo是一款高性能、分布式、轻量级、微服务的游戏服务器框架。框架采用了go语言开发,得益于go本身对高并发的强大支持,框架足够简洁,效率足够高效。服务器框架二次开发简单易上手,实现了高性能的异步网络库,分布式节点间的通信采用了高性能通信中间件nats,能够实现每秒20万的Qps,日志管理,常规的关系型数据库(mysql)和非关系型数据库redis的支持,goroutine的安全定时器工具等。

服务器框架可用于包括但不限于游戏服务器等的应用,可以在框架开发阶段上节省大量时间。同时可以通过nats的发布订阅通道进行服务器的热更新等操作.

优势特点
1) 开发效率高
2) 支持自定义的通信协议
3) 采用nats高性能通信中间,实现了异步发布订阅,请求响应等模式的通信请求
4) 分布式、微服务的架构,方便横向拓展
5) 协程安全的定时器实现
6) 对协程安全封装,优雅的实现开启退出
7) 内置redis、mysql数据库支持
安装教程

由于使用了nats做为通信中间件,所以需要安装nats服务器。使用docker安装非常的方便,简洁.

docker pull nats:latest
docker run -p 4222:4222 -p 8222:8222 -p 6222:6222 -ti nats:latest

然后安装依赖项,golang 1.13版本提供非常方便的依赖项管理工具go mod,你只需要输入go mod tidy,便可非常方便快捷的倒入依赖项。

当然你也可通过go get来手动导入依赖包

go get github.com/go-redis/redis
go get github.com/jinzhu/gorm
go get github.com/nats-io/nats.go
go get github.com/satori/go.uuid
go get github.com/zhnxin/csvreader
使用说明

最简单的一个例子:

import (
	"gitee.com/tuanzijialianmeng/star_go"
)

func main() {
	// 开启日志
	starGo.StartLog("log", starGo.Debug)

	// 启动服务器
	starGo.Start()

	// 开启tcp连接
	err := starGo.StartTcpServer("127.0.0.1:9999", nil, 4)
	if err != nil {
		starGo.ErrorLog(err)
		return
	}

	// 开启nats连接
	starGo.StartNatConn("127.0.0.1:4222")

	// 等待系统退出
	starGo.WaitForSystemExit()
}
写在最后

我的想法是打造一款高性能,分布式,微服务,轻量级的游戏服务器框架初次写开源框架,肯定有很多不足和考虑不到的地方,如果有什么建议和意见欢迎加群提出。

starGo 游戏服务器框架交流群 937191977

Documentation

Index

Constants

View Source
const (
	Debug logLv = iota //调试信息
	Info               //资讯讯息
	Warn               //警告状况发生
	Error              //一般错误,可能导致功能不正常
	Fatal              //严重错误,会导致进程退出
)

Variables

This section is empty.

Functions

func BytesToInt32

func BytesToInt32(data []byte, bigEndian bool) int32

func Daemon

func Daemon(skip ...string)

func DebugLog

func DebugLog(v ...interface{})

func ErrorLog

func ErrorLog(v ...interface{})

func FatalLog

func FatalLog(v ...interface{})

func GetNewLineString

func GetNewLineString() string

根据不同平台获取换行符

func GetNewUUID

func GetNewUUID() string

获取新的UUID字符串

func GetRandomString

func GetRandomString(l int) string

获取随机长度的字符串

func Go

func Go(f func(Stop chan struct{}))

func InfoLog

func InfoLog(v ...interface{})

func Int

func Int() int

func Int32

func Int32() int32

func Int32ToBytes

func Int32ToBytes(n int32, bigEndian bool) []byte

func Int32n

func Int32n(n int32) int32

func Int64

func Int64() int64

func Int64n

func Int64n(n int64) int64

func IntN

func IntN(n int) int

func IsDirExists

func IsDirExists(path string) bool

文件夹是否存在(obsolete)

func IsEmpty

func IsEmpty(content string) bool

检查一个字符串是否是空字符串

func IsFileExists

func IsFileExists(path string) (bool, error)

文件是否存在

func IsUUIDEmpty

func IsUUIDEmpty(uuid string) bool

判断UUID是否为空

func IsUUIDEqual

func IsUUIDEqual(uuid1, uuid2 string) bool

比较UUID是否相等

func Md5Bytes

func Md5Bytes(b []byte, ifUpper bool) string

对字符数组进行MD5加密,并且可以选择返回大、小写

func Md5String

func Md5String(s string, ifUpper bool) string

对字符串进行MD5加密,并且可以选择返回大、小写

func Publish

func Publish(channel string, data []byte)

发布消息

func ReadJsonFile

func ReadJsonFile(path string, v interface{}) error

读取json文件

func RegisterFiveMinuteFunc

func RegisterFiveMinuteFunc(funcName string, f timerFunc)

func RegisterOneMinuteFunc

func RegisterOneMinuteFunc(funcName string, f timerFunc)

func RegisterSystemExitFunc

func RegisterSystemExitFunc(f func())

func RegisterSystemReloadFunc

func RegisterSystemReloadFunc(f func())

func RegisterTcpClient

func RegisterTcpClient(c *Client)

func RegisterThirtyMinuteFunc

func RegisterThirtyMinuteFunc(funcName string, f timerFunc)

func RegisterWebSocketClient

func RegisterWebSocketClient(c *WebSocketClient)

func Shuffle

func Shuffle(n int, swap func(i, j int))

func SimpleTack

func SimpleTack() string

func Stack

func Stack()

func StartLog

func StartLog(dirPatch string, lv logLv)

func StartNatConn

func StartNatConn(addr string)

func StartTcpServer

func StartTcpServer(addr string, handler ClientCallBack, clientExpireHandler ClientExpireCallBack, headerLen int32) error

func StartUdpServer

func StartUdpServer(addr string, handler ClientCallBack, headerLen int32) error

func StartWebSocketServer

func StartWebSocketServer(addr string, url string, handler ClientCallBack, clientExpireHandler ClientExpireCallBack, headerLen int32) error

启动服务器

func SubscribeAsync

func SubscribeAsync(channel string, cb NatCallBack)

异步模式订阅

func SubscribeChannel

func SubscribeChannel(channel string, channelCount int32, cb NatCallBack)

管道模式订阅

func SubscribeQueue

func SubscribeQueue(channel, queue string, cb NatCallBack)

队列模式订阅

func ToDate

func ToDate(timeVal string) (time.Time, error)

转换成时间格式

func ToDateString

func ToDateString(timeVal time.Time) string

转换成日期字符串

func ToDateTime

func ToDateTime(timeVal string) (time.Time, error)

转换成日期格式

func ToDateTimeString

func ToDateTimeString(timeVal time.Time) string

转换成时间字符串

func Try

func Try(f func(), handler func(interface{}))

func Uint32

func Uint32() uint32

func Uint64

func Uint64() uint64

func WaitForSystemExit

func WaitForSystemExit()

func WarnLog

func WarnLog(v ...interface{})

Types

type Client

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

func GetTcpClient

func GetTcpClient(addr string) *Client

func (*Client) AppendReceiveQueue

func (c *Client) AppendReceiveQueue(message []byte)

func (*Client) AppendSendQueue

func (c *Client) AppendSendQueue(message []byte)

func (*Client) GetActiveTime

func (c *Client) GetActiveTime() int64

func (*Client) GetConn

func (c *Client) GetConn() net.Conn

func (*Client) GetReceiveData

func (c *Client) GetReceiveData(headerLen int32) (message []byte, exists bool)

func (*Client) GetStop

func (c *Client) GetStop() bool

func (*Client) SetActiveTime

func (c *Client) SetActiveTime()

func (*Client) SetStop

func (c *Client) SetStop()

type ClientCallBack

type ClientCallBack func(message []byte, addr string)

type ClientExpireCallBack

type ClientExpireCallBack func(addr []string)

type Csv

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

func NewCsvReader

func NewCsvReader() *Csv

func (*Csv) UnMarshalFile

func (c *Csv) UnMarshalFile(file string, data interface{})

func (*Csv) UnMarshalFileWithHeader

func (c *Csv) UnMarshalFileWithHeader(file string, data interface{}, header []string)

type Mysql

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

func NewMysql

func NewMysql(connection string) *Mysql

func (*Mysql) GetConnectionStr

func (m *Mysql) GetConnectionStr() string

func (*Mysql) GetDb

func (m *Mysql) GetDb() *gorm.DB

func (*Mysql) RegisterTableModel

func (m *Mysql) RegisterTableModel(model interface{}) error

func (*Mysql) RegisterTableModelForTableName

func (m *Mysql) RegisterTableModelForTableName(tableName string, model interface{}) error

type NatCallBack

type NatCallBack func(message []byte)

type Redis

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

func NewRedis

func NewRedis(addr, pwd string, db int) *Redis

func (*Redis) GetConnection

func (r *Redis) GetConnection() *redis.Client

func (*Redis) HDel

func (r *Redis) HDel(key string, field ...string) int64

func (*Redis) HExists

func (r *Redis) HExists(key, field string) bool

func (*Redis) HGet

func (r *Redis) HGet(key, field string) string

func (*Redis) HGetAll

func (r *Redis) HGetAll(key string) map[string]string

func (*Redis) HIncrBy

func (r *Redis) HIncrBy(key, field string, incr int64) int64

func (*Redis) HIncrByFloat

func (r *Redis) HIncrByFloat(key, field string, incr float64) float64

func (*Redis) HKeys

func (r *Redis) HKeys(key string) []string

func (*Redis) HLen

func (r *Redis) HLen(key string) int64

func (*Redis) HMGet

func (r *Redis) HMGet(key string, field ...string) []interface{}

func (*Redis) HMSet

func (r *Redis) HMSet(key string, field map[string]interface{}) string

func (*Redis) HSet

func (r *Redis) HSet(key, field string, value interface{}) bool

func (*Redis) HSetNX

func (r *Redis) HSetNX(key string, field string, value interface{}) bool

func (*Redis) HVals

func (r *Redis) HVals(key string) []string

type UdpClient

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

func GetUdpClient

func GetUdpClient(addr string) *UdpClient

func (*UdpClient) GetAddr

func (c *UdpClient) GetAddr() *net.UDPAddr

func (*UdpClient) GetReceiveData

func (c *UdpClient) GetReceiveData(headerLen int32, data []byte) (message []byte, exists bool)

func (*UdpClient) GetStop

func (c *UdpClient) GetStop() bool

func (*UdpClient) SetSendQueue

func (c *UdpClient) SetSendQueue(data []byte)

func (*UdpClient) SetStop

func (c *UdpClient) SetStop()

type WebSocketClient

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

func (*WebSocketClient) AppendSendQueue

func (c *WebSocketClient) AppendSendQueue(message []byte)

func (*WebSocketClient) GetActiveTime

func (c *WebSocketClient) GetActiveTime() int64

func (*WebSocketClient) GetConn

func (c *WebSocketClient) GetConn() *websocket.Conn

func (*WebSocketClient) GetReceiveData

func (c *WebSocketClient) GetReceiveData(headerLen int32, data []byte) (message []byte, exists bool)

func (*WebSocketClient) GetStop

func (c *WebSocketClient) GetStop() bool

func (*WebSocketClient) SetActiveTime

func (c *WebSocketClient) SetActiveTime()

func (*WebSocketClient) SetStop

func (c *WebSocketClient) SetStop()

Jump to

Keyboard shortcuts

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