Exemple #1
0
// 使之脱离终端,变为守护进程
func (s *GoServer) daemonize() {
	if plugins.IsInArg("-child") {
		return
	}

	args := os.Args[1:]
	args = append(args, "-child=true")

	for sn, _ := range s.ServerCfg {
		newArgs := append(args, "-server="+sn)
		cmd := exec.Command(os.Args[0], newArgs...)
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr
		cmd.Start()

		msg := "Starting the server " + sn + " on [PID]"
		fmt.Println(msg, cmd.Process.Pid)
		s.Log.Add(msg, cmd.Process.Pid)
	}

	// 等待子进程启动.
	time.Sleep(1 * time.Second)

	fmt.Println("GoServer starting successfully.")
}
Exemple #2
0
// 创建监听进程
func (s *GoServer) createSocketsAndListen() bool {
	if !plugins.IsInArg("-child") {
		return false
	}
	childProcessName := plugins.GetArg("-server")
	config := s.ServerCfg[childProcessName]

	if config["name"] == "" {
		s.Log.Add("No found out the worker config for:" + childProcessName)
		return false
	}

	port := ""
	if config["port"] == "" {
		return false
	} else {
		port = config["port"]
	}

	protocol := config["protocol"]
	ip := config["ip"]

	// 开启多核并行执行.
	NCPU := runtime.NumCPU()
	runtime.GOMAXPROCS(NCPU)

	switch config["protocol"] {
	case "tcp":
		return s.createTcpSocketsAndListen(protocol, ip, port, &config)
	case "udp":
		return s.createUdpSocketsAndListen(protocol, ip, port, &config)
	default:
		s.Log.Add("sorry can not support this protocol: %s", config["protocol"])
		return false
	}
}