goNet

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2020 License: Apache-2.0 Imports: 12 Imported by: 0

README

goNetlogo

version

v 1.0.0

介绍

一个基于go语言开发的网络脚手架,参考cellnetbeego两大开框架的设计,使用非常方便简洁,轻松让你开发出高并发高性能的网络应用,可以用于游戏,app等任何领域的通讯。

主要特性及追求目标

  • 高并发
  • 高性能
  • 简单易用
  • 线性安全
  • 兼容性强
  • 多领域应用
  • 防崩溃
  • 错误快速定位

通讯协议支持

  • TCP
  • UDP
  • WEBSOCKET
  • QUIC
  • KCP
  • HTTP
  • RPC

数据编码格式支持

  • json
  • xml
  • binary
  • protobuf

关键技术

  • 会话池(session pool)
  • 协程池(goroutine pool)

安装教程

1. git clone到 GOPATH/src目录下

git clone https://github.com/Quantumoffices/goNet.git

使用样例参考

  • 服务端
package main

import (
	"goNet"
	_ "goNet/codec/json"
	_ "goNet/peer/tcp"
)

func main() {
	p := goNet.NewPeer("server",":8087")
	p.Start()
}

  • 客户端
package main

import (
	"goNet"
	_ "goNet/codec/json"
	_ "goNet/peer/tcp"
)

func main() {
	p := goNet.NewPeer("client", ":8087")
	p.Start()
       //todo something
}
  • 消息处理实现及注册
package msg
import (
	"goNet"
)
//消息注册
func init() {
	goNet.RegisterMessage(0, Ping{})
	goNet.RegisterMessage(1, Pong{})
}

//心跳
type Ping struct {
	TimeStamp int64 `json:"time_stamp",xml:"time_stamp"`
}
type Pong struct {
	TimeStamp int64 `json:"time_stamp",xml:"time_stamp"`
}
type SessionClose struct {
}

//消息需要实现 goNet.Message接口
func (p *Ping) Handle(session Session) {
	logrus.Infof("session_%v ping at time=%v", session.ID(), time.Unix(p.TimeStamp, 0).String())
	session.Send(Pong{TimeStamp: time.Now().Unix(),})
}
func (p *Pong) Handle(session Session) {
	logrus.Infof("session_%v pong at time=%v", session.ID(), time.Unix(p.TimeStamp, 0).String())
}

func (s *SessionClose) Handle(session Session) {
	logrus.Errorf("session_%v closed", session.ID())
}

在线游戏demo

测试

FAQ

参与贡献

QQ群:795611332

Documentation

Index

Constants

View Source
const (
	//无效会话id
	INVALID_SESSION_ID uint32 = 0
)
View Source
const (
	POOL_DEFAULT_SIZE = 1
)
View Source
const SYSTEM_CONTROLLER_IDX = 0

Variables

View Source
var Log = logrus.New()
View Source
var Opts = &Options{}
View Source
var (
	SessionManager = newSessionManager()
)

Functions

func GetMsgBelongToControllerIdx

func GetMsgBelongToControllerIdx(msgIndex int) int

func GetMsgByIdx

func GetMsgByIdx(msgIndex int) interface{}

func GetMsgIdxByType

func GetMsgIdxByType(t reflect.Type) int

func NewLogrusHook

func NewLogrusHook(field string, levels ...logrus.Level) logrus.Hook

func RegisterMsg

func RegisterMsg(msgIndex, controllerIndex int, msg interface{})

注册消息

func RegisterPeer

func RegisterPeer(peer Peer)

func SubmitMsgToAntsPool

func SubmitMsgToAntsPool(c Controller, s Session, msg interface{})

提交到协程池处理消息

func UpdateSystemController

func UpdateSystemController(c Controller)

替换系统控制器

Types

type Controller

type Controller interface {
	OnMsg(session Session, msg interface{})
}

控制器

type Option

type Option func(opts *Options)

Option represents the optional function.

func WithAddr

func WithAddr(addr string) Option

set addr

func WithOptions

func WithOptions(options Options) Option

WithOptions accepts the whole options config.

func WithPanicHandler

func WithPanicHandler(panicHandler func(interface{})) Option

WithPanicHandler sets up panic handler.

func WithPeerType

func WithPeerType(peerType PeerType) Option

set peer type

func WithReadDeadline

func WithReadDeadline(dur time.Duration) Option

set read duration

func WithRoutinePoolSize

func WithRoutinePoolSize(size int) Option

bind addr

func WithWriteDeadline

func WithWriteDeadline(dur time.Duration) Option

set write duration

type Options

type Options struct {
	//listen or dial addr
	Addr string
	//peer type
	PeerType PeerType
	//SetWriteDeadline sets the write deadline or read deadline on the underlying connection.
	ReadDeadline, WriteDeadline time.Duration
	//set the routine pool size
	//0.mean use default set
	PoolSize int
	// PanicHandler is used to handle panics from each worker goroutine.
	PanicHandler func(interface{})
}

type Peer

type Peer interface {
	// 开启服务
	Start()

	// 停止服务
	Stop()
}

func NewPeer

func NewPeer(opts ...Option) Peer

type PeerIdentify

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

端属性

func (*PeerIdentify) Addr

func (p *PeerIdentify) Addr() string

func (*PeerIdentify) SetAddr

func (p *PeerIdentify) SetAddr(addr string)

func (*PeerIdentify) SetType

func (p *PeerIdentify) SetType(t PeerType)

func (*PeerIdentify) Type

func (p *PeerIdentify) Type() PeerType

type PeerType

type PeerType string

端类型

const (
	PEERTYPE_SERVER PeerType = "server" //服务端
	PEERTYPE_CLIENT PeerType = "client" //客户端
)

type Ping

type Ping struct {
	TimeStamp int64 `json:"time_stamp",xml:"time_stamp"`
}

心跳

type Pong

type Pong struct {
	TimeStamp int64 `json:"time_stamp",xml:"time_stamp"`
}

type ProtoCol

type ProtoCol string

通讯协议

const (
	TCP  ProtoCol = "tcp"
	KCP  ProtoCol = "kcp"
	UDP  ProtoCol = "udp"
	WS   ProtoCol = "websocket"
	HTTP ProtoCol = "http"
	QUIC ProtoCol = "quic"
	RPC  ProtoCol = "rpc"
)

type Session

type Session interface {
	//原始套接字
	Socket() interface{}

	// 发送消息,消息需要以指针格式传入
	Send(msg interface{})

	// 断开
	Close()

	// ID
	ID() uint32

	//数据存储
	Value(v ...interface{}) interface{}
}

会话

type SessionClose

type SessionClose struct {
}

type SessionConnect

type SessionConnect struct {
}

会话

type SessionController

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

func (*SessionController) AddController

func (s *SessionController) AddController(index int, c Controller)

func (*SessionController) GetController

func (s *SessionController) GetController(index int) (Controller, error)

type SessionIdentify

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

核心会话标志

func (*SessionIdentify) ID

func (s *SessionIdentify) ID() uint32

func (*SessionIdentify) SetID

func (s *SessionIdentify) SetID(id uint32)

type SystemController

type SystemController struct {
}

系统控制模块

func (*SystemController) OnMsg

func (*SystemController) OnMsg(session Session, msg interface{})

Directories

Path Synopsis
xml
demo
tcp/client command
tcp/server command
udp/client command
udp/server command
ws/client command
ws/server command
peer
tcp
udp
ws

Jump to

Keyboard shortcuts

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