当前位置: 首页>>代码示例>>Golang>>正文


Golang rpc.NewClientWithCodec函数代码示例

本文整理汇总了Golang中net/rpc.NewClientWithCodec函数的典型用法代码示例。如果您正苦于以下问题:Golang NewClientWithCodec函数的具体用法?Golang NewClientWithCodec怎么用?Golang NewClientWithCodec使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了NewClientWithCodec函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: ConnectRPC

func ConnectRPC(amqpConn *amqp.Connection) {
	if !*paulingDisabled {
		codec, err := amqprpc.NewClientCodec(amqpConn, config.Constants.PaulingQueue, amqprpc.JSONCodec{})
		if err != nil {
			logrus.Fatal(err)
		}

		pauling = rpc.NewClientWithCodec(codec)

	}
	if !*fumbleDisabled {
		codec, err := amqprpc.NewClientCodec(amqpConn, config.Constants.FumbleQueue, amqprpc.JSONCodec{})
		if err != nil {
			logrus.Fatal(err)
		}

		fumble = rpc.NewClientWithCodec(codec)
	}
	if !*twitchbotDisabled {
		codec, err := amqprpc.NewClientCodec(amqpConn, config.Constants.TwitchBotQueue, amqprpc.JSONCodec{})
		if err != nil {
			logrus.Fatal(err)
		}
		twitchbot = rpc.NewClientWithCodec(codec)
	}
}
开发者ID:TF2Stadium,项目名称:Helen,代码行数:26,代码来源:rpc.go

示例2: NewDirectRPCClient

// NewDirectRPCClient creates a rpc client
func NewDirectRPCClient(c *Client, clientCodecFunc ClientCodecFunc, network, address string, timeout time.Duration) (*rpc.Client, error) {
	//if network == "http" || network == "https" {
	if network == "http" {
		return NewDirectHTTPRPCClient(c, clientCodecFunc, network, address, "", timeout)
	}

	var conn net.Conn
	var tlsConn *tls.Conn
	var err error

	if c != nil && c.TLSConfig != nil {
		dialer := &net.Dialer{
			Timeout: timeout,
		}
		tlsConn, err = tls.DialWithDialer(dialer, network, address, c.TLSConfig)
		//or conn:= tls.Client(netConn, &config)

		conn = net.Conn(tlsConn)
	} else {
		conn, err = net.DialTimeout(network, address, timeout)
	}

	if err != nil {
		return nil, err
	}

	if c == nil || c.PluginContainer == nil {
		return rpc.NewClientWithCodec(clientCodecFunc(conn)), nil
	}
	return rpc.NewClientWithCodec(newClientCodecWrapper(c.PluginContainer, clientCodecFunc(conn))), nil
}
开发者ID:xxxlihui,项目名称:rpcx,代码行数:32,代码来源:client.go

示例3: doTestMsgpackRpcSpecGoClientToPythonSvc

func doTestMsgpackRpcSpecGoClientToPythonSvc(t *testing.T) {
	if testSkipRPCTests {
		return
	}
	// openPorts are between 6700 and 6800
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	openPort := strconv.FormatInt(6700+r.Int63n(99), 10)
	// openPort := "6792"
	cmd := exec.Command("python", "test.py", "rpc-server", openPort, "4")
	checkErrT(t, cmd.Start())
	bs, err2 := net.Dial("tcp", ":"+openPort)
	for i := 0; i < 10 && err2 != nil; i++ {
		time.Sleep(50 * time.Millisecond) // time for python rpc server to start
		bs, err2 = net.Dial("tcp", ":"+openPort)
	}
	checkErrT(t, err2)
	cc := MsgpackSpecRpc.ClientCodec(bs, testMsgpackH)
	cl := rpc.NewClientWithCodec(cc)
	defer cl.Close()
	var rstr string
	checkErrT(t, cl.Call("EchoStruct", TestRpcABC{"Aa", "Bb", "Cc"}, &rstr))
	//checkEqualT(t, rstr, "{'A': 'Aa', 'B': 'Bb', 'C': 'Cc'}")
	var mArgs MsgpackSpecRpcMultiArgs = []interface{}{"A1", "B2", "C3"}
	checkErrT(t, cl.Call("Echo123", mArgs, &rstr))
	checkEqualT(t, rstr, "1:A1 2:B2 3:C3", "rstr=")
	cmd.Process.Kill()
}
开发者ID:CNDonny,项目名称:scope,代码行数:27,代码来源:codec_test.go

