예제 #1
0
func listener(mgr *session.ConnMgr, add *net.TCPAddr) {
	fmt.Println("glivesvr>>>server port %d listen start.", add.Port)
	l, e := net.ListenTCP("tcp", add)
	if e != nil {
		log.Error(e.Error())
	}
	for {
		c, e := l.Accept()
		if e != nil {
			log.Error(e.Error())
		}
		cli := mgr.NewSession(&c)
		go cli.Start()
	}
}
예제 #2
0
func main() {

	file, _ := os.Getwd()

	fmt.Println("glivesvr>>>Glive Server running...")
	e := log.NewLogger(1*log.MB, "/Users/Yate/DevEnv/logs/glive.log")
	if e != nil {
		fmt.Println(e.Error())
		os.Exit(0)
	}

	conf := flag.String("-f", file+"/example.conf", "server config file")
	flag.Parse()
	if *conf == "" {
		println("not has -f paramter")
		os.Exit(0)
	}
	f, e := os.Open(*conf)
	if e != nil {
		println(e.Error())
		os.Exit(0)
	}
	defer f.Close()

	fmt.Println("glivesvr>>>config file reading...")
	paramter := make(map[string]string)
	r := bufio.NewReader(f)
	for v, _, e := r.ReadLine(); e == nil; v, _, e = r.ReadLine() {
		if m, _ := regexp.Match("^#.*", v); m {

		} else if m, _ := regexp.Match(".*=.*", v); m {
			s := string(v)
			if n := strings.Index(s, "="); n != -1 {
				paramter[s[:n]] = s[n+1:]
			}
		}
	}

	p := ":" + paramter["port"]
	add, e := net.ResolveTCPAddr("tcp", p)
	if e != nil {
		log.Error(e.Error())
	}

	mgr := session.ConnMgr{Ob: &session.Observer{Ch: make(chan int)}, Sessions: make(map[int64]*session.Client)}
	go listener(&mgr, add)

	for {
		switch <-mgr.Ob.Ch {
		case 1:
			os.Exit(0)
		default:
			os.Exit(0)
		}
	}
}