stnet

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2022 License: MIT Imports: 19 Imported by: 0

README

stnet is a simple net lib. example

rpc server

package main

import (
	"github.com/gotask/gost/stnet"
	"time"
)

type Test struct {
}

func (t *Test) Loop() {

}
func (t *Test) HandleError(current *stnet.CurrentContent, err error) {

}

func (t *Test) HashProcessor(current *stnet.CurrentContent) (processorID int) {
	return -1
}

func (t *Test) Add(a, b int) int {
	return a + b
}

func main() {
	s := stnet.NewServer(10, 32)
	rpc := stnet.NewServiceRpc(&Test{})
	s.AddRpcService("ht", ":8085", 0, rpc, 0)
	s.Start()

	for {
		time.Sleep(time.Hour)
	}
}

rpc client

func main() {
	s := stnet.NewServer(10, 32)
	rpc := stnet.NewServiceRpc(&Test{})
	svr, e := s.AddRpcService("ht", "", 0, rpc, 0)
	if e != nil {
		fmt.Println(e)
		return
	}
	c := svr.NewConnect("127.0.0.1:8085", nil)
	s.Start()

	for {
		rpc.RpcCall(c.Session(), "Add", 1, 2, func(r int) {
			fmt.Println(r)
		}, func(r int32) {
			fmt.Println(r)
		})
		time.Sleep(time.Second)
	}
}

Documentation

Index

Constants

View Source
const (
	EncodeTyepSpb  = 0
	EncodeTyepJson = 1
)
View Source
const (
	RpcErrNoRemoteFunc = -1
	RpcErrCallTimeout  = -2
	RpcErrFuncParamErr = -3
)
View Source
const (
	SpbPackDataType_Integer_Positive = 0
	SpbPackDataType_Integer_Negative = 1
	SpbPackDataType_Float            = 2
	SpbPackDataType_Double           = 3
	SpbPackDataType_String           = 4
	SpbPackDataType_Vector           = 5
	SpbPackDataType_Map              = 6
	SpbPackDataType_StructBegin      = 7
	SpbPackDataType_StructEnd        = 8
)

Variables

View Source
var (
	ErrSocketClosed   = errors.New("socket closed")
	ErrSocketIsOpen   = errors.New("socket is open")
	ErrSendOverTime   = errors.New("send message over time")
	ErrSendBuffIsFull = errors.New("send buffer is full")
	ErrMsgParseNil    = errors.New("MsgParse is nil")
)
View Source
var (
	MsgBuffSize = 1024
	MinMsgSize  = 64
	MaxMsgSize  = 2 * 1024 * 1024

	//the length of send queue
	WriterListLen = 256
	RecvListLen   = 256
)

message recv buffer size

View Source
var GlobalSessionID uint64

session id

View Source
var (
	SysLog *stlog.Logger
)
View Source
var (
	TimeOut int64 = 5 //sec
)

Functions

func CanSetBool added in v1.1.0

func CanSetBool(x reflect.Value) bool

func CanSetFloat added in v1.1.0

func CanSetFloat(x reflect.Value) bool

func CanSetInt added in v1.1.0

func CanSetInt(x reflect.Value) bool

func CanSetUint added in v1.1.0

func CanSetUint(x reflect.Value) bool

func HttpRsp404 added in v1.1.0

func HttpRsp404(current *CurrentContent)

func HttpRspOk added in v1.1.0

func HttpRspOk(current *CurrentContent)

func HttpRspString added in v1.1.0

func HttpRspString(current *CurrentContent, rsp string)

func Marshal added in v1.1.0

func Marshal(m interface{}, encodeType int) ([]byte, error)

func SpbDecode added in v1.1.0

func SpbDecode(data []byte, x interface{}) error

func SpbEncode added in v1.1.0

func SpbEncode(data interface{}) ([]byte, error)

func Unmarshal added in v1.1.0

func Unmarshal(data []byte, m interface{}, encodeType int) error

Types

type BufferPool

type BufferPool struct {
}

func (*BufferPool) Alloc

func (bp *BufferPool) Alloc(bufsize int) []byte

func (*BufferPool) Free

func (bp *BufferPool) Free([]byte)

type CMDType

type CMDType int
const (
	Data CMDType = 0 + iota
	Open
	Close
	HeartBeat
	System
)

type Closer added in v1.1.0

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

func NewCloser added in v1.1.0

func NewCloser(isclose bool) *Closer

func (*Closer) Close added in v1.1.0

func (c *Closer) Close()

func (*Closer) IsClose added in v1.1.0

