예제 #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
파일: opendecoupler.go 프로젝트: R358/brace
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)
}