cellnet

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

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

Go to latest
Published: Apr 21, 2016 License: MIT Imports: 5 Imported by: 0

README

Cellnet

简单,方便,高效的Go语言的游戏服务器底层

特性

异步单线程多进程架构

  • 无需处理繁琐的多线程安全问题
  • 底层IO仍然使用goroutine进行处理, 保证IO吞吐率
  • 性能敏感的业务拆离为单独进程进行处理

数据协议

  • 封包类型采用Type-Length-Value的私有tcp封包, 自带序列号防御简单的封包复制
  • 消息统一使用Protobuf格式进行通信

网关

  • 基本的网关透传框架
  • 广播,列表广播
  • 可定制的消息路由规则

RPC

  • 异步远程过程调用

模块化

  • 鼓励使用统一的模块化命名及拆分方法进行隔离降偶

日志

  • 分级日志
  • 可以方便的通过日志查看收发消息(Protobuf)的每一个字段消息

第三方库依赖

  • github.com/golang/protobuf/proto
  • github.com/davyxu/golog
  • gopkg.in/mgo.v2

例子

Echo



func server() {

	pipe := cellnet.NewEventPipe()

	evq := socket.NewAcceptor(pipe).Start("127.0.0.1:7234")

	socket.RegisterSessionMessage(evq, "coredef.TestEchoACK", func(content interface{}, ses cellnet.Session) {
		msg := content.(*coredef.TestEchoACK)

		log.Debugln("server recv:", msg.String())

		ses.Send(&coredef.TestEchoACK{
			Content: msg.String,
		})

	})

	pipe.Start()

}

func client() {

	pipe := cellnet.NewEventPipe()

	evq := socket.NewConnector(pipe).Start("127.0.0.1:7234")

	socket.RegisterSessionMessage(evq, "coredef.TestEchoACK", func(content interface{}, ses cellnet.Session) {
		msg := content.(*coredef.TestEchoACK)

		log.Debugln("client recv:", msg.String())

	})

	socket.RegisterSessionMessage(evq, "coredef.SessionConnected", func(content interface{}, ses cellnet.Session) {

		ses.Send(&coredef.TestEchoACK{
			Content: "hello",
		})

	})

	pipe.Start()
}

TODO

网关

  • 处理多同名svc时的转发

MongoDB

  • DB内存映射框架
  • DB存储日志

Wiki

https://github.com/davyxu/cellnet/wiki 这里有文档和架构,设计解析

备注

感觉不错请fork和star, 谢谢!

博客: http://www.cppblog.com/sunicdavy

知乎: http://www.zhihu.com/people/xu-bo-62-87

邮箱: sunicdavy@qq.com

战魂小筑技术讨论群: 309800774 加群请说明cellnet

cellnet发问请直接@成都_黑色灵猫

Documentation

Overview

dispatcher包提供消息队列, 消息注册+派发 封装消息解包, 打包的过程

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildPacket

func BuildPacket(data interface{}) (*Packet, *MessageMeta)

消息到封包

func ParsePacket

func ParsePacket(pkt *Packet, msgType reflect.Type) (interface{}, error)

封包到消息

func RegisterMessageMeta

func RegisterMessageMeta(name string, msg proto.Message, id uint32)

注册消息元信息(代码生成专用)

func VisitMessageMeta

func VisitMessageMeta(callback func(*MessageMeta))

遍历消息元信息

Types

type Connector

type Connector interface {

	// 连接后的Session
	DefaultSession() Session

	// 自动重连间隔, 0表示不重连, 默认不重连
	SetAutoReconnectSec(sec int)
}

type EventDispatcher

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

func NewEventDispatcher

func NewEventDispatcher() *EventDispatcher

func (*EventDispatcher) Add

func (self *EventDispatcher) Add(name string, callback func(...interface{}))

func (*EventDispatcher) Clear

func (self *EventDispatcher) Clear()

func (*EventDispatcher) Invoke

func (self *EventDispatcher) Invoke(name string, args ...interface{})

func (*EventDispatcher) Remove

func (self *EventDispatcher) Remove(name string, callback func(...interface{}))

type EventPipe

type EventPipe interface {
	AddQueue() EventQueue

	Start()

	Stop(int)

	Wait() int
}

func NewEventPipe

func NewEventPipe() EventPipe

type EventQueue

type EventQueue interface {

	// 注册事件回调
	RegisterCallback(id uint32, f func(interface{}))

	// 设置事件截获钩子, 在CallData中调用钩子
	InjectData(func(interface{}) bool)

	// 投递事件, 通过队列到达消费者端
	PostData(data interface{})

	// 直接调用消费者端的handler
	CallData(data interface{})

	// 延时投递
	DelayPostData(dur time.Duration, callback func())

	// 开启并发模式, 回调被调用的线程为post方线程
	EnableConcurrenceMode(enable bool)
}

type MessageMeta

type MessageMeta struct {
	Type reflect.Type
	Name string
	ID   uint32
}

func MessageMetaByID

func MessageMetaByID(id uint32) *MessageMeta

根据id查找消息元信息

func MessageMetaByName

func MessageMetaByName(name string) *MessageMeta

根据名字查找消息元信息

func MessageMetaByType

func MessageMetaByType(rtype reflect.Type) *MessageMeta

根据类型名字查找消息元信息

type Packet

type Packet struct {
	MsgID uint32 // 消息ID
	Data  []byte
}

普通封包

func (Packet) ContextID

func (self Packet) ContextID() uint32

type Peer

type Peer interface {

	// 开启
	Start(address string) Peer

	// 关闭
	Stop()

	// 名字
	SetName(string)
	Name() string

	// 事件
	EventQueue

	// 连接管理
	SessionManager
}

type Session

type Session interface {

	// 发包
	Send(interface{})

	// 直接发送封包
	RawSend(*Packet)

	// 断开
	Close()

	// 标示ID
	ID() int64

	// 归属端
	FromPeer() Peer
}

type SessionManager

type SessionManager interface {

	// 获取一个连接
	GetSession(int64) Session

	// 遍历连接
	IterateSession(func(Session) bool)

	// 连接数量
	SessionCount() int
}

type Timer

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

func NewTimer

func NewTimer(eq EventQueue, dur time.Duration, callback func(*Timer)) *Timer

func (*Timer) Stop

func (self *Timer) Stop()

Directories

Path Synopsis
本包只做mongodb的异步操作实现 只实现常用功能 复杂操作可以使用mongodb的原始接口进行操作
本包只做mongodb的异步操作实现 只实现常用功能 复杂操作可以使用mongodb的原始接口进行操作
proto
coredef
Generated by github.com/davyxu/cellnet/protoc-gen-msg DO NOT EDIT! Source: coredef.proto Package coredef is a generated protocol buffer package.
Generated by github.com/davyxu/cellnet/protoc-gen-msg DO NOT EDIT! Source: coredef.proto Package coredef is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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