func (c *Closer) IsClose() bool

func (*Closer) Open added in v1.1.0

func (c *Closer) Open()

type Connect

type Connect struct {
	*Connector
	Master *Service
}

func (*Connect) Close

func (ct *Connect) Close()

func (*Connect) Imp

func (ct *Connect) Imp() ServiceImp

type Connector

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

func NewConnector

func NewConnector(address string, msgparse MsgParse, userdata interface{}) *Connector

NewConnector reconnect at 0s 1s 4s 9s 16s...;when call send changeAddr NotifyReconn, reconnect at once

func (*Connector) Addr

func (c *Connector) Addr() string

func (*Connector) ChangeAddr

func (c *Connector) ChangeAddr(addr string)

func (*Connector) Close

func (c *Connector) Close()

func (*Connector) GetID

func (c *Connector) GetID() uint64

func (*Connector) IsClose

func (c *Connector) IsClose() bool

func (*Connector) IsConnected

func (c *Connector) IsConnected() bool

func (*Connector) NotifyReconn

func (c *Connector) NotifyReconn()

func (*Connector) ReconnCount

func (c *Connector) ReconnCount() int

func (*Connector) Send

func (c *Connector) Send(data []byte) error

func (*Connector) Session

func (c *Connector) Session() *Session

type CurrentContent

type CurrentContent struct {
	GoroutineID int
	Sess        *Session
	UserDefined interface{}
	Peer        net.Addr
}

type FuncOnClose

type FuncOnClose = func(*Session)

this will be called when session closed

type FuncOnOpen

type FuncOnOpen = func(*Session)

this will be called when session open

type HttpService

type HttpService interface {
	Init() bool
	Loop()
	Handle(current *CurrentContent, req *http.Request, e error)
	HashProcessor(current *CurrentContent, req *http.Request) (processorID int)
}

type Listener

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

func NewListener

func NewListener(address string, msgparse MsgParse, heartbeat uint32) (*Listener, error)

func NewUdpListener added in v1.1.0

func NewUdpListener(address string, msgparse MsgParse, heartbeat uint32) (*Listener, error)

func (*Listener) Close

func (ls *Listener) Close()

func (*Listener) GetSession

func (ls *Listener) GetSession(id uint64) *Session

func (*Listener) IterateSession

func (ls *Listener) IterateSession(callback func(*Session) bool)

type LoopService

type LoopService interface {
	Init() bool
	Loop()
}

type MsgParse

type MsgParse interface {
	//*Session:session which recved message
	//CMDType:event type of msg
	//[]byte:recved data now;
	//int:length of recved data parsed;
	ParseMsg(sess *Session, data []byte) int
	// contains filtered or unexported methods
}

type ReqProto added in v1.1.0

type ReqProto struct {
	ReqCmdId  uint32
	ReqCmdSeq uint32
	ReqData   []byte
	IsOneWay  bool
	FuncName  string
}

type RpcFuncException added in v1.1.0

type RpcFuncException func(rspCode int32)

type RpcService added in v1.1.0

type RpcService interface {
	Loop()
	HandleError(current *CurrentContent, err error)

	HashProcessor(current *CurrentContent) (processorID int)
}

type RspProto added in v1.1.0

type RspProto struct {
	RspCmdId  uint32
	RspCmdSeq uint32
	PushSeqId uint32
	RspCode   int32
	RspData   []byte
	FuncName  string
}

type Server

type Server struct {
	ProcessorThreadsNum int //number of threads in server.
	// contains filtered or unexported fields
}

func NewServer

func NewServer(loopmsec uint32, threadnum int) *Server

func (*Server) AddEchoService

func (svr *Server) AddEchoService(name, address string, heartbeat uint32, threadId int) (*Service, error)

func (*Server) AddHttpService

func (svr *Server) AddHttpService(name, address string, heartbeat uint32, imp HttpService, threadId int) (*Service, error)

func (*Server) AddLoopService

func (svr *Server) AddLoopService(name string, imp LoopService, threadId int) (*Service, error)

func (*Server) AddRpcService added in v1.1.0

func (svr *Server) AddRpcService(name, address string, heartbeat uint32, imp *ServiceRpc, threadId int) (*Service, error)

AddRpcService imp: NewServiceRpc

func (*Server) AddService

func (svr *Server) AddService(name, address string, heartbeat uint32, imp ServiceImp, threadId int) (*Service, error)

AddService must be called before server started. address could be null,then you get a service without listen. when heartbeat(second)=0,heartbeat will be close. call service.NewConnect start a connector

func (*Server) AddTcpProxyService

