116 lines
3.2 KiB
Go
116 lines
3.2 KiB
Go
package rpcx
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/smallnest/rpcx/client"
|
|
)
|
|
|
|
func newSys(options Options) (sys *RPCX, err error) {
|
|
sys = &RPCX{
|
|
options: options,
|
|
metadata: fmt.Sprintf("stype=%s&sid=%s&version=%s&addr=%s", options.ServiceType, options.ServiceId, options.ServiceVersion, "tcp@"+options.ServiceAddr),
|
|
}
|
|
sys.service = newService(sys)
|
|
sys.client = newClient(sys)
|
|
// if options.RpcxStartType == RpcxStartByAll || options.RpcxStartType == RpcxStartByService { //创建RPCX 服务端
|
|
|
|
// }
|
|
|
|
// if options.RpcxStartType == RpcxStartByAll || options.RpcxStartType == RpcxStartByClient { //创建RPCX 客户端
|
|
|
|
// }
|
|
return
|
|
}
|
|
|
|
type RPCX struct {
|
|
options Options
|
|
metadata string
|
|
service IRPCXServer
|
|
client IRPCXClient
|
|
}
|
|
|
|
func (this *RPCX) Start() (err error) {
|
|
this.service.Start()
|
|
this.client.Start()
|
|
return
|
|
}
|
|
|
|
func (this *RPCX) Stop() (err error) {
|
|
this.service.Stop()
|
|
this.client.Stop()
|
|
return
|
|
}
|
|
|
|
func (this *RPCX) Register(rcvr interface{}) (err error) {
|
|
this.service.Register(rcvr)
|
|
return
|
|
}
|
|
|
|
func (this *RPCX) RegisterFunction(fn interface{}) (err error) {
|
|
this.service.RegisterFunction(fn)
|
|
return
|
|
}
|
|
|
|
func (this *RPCX) RegisterFunctionName(name string, fn interface{}) (err error) {
|
|
this.service.RegisterFunctionName(name, fn)
|
|
return
|
|
}
|
|
|
|
func (this *RPCX) UnregisterAll() (err error) {
|
|
return this.service.UnregisterAll()
|
|
}
|
|
|
|
//同步调用
|
|
func (this *RPCX) Call(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}) (err error) {
|
|
return this.client.Call(ctx, servicePath, serviceMethod, args, reply)
|
|
}
|
|
|
|
//异步调用
|
|
func (this *RPCX) Go(ctx context.Context, servicePath string, serviceMethod string, args interface{}, reply interface{}, done chan *client.Call) (call *client.Call, err error) {
|
|
return this.client.Go(ctx, servicePath, serviceMethod, args, reply, done)
|
|
}
|
|
|
|
// func (this *RPCX) PostCall(ctx context.Context, serviceName, methodName string, args, reply interface{}) (interface{}, error) {
|
|
// clientConn := ctx.Value(server.RemoteConnContextKey).(net.Conn)
|
|
// fmt.Printf("PostCall servicePath:%v serviceMethod:%v RemoteAddr:%v \n", serviceName, methodName, clientConn.RemoteAddr().String())
|
|
// return args, nil
|
|
// }
|
|
|
|
///日志***********************************************************************
|
|
func (this *RPCX) Debug() bool {
|
|
return this.options.Debug
|
|
}
|
|
|
|
func (this *RPCX) Debugf(format string, a ...interface{}) {
|
|
if this.options.Debug {
|
|
this.options.Log.Debugf("[SYS RPCX] "+format, a...)
|
|
}
|
|
}
|
|
func (this *RPCX) Infof(format string, a ...interface{}) {
|
|
if this.options.Debug {
|
|
this.options.Log.Infof("[SYS RPCX] "+format, a...)
|
|
}
|
|
}
|
|
func (this *RPCX) Warnf(format string, a ...interface{}) {
|
|
if this.options.Debug {
|
|
this.options.Log.Warnf("[SYS RPCX] "+format, a...)
|
|
}
|
|
}
|
|
func (this *RPCX) Errorf(format string, a ...interface{}) {
|
|
if this.options.Debug {
|
|
this.options.Log.Errorf("[SYS RPCX] "+format, a...)
|
|
}
|
|
}
|
|
func (this *RPCX) Panicf(format string, a ...interface{}) {
|
|
if this.options.Debug {
|
|
this.options.Log.Panicf("[SYS RPCX] "+format, a...)
|
|
}
|
|
}
|
|
func (this *RPCX) Fatalf(format string, a ...interface{}) {
|
|
if this.options.Debug {
|
|
this.options.Log.Fatalf("[SYS RPCX] "+format, a...)
|
|
}
|
|
}
|