示例#1
0
文件: bortplug.go 项目: voxadam/bort
func main() {
	if err := rpc.Register(plug); err != nil {
		log.Fatal(err)
	}

	flag.Parse()
	config()
	bort.PluginInit(cfg.OutboxSize)

	listen, err := net.Listen("tcp", cfg.Address)
	if err != nil {
		log.Fatalln(err)
	}
	for {
		con, err := listen.Accept()
		if err != nil {
			log.Println(err)
			time.Sleep(1 * time.Second)
			continue
		}
		log.Println("connected to bort")
		rpc.ServeConn(con)
		log.Println("disconnected from bort")
	}
}
示例#2
0
func (rs *RpcServer) Start() error {

	serverAddr, err := net.ResolveTCPAddr(rs.net, rs.addr)
	if err != nil {
		return err
	}
	serverListener, err := net.ListenTCP(rs.net, serverAddr)
	if err != nil {
		return err
	}

	rs.listener = serverListener

	go func(listener *net.TCPListener) {
		for {
			conn, err := listener.AcceptTCP()
			if err != nil {
				global.Logger.Warn("the RPC server accept the client link failure: %s", err.Error())
				rs.Stop()
				global.Logger.Warn("the RPC server has stopped!")
				return
			}
			rpc.ServeConn(conn)
		}
	}(rs.listener)

	return nil
}
示例#3
0
func main() {
	arith := new(Arith)
	rpc.Register(arith)
	/*
		rpc.HandleHTTP()

		err := http.ListenAndServe(":1234", nil)
		if err != nil {
			fmt.Println(err.Error())
		}
	*/

	tcpAddr, err := net.ResolveTCPAddr("tcp", ":1234")
	if err != nil {
		log.Fatal("resovleTCPAddr failed.", err)
		return
	}

	ln, err := net.ListenTCP("tcp", tcpAddr)
	if err != nil {
		log.Fatal("listen failed.", err)
		return
	}

	for {
		conn, err := ln.Accept()
		if err != nil {
			continue
		}

		// go routine
		go rpc.ServeConn(conn)
	}
}
示例#4
0
文件: server.go 项目: alvalea/go-rpc
func main() {

	arith := new(Arith)
	rpc.Register(arith)

	tcpAddr, err := net.ResolveTCPAddr("tcp", ":1234")
	checkError(err)

	listener, err := net.ListenTCP("tcp", tcpAddr)
	checkError(err)

	/* This works:
	rpc.Accept(listener)
	*/
	/* and so does this:
	 */
	for {
		conn, err := listener.Accept()
		if err != nil {
			continue
		}
		rpc.ServeConn(conn)
	}

}
示例#5
0
文件: server.go 项目: nikvdp/Vimfiles
func (this *daemon) loop() {
	conn_in := make(chan net.Conn)
	go func() {
		for {
			c, err := this.listener.Accept()
			if err != nil {
				panic(err)
			}
			conn_in <- c
		}
	}()

	timeout := time.Duration(g_config.CloseTimeout) * time.Second
	countdown := time.NewTimer(timeout)

	for {
		// handle connections or server CMDs (currently one CMD)
		select {
		case c := <-conn_in:
			rpc.ServeConn(c)
			countdown.Reset(timeout)
			runtime.GC()
		case cmd := <-this.cmd_in:
			switch cmd {
			case daemon_close:
				return
			}
		case <-countdown.C:
			return
		}
	}
}
示例#6
0
func runServer() {
	go processCommands()
	go runHTTPServer()

	s := new(Server)
	rpc.Register(s)
	l, err := net.Listen("unix", sock)
	defer os.Remove(sock)
	if err != nil {
		fatal(err)
	}
	s.quit = make(chan bool)
	conns := make(chan net.Conn)
	go func() {
		for {
			c, err := l.Accept()
			if err != nil {
				fatal(err)
			}
			conns <- c
		}
	}()
	for {
		select {
		case c := <-conns:
			go rpc.ServeConn(c)
		case <-s.quit:
			fmt.Println("Quitting.")
			// Give shutdown RPC time to return normally.
			time.Sleep(10 * time.Millisecond)
			return
		}
	}
}
示例#7
0
func main() {

	math := new(Math)
	rpc.Register(math)

	tcpAddr, err := net.ResolveTCPAddr("tcp", ":1234")
	if err != nil {
		fmt.Println("Fatal error ", err.Error())
		os.Exit(1)
	}

	listener, err := net.ListenTCP("tcp", tcpAddr)
	if err != nil {
		fmt.Println("Fatal error ", err.Error())
		os.Exit(1)
	}

	for {
		conn, err := listener.Accept()
		if err != nil {
			continue
		}
		rpc.ServeConn(conn)
	}

}
示例#8
0
func BenchmarkGOBRPC_pipe(b *testing.B) {
	cli, srv := net.Pipe()
	go rpc.ServeConn(srv)
	client := rpc.NewClient(cli)
	defer client.Close()
	benchmarkRPC(b, client)
}
示例#9
0
func main() {
	err := rpc.Register(new(Calc))
	if err != nil {
		fmt.Println(err)
		return
	}

	listen, err := net.Listen("tcp", ":6000")
	if err != nil {
		fmt.Println(err)
		return
	}
	defer listen.Close()

	for {
		conn, err := listen.Accept()
		if err != nil {
			fmt.Println(err)
			continue
		}
		defer conn.Close()

		go rpc.ServeConn(conn)
	}

}
示例#10
0
func (server *UmgmtServer) Serve() error {
	relog.Info("started umgmt server: %v", server.listener.Addr())
	for !server.quit {
		conn, err := server.listener.Accept()
		if err != nil {
			if checkError(err, syscall.EINVAL) {
				if server.quit {
					return nil
				}
				return err
			}
			// syscall.EMFILE, syscall.ENFILE could happen here if you run out of file descriptors
			relog.Error("accept error %v", err)
			continue
		}

		server.Lock()
		server.connMap[conn] = true
		server.Unlock()

		rpc.ServeConn(conn)

		server.Lock()
		delete(server.connMap, conn)
		server.Unlock()
	}
	return nil
}
示例#11
0
文件: rpc.go 项目: woozhijun/judge
func Start() {
	if !g.Config().Rpc.Enabled {
		return
	}
	addr := g.Config().Rpc.Listen
	tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
	if err != nil {
		log.Fatalf("net.ResolveTCPAddr fail: %s", err)
	}

	listener, err := net.ListenTCP("tcp", tcpAddr)
	if err != nil {
		log.Fatalf("listen %s fail: %s", addr, err)
	} else {
		log.Println("rpc listening", addr)
	}

	rpc.Register(new(Judge))

	for {
		conn, err := listener.Accept()
		if err != nil {
			log.Printf("listener.Accept occur error: %s", err)
			continue
		}
		go rpc.ServeConn(conn)
	}
}
示例#12
0
func RunCatServerListener(listenURL string) {

	println("Server opens connection.")
	csr := CatServerRPC{make(map[string]*CatInfo)}
	rpc.Register(&csr)

	l, e := net.Listen("tcp", ":1887")
	if e != nil {
		log.Println("listen error:", e)
	}

	conChan := make(chan *net.Conn, 100)

	// Close after 2 seconds, make it flap..
	time.AfterFunc(2*time.Second, func() {
		println("-- Server flaps connection.")
		l.Close()
		for a := range conChan {
			(*a).Close()
		}
	})

	for {
		c, e := l.Accept()
		if e != nil {
			break // We cause this error.
		}
		conChan <- &c
		go rpc.ServeConn(c)
	}

	println("Server exiting.")

}
示例#13
0
文件: rpc.go 项目: yubo/gotty
func rpcInit() error {
	cmd := new(Cmd)

	rpc.Register(cmd)

	l, err := net.Listen("unix", GlobalOpt.UnixSocket)
	if err != nil {
		return err
	}
	go func() {
		var tempDelay time.Duration
		for {
			conn, err := l.Accept()
			if err != nil {
				if tempDelay == 0 {
					tempDelay = 5 * time.Millisecond
				} else {
					tempDelay *= 2
				}
				if max := 1 * time.Second; tempDelay > max {
					tempDelay = max
				}
				time.Sleep(tempDelay)
				continue
			}
			tempDelay = 0
			go func() {
				rpc.ServeConn(conn)
			}()
		}
	}()
	return nil
}
示例#14
0
文件: server.go 项目: rosrad/go-rep
func (this *daemon) loop() {
	conn_in := make(chan net.Conn)
	go func() {
		for {
			c, err := this.listener.Accept()
			if err != nil {
				panic(err)
			}
			conn_in <- c
		}
	}()
	for {
		// handle connections or server CMDs (currently one CMD)
		select {
		case c := <-conn_in:
			rpc.ServeConn(c)
			runtime.GC()
		case cmd := <-this.cmd_in:
			switch cmd {
			case daemon_close:
				return
			}
		}
	}
}
示例#15
0
文件: server.go 项目: pocke/lemonade
func Serve(c *lemon.CLI) error {
	port := c.Port
	allowIP := c.Allow
	LineEndingOpt = c.LineEnding
	ra, err := iprange.New(allowIP)
	if err != nil {
		return err
	}

	addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf(":%d", port))
	if err != nil {
		return err
	}
	l, err := net.ListenTCP("tcp", addr)
	if err != nil {
		return err
	}

	for {
		conn, err := l.Accept()
		if err != nil {
			log.Println(err)
			continue
		}
		log.Printf("Request from %s", conn.RemoteAddr())
		if !ra.InlucdeConn(conn) {
			continue
		}
		connCh <- conn
		rpc.ServeConn(conn)
	}
}
示例#16
0
func Handle(listener *Listener, coordinator Coordinator) {
	coordinator.Register()

	for {
		conn, err := listener.Accept()
		if err != nil {
			log.Printf("Error: %s\n", err)
			continue
		}
		class, err := readOneByte(conn)
		if err != nil {
			conn.Close()
		}

		switch class {
		case 'm':
			log.Printf("Minion has connected.\n")
			mConn := conn.(*Conn)
			go coordinator.Handle(rpc.NewClient(conn), mConn)
		case 'r':
			log.Printf("Administrator has connected.\n")
			go rpc.ServeConn(conn)
		default:
			log.Printf("No idea what that was. Closing. Value %c\n", class)
			conn.Close()
		}
	}
}
示例#17
0
文件: rpc.go 项目: peteryj/graph
func Start() {
	if !g.Config().Rpc.Enabled {
		return
	}
	addr := g.Config().Rpc.Listen
	tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
	if err != nil {
		log.Fatalf("net.ResolveTCPAddr fail: %s", err)
	}

	listener, err := net.ListenTCP("tcp", tcpAddr)
	if err != nil {
		log.Fatalf("listen %s fail: %s", addr, err)
	} else {
		log.Println("rpc listening", addr)
	}

	rpc.Register(new(Graph))

	go func() {
		var tempDelay time.Duration // how long to sleep on accept failure
		for {
			conn, err := listener.Accept()
			if err != nil {
				if tempDelay == 0 {
					tempDelay = 5 * time.Millisecond
				} else {
					tempDelay *= 2
				}
				if max := 1 * time.Second; tempDelay > max {
					tempDelay = max
				}
				time.Sleep(tempDelay)
				continue
			}
			tempDelay = 0
			go func() {
				e := connects.insert(conn)
				defer connects.remove(e)
				rpc.ServeConn(conn)
			}()
		}
	}()

	select {
	case <-Close_chan:
		log.Println("api recv sigout and exit...")
		listener.Close()
		Close_done_chan <- 1

		connects.Lock()
		for e := connects.list.Front(); e != nil; e = e.Next() {
			e.Value.(net.Conn).Close()
		}
		connects.Unlock()

		return
	}

}
示例#18
0
文件: slave.go 项目: spicavigo/gomr
func Run(configFile string, port string) {

	t := new(Job)
	resetJob(t)
	config := configparser.ParseFile(configFile)
	t.NumMap = config.NumMap
	t.NumReduce = config.NumReduce
	t.Master = config.Master
	t.ReduceDataPort = config.ReduceDataPort

	os.RemoveAll(config.MapDir)
	os.RemoveAll(config.ReduceDir)
	os.Mkdir(config.MapDir, 0755)
	os.Mkdir(config.ReduceDir, 0755)

	rpc.Register(t)

	listener, e := net.Listen("tcp", fmt.Sprintf(":%s", port))
	Port = port

	if e != nil {
		log.Fatal("listen error:", e)
	}
	for {
		if conn, err := listener.Accept(); err != nil {
			log.Fatal("accept error: " + err.Error())
		} else {
			go rpc.ServeConn(conn)
		}
	}
}
示例#19
0
文件: server.go 项目: rinor/cgrates
func (s *Server) ServeGOB(addr string) {
	if !s.rpcEnabled {
		return
	}
	lGOB, e := net.Listen("tcp", addr)
	if e != nil {
		log.Fatal("ServeGOB listen error:", e)
	}
	Logger.Info(fmt.Sprintf("Starting CGRateS GOB server at <%s>.", addr))
	errCnt := 0
	var lastErrorTime time.Time
	for {
		conn, err := lGOB.Accept()
		if err != nil {
			Logger.Err(fmt.Sprintf("<CGRServer> GOB accept error: <%s>", err.Error()))
			now := time.Now()
			if now.Sub(lastErrorTime) > time.Duration(5*time.Second) {
				errCnt = 0 // reset error count if last error was more than 5 seconds ago
			}
			lastErrorTime = time.Now()
			errCnt += 1
			if errCnt > 50 { // Too many errors in short interval, network buffer failure most probably
				break
			}
			continue
		}

		//utils.Logger.Info(fmt.Sprintf("<CGRServer> New incoming connection: %v", conn.RemoteAddr()))
		go rpc.ServeConn(conn)
	}
}
示例#20
0
文件: main.go 项目: rdterner/debug
func main() {
	log.SetFlags(0)
	log.SetPrefix("ogleproxy: ")
	flag.Parse()
	if *textFlag == "" {
		fmt.Printf("OGLE BAD\n")
		flag.Usage()
		os.Exit(2)
	}
	s, err := server.New(*textFlag)
	if err != nil {
		fmt.Printf("OGLE BAD\n%s\n", err)
		os.Exit(2)
	}
	err = rpc.Register(s)
	if err != nil {
		fmt.Printf("OGLE BAD\n%s\n", err)
		os.Exit(2)
	}
	fmt.Println("OGLE OK")
	log.Print("start server")
	// TODO: Usually done in a go.
	rpc.ServeConn(&rwc{
		os.Stdin,
		os.Stdout,
	})
	log.Print("finish server")
}
示例#21
0
文件: worker.go 项目: rprp/hivego
//启动HTTP服务监控指定端口
func ListenAndServer(port string) { // {{{
	executer := new(CmdExecuter)
	rpc.Register(executer)

	l.Infoln("Worker is running Port:", port)

	tcpAddr, err := net.ResolveTCPAddr("tcp", port)
	checkErr(err)

	listener, err := net.ListenTCP("tcp", tcpAddr)
	checkErr(err)

	go func() {
		for {
			conn, err := listener.Accept()
			if err != nil {
				continue
			}
			go func() {
				rpc.ServeConn(conn)
			}()
		}
	}()

} // }}}
示例#22
0
文件: tcpserver.go 项目: Kluchy/EDHT
func main() {

	tuple := new(Tuple)
	hashtable := new(Hashtable)
	rpc.Register(tuple)
	rpc.Register(hashtable)

	tcpAddr, err := net.ResolveTCPAddr("tcp", ":1234")
	checkError(err)
	fmt.Println(tcpAddr)
	listener, err := net.ListenTCP("tcp", tcpAddr)
	checkError(err)

	/* This works:
	rpc.Accept(listener)
	*/
	/* and so does this:
	 */
	for {
		conn, err := listener.Accept()
		if err != nil {
			continue
		}
		rpc.ServeConn(conn)
	}
	fmt.Println("END OF MAIN")

}
示例#23
0
func (self *Service) Loop() {
	listener, err := net.Listen(GetServiceSocket(self.tcpPort))
	if err != nil {
		panic(err)
	}
	connInput := make(chan net.Conn, 2)
	go func() {
		for {
			conn, err := listener.Accept()
			if err != nil {
				log.Printf("Service cannot accept new connections: %v", err)
				self.Close()
			}
			connInput <- conn
		}
	}()
	for {
		select {
		case conn := <-connInput:
			rpc.ServeConn(conn)
			runtime.GC()
		case command := <-self.commandInput:
			if command == SERVICE_COMMAND_CLOSE {
				return
			} else if command == SERVICE_COMMAND_RELOAD {
				self.ReloadImpl()
			}
		}
	}
}
示例#24
0
func server(port string) {
	fmt.Println("Gosync server starting at port", port)
	// create a list of ServFiles on server
	fileList = CreateServFileList(".")
	// Print the serv file list
	fmt.Println("Files being served:")
	for _, file := range fileList {
		fmt.Println(file.Name, ":", file.Sha1)
	}

	rpc.Register(new(Server))
	listener, err := net.Listen("tcp", ":"+port)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("Awaiting connection")
	for {
		c, err := listener.Accept()
		if err != nil {
			continue
		}
		fmt.Println("Accepted connection from", c.RemoteAddr())
		go rpc.ServeConn(c)
	}
}
示例#25
0
func RunReceiverRPC() {
	log.Info("Starting Receiver RPC\n")
	ln, err := net.ListenUnix("unix", appAddr)
	if err != nil {
		log.Errorf("Failed to start RPC server: %v", err)
		return
	}

	Receiver = receiverrpc.Receiver{}
	rpc.Register(&Receiver)
	log.Infof("RPC server is listening on %s\n", appAddr.String())

	defer func() {
		// close the listener sock
		log.Info("Closing listener socket.\n")
		ln.Close()
	}()

	for {
		ln.SetDeadline(time.Now().Add(time.Second))
		conn, err := ln.AcceptUnix()
		if err != nil {
			if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
				// just accept timeout, not an error
				continue
			}
			log.Error("Failed to accept: %s", err.Error())
			continue
		}
		rpc.ServeConn(conn)
	}
}
示例#26
0
func (ts *TestServer) Start() error {
	var err error

	ts.stopCh = make(chan struct{})

	ts.listener, err = net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		return err
	}

	go func() {
		for {
			conn, err := ts.listener.Accept()
			if err != nil {
				select {
				case <-ts.stopCh:
					return
				default:
					fmt.Println(err)
				}
			} else {
				go rpc.ServeConn(conn)
			}
		}
	}()

	return nil
}
示例#27
0
func main() {
	var err error
	var clientConn net.Conn
	var errLoggedListen, errLoggedClientConn = false, false
	runtime.GOMAXPROCS(4)
	if err = rpc.Register(rpcService); err != nil {
		os.Exit(1)
	}
	for {
		if rpcListener == nil {
			if rpcListener, err = net.Listen("tcp", ":9669"); err != nil {
				rpcListener = nil
				if !errLoggedListen {
					errLoggedListen = true
					logf("Failed to establish TCP listener, will keep retrying. Error: %+v", err)
				}
			} else {
				errLoggedListen = false
				logf("TCP listener established.")
			}
		}
		if rpcListener != nil {
			if clientConn, err = rpcListener.Accept(); err == nil {
				errLoggedClientConn = false
				go rpc.ServeConn(clientConn)
			} else if !errLoggedClientConn {
				errLoggedClientConn = true
				rpcListener.Close()
				rpcListener = nil
				logf("Failed to accept TCP connection, will re-establish TCP listener. Error: %+v", err)
			}
		}
	}
}
func (cli *CliRpcService) Start() error {
	var err error

	cli.stopCh = make(chan struct{})

	cli.listener, err = net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		return err
	}

	go func() {
		for {
			conn, err := cli.listener.Accept()
			if err != nil {
				select {
				case <-cli.stopCh:
					return
				default:
					fmt.Println(err)
				}
			} else {
				go rpc.ServeConn(conn)
			}
		}
	}()

	return nil
}
示例#29
0
func acceptorRun(ln net.Listener) {
	for {
		c, err := ln.Accept()
		if err != nil {
			continue
		}
		rpc.ServeConn(c)
	}
}
示例#30
0
func child() {
	logger := log.New(os.Stderr, "child: ", log.Ldate|log.Ltime)

	logger.Println("child mode")
	echo := new(Echo)
	rpc.Register(echo)
	m := moreio.NewReadWriteCloser(os.Stdin, os.Stdout)
	rpc.ServeConn(m)
}