func (svr *Server) AddTcpProxyService(address string, heartbeat uint32, threadId int, proxyaddr []string, proxyweight []int) error

func (*Server) Start

func (svr *Server) Start() error

func (*Server) Stop

func (svr *Server) Stop()

type Service

type Service struct {
	*Listener
	Name string
	// contains filtered or unexported fields
}

func (*Service) GetConnect

func (service *Service) GetConnect(id uint64) *Connect

func (*Service) Imp

func (service *Service) Imp() ServiceImp

func (*Service) IterateConnect

func (service *Service) IterateConnect(callback func(*Connect) bool)

func (*Service) NewConnect

func (service *Service) NewConnect(address string, userdata interface{}) *Connect

func (*Service) ParseMsg

func (service *Service) ParseMsg(sess *Session, data []byte) int

type ServiceBase

type ServiceBase struct {
}

ServiceImpBase

func (*ServiceBase) Destroy

func (service *ServiceBase) Destroy()

func (*ServiceBase) HandleError

func (service *ServiceBase) HandleError(current *CurrentContent, err error)

func (*ServiceBase) HandleMessage

func (service *ServiceBase) HandleMessage(current *CurrentContent, msgID uint64, msg interface{})

func (*ServiceBase) HashProcessor

func (service *ServiceBase) HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int)

func (*ServiceBase) HeartBeatTimeOut

func (service *ServiceBase) HeartBeatTimeOut(sess *Session)

func (*ServiceBase) Init

func (service *ServiceBase) Init() bool

func (*ServiceBase) Loop

func (service *ServiceBase) Loop()

func (*ServiceBase) SessionClose

func (service *ServiceBase) SessionClose(sess *Session)

func (*ServiceBase) SessionOpen

func (service *ServiceBase) SessionOpen(sess *Session)

func (*ServiceBase) Unmarshal

func (service *ServiceBase) Unmarshal(sess *Session, data []byte) (lenParsed int, msgID int64, msg interface{}, err error)

type ServiceEcho

type ServiceEcho struct {
	ServiceBase
}

ServiceImpEcho

func (*ServiceEcho) HashProcessor

func (service *ServiceEcho) HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int)

func (*ServiceEcho) Unmarshal

func (service *ServiceEcho) Unmarshal(sess *Session, data []byte) (lenParsed int, msgID int64, msg interface{}, err error)

type ServiceHttp

type ServiceHttp struct {
	ServiceBase
	// contains filtered or unexported fields
}

ServiceHttp

func (*ServiceHttp) HandleError

func (service *ServiceHttp) HandleError(current *CurrentContent, err error)

func (*ServiceHttp) HandleMessage

func (service *ServiceHttp) HandleMessage(current *CurrentContent, msgID uint64, msg interface{})

func (*ServiceHttp) HashProcessor

func (service *ServiceHttp) HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int)

func (*ServiceHttp) Init added in v1.1.0

func (service *ServiceHttp) Init() bool

func (*ServiceHttp) Loop added in v1.1.0

func (service *ServiceHttp) Loop()

func (*ServiceHttp) Unmarshal

func (service *ServiceHttp) Unmarshal(sess *Session, data []byte) (lenParsed int, msgID int64, msg interface{}, err error)

type ServiceImp

type ServiceImp interface {
	Init() bool
	Loop()
	Destroy()
	HandleMessage(current *CurrentContent, msgID uint64, msg interface{})
	HandleError(*CurrentContent, error)
	SessionOpen(sess *Session)
	SessionClose(sess *Session)
	HeartBeatTimeOut(sess *Session)

	// Unmarshal protocol parsed
	//lenParsed is the length read from 'data'.
	//msgID and msg are messages parsed from data.
	//when lenParsed <= 0 or msgID < 0,msg and err will be ignored.
	Unmarshal(sess *Session, data []byte) (lenParsed int, msgID int64, msg interface{}, err error)
	// HashProcessor sess msgID msg are returned by func of Unmarshal
	//processorID is the thread who process this msg;it should between 1-ProcessorThreadsNum.
	//if processorID == 0, it only uses main thread of the service.
	//if processorID < 0, it will use hash of session id.
	HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int)
}

type ServiceLoop

type ServiceLoop struct {
	ServiceBase
	// contains filtered or unexported fields
}

func (*ServiceLoop) Init

func (service *ServiceLoop) Init() bool

func (*ServiceLoop) Loop

func (service *ServiceLoop) Loop()

type ServiceProxyC

type ServiceProxyC struct {
	ServiceBase
}