示例4: ValidateAddPeer

// ValidateAddPeer is the validation function for AddPeer to invoke the rpc
// server call
func ValidateAddPeer(p *peer.PeerAddRequest) (*services.RPCPeerAddResp, error) {
	args := &services.RPCPeerAddReq{Name: new(string), Addresses: p.Addresses}
	*args.Name = p.Name

	rsp := new(services.RPCPeerAddResp)
	//TODO : port 9876 is hardcoded for now, can be made configurable
	remoteAddress := fmt.Sprintf("%s:%s", p.Name, "9876")
	rpcConn, e := net.Dial("tcp", remoteAddress)
	if e != nil {
		log.WithField("error", e).Error("net.Dial() call failed")
		opRet = -1
		opError = e.Error()
		rsp.OpRet = &opRet
		rsp.OpError = &opError
		return rsp, e
	}
	client := rpc.NewClientWithCodec(pbcodec.NewClientCodec(rpcConn))
	defer client.Close()

	e = client.Call("PeerService.ValidateAdd", args, rsp)
	if e != nil {
		log.Error("Failed to execute PeerService.Validate() rpc call")
		opRet = -1
		opError = e.Error()
		rsp.OpRet = &opRet
		rsp.OpError = &opError
		return rsp, e
	}
	return rsp, nil
}
开发者ID:sac,项目名称:glusterd2,代码行数:32,代码来源:peer-rpc-clnt.go

示例5: wsrpcEvents

// WebSocket handler for JSON RPC events, we call the client.
func (h *Handler) wsrpcEvents() http.Handler {
	type conn struct {
		io.Reader
		io.WriteCloser
	}
	f := func(ws *websocket.Conn) {
		id, dr := h.pool.Register()
		defer h.pool.Unregister(id)
		stop := make(chan struct{})
		r, w := io.Pipe()
		defer w.Close()
		go func() {
			io.Copy(w, ws)
			close(stop)
		}()
		rwc := &conn{Reader: r, WriteCloser: ws}
		cli := rpc.NewClientWithCodec(jsonrpc.NewClientCodec(rwc))
		for {
			select {
			case r := <-dr:
				err := cli.Call("SM.Deliver", r, nil)
				if err != nil {
					return
				}
			case <-stop:
				return
			}
		}
	}
	return auth(cors(websocket.Handler(f).ServeHTTP, "GET"))
}
开发者ID:DisruptiveMind,项目名称:go-smpp,代码行数:32,代码来源:http.go

示例6: getClient

func (lt *localRPCTransport) getClient(id roachpb.StoreID) (*netrpc.Client, error) {
	lt.mu.Lock()
	defer lt.mu.Unlock()

	select {
	case <-lt.closed:
		return nil, util.Errorf("transport is closed")
	default:
	}

	client, ok := lt.clients[id]
	if ok {
		return client, nil
	}

	srvWithAddr, ok := lt.servers[id]
	if !ok {
		return nil, util.Errorf("unknown peer %v", id)
	}
	address := srvWithAddr.addr.String()

	// If this wasn't test code we wouldn't want to call Dial while holding the lock.
	conn, err := codec.TLSDialHTTP("tcp", address, base.NetworkTimeout, nil)
	if err != nil {
		return nil, err
	}
	client = netrpc.NewClientWithCodec(codec.NewClientCodec(conn))
	lt.clients[id] = client
	return client, err
}
开发者ID:kaustubhkurve,项目名称:cockroach,代码行数:30,代码来源:raft_transport.go

示例7: getClient

// getClient is used to get a cached or new client
func (c *Conn) getClient() (*StreamClient, error) {
	// Check for cached client
	c.clientLock.Lock()
	front := c.clients.Front()
	if front != nil {
		c.clients.Remove(front)
	}
	c.clientLock.Unlock()
	if front != nil {
		return front.Value.(*StreamClient), nil
	}

	// Open a new session
	stream, err := c.session.Open()
	if err != nil {
		return nil, err
	}

	// Create the RPC client
	cc := codec.GoRpc.ClientCodec(stream, msgpackHandle)
	client := rpc.NewClientWithCodec(cc)

	// Return a new stream client
	sc := &StreamClient{
		stream: stream,
		client: client,
	}
	return sc, nil
}
开发者ID:nbrownus,项目名称:consul,代码行数:30,代码来源:pool.go

示例8: main

