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


Golang Server.ServeConn方法代码示例

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


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

示例1: 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
}
开发者ID:kedebug,项目名称:golang-programming,代码行数:65,代码来源:paxos.go

示例2: 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
}
开发者ID:khangtoh,项目名称:packer,代码行数:61,代码来源:plugin.go

示例3: 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
}
开发者ID:tartaruszen,项目名称:dbstream,代码行数:10,代码来源:scheduler.go

示例4: 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])
	}
}
开发者ID:funkygao,项目名称:govtil,代码行数:12,代码来源:birpc.go

示例5: Serve

// Serve announces an RPC service on the client using the given name
// (which must currently be unique amongst all clients).
func (c *Client) Serve(clientName string, rpcServer *rpc.Server) error {
	var clientId string
	rpcServer.RegisterName("ClientRPC", clientRPC{}) // TODO better name
	if err := c.Server.Call("Ncnet-publisher.Publish", &clientName, &clientId); err != nil {
		return err
	}
	clientconn, err := ncnet.Dial(c.Importer, clientId)
	if err != nil {
		return err
	}

	go rpcServer.ServeConn(clientconn)
	return nil
}
开发者ID:zenoss,项目名称:rog-go,代码行数:16,代码来源:ncrpc.go

示例6: accept

func (lt *localRPCTransport) accept(server *rpc.Server, listener net.Listener) {
	for {
		conn, err := listener.Accept()
		if err != nil {
			if opError, ok := err.(*net.OpError); ok {
				if opError.Err.Error() == "use of closed network connection" {
					return
				}
			}
			glog.Errorf("rpc.Serve: accept: %s", err.Error())
		}
		go server.ServeConn(conn)
	}
}
开发者ID:Zemnmez,项目名称:cockroach,代码行数:14,代码来源:transport.go

示例7: Dial

func Dial(url string, srv *rpc.Server) (client *rpc.Client, err error) {
	conn, err := direct.Dial(url)
	if err != nil {
		return
	}
	muxed, err := muxconn.Split(conn, 2)
	if err != nil {
		return
	}

	// Server on second, client on first (reverse of above)
	client = rpc.NewClient(muxed[0])
	go srv.ServeConn(muxed[1])
	return
}
开发者ID:funkygao,项目名称:govtil,代码行数:15,代码来源:birpc.go

示例8: serveSingleConn

func serveSingleConn(s *rpc.Server) string {
	l := netListenerInRange(portRangeMin, portRangeMax)

	// Accept a single connection in a goroutine and then exit
	go func() {
		defer l.Close()
		conn, err := l.Accept()
		if err != nil {
			panic(err)
		}

		s.ServeConn(conn)
	}()

	return l.Addr().String()
}
开发者ID:B-Rich,项目名称:packer,代码行数:16,代码来源:server.go

示例9: waitForConnections

func (c *Simple) waitForConnections(rpcs *rpc.Server) {
	for {
		conn, err := c.listener.Accept()
		if err == nil {
			if *use_codec {
				//rpcCodec := codec.GoRpc.ServerCodec(conn, &mh)
				rpcCodec := codec.MsgpackSpecRpc.ServerCodec(conn, &mh)
				go rpcs.ServeCodec(rpcCodec)
			} else {
				go rpcs.ServeConn(conn)
			}
		} else {
			// handle error
			//fmt.Println("ERROR: ", err)
		}
	}
}
开发者ID:narula,项目名称:bench,代码行数:17,代码来源:simple-server.go

示例10: serve

// serve starts listening for RPC calls, and creates a new thread for
// each incoming connection.
func serve(address string, rpcs *rpc.Server) {
	l, err := net.Listen("tcp", address)

	if err != nil {
		panic(err)
	}

	for {
		conn, err := l.Accept()

		if err != nil {
			panic(err)
		}

		go rpcs.ServeConn(conn)
	}
}
开发者ID:rlayte,项目名称:toystore,代码行数:19,代码来源:server.go

示例11: 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.stateMap = make(map[int]State)
	px.maxCanDisregard = -1
	px.lastDoneSignalled = -1

	flag.Parse()
	px.me = me

	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 {
			glog.Fatalf("listen error: ", e)
		}
		px.l = l

		// create a thread to accept RPC connections
		go func() {
			for px.isdead() == false {
				conn, err := px.l.Accept()
				if err == nil && px.isdead() == false {
					atomic.AddInt32(&px.rpcCount, 1)
					go rpcs.ServeConn(conn)
				} else if err == nil {
					conn.Close()
				}
				if err != nil && px.isdead() == false {
					//////fmt.Printf("Paxos(%v) accept: %v\n", me, err.Error())
				}
			}
		}()
	}

	return px
}
开发者ID:anupamaggarwal,项目名称:yak,代码行数:50,代码来源:paxos.go

