goNet

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: May 29, 2020 License: Apache-2.0 Imports: 8 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 (
	POOL_DEFAULT_SIZE = 1
)
View Source
const SYSTEM_CONTROLLER_IDX = 0

Variables

This section is empty.

Functions

func Broadcast added in v1.0.5

func Broadcast(msg interface{})

广播

func FindMsg added in v1.0.7

func FindMsg(msgID int) interface{}

func FindMsgIDByType added in v1.0.7

func FindMsgIDByType(t reflect.Type) int

func GetMsgBelongToControllerIdx

func GetMsgBelongToControllerIdx(msgIndex int) int

func RecycleSession added in v1.0.5

func RecycleSession(ses Session)

func RegisterMsg

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

注册消息

func RegisterPeer

func RegisterPeer(peer Peer)

func RegisterSessionType added in v1.0.3

func RegisterSessionType(ses interface{})

func SessionCount added in v1.0.5

func SessionCount() int

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 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{})
	//Maximum number of connections allowed
	//0.mean no limit
	AllowMaxConn int
}

options

type Peer

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

	// 停止服务
	Stop()
}

func NewPeer

func NewPeer(opts Options) Peer

type PeerIdentify

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

端属性

func (*PeerIdentify) Addr

func (p *PeerIdentify) Addr() string

func (*PeerIdentify) Options added in v1.0.3

func (p *PeerIdentify) Options(o Options)

func (*PeerIdentify) SetAddr

func (p *PeerIdentify) SetAddr(addr string)

func (*PeerIdentify) SetOptions added in v1.0.3

func (p *PeerIdentify) SetOptions(o Options)

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 b
	ID() uint64

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

	//加入或者更新消息控制模块
	JoinOrUpdateController(index int, c Controller)
}

会话

func AddSession added in v1.0.5

func AddSession() Session

func FindSession added in v1.0.5

func FindSession(id uint64) (Session, bool)

type SessionClose

type SessionClose struct {
}

type SessionConnect

type SessionConnect struct {
}

会话

type SessionController

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

消息路由

func (*SessionController) GetController

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

func (*SessionController) JoinOrUpdateController added in v1.0.7

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

type SessionIdentify

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

核心会话标志

func (*SessionIdentify) ID

func (s *SessionIdentify) ID() uint64

type SessionStore added in v1.0.3

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

存储功能

func (*SessionStore) Value added in v1.0.3

func (s *SessionStore) Value(v ...interface{}) interface{}

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