func main() {
	conn, err := net.Dial("unix", "/tmp/navc.sock")
	if err != nil {
		log.Fatal("dial socket", err)
	}
	defer conn.Close()

	codec := jsonrpc.NewClientCodec(conn)
	defer codec.Close()

	client := rpc.NewClientWithCodec(codec)
	defer client.Close()

	// sample call
	args := Symbol{"", "", "sample/a.c", 16, 2}
	var reply Symbol
	err = client.Call("RequestHandler.GetSymbolDecl",
		&args,
		&reply)
	if err != nil {
		log.Fatal("calling ", err)
	}

	log.Println(reply)
}
开发者ID:rahulpathakgit,项目名称:navc,代码行数:25,代码来源:client.go

示例9: TestRPC

func TestRPC(b *testing.T) {
	conn, err := amqp.Dial(*url)
	if err != nil {
		b.Fatal(err)
	}

	serverCodec, err := NewServerCodec(conn, *queue, JSONCodec{})
	if err != nil {
		b.Fatal(err)
	}

	server := rpc.NewServer()

	err = server.Register(new(RPC))
	if err != nil {
		b.Fatal(err)
	}

	go func() { server.ServeCodec(serverCodec) }()

	var clientCodecs []rpc.ClientCodec
	var clients []*rpc.Client
	wait := new(sync.WaitGroup)
	mu := new(sync.Mutex)

	wait.Add(10)

	for i := 0; i < 10; i++ {
		go func() {
			conn, err := amqp.Dial(*url)
			if err != nil {
				b.Fatal(err)
			}

			codec, err := NewClientCodec(conn, *queue, JSONCodec{})
			if err != nil {
				b.Fatal(err)
			}

			mu.Lock()
			clientCodecs = append(clientCodecs, codec)
			clients = append(clients, rpc.NewClientWithCodec(codec))
			mu.Unlock()
			wait.Done()
		}()
	}

	wait.Wait()

	for i := 0; i < 10; i++ {
		wait.Add(10)
		go func() {
			for _, client := range clients {
				go doCall(b, client, wait)
			}
		}()
	}

	wait.Wait()
}
开发者ID:vibhavp,项目名称:amqp-rpc,代码行数:60,代码来源:rpc_test.go

示例10: TestWebSocket_Send

func TestWebSocket_Send(t *testing.T) {
	mux := http.NewServeMux()
	h := Handler{Tx: newTransceiver()}
	<-h.Register(mux)
	s := httptest.NewServer(mux)
	defer s.Close()
	url := strings.Replace(s.URL, "http:", "ws:", -1)
	ws, err := websocket.Dial(url+"/v1/ws/jsonrpc", "", "http://localhost")
	if err != nil {
		t.Fatal(err)
	}
	defer ws.Close()
	cli := rpc.NewClientWithCodec(jsonrpc.NewClientCodec(ws))
	args := &ShortMessage{
		Dst:  "root",
		Text: "hello world",
	}
	var resp ShortMessageResp
	err = cli.Call("SM.Submit", args, &resp)
	if err != nil {
		t.Fatal(err)
	}
	want := "foobar"
	if resp.MessageID != want {
		t.Fatalf("unexpected message id: want %q, have %q",
			want, resp.MessageID)
	}
}
开发者ID:fiorix,项目名称:sms-api-server,代码行数:28,代码来源:http_test.go

示例11: NewClient

// NewClient returns instance of rpc.Client object, that is used to send request to xmlrpc service.
func NewClient(requrl string, transport http.RoundTripper) (*Client, error) {
	if transport == nil {
		transport = http.DefaultTransport
	}

	httpClient := &http.Client{Transport: transport}

	jar, err := cookiejar.New(nil)

	if err != nil {
		return nil, err
	}

	u, err := url.Parse(requrl)

	if err != nil {
		return nil, err
	}

	codec := clientCodec{
		url:        u,
		httpClient: httpClient,
		ready:      make(chan uint64),
		responses:  make(map[uint64]*http.Response),
		cookies:    jar,
	}

	return &Client{rpc.NewClientWithCodec(&codec)}, nil
}
开发者ID:RezaDKhan,项目名称:terraform,代码行数:30,代码来源:client.go

示例12: benchmarkEchoProtoRPC