func (*ServiceProxyC) HashProcessor

func (service *ServiceProxyC) HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int)

func (*ServiceProxyC) Unmarshal

func (service *ServiceProxyC) Unmarshal(sess *Session, data []byte) (lenParsed int, msgID int64, msg interface{}, err error)

type ServiceProxyS

type ServiceProxyS struct {
	ServiceBase
	// contains filtered or unexported fields
}

func (*ServiceProxyS) HandleError

func (service *ServiceProxyS) HandleError(current *CurrentContent, err error)

func (*ServiceProxyS) HashProcessor

func (service *ServiceProxyS) HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int)

func (*ServiceProxyS) HeartBeatTimeOut

func (service *ServiceProxyS) HeartBeatTimeOut(sess *Session)

func (*ServiceProxyS) SessionClose

func (service *ServiceProxyS) SessionClose(sess *Session)

func (*ServiceProxyS) Unmarshal

func (service *ServiceProxyS) Unmarshal(sess *Session, data []byte) (lenParsed int, msgID int64, msg interface{}, err error)

type ServiceRpc added in v1.1.0

type ServiceRpc struct {
	ServiceBase
	// contains filtered or unexported fields
}

func NewServiceRpc added in v1.1.0

func NewServiceRpc(imp RpcService) *ServiceRpc

func (*ServiceRpc) HandleError added in v1.1.0

func (service *ServiceRpc) HandleError(current *CurrentContent, err error)

func (*ServiceRpc) HandleMessage added in v1.1.0

func (service *ServiceRpc) HandleMessage(current *CurrentContent, msgID uint64, msg interface{})

func (*ServiceRpc) HashProcessor added in v1.1.0

func (service *ServiceRpc) HashProcessor(current *CurrentContent, msgID uint64, msg interface{}) (processorID int)

func (*ServiceRpc) Init added in v1.1.0

func (service *ServiceRpc) Init() bool

func (*ServiceRpc) Loop added in v1.1.0

func (service *ServiceRpc) Loop()

func (*ServiceRpc) RpcCall added in v1.1.0

func (service *ServiceRpc) RpcCall(sess *Session, funcName string, params ...interface{}) error

RpcCall remotesession remotefunction(string) functionparams callback(could nil) exception(could nil, func(rspCode int32))

func (*ServiceRpc) RpcCall_Sync added in v1.1.0

func (service *ServiceRpc) RpcCall_Sync(sess *Session, funcName string, params ...interface{}) error

func (*ServiceRpc) SendReq added in v1.1.0

func (service *ServiceRpc) SendReq(sess *Session, req ReqProto) error

func (*ServiceRpc) SendRsp added in v1.1.0

func (service *ServiceRpc) SendRsp(sess *Session, rsp RspProto) error

func (*ServiceRpc) SendUdpReq added in v1.1.0

func (service *ServiceRpc) SendUdpReq(sess *Session, peer net.Addr, req ReqProto) error

func (*ServiceRpc) SendUdpRsp added in v1.1.0

func (service *ServiceRpc) SendUdpRsp(sess *Session, peer net.Addr, rsp RspProto) error

func (*ServiceRpc) UdpRpcCall added in v1.1.0

func (service *ServiceRpc) UdpRpcCall(sess *Session, peer net.Addr, funcName string, params ...interface{}) error

func (*ServiceRpc) UdpRpcCall_Sync added in v1.1.0

func (service *ServiceRpc) UdpRpcCall_Sync(sess *Session, peer net.Addr, funcName string, params ...interface{}) error

func (*ServiceRpc) Unmarshal added in v1.1.0

func (service *ServiceRpc) Unmarshal(sess *Session, data []byte) (lenParsed int, msgID int64, msg interface{}, err error)

type Session

type Session struct {
	UserData interface{}
	// contains filtered or unexported fields
}

func NewSession

func NewSession(con net.Conn, msgparse MsgParse, onopen FuncOnOpen, onclose FuncOnClose, heartbeat uint32, isudp bool) (*Session, error)

func (*Session) Close

func (s *Session) Close()

func (*Session) Connector

func (s *Session) Connector() *Connector

func (*Session) GetID

func (s *Session) GetID() uint64

func (*Session) IsClose

func (s *Session) IsClose() bool

func (*Session) Peer added in v1.1.0

func (s *Session) Peer() net.Addr

func (*Session) RemoteAddr

func (s *Session) RemoteAddr() string

func (*Session) Send

func (s *Session) Send(data []byte, peer net.Addr) error

type Spb added in v1.1.0

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

Jump to

Keyboard shortcuts

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