goNet

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 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 (
	Default_Handler_Count = 10
)
View Source
const System_Route_ID = 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 HandleEvent added in v1.1.0

func HandleEvent(e Event)

处理事件

func NewSessionManager added in v1.1.0

func NewSessionManager() *sessionManager

func RecycleSession added in v1.0.5

func RecycleSession(s 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 TuneHandlerCount added in v1.1.1

func TuneHandlerCount(count int)

调整处理者的数量

func UpdateSysRoute added in v1.1.0

func UpdateSysRoute(c Route)

Types

type Event added in v1.1.0

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

事件

func CreateEvent added in v1.1.0

func CreateEvent(s Session, c Route, msg interface{}) Event

构造事件

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 Route added in v1.1.0

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

消息路由接口

type Session

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

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

	// 断开
	Close()

	// ID b
	ID() uint64

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

	//加入或者更新路由
	JoinOrUpdateRoute(index int, c Route)
}

会话

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 SessionIdentify

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

核心会话标志

func (*SessionIdentify) ID

func (s *SessionIdentify) ID() uint64

type SessionRoute added in v1.1.0

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

消息路由

func (*SessionRoute) GetRoute added in v1.1.3

func (s *SessionRoute) GetRoute(index int) (Route, error)

func (*SessionRoute) JoinOrUpdateRoute added in v1.1.3

func (s *SessionRoute) JoinOrUpdateRoute(index int, c Route)

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
peer
tcp
udp
ws

Jump to

Keyboard shortcuts

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