Пример #1
0
func main() {
	PrintVersion()

	runtime.GOMAXPROCS(runtime.NumCPU())

	flags.Parse()

	GMonitor.Init()
	GMonitor.InitServer(*flags.ServerType, *flags.HostId)
	go GMonitor.Report(*flags.MonitorHost)

	InitClientPool()

	rand.Seed(time.Now().UTC().UnixNano())

	localposterAPI := new(LocalPosterAPI)
	rpc.Register(localposterAPI)

	tcpAddr, err := net.ResolveTCPAddr("tcp", *(flags.ListenAddress)+":"+strconv.Itoa(int(*(flags.ListenPort))))
	checkError(err)

	for i := 0; i < LocalPostChannelSize; i++ {
		channel[i] = make(chan *GeneralMessage, LocalPostChannelLength)
	}
	for i := 0; i < LocalPostChannelSize; i++ {
		go ProcessRequest(&channel[i], i)
		go CheckServerStatusUpdate(i, *flags.ServerType, int(*flags.RgId))
	}

	{
		servertype := make([]string, 0)
		rg := make([]int, 0)
		rgid := int(*flags.RgId)
		servertype = append(servertype, POSTERSERVER)
		rg = append(rg, rgid)

		servertype = append(servertype, REDISSERVER)
		rg = append(rg, rgid)

		servertype = append(servertype, MEMCACHEDSERVER)
		rg = append(rg, rgid)
		go serverstatus.SyncServerStatusChanged(*flags.RouterServerURL, servertype, rg)
		go serverstatus.RegistServer(*flags.RouterRegistURL, *flags.ServerType, *flags.HostId, int(*flags.RgId), *flags.ListenAddress, *flags.ListenPort, *flags.CellId)
	}

	listener, err := net.ListenTCP("tcp", tcpAddr)
	checkError(err)
	for {
		conn, err := listener.Accept()
		if err != nil {
			continue
		}
		fmt.Println("local  ", conn.RemoteAddr().Network(), " ", conn.RemoteAddr().String())
		go rpcProcess(conn)

	}
}
Пример #2
0
func main() {

	PrintVersion()

	runtime.GOMAXPROCS(runtime.NumCPU())

	flags.Parse()

	GMonitor.Init()
	GMonitor.InitServer(*flags.ServerType, *flags.HostId)
	go GMonitor.Report(*flags.MonitorHost)

	InitClientPool()

	rand.Seed(time.Now().UTC().UnixNano())

	// set message id
	sid.SetIndex(*flags.SnId)

	loadFile()

	sleeptime = 20

	for i := 0; i < GateWayChannelSize; i++ {
		channel[i] = make(chan *HttpContext, GateWayChannelLength)
	}
	for i := 0; i < GateWayWorkThreadSize; i++ {
		go ProcessHttpRequest(i)
		go CheckServerStatusUpdate(i, *flags.ServerType, int(*flags.RgId))
	}

	{
		servertype := make([]string, 0)
		rg := make([]int, 0)
		servertype = append(servertype, LOCALPOSTSERVER)
		rgid := int(*flags.RgId)
		rg = append(rg, rgid)
		servertype = append(servertype, CACHESERVER)
		rg = append(rg, rgid)
		go serverstatus.SyncServerStatusChanged(*flags.RouterServerURL, servertype, rg)

		go serverstatus.RegistServer(*flags.RouterRegistURL, *flags.ServerType, *flags.HostId, int(*flags.RgId), *flags.ListenAddress, *flags.ListenPort, *flags.CellId)
	}
	var regexpHandler RegexpHandler
	xp, err := regexp.Compile("/api/user/[0-9]*")
	if err == nil {
		regexpHandler.HandleFunc(xp, userinfo) // user infor
	}
	xp, err = regexp.Compile("/api/f/inbox/[0-9]*")
	if err == nil {
		regexpHandler.HandleFunc(xp, friendgroupinbox) // friend group timeline
	}
	xp, err = regexp.Compile("/api/f/outbox/[0-9]*")
	if err == nil {
		regexpHandler.HandleFunc(xp, friendgroupoutbox) // friend group personal page
	}

	xp, err = regexp.Compile("/api/c/inbox/[0-9]*")
	if err == nil {
		regexpHandler.HandleFunc(xp, chatmessageinbox) // chat message recevice
	}
	xp, err = regexp.Compile("/api/c/outbox/[0-9]*")
	if err == nil {
		regexpHandler.HandleFunc(xp, chatmessageoutbox) // chat message sended
	}
	xp, err = regexp.Compile("/api/f/[0-9]*/[0-9]*/[0-9]*/") // post a message to friend group  #api/f/$userid/$indexid/$messageid
	if err == nil {
		regexpHandler.HandleFunc(xp, postfriend) //设定访问的路径
	}

	xp, err = regexp.Compile("/api/c/[0-9]*/[0-9]*/[0-9]*/") // post a message to chat message
	if err == nil {
		regexpHandler.HandleFunc(xp, postchatmessage) //设定访问的路径
	}

	xp, err = regexp.Compile("/(css|js|images)/.*")
	if err == nil {
		regexpHandler.HandleFunc(xp, file) //设定访问的路径
	}

	xp, err = regexp.Compile("/*html")
	if err == nil {
		regexpHandler.HandleFunc(xp, file) //设定访问的路径
	}

	server := &http.Server{
		Addr:           *(flags.ListenAddress) + ":" + strconv.Itoa(int(*(flags.ListenPort))),
		Handler:        regexpHandler,
		ReadTimeout:    120 * time.Second,
		WriteTimeout:   120 * time.Second,
		MaxHeaderBytes: 1 << 20,
	}
	//hostid string, serverType string, rg int, host string, port uint)
	log.Fatal(server.ListenAndServe())
}