github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/packer/rpc/dial.go (about)

     1  package rpc
     2  
     3  import (
     4  	"net"
     5  	"net/rpc"
     6  )
     7  
     8  // rpcDial makes a TCP connection to a remote RPC server and returns
     9  // the client. This will set the connection up properly so that keep-alives
    10  // are set and so on and should be used to make all RPC connections within
    11  // this package.
    12  func rpcDial(address string) (*rpc.Client, error) {
    13  	tcpConn, err := tcpDial(address)
    14  	if err != nil {
    15  		return nil, err
    16  	}
    17  
    18  	// Create an RPC client around our connection
    19  	return rpc.NewClient(tcpConn), nil
    20  }
    21  
    22  // tcpDial connects via TCP to the designated address.
    23  func tcpDial(address string) (*net.TCPConn, error) {
    24  	conn, err := net.Dial("tcp", address)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  
    29  	// Set a keep-alive so that the connection stays alive even when idle
    30  	tcpConn := conn.(*net.TCPConn)
    31  	tcpConn.SetKeepAlive(true)
    32  	return tcpConn, nil
    33  }