示例12: StartDNS

func StartDNS(servers []string, rpcs *rpc.Server) *DNSserver {
	// call gob.Register on structures you want
	// Go's RPC library to marshall/unmarshall.
	gob.Register(DNSserver{})
	gob.Register(GetServerArgs{})
	gob.Register(GetServerReply{})

	dns := new(DNSserver)
	dns.servers = servers
	dns.address = portDNS()

	if rpcs != nil {
		// caller will create socket &c
		rpcs.Register(dns)
	} else {
		rpcs = rpc.NewServer()
		rpcs.Register(dns)

		os.Remove(dns.address)
		l, e := net.Listen("unix", dns.address)
		if e != nil {
			log.Fatal("listen error: ", e)
		}
		dns.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 {
				conn, err := dns.l.Accept()
				if err == nil {
					go rpcs.ServeConn(conn)
				}
				if err != nil {
					fmt.Printf("DNSerror: %v\n", err.Error())
				}
			}
		}()
	}

	return dns
}
开发者ID:jmoldow,项目名称:MylarDistributedDB,代码行数:44,代码来源:server.go

示例13: FinishStartServer

//
// servers[] contains the ports of the set of
// servers that will cooperate via Paxos to
// form the fault-tolerant shardmaster service.
// me is the index of the current server in servers[].
//
func FinishStartServer(sm *ShardMaster, servers []string, me int, rpcs *rpc.Server) *ShardMaster {
	os.Remove(servers[me])
	l, e := net.Listen("unix", servers[me])
	if e != nil {
		log.Fatal("listen error: ", e)
	}
	sm.l = l

	go sm.LogWalker()

	// please do not change any of the following code,
	// or do anything to subvert it.

	go func() {
		for sm.dead == false {
			conn, err := sm.l.Accept()
			if err == nil && sm.dead == false {
				if sm.unreliable && (rand.Int63()%1000) < 100 {
					// discard the request.
					conn.Close()
				} else if sm.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)
					}
					go rpcs.ServeConn(conn)
				} else {
					go rpcs.ServeConn(conn)
				}
			} else if err == nil {
				conn.Close()
			}
			if err != nil && sm.dead == false {
				fmt.Printf("ShardMaster(%v) accept: %v\n", me, err.Error())
				sm.Kill()
			}
		}
	}()

	return sm
}
开发者ID:jefesaurus,项目名称:6824final,代码行数:50,代码来源:server.go

示例14: RPCAccept

// RPCAccept accepts connections on the listener and dispatches them to the
// RPC server for service. Unfortunately the native Go rpc.Accept function
// fatals on any accept error, including temporary failures and closure of
// the listener.
func RPCAccept(ln net.Listener, server *rpc.Server) error {
	errClosing := errors.New("use of closed network connection")
	for {
		conn, err := ln.Accept()
		if err != nil {
			if ne, ok := err.(net.Error); ok && ne.Temporary() {
				log.Warningf("RPC accept temporary error: %v", err)
				time.Sleep(1 * time.Second)
				continue
			}
			if oe, ok := err.(*net.OpError); ok && oe.Err.Error() == errClosing.Error() {
				log.Infoln("RPC accept connection closed")
				return nil
			}
			log.Errorf("RPC accept error: %v", err)
			return err
		}
		go server.ServeConn(conn)
	}
}
开发者ID:Cepave,项目名称:lvs-metrics,代码行数:24,代码来源:server.go

示例15: accept

func (lt *localRPCTransport) accept(server *rpc.Server, listener net.Listener) {
	for {
		conn, err := listener.Accept()
		if err != nil {
			if strings.HasSuffix(err.Error(), "use of closed network connection") {
				return
			}
			// TODO(bdarnell): are any transient errors possible here?
			log.Fatalf("localRPCTransport.accept: %s", err)
			continue
		}
		lt.mu.Lock()
		lt.conns[conn] = struct{}{}
		lt.mu.Unlock()
		go func(conn net.Conn) {
			defer func() {
				lt.mu.Lock()
				defer lt.mu.Unlock()
				delete(lt.conns, conn)
			}()
			server.ServeConn(conn)
		}(conn)
	}
}
开发者ID:Hellblazer,项目名称:cockroach,代码行数:24,代码来源:transport.go


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