func benchmarkEchoProtoRPC(b *testing.B, size int) {
	var client *rpc.Client
	benchmarkEcho(b, size, listenAndServeProtoRPC,
		func(addr net.Addr) {
			conn, err := tls.Dial(addr.Network(), addr.String(), clientTLSConfig)
			if err != nil {
				b.Fatal(err)
			}
			client = rpc.NewClientWithCodec(NewClientCodec(conn))
		},
		func() {
			if err := client.Close(); err != nil {
				b.Fatal(err)
			}
		},
		func(echoMsg string) string {
			args := EchoRequest{Msg: echoMsg}
			reply := EchoResponse{}
			if err := client.Call("Echo.Echo", &args, &reply); err != nil {
				b.Fatal(err)
			}
			return reply.Msg
		},
	)
}
开发者ID:iamqizhao,项目名称:rpc-bench,代码行数:25,代码来源:rpc_test.go

示例13: handleProbeWS

// handleProbeWS accepts websocket connections from the probe and registers
// them in the control router, such that HandleControl calls can find them.
func (cr *controlRouter) handleProbeWS(w http.ResponseWriter, r *http.Request) {
	probeID := r.Header.Get(xfer.ScopeProbeIDHeader)
	if probeID == "" {
		respondWith(w, http.StatusBadRequest, xfer.ScopeProbeIDHeader)
		return
	}

	conn, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		log.Printf("Error upgrading to websocket: %v", err)
		return
	}
	defer conn.Close()

	codec := xfer.NewJSONWebsocketCodec(conn)
	client := rpc.NewClientWithCodec(codec)
	handler := controlHandler{
		id:     rand.Int63(),
		client: client,
	}

	cr.set(probeID, handler)

	codec.WaitForReadError()

	cr.rm(probeID, handler)
	client.Close()
}
开发者ID:rnd-ua,项目名称:scope,代码行数:30,代码来源:controls.go

示例14: sendBlock

func sendBlock(dn *DataNodeState, blockID BlockID, peers []string) {
	if err := dn.Manager.LockRead(blockID); err != nil {
		log.Println("Couldn't lock", blockID)
		return
	}
	defer dn.Manager.UnlockRead(blockID)

	var peerConn net.Conn
	var forwardTo []string
	var err error
	// Find an online peer
	for i, addr := range peers {
		peerConn, err = net.Dial("tcp", addr)
		if err == nil {
			forwardTo = append(peers[:i], peers[i+1:]...)
			break
		}
	}
	if peerConn == nil {
		log.Println("Couldn't forward block",
			blockID,
			"to any DataNodes in:",
			strings.Join(peers, " "))
		return
	}
	peerCodec := jsonrpc.NewClientCodec(peerConn)
	if Debug {
		peerCodec = LoggingClientCodec(
			peerConn.RemoteAddr().String(),
			peerCodec)
	}
	peer := rpc.NewClientWithCodec(peerCodec)
	defer peer.Close()

	size, err := dn.Store.BlockSize(blockID)
	if err != nil {
		log.Fatal("Stat error: ", err)
	}

	err = peer.Call("Forward",
		&ForwardBlock{blockID, forwardTo, size},
		nil)
	if err != nil {
		log.Fatal("Forward error: ", err)
	}

	err = dn.Store.ReadBlock(blockID, peerConn)
	if err != nil {
		log.Fatal("Copying error: ", err)
	}

	hash, err := dn.Store.ReadChecksum(blockID)
	if err != nil {
		log.Fatalln("Reading checksum:", err)
	}
	err = peer.Call("Confirm", hash, nil)
	if err != nil {
		log.Fatal("Confirm error: ", err)
	}
}
开发者ID:yonglehou,项目名称:golang-distributed-filesystem,代码行数:60,代码来源:rpc.go

示例15: main

func main() {

	conn, err := net.Dial("tcp", "localhost:9000")
	if err != nil {
		log.Fatal("dialing:", err)
	}

	var h codec.MsgpackHandle
	rpcCodec := codec.MsgpackSpecRpc.ClientCodec(conn, &h)
	client := rpc.NewClientWithCodec(rpcCodec)

	args := []string{"foo1", "foo2", "foo3"}
	var reply string
	err = client.Call("Echo123", args, &reply)
	if err != nil {
		log.Fatal("Echo123 error:", err)
	}
	fmt.Println("reply=", reply)

	arg := &EchoArgument{A: "a string", B: "b string", C: "c string"}
	err = client.Call("EchoStruct", arg, &reply)
	if err != nil {
		log.Fatal("EchoStruct error:", err)
	}
	fmt.Println("reply=", reply)

}
开发者ID:KanwarGill,项目名称:trifles,代码行数:27,代码来源:cli.go


注:本文中的net/rpc.NewClientWithCodec函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。