/
default.go
64 lines (52 loc) · 2.17 KB
/
default.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package xclient
import (
"context"
"github.com/hwcer/cosrpc/xshare"
"github.com/hwcer/registry"
"github.com/smallnest/rpcx/client"
"time"
)
var Default = New(nil)
func ping(c *xshare.Context) interface{} {
return time.Now().Unix()
}
func Service(name string, handler ...interface{}) *registry.Service {
service := Default.Service(name, handler...)
_ = service.Register(ping)
return service
}
func Call(ctx context.Context, servicePath, serviceMethod string, args, reply any) (err error) {
return Default.Call(ctx, servicePath, registry.Join(serviceMethod), args, reply)
}
func XCall(ctx context.Context, servicePath, serviceMethod string, args, reply any) (err error) {
return Default.XCall(ctx, servicePath, registry.Join(serviceMethod), args, reply)
}
// Async 异步调用,仅仅调用无返回值
func Async(ctx context.Context, servicePath, serviceMethod string, args any) (call *client.Call, err error) {
return Default.Async(ctx, servicePath, registry.Join(serviceMethod), args)
}
// CallWithServerId 通过特定服务器ID发消息
func CallWithServerId(ctx context.Context, sid int32, servicePath, serviceMethod string, args, reply any) (err error) {
metadata := make(xshare.Metadata)
metadata.SetServerId(sid)
return Default.CallWithMetadata(metadata.Json(), nil, servicePath, registry.Join(serviceMethod), args, reply)
}
// CallWithAddress 通过服务器地址发消息
func CallWithAddress(address string, servicePath, serviceMethod string, args, reply any) (err error) {
metadata := make(xshare.Metadata)
metadata.SetAddress(address)
return Default.CallWithMetadata(metadata.Json(), nil, servicePath, registry.Join(serviceMethod), args, reply)
}
// CallWithMetadata 自定义metadata
func CallWithMetadata(req, res xshare.Metadata, servicePath, serviceMethod string, args, reply any) (err error) {
return Default.CallWithMetadata(req, res, servicePath, registry.Join(serviceMethod), args, reply)
}
func Broadcast(ctx context.Context, servicePath, serviceMethod string, args, reply any) (err error) {
return Default.Broadcast(ctx, servicePath, serviceMethod, args, reply)
}
func Start() (err error) {
return Default.Start()
}
func Close() (err error) {
return Default.Close()
}