netx

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

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

Go to latest
Published: Mar 14, 2017 License: MIT Imports: 10 Imported by: 2

README

netx --简单的一个Net框架

GoDoc

2.0.0 架构

一个全新版本,将代码进行了大的整理,每个功能都单独定义到具体的go源文件中,并且重新定义了接口以及架构

                    --------       --------
                   | server |     | client |
                    --------       --------
                        |             |
                         -------------
                               |
                          -----------
                         | bootstrap |
                          -----------
                               |
                          -----------
                         | protocols |
                          -----------
                               |
                           ---------
                          | handler |
                           ---------

Bootstrap

接口定义

   type Bootstrap interface {
      //创建一个新的Server
      NewServer(netType, host string) *Server
      //创建一个信的Client
      NewClient(netType, host string) *Client
      //当连接创立的时候需要被调用,可用于自定义扩展
      Connection(conn net.Conn) (*Context, error)
      //关闭多有的链接
      Close() error
      //获取统计接口信息
      GetStatInfo() StatInfo
   }

创建Bootstrap

func NewBootStrap(config *Config, contextFactory *ContextFactory, connectionSetting func(conn net.Conn)) Bootstrap

其中Config主要用于设置Bootstrap连接数限制以及是否并发处理,将来可能还需要扩展

 type Config struct {
	//最大连接数
	MaxConnection int
	//最大并发处理个数
	MaxConcurrentHandler int
	//是否顺序处理消息,默认false,即可以并发处理消息
	OrderHandler bool
}

func connectionSetting(conn net.Conn)方法主要是在创建连接的时候对net.Conn的属性进行自定义的扩展,没有默认方法,将来优化后可能会有默认方法

contextFactory的创建需要使用

func NewContextFactory(initContextFunc func(context *Context)) *ContextFactory

在建立连接后创建Context的时候对Context进行初始化设置,如设置protocol,handler等

Bootstrap关闭

调用Close()方法,将会关闭由Bootstrap管理的所有Connection.

Server&Client

创建Server

调用Bootstrap.NewServer()来创建,==目前仅支持TCP==

Server的启动

调用Server.Bind()来启动监听.如果想要监听多个端口,请创建多了Server,可以共用一个Bootstrap来管理链接

创建Client

调用Bootstrap.NewClient()来创建,==目前仅支持TCP==

Client启动

调用Client.Client(timeout time.Duration)

例子

func TestServer(t *testing.T) {
	contextFactory := NewContextFactory(func(context *Context) {
		//设置
		context.SetProtocols([]Protocol{new(defaultProtocol)})
		context.SetHandler(new(testHandler))
	})
	bootstrap := NewBootStrap(new(Config), contextFactory, nil)
	server := bootstrap.NewServer("tcp", "127.0.0.1:9991")
	err := server.Bind()
	if err != nil {
		t.Fatalf("启动服务器出现错误:%s", err)
	}
	client := bootstrap.NewClient("tcp", "127.0.0.1:9991")
	err = client.Connect(3 * time.Second)
	if err != nil {
		t.Fatalf("连接服务器出现错误:%s", err)
	}
	context := client.GetContext()
	for i := 0; i < 10; i++ {
		context.Write([]byte(fmt.Sprintln("开始了\nnext")))
	}
	time.Sleep(time.Millisecond * 300)
	context.Close()
	server.Close()
	bootstrap.Close()
}

备忘:

  1. 考虑提供https://github.com/google/gopacket.git的引用
  2. 考虑内存池的使用

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bootstrap

type Bootstrap interface {
	//NewServer 创建一个新的Server
	NewServer(netType, host string) Server
	//NewClient 创建一个信的Client
	NewClient(netType, host string) Client
	//Close 关闭多有的链接
	Close() error
}

Bootstrap 启动接口

func NewBootStrap

func NewBootStrap(config *Config, initContextFunc InitConnContextFunc, connectionSetting func(conn net.Conn)) Bootstrap

NewBootStrap 初始化Bootstrap

type Client

type Client interface {
	GetConnContext() ConnContext
	Connect(timeout time.Duration) error
	Close() error
}

Client net client interface

type Config

type Config struct {
	//最大连接数
	MaxConnection int `json:"max_connection"`
	//最大并发处理个数
	MaxConcurrentHandler int `json:"max_concurrent_handler"`
	//是否顺序处理消息,默认false,即可以并发处理消息
	SyncHandler bool `json:"sync_handler"`
}

Config net config

type ConnContext

type ConnContext interface {
	GetID() int64
	AddListen(name string, listen ContextListen) error
	RemoveListen(name string)
	GetListens() map[string]ContextListen
	RemoteAddr() net.Addr
	LocalAddr() net.Addr
	IsOpen() bool
	SetHandler(handler Handler)
	SetProtocols(protocols ...Protocol)
	Start(cxt context.Context)
	Close(cxt context.Context) error
	Write(cxt context.Context, data interface{})
	FireException(cxt context.Context, err error)
}

ConnContext 建立建立后的上下文

type ContextListen

type ContextListen interface {
	OnActive(context.Context, ConnContext)
	OnClose(context.Context, ConnContext)
	OnException(context.Context, ConnContext, error)
}

ContextListen listen

type Handler

type Handler interface {
	Active(cxt context.Context, context ConnContext)
	Exception(cxt context.Context, connContext ConnContext, err error)
	Read(cxt context.Context, connContext ConnContext, data interface{})
	Close(context.Context, ConnContext)
}

Handler 处理器接口

type InitConnContextFunc

type InitConnContextFunc func(cxt context.Context, connContext ConnContext)

InitConnContextFunc init connContext

type Protocol

type Protocol interface {
	//用于激活 Protocol 的后台任务
	//Start(cxt context.Context, context ConnContext)// TODO 待考虑
	//数据编码
	Encode(cxt context.Context, context ConnContext, chain ProtocolChain, data interface{})
	//数据解码
	Decode(cxt context.Context, context ConnContext, chain ProtocolChain, data interface{})

	EncodeDestroy()

	DecodeDestroy()
}

Protocol protocol struct

type ProtocolChain

type ProtocolChain interface {
	Fire(cxt context.Context, connContext ConnContext, data interface{})
}

ProtocolChain Protocol chain interface

type Server

type Server interface {
	GetBootstrap() Bootstrap
	Bind() (err error)
	Close() error
}

Server net server

Directories

Path Synopsis
Package signal is a generated protocol buffer package.
Package signal is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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