Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" "github.com/name5566/leaf/chanrpc" "sync" ) func main() { s := chanrpc.NewServer(10) var wg sync.WaitGroup wg.Add(1) // goroutine 1 go func() { s.Register("f0", func(args []interface{}) (err error) { return }) s.Register("f1", func(args []interface{}) (interface{}, error) { return 1, nil }) s.Register("fn", func(args []interface{}) ([]interface{}, error) { return []interface{}{1, 2, 3}, nil }) s.Register("add", func(args []interface{}) (interface{}, error) { n1 := args[0].(int) n2 := args[1].(int) return n1 + n2, nil }) wg.Done() for { s.Exec(<-s.ChanCall) } }() wg.Wait() wg.Add(1) // goroutine 2 go func() { c := s.Open(10) // sync err := c.Call0("f0") if err != nil { fmt.Println(err) } r1, err := c.Call1("f1") if err != nil { fmt.Println(err) } else { fmt.Println(r1) } rn, err := c.CallN("fn") if err != nil { fmt.Println(err) } else { fmt.Println(rn[0], rn[1], rn[2]) } ra, err := c.Call1("add", 1, 2) if err != nil { fmt.Println(err) } else { fmt.Println(ra) } // asyn c.AsynCall("f0", func(err error) { if err != nil { fmt.Println(err) } }) c.AsynCall("f1", func(ret interface{}, err error) { if err != nil { fmt.Println(err) } else { fmt.Println(ret) } }) c.AsynCall("fn", func(ret []interface{}, err error) { if err != nil { fmt.Println(err) } else { fmt.Println(ret[0], ret[1], ret[2]) } }) c.AsynCall("add", 1, 2, func(ret interface{}, err error) { if err != nil { fmt.Println(err) } else { fmt.Println(ret) } }) c.Cb(<-c.ChanAsynRet) c.Cb(<-c.ChanAsynRet) c.Cb(<-c.ChanAsynRet) c.Cb(<-c.ChanAsynRet) // go s.Go("f0") wg.Done() }() wg.Wait() }
Output: 1 1 2 3 3 1 1 2 3 3
Index ¶
- Constants
- func Assert(i interface{}) []interface{}
- type CallInfo
- type Client
- func (c *Client) AsynCall(id interface{}, _args ...interface{})
- func (c *Client) Attach(s *Server)
- func (c *Client) Call0(id interface{}, args ...interface{}) error
- func (c *Client) Call1(id interface{}, args ...interface{}) (interface{}, error)
- func (c *Client) CallN(id interface{}, args ...interface{}) ([]interface{}, error)
- func (c *Client) Cb(ri *RetInfo)
- func (c *Client) Close()
- func (c *Client) GetServer() *Server
- func (c *Client) Idle() bool
- func (c *Client) RpcCall(id interface{}, args ...interface{})
- type ExtRetFunc
- type FuncInfo
- type RetInfo
- type Server
- func (s *Server) Call0(id interface{}, args ...interface{}) error
- func (s *Server) Call1(id interface{}, args ...interface{}) (interface{}, error)
- func (s *Server) CallN(id interface{}, args ...interface{}) ([]interface{}, error)
- func (s *Server) Close()
- func (s *Server) Exec(ci *CallInfo)
- func (s *Server) Go(id interface{}, args ...interface{})
- func (s *Server) Open(l int) *Client
- func (s *Server) Register(id interface{}, f interface{})
- func (s *Server) RegisterFromType(id interface{}, f interface{}, fType int)
Examples ¶
Constants ¶
View Source
const ( FuncCommon = iota FuncExtRet FuncRoute )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
type Client struct { ChanSyncRet chan *RetInfo ChanAsynRet chan *RetInfo // contains filtered or unexported fields }
type ExtRetFunc ¶
type ExtRetFunc func(ret interface{}, err error)
type RetInfo ¶
type RetInfo struct { // nil // interface{} // []interface{} Ret interface{} Err error // callback: // func(Err error) // func(Ret interface{}, Err error) // func(Ret []interface{}, Err error) Cb interface{} }
type Server ¶
type Server struct { ChanCall chan *CallInfo // contains filtered or unexported fields }
one server per goroutine (goroutine not safe) one client per goroutine (goroutine not safe)
func (*Server) RegisterFromType ¶
you must call the function before calling Open and Go
Click to show internal directories.
Click to hide internal directories.