本文整理汇总了Golang中net/rpc.Server类的典型用法代码示例。如果您正苦于以下问题:Golang Server类的具体用法?Golang Server怎么用?Golang Server使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Server类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: serve
// This serves a single RPC connection on the given RPC server on
// a random port.
func serve(server *rpc.Server) (err error) {
if os.Getenv(MagicCookieKey) != MagicCookieValue {
return errors.New("Please do not execute plugins directly. Packer will execute these for you.")
}
// If there is no explicit number of Go threads to use, then set it
if os.Getenv("GOMAXPROCS") == "" {
runtime.GOMAXPROCS(runtime.NumCPU())
}
minPort, err := strconv.ParseInt(os.Getenv("PACKER_PLUGIN_MIN_PORT"), 10, 32)
if err != nil {
return
}
maxPort, err := strconv.ParseInt(os.Getenv("PACKER_PLUGIN_MAX_PORT"), 10, 32)
if err != nil {
return
}
log.Printf("Plugin minimum port: %d\n", minPort)
log.Printf("Plugin maximum port: %d\n", maxPort)
// Set the RPC port range
packrpc.PortRange(int(minPort), int(maxPort))
var address string
var listener net.Listener
for port := minPort; port <= maxPort; port++ {
address = fmt.Sprintf("127.0.0.1:%d", port)
listener, err = net.Listen("tcp", address)
if err != nil {
err = nil
continue
}
break
}
defer listener.Close()
// Output the address to stdout
log.Printf("Plugin address: %s\n", address)
fmt.Println(address)
os.Stdout.Sync()
// Accept a connection
log.Println("Waiting for connection...")
conn, err := listener.Accept()
if err != nil {
log.Printf("Error accepting connection: %s\n", err.Error())
return
}
// Serve a single connection
log.Println("Serving a plugin connection...")
server.ServeConn(conn)
return
}
示例2: RegistFunctions
func (c *ConsumerServer) RegistFunctions(server *rpc.Server) error {
err := server.Register(new(Call))
if err != nil {
log.Printf("Consumer regist object failed! err:%s \n", err)
return err
}
return nil
}
示例3: ConnectServer
func ConnectServer(addr string, s *rpc.Server) error {
if conn, err := net.Dial("tcp", addr); err == nil {
s.ServeCodec(NewServerCodec(conn))
} else {
return err
}
return nil
}
示例4: RegisterServices
func RegisterServices(server *rpc.Server) {
for _, s := range services {
//TODO : the service type is as of now int, need to find out a
// way how to get the type of an object
server.Register(s)
}
}
示例5: NewDryMartini
// Create a new DryMartini object with its own kademlia and RPC server
func NewDryMartini(listenStr string, keylen int) *DryMartini {
var err error
var s *rpc.Server
var dm *DryMartini
dm = new(DryMartini)
dm.EasyNewFlowIndex = 0
//Initialize key pair
dm.KeyPair, err = rsa.GenerateKey(rand.Reader, keylen)
if err != nil {
dbg.Printf("Failed to generate key! %s", true, err)
panic(1)
}
//Initialize flow struct
dm.Bartender = make(map[UUID]MartiniPick)
dm.Momento = make(map[UUID][]FlowIDSymmKeyPair)
dm.MapFlowIndexToFlowID = make(map[int]FlowInfo)
var host net.IP
var port uint16
host, port, err = kademlia.AddrStrToHostPort(listenStr)
//Initialize our Kademlia
//portStr := strconv.FormatUint(uint64(port), 10)
//var rpcPathStr string = kademlia.RpcPath+portStr
var rpcPathStr = "junk"
dbg.Printf("making new Kademlia with listenStr:%s, rpcPath\n", Verbose, listenStr, rpcPathStr)
dm.KademliaInst, s = kademlia.NewKademlia(listenStr, &rpcPathStr)
kademlia.BucketsAsArray(dm.KademliaInst)
//myMartiniContact <- ip, port, public key
dm.myMartiniContact.NodeIP = host.String()
dm.myMartiniContact.NodePort = port
dm.myMartiniContact.PubKey = dm.KeyPair.PublicKey.N.String()
dm.myMartiniContact.PubExp = dm.KeyPair.PublicKey.E
dbg.Printf("NewDryMartini: making new Kademlia with NodeIP: %s. NodePort:%d\n", Verbose, dm.myMartiniContact.NodeIP, dm.myMartiniContact.NodePort)
/*
if Verbose {
dbg.Printf("NodeIP: %s\n", dm.myMartiniContact.NodeIP)
dbg.Printf("NodePort: %d\n", dm.myMartiniContact.NodePort)
dbg.Printf("PubKey: %s\n", dm.myMartiniContact.PubKey)
dbg.Printf("PubExp: %d\n", dm.myMartiniContact.PubKey)
}*/
//register
err = s.Register(dm)
if err != nil {
dbg.Printf("Failed to register Drymartini! %s", true, err)
panic(1)
}
return dm
}
示例6: Serve
func Serve(s *rpc.Server, l net.Listener) {
for {
conn, err := l.Accept()
if err != nil {
log.Fatal("rpc.Serve: accept:", err.Error())
}
go s.ServeCodec(NewServerCodec(conn))
}
}
示例7: registerComponent
// registerComponent registers a single Packer RPC component onto
// the RPC server. If id is true, then a unique ID number will be appended
// onto the end of the endpoint.
//
// The endpoint name is returned.
func registerComponent(server *rpc.Server, name string, rcvr interface{}, id bool) string {
endpoint := name
if id {
fmt.Sprintf("%s.%d", endpoint, atomic.AddUint64(&endpointId, 1))
}
server.RegisterName(endpoint, rcvr)
return endpoint
}
示例8: waitForConnExit
func waitForConnExit(c net.Conn, server *rpc.Server) (ret chan bool) {
ret = make(chan bool)
go func() {
tcpConn := c.(*net.TCPConn)
tcpConn.SetKeepAlive(true)
server.ServeConn(c)
ret <- true
}()
return ret
}
示例9: ServeRedis
func ServeRedis(l net.Listener, s *rpc.Server) error {
for {
conn, err := l.Accept()
if err != nil {
return err
}
codec := NewRedisServerCodec(conn)
go s.ServeCodec(codec)
}
}
示例10: ServeMsgpack
func ServeMsgpack(l net.Listener, s *rpc.Server) error {
for {
conn, err := l.Accept()
if err != nil {
return err
}
codec := codec.MsgpackSpecRpc.ServerCodec(conn, &msgpackHandle)
go s.ServeCodec(codec)
}
}
示例11: DispatchForever
func DispatchForever(connch <-chan net.Conn, srv *rpc.Server, clientch chan<- *rpc.Client) {
for conn := range connch {
muxed, err := muxconn.Split(conn, 2)
if err != nil {
log.Println("birpc: Failed to mux incoming connection from", conn.RemoteAddr().String(), "to", conn.LocalAddr().String(), ", dropping")
continue
}
// Server on first muxed conn, client on second
go srv.ServeConn(muxed[0])
clientch <- rpc.NewClient(muxed[1])
}
}
示例12: NewJSONRPCHandler
// NewJSONRPCHandler makes a JSON-RPC handler for s.
func NewJSONRPCHandler(s *rpc.Server) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
w.Header().Set("Content-Type", "application/json")
var b bytes.Buffer
var codec = jsonrpc.NewServerCodec(&readWriteCloser{r: r.Body, w: &b})
if err := s.ServeRequest(codec); err != nil {
panic(err)
}
codec.Close()
io.Copy(w, &b)
})
}
示例13: NewServerCodec
// NewServerCodec returns a new rpc.ServerCodec using JSON-RPC 2.0 on conn,
// which will use srv to execute batch requests.
//
// If srv is nil then rpc.DefaultServer will be used.
func NewServerCodec(conn io.ReadWriteCloser, srv *rpc.Server) rpc.ServerCodec {
if srv == nil {
srv = rpc.DefaultServer
}
srv.Register(JSONRPC2{})
return &serverCodec{
dec: json.NewDecoder(conn),
enc: json.NewEncoder(conn),
c: conn,
srv: srv,
pending: make(map[uint64]*json.RawMessage),
}
}
示例14: RegistFunctions
func (p *ProducerServer) RegistFunctions(server *rpc.Server) error {
err := server.Register(new(Call))
if err != nil {
log.Printf("register function failed!err:%s \n", err)
return err
}
err = server.Register(new(PMSync))
if err != nil {
log.Printf("regist PMSync failed! err:%s \n", err)
return err
}
return nil
}
示例15: Make
//
// the application wants to create a paxos peer.
// the ports of all the paxos peers (including this one)
// are in peers[]. this servers port is peers[me].
//
func Make(peers []string, me int, rpcs *rpc.Server) *Paxos {
px := &Paxos{}
px.peers = peers
px.me = me
// Your initialization code here.
if rpcs != nil {
// caller will create socket &c
rpcs.Register(px)
} else {
rpcs = rpc.NewServer()
rpcs.Register(px)
// prepare to receive connections from clients.
// change "unix" to "tcp" to use over a network.
os.Remove(peers[me]) // only needed for "unix"
l, e := net.Listen("unix", peers[me])
if e != nil {
log.Fatal("listen error: ", e)
}
px.l = l
// please do not change any of the following code,
// or do anything to subvert it.
// create a thread to accept RPC connections
go func() {
for px.dead == false {
conn, err := px.l.Accept()
if err == nil && px.dead == false {
if px.unreliable && (rand.Int63()%1000) < 100 {
// discard the request.
conn.Close()
} else if px.unreliable && (rand.Int63()%1000) < 200 {
// process the request but force discard of reply.
c1 := conn.(*net.UnixConn)
f, _ := c1.File()
err := syscall.Shutdown(int(f.Fd()), syscall.SHUT_WR)
if err != nil {
fmt.Printf("shutdown: %v\n", err)
}
px.rpcCount++
go rpcs.ServeConn(conn)
} else {
px.rpcCount++
go rpcs.ServeConn(conn)
}
} else if err == nil {
conn.Close()
}
if err != nil && px.dead == false {
fmt.Printf("Paxos(%v) accept: %v\n", me, err.Error())
}
}
}()
}
return px
}