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) } }
// 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 }
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() }
// 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 }
// 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")) }
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 }
// 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 }
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) }
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() }
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) } }
// 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 }
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 }, ) }
// 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() }
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) } }
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) }
func TestWebSocket_Query(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 := &QueryMessage{ Src: "nobody", MessageID: "foobar", } var resp QueryMessageResp err = cli.Call("SM.Query", args, &resp) if err != nil { t.Fatal(err) } want := "DELIVERED" if resp.MsgState != want { t.Fatalf("unexpected message state: want %q, have %q", want, resp.MsgState) } }
// NewDirectHTTPRPCClient creates a rpc http client func NewDirectHTTPRPCClient(c *Client, clientCodecFunc ClientCodecFunc, network, address string, path string, timeout time.Duration) (*rpc.Client, error) { if path == "" { path = rpc.DefaultRPCPath } 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, "tcp", address, c.TLSConfig) //or conn:= tls.Client(netConn, &config) conn = net.Conn(tlsConn) } else { conn, err = net.DialTimeout("tcp", address, timeout) } if err != nil { return nil, err } io.WriteString(conn, "CONNECT "+path+" HTTP/1.0\n\n") // Require successful HTTP response // before switching to RPC protocol. resp, err := http.ReadResponse(bufio.NewReader(conn), &http.Request{Method: "CONNECT"}) if err == nil && resp.Status == connected { if c == nil || c.PluginContainer == nil { return rpc.NewClientWithCodec(clientCodecFunc(conn)), nil } return rpc.NewClientWithCodec(newClientCodecWrapper(c.PluginContainer, clientCodecFunc(conn))), nil } if err == nil { err = errors.New("unexpected HTTP response: " + resp.Status) } conn.Close() return nil, &net.OpError{ Op: "dial-http", Net: network + " " + address, Addr: nil, Err: err, } }
func RPCClientWithCodec(tcp_addr string) *rpc.Client { conn, err := net.DialTimeout("tcp", tcp_addr, time.Second*5) if goutils.CheckErr(err) { return nil } encBuf := bufio.NewWriter(conn) codec := &RPCgobClientCodec{conn, gob.NewDecoder(conn), gob.NewEncoder(encBuf), encBuf} c := rpc.NewClientWithCodec(codec) return c }
func newHClient(hrpcAddr string) (*hClient, error) { hcr := hClient{} conn, err := net.Dial("tcp", hrpcAddr) if err != nil { return nil, errors.New(fmt.Sprintf("Error contacting the HRPC server "+ "at %s: %s", hrpcAddr, err.Error())) } hcr.rpcClient = rpc.NewClientWithCodec(&HrpcClientCodec{rwc: conn}) return &hcr, nil }
// connect attempts a single connection attempt. On success, updates `c.conn`. func (c *Client) connect() error { conn, err := tlsDialHTTP(c.addr.NetworkField, c.addr.StringField, c.tlsConfig) if err != nil { return err } c.conn.Store(internalConn{ conn: conn, client: rpc.NewClientWithCodec(codec.NewClientCodec(conn)), }) return nil }
func rpcClient(t *testing.T, s *Server) *rpc.Client { addr := s.config.RPCAddr conn, err := net.DialTimeout("tcp", addr.String(), time.Second) if err != nil { t.Fatalf("err: %v", err) } // Write the Consul RPC byte to set the mode conn.Write([]byte{byte(rpcConsul)}) cc := codec.GoRpc.ClientCodec(conn, msgpackHandle) return rpc.NewClientWithCodec(cc) }
// StartProviderCodec starts a provider-style plugin application at the given // path and args, and returns an RPC client that communicates with the plugin // using the ClientCodec returned by f over the plugin's Stdin and Stdout. The // writer passed to output will receive output from the plugin's stderr. // Closing the RPC client returned from this function will shut down the plugin // application. func StartProviderCodec( f func(io.ReadWriteCloser) rpc.ClientCodec, output io.Writer, path string, args ...string, ) (*rpc.Client, error) { pipe, err := start(makeCommand(output, path, args)) if err != nil { return nil, err } return rpc.NewClientWithCodec(f(pipe)), nil }
func DialMsgpack(addr string, timeout time.Duration) (*rpc.Client, error) { conn, err := net.DialTimeout("tcp", addr, timeout) if err != nil { return nil, err } _, err = conn.Write([]byte{RpcProto}) if err != nil { conn.Close() return nil, err } codec := codec.MsgpackSpecRpc.ClientCodec(conn, &msgpackHandle) return rpc.NewClientWithCodec(codec), nil }
func ExampleNewClientCodec() { conn, err := amqp.Dial("amqp://*****:*****@localhost:5672/") if err != nil { log.Fatal(err) } clientCodec, err := amqprpc.NewClientCodec(conn, "rpc_queue", amqprpc.GobCodec{}) if err != nil { log.Fatal(err) } client := rpc.NewClientWithCodec(clientCodec) client.Call("Foo.Bar", struct{}{}, &struct{}{}) }
func main() { addresses := map[int]string{ 1: "127.0.0.1:10000", 2: "127.0.0.1:10001", 3: "127.0.0.1:10002", } w := new(Worker) if err := rpc.Register(w); err != nil { log.Fatal(err) } for _, address := range addresses { if err := runServer(address); err != nil { log.Fatal(err) } } clients := make(map[int]*rpc.Client) for id, address := range addresses { conn, err := net.Dial("tcp", address) if err != nil { log.Fatal("dial", err) } cc := &ccodec{ rwc: conn, } clients[id] = rpc.NewClientWithCodec(cc) } for i := 0; i < 10; i++ { /* client := clients[1] if client == nil { log.Fatal("Worker is nil") } */ for id, client := range clients { query := new(Query) query.d = []byte(fmt.Sprintf("id:%d Rand: %d", id, rand.Int())) reply := new(Reply) if err := client.Call("Worker.Receive", query, reply); err != nil { log.Fatal("call", err) } fmt.Printf("Returned: %s\n", string(reply.d)) } } }
func (c *Client) fillRpcClient(conn net.Conn) { switch c.encodingType { case EncodingTypeJson: c.Client = jsonrpc.NewClient(conn) case EncodingTypeMsgpack: rpcCodec := codec.MsgpackSpecRpc.ClientCodec(conn, new(codec.MsgpackHandle)) c.Client = rpc.NewClientWithCodec(rpcCodec) case EncodingTypeGob: c.Client = rpc.NewClient(conn) default: c.Client = jsonrpc.NewClient(conn) } }
// connect attempts a single connection attempt. On success, updates `c.conn`. func (c *Client) connect() error { conn, err := tlsDialHTTP(c.addr.NetworkField, c.addr.StringField, c.tlsConfig) if err != nil { return err } if oldConn := (*internalConn)(atomic.SwapPointer(&c.conn, unsafe.Pointer(&internalConn{ conn: conn, client: rpc.NewClientWithCodec(codec.NewClientCodec(conn)), }))); oldConn != nil { oldConn.conn.Close() } return nil }
// Dial connects to a Thrift RPC server at the specified network address using the specified protocol. func Dial(network, address string, framed bool, protocol Protocol) (*rpc.Client, error) { conn, err := net.Dial(network, address) if err != nil { return nil, err } codec := &clientCodec{ transport: conn, protocol: protocol, } if framed { codec.transport = NewFramedReadWriteCloser(conn, DefaultMaxFrameSize) } return rpc.NewClientWithCodec(codec), nil }
func TestAll(t *testing.T) { onceAll.Do(func() { go listenAndServeArithAndEchoService("tcp", ":1414") }) conn, err := net.Dial("tcp", "127.0.0.1:1414") if err != nil { t.Fatalf(`net.Dial("tcp", "127.0.0.1:1414"): %v`, err) } client := rpc.NewClientWithCodec(NewClientCodec(conn)) defer client.Close() testArithClient(t, client) testEchoClient(t, client) }
func newClientWithMux(mux *MuxConn, streamId uint32) (*Client, error) { clientConn, err := mux.Dial(streamId) if err != nil { return nil, err } var h codec.MsgpackHandle clientCodec := codec.GoRpc.ClientCodec(clientConn, &h) return &Client{ mux: mux, client: rpc.NewClientWithCodec(clientCodec), closeMux: false, }, nil }