mirror of
https://github.com/duanhf2012/origin.git
synced 2026-03-06 22:27:36 +08:00
优化代码规范
This commit is contained in:
@@ -1,90 +1,91 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
"github.com/duanhf2012/origin/v2/network"
|
||||
"github.com/nats-io/nats.go"
|
||||
"reflect"
|
||||
"time"
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/duanhf2012/origin/v2/log"
|
||||
)
|
||||
|
||||
//跨结点连接的Client
|
||||
// NatsClient 跨结点连接的Client
|
||||
type NatsClient struct {
|
||||
localNodeId string
|
||||
localNodeId string
|
||||
notifyEventFun NotifyEventFun
|
||||
|
||||
|
||||
natsConn *nats.Conn
|
||||
client *Client
|
||||
client *Client
|
||||
}
|
||||
|
||||
func (nc *NatsClient) Start(natsConn *nats.Conn) error{
|
||||
func (nc *NatsClient) Start(natsConn *nats.Conn) error {
|
||||
nc.natsConn = natsConn
|
||||
_,err := nc.natsConn.QueueSubscribe("oc."+nc.localNodeId, "oc",nc.onSubscribe)
|
||||
_, err := nc.natsConn.QueueSubscribe("oc."+nc.localNodeId, "oc", nc.onSubscribe)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (nc *NatsClient) onSubscribe(msg *nats.Msg){
|
||||
func (nc *NatsClient) onSubscribe(msg *nats.Msg) {
|
||||
//处理消息
|
||||
nc.client.processRpcResponse(msg.Data)
|
||||
}
|
||||
|
||||
func (nc *NatsClient) SetConn(conn *network.TCPConn){
|
||||
func (nc *NatsClient) SetConn(conn *network.TCPConn) {
|
||||
}
|
||||
|
||||
func (nc *NatsClient) Close(waitDone bool){
|
||||
func (nc *NatsClient) Close(waitDone bool) {
|
||||
}
|
||||
|
||||
func (nc *NatsClient) Run(){
|
||||
func (nc *NatsClient) Run() {
|
||||
}
|
||||
|
||||
func (nc *NatsClient) OnClose(){
|
||||
func (nc *NatsClient) OnClose() {
|
||||
}
|
||||
|
||||
func (rc *NatsClient) Bind(server IServer){
|
||||
func (nc *NatsClient) Bind(server IServer) {
|
||||
s := server.(*NatsServer)
|
||||
rc.natsConn = s.natsConn
|
||||
nc.natsConn = s.natsConn
|
||||
}
|
||||
|
||||
func (rc *NatsClient) Go(nodeId string,timeout time.Duration,rpcHandler IRpcHandler,noReply bool, serviceMethod string, args interface{}, reply interface{}) *Call {
|
||||
func (nc *NatsClient) Go(nodeId string, timeout time.Duration, rpcHandler IRpcHandler, noReply bool, serviceMethod string, args interface{}, reply interface{}) *Call {
|
||||
_, processor := GetProcessorType(args)
|
||||
InParam, err := processor.Marshal(args)
|
||||
if err != nil {
|
||||
log.Error("Marshal is fail",log.ErrorAttr("error",err))
|
||||
log.Error("Marshal is fail", log.ErrorAttr("error", err))
|
||||
call := MakeCall()
|
||||
call.DoError(err)
|
||||
return call
|
||||
}
|
||||
|
||||
return rc.client.rawGo(nodeId,rc,timeout,rpcHandler,processor, noReply, 0, serviceMethod, InParam, reply)
|
||||
return nc.client.rawGo(nodeId, nc, timeout, rpcHandler, processor, noReply, 0, serviceMethod, InParam, reply)
|
||||
}
|
||||
|
||||
func (rc *NatsClient) RawGo(nodeId string,timeout time.Duration,rpcHandler IRpcHandler,processor IRpcProcessor, noReply bool, rpcMethodId uint32, serviceMethod string, rawArgs []byte, reply interface{}) *Call {
|
||||
return rc.client.rawGo(nodeId,rc,timeout,rpcHandler,processor, noReply, rpcMethodId, serviceMethod, rawArgs, reply)
|
||||
func (nc *NatsClient) RawGo(nodeId string, timeout time.Duration, rpcHandler IRpcHandler, processor IRpcProcessor, noReply bool, rpcMethodId uint32, serviceMethod string, rawArgs []byte, reply interface{}) *Call {
|
||||
return nc.client.rawGo(nodeId, nc, timeout, rpcHandler, processor, noReply, rpcMethodId, serviceMethod, rawArgs, reply)
|
||||
}
|
||||
|
||||
func (rc *NatsClient) AsyncCall(nodeId string,timeout time.Duration,rpcHandler IRpcHandler, serviceMethod string, callback reflect.Value, args interface{}, replyParam interface{},cancelable bool) (CancelRpc,error) {
|
||||
cancelRpc,err := rc.client.asyncCall(nodeId,rc,timeout,rpcHandler, serviceMethod, callback, args, replyParam,cancelable)
|
||||
func (nc *NatsClient) AsyncCall(nodeId string, timeout time.Duration, rpcHandler IRpcHandler, serviceMethod string, callback reflect.Value, args interface{}, replyParam interface{}, cancelable bool) (CancelRpc, error) {
|
||||
cancelRpc, err := nc.client.asyncCall(nodeId, nc, timeout, rpcHandler, serviceMethod, callback, args, replyParam, cancelable)
|
||||
if err != nil {
|
||||
callback.Call([]reflect.Value{reflect.ValueOf(replyParam), reflect.ValueOf(err)})
|
||||
}
|
||||
|
||||
return cancelRpc,nil
|
||||
return cancelRpc, nil
|
||||
}
|
||||
|
||||
func (rc *NatsClient) WriteMsg (nodeId string,args ...[]byte) error{
|
||||
buff := make([]byte,0,4096)
|
||||
for _,ar := range args {
|
||||
buff = append(buff,ar...)
|
||||
func (nc *NatsClient) WriteMsg(nodeId string, args ...[]byte) error {
|
||||
buff := make([]byte, 0, 4096)
|
||||
for _, ar := range args {
|
||||
buff = append(buff, ar...)
|
||||
}
|
||||
|
||||
var msg nats.Msg
|
||||
msg.Subject = "os."+nodeId
|
||||
msg.Subject = "os." + nodeId
|
||||
msg.Data = buff
|
||||
msg.Header = nats.Header{}
|
||||
msg.Header.Set("fnode",rc.localNodeId)
|
||||
return rc.natsConn.PublishMsg(&msg)
|
||||
msg.Header.Set("fnode", nc.localNodeId)
|
||||
return nc.natsConn.PublishMsg(&msg)
|
||||
}
|
||||
|
||||
func (rc *NatsClient) IsConnected() bool{
|
||||
return rc.natsConn!=nil && rc.natsConn.Status() == nats.CONNECTED
|
||||
func (nc *NatsClient) IsConnected() bool {
|
||||
return nc.natsConn != nil && nc.natsConn.Status() == nats.CONNECTED
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user