1 Star 0 Fork 1

Collin / leaf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
server.go 3.09 KB
一键复制 编辑 原始数据 按行查看 历史
Collin 提交于 2021-10-09 11:54 . adjust file structure
package chanrpc
import (
"errors"
"fmt"
"runtime"
"gitee.com/nbcx/leaf/conf"
"gitee.com/nbcx/leaf/log"
)
// one server per goroutine (goroutine not safe)
// one client per goroutine (goroutine not safe)
type Server struct {
// id -> function
//
// function:
// func(args []interface{})
// func(args []interface{}) interface{}
// func(args []interface{}) []interface{}
functions map[interface{}]interface{}
ChanCall chan *CallInfo
}
type CallInfo struct {
f interface{}
args []interface{}
chanRet chan *RetInfo
cb interface{}
}
func NewServer(l int) *Server {
s := new(Server)
s.functions = make(map[interface{}]interface{})
s.ChanCall = make(chan *CallInfo, l)
return s
}
// you must call the function before calling Open and Go
func (s *Server) Register(id interface{}, f interface{}) {
switch f.(type) {
case func([]interface{}):
case func([]interface{}) interface{}:
case func([]interface{}) []interface{}:
default:
panic(fmt.Sprintf("function id %v: definition of function is invalid", id))
}
if _, ok := s.functions[id]; ok {
panic(fmt.Sprintf("function id %v: already registered", id))
}
s.functions[id] = f
}
func (s *Server) ret(ci *CallInfo, ri *RetInfo) (err error) {
if ci.chanRet == nil {
return
}
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
ri.cb = ci.cb
ci.chanRet <- ri
return
}
func (s *Server) exec(ci *CallInfo) (err error) {
defer func() {
if r := recover(); r != nil {
if conf.LenStackBuf > 0 {
buf := make([]byte, conf.LenStackBuf)
l := runtime.Stack(buf, false)
err = fmt.Errorf("%v: %s", r, buf[:l])
} else {
err = fmt.Errorf("%v", r)
}
s.ret(ci, &RetInfo{err: fmt.Errorf("%v", r)})
}
}()
// execute
switch ci.f.(type) {
case func([]interface{}):
ci.f.(func([]interface{}))(ci.args)
return s.ret(ci, &RetInfo{})
case func([]interface{}) interface{}:
ret := ci.f.(func([]interface{}) interface{})(ci.args)
return s.ret(ci, &RetInfo{ret: ret})
case func([]interface{}) []interface{}:
ret := ci.f.(func([]interface{}) []interface{})(ci.args)
return s.ret(ci, &RetInfo{ret: ret})
}
panic("bug")
}
func (s *Server) Exec(ci *CallInfo) {
err := s.exec(ci)
if err != nil {
log.Error("%v", err)
}
}
// goroutine safe
func (s *Server) Go(id interface{}, args ...interface{}) {
f := s.functions[id]
if f == nil {
return
}
defer func() {
recover()
}()
s.ChanCall <- &CallInfo{
f: f,
args: args,
}
}
// goroutine safe
func (s *Server) Call0(id interface{}, args ...interface{}) error {
return s.Open(0).Call0(id, args...)
}
// goroutine safe
func (s *Server) Call1(id interface{}, args ...interface{}) (interface{}, error) {
return s.Open(0).Call1(id, args...)
}
// goroutine safe
func (s *Server) CallN(id interface{}, args ...interface{}) ([]interface{}, error) {
return s.Open(0).CallN(id, args...)
}
func (s *Server) Close() {
close(s.ChanCall)
for ci := range s.ChanCall {
s.ret(ci, &RetInfo{
err: errors.New("chanrpc server closed"),
})
}
}
// goroutine safe
func (s *Server) Open(l int) *Client {
c := NewClient(l)
c.Attach(s)
return c
}
1
https://gitee.com/nbcx/leaf.git
git@gitee.com:nbcx/leaf.git
nbcx
leaf
leaf
78613ecb5808

搜索帮助

53164aa7 5694891 3bd8fe86 5694891