Example #1
0
func main() {
	fmt.Println("server starting...")
	flag.Parse()
	config.SetConfig("config", *flag.String("config", "config.xml", "config xml file for start"))
	config.SetConfig("logfilename", *flag.String("logfilename", "/log/logfilename.log", "log file name"))
	config.SetConfig("deamon", *flag.String("deamon", "false", "need run as demo"))
	config.SetConfig("port", *flag.String("port", "8000", "http port "))
	config.SetConfig("log", *flag.String("log", "debug", "logger level "))
	config.SetConfig("loginServerList", *flag.String("loginServerList", "loginServerList.xml", "server list config"))
	config.LoadFromFile(config.GetConfigStr("config"), "global")
	if err := config.LoadFromFile(config.GetConfigStr("config"), "ApkServer"); err != nil {
		fmt.Println(err)
		return
	}
	var err error
	converter, err = iconv.NewCoder(iconv.GBK18030_UTF8_IDX)
	if err != nil {
		fmt.Println("iconv err:", err)
		return
	}
	socketMap = make(map[*websocket.Conn]string)
	gamesMap = make(map[string]int)
	gtdMap = make(map[string]GameTempateData)

	http.Handle("/js/", http.StripPrefix("/js/", http.FileServer(http.Dir("./js/"))))
	//http.Handle("/", http.FileServer(http.Dir(".")))
	InitGames()
	//err := http.ListenAndServe("180.168.197.87:18080", nil)
	err = http.ListenAndServe(config.GetConfigStr("ip")+":"+config.GetConfigStr("port"), nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
	fmt.Println("server stop...")
}
Example #2
0
func main() {
	flag.Parse()
	config.SetConfig("config", *flag.String("config", "config.xml", "config xml file for start"))
	config.SetConfig("logfilename", *flag.String("logfilename", "/log/logfilename.log", "log file name"))
	config.SetConfig("deamon", *flag.String("deamon", "false", "need run as demo"))
	config.SetConfig("port", *flag.String("port", "8000", "http port "))
	config.SetConfig("log", *flag.String("log", "debug", "logger level "))
	config.LoadFromFile(config.GetConfigStr("config"), "global")
	if err := config.LoadFromFile(config.GetConfigStr("config"), "MobileLogServer"); err != nil {
		fmt.Println(err)
		return
	}
	monitorMap = make(map[*websocket.Conn]*websocket.Conn)
	logger, err := logging.NewTimeRotationHandler(config.GetConfigStr("logfilename"), "060102-15")
	if err != nil {
		fmt.Println(err)
		return
	}
	logger.SetLevel(logging.DEBUG)
	logging.AddHandler("MLOG", logger)
	logging.Info("server startin...")
	http.Handle("/ws", websocket.Handler(MonitorServer))
	http.Handle("/", http.FileServer(http.Dir(".")))
	http.HandleFunc("/log/fxsj", LogServer)
	http.HandleFunc("/log/hxsg", LogServer)
	http.HandleFunc("/testlua", testLua)
	http.HandleFunc("/log/sbjs", LogServer)
	err = http.ListenAndServe(config.GetConfigStr("ip")+":"+config.GetConfigStr("port"), nil)
	if err != nil {
		fmt.Println(err)
		logging.Error("ListenAndServe:%s", err.Error())
	}
	logging.Info("server stop...")
}
Example #3
0
func main() {
	if err := config.LoadFromFile("loginServerList.xml", "GMServerList"); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(config.GetConfigStr("ip") + ":" + config.GetConfigStr("port"))
	raddr, _ := net.ResolveTCPAddr("tcp", config.GetConfigStr("ip")+":"+config.GetConfigStr("port"))
	conn, err := net.DialTCP("tcp", nil, raddr)
	if err != nil {
		fmt.Println("conn err:", err)
		return
	}
	fmt.Println("new connection:", conn.RemoteAddr())
	task := gotcp.NewTask(conn, "Client")
	task.SetHandleReadFun(base.HandleReadFunBw)
	task.SetHandleWriteFun(base.HandleWriteFunBw)
	task.SetHandleParseFun(base.HandleParseBw)
	task.SetHandleHeartBteaFun(base.HandleHeartBeatRequestBw, time.Second*10)
	task.SetHandleMessage(&gmHandleMessageMap)
	cmd := Cmd.NewStRequestLoginGmUserCmd()
	cmd.Version = 2012102901
	for i, b := range "webmaster" {
		cmd.Name[i] = byte(b)
	}
	for i, b := range "TZ95d5KV" {
		cmd.Password[i] = byte(b)
	}
	//buf := bytes.NewBuffer(cmd.name)
	//buf.WriteString("webmaster")
	//buf = bytes.NewBuffer(cmd.password)
	//buf.WriteString("TZ95d5KV")
	task.SendCmd(cmd)
	task.Id = rand.Int63()
	task.Name = conn.RemoteAddr().String()
	task.Start()
	tick := time.Tick(time.Second * 5)
	for {
		select {
		case <-tick:
			//case t := <-tick:
			//	fmt.Println(t)
		}
	}
	time.Sleep(time.Second * 10)
	task.Stop()
}
Example #4
0
func downloadApkFunc(w http.ResponseWriter, r *http.Request) {
	fmt.Println(r.URL.String())
	gameurl := strings.Split(r.URL.String(), "/")[1]
	num, ok := gamesMap[gameurl]
	if !ok {
		w.Write([]byte("不可识别的游戏"))
		defer r.Body.Close()
		return
	}
	if _, err := os.Open(config.GetConfigStr("download_apk"+"_"+gameurl) + "/" + config.GetConfigStr("download_apk"+"_"+gameurl+"_file")); err != nil {
		//w.Write([]byte(config.GetConfigStr("download_apk"+"_"+gameurl) + "/" + config.GetConfigStr("download_apk"+"_"+gameurl+"_file")))
		if num != 0 {
			w.Write([]byte("有人正在做版本,请稍后再试:" + gameurl + ":" + strconv.Itoa(num)))
		} else {
			w.Write([]byte("目前没有版本可以下载,请先做版本:" + gameurl + ":" + strconv.Itoa(num)))
		}
		return
	}
	http.Redirect(w, r, strings.Replace(r.URL.String(), "download_apk", "apk", 1)+"/"+config.GetConfigStr("download_apk"+"_"+gameurl+"_file"), 303)
}
Example #5
0
func InitGames() {
	cfg := config.GetConfig()
	for k, _ := range *(*map[string]string)(cfg) {
		if strings.Contains(k, "mobile_game_") {
			url := strings.Split(k, "_")
			if len(url) == 3 {
				gamesMap[url[2]] = 0
				http.Handle("/"+url[2]+"/ws", websocket.Handler(SetupServer))
				http.HandleFunc("/"+url[2], mobileGameFunc)
				http.HandleFunc("/"+url[2]+"/download_win", downloadWinFunc)
				http.HandleFunc("/"+url[2]+"/download_apk", downloadApkFunc)
				http.Handle("/"+url[2]+"/apk/", http.StripPrefix("/"+url[2]+"/apk/", http.FileServer(http.Dir(config.GetConfigStr("download_apk"+"_"+url[2])))))
				http.Handle("/"+url[2]+"/win/", http.StripPrefix("/"+url[2]+"/win/", http.FileServer(http.Dir(config.GetConfigStr("download_win"+"_"+url[2])))))
				fmt.Println("/"+url[2]+"/win", config.GetConfigStr("download_win"+"_"+url[2]))
				gamedata := GameTempateData{Name: config.GetConfigStr(k + "_name"), UrlName: url[2]}
				gtdMap[url[2]] = gamedata
			}
			//http.Handle("/"+url[2], http.StripPrefix("/"+url[2], http.FileServer(http.Dir(v))))

		}
	}
}
Example #6
0
func SetupServer(ws *websocket.Conn) {
	var message JSONCommand
	//world := createWorld()

	for {
		err := websocket.JSON.Receive(ws, &message)
		if err != nil {
			if v, ok := socketMap[ws]; ok {
				gamesMap[v]--
				Broadcask("0", []byte("退出减少后当前用户数量:"+v+":"+strconv.Itoa(gamesMap[v])), ws)
			}
			delete(socketMap, ws)
			log.Print("Receive error - stopping worker: ", err)
			break
		}
		fmt.Println(message.Id, message.Data)
		if strings.Contains(message.Id, "setup apk") {
			game := message.Data
			if _, ok := socketMap[ws]; !ok {
				if v, ok := gamesMap[game]; ok {
					gamesMap[game]++
					socketMap[ws] = game
					Broadcask("0", []byte("增加后当前用户数量:"+game+":"+strconv.Itoa(gamesMap[game])), ws)
					if v == 0 {
						SendMessage("0", []byte("start setup apk:"+game+" now..."), ws)
						//execBat("publish_android.bat", ws)
						//execBat("copy_apk.bat", ws)
						SendMessage("0", []byte(config.GetConfigStr("bat_apk_"+game)), ws)
						if execBat(config.GetConfigStr("bat_apk_"+game), ws) == true {
							Broadcask("setup finish apk", []byte(game), ws)
						}
						for k, v2 := range socketMap {
							if v2 == game {
								gamesMap[v2]--
								delete(socketMap, k)
								Broadcask("0", []byte("完成减少后当前用户数量:"+v2+":"+strconv.Itoa(gamesMap[v2])), ws)
							}
						}
						for _, v2 := range socketMap {
							if v2 == game {
								//k.Close()
							}
						}
					} else {
						SendMessage("0", []byte("wait setup apk:"+game+" now..."), ws)
					}
				}
			} else {
				SendMessage("0", []byte("重复加入"), ws)
			}
		}
		if strings.Contains(message.Id, "setup win") {
			game := message.Data
			if _, ok := socketMap[ws]; !ok {
				if v, ok := gamesMap[game]; ok {
					gamesMap[game]++
					socketMap[ws] = game
					Broadcask("0", []byte("增加后当前用户数量:"+game+":"+strconv.Itoa(gamesMap[game])), ws)
					if v == 0 {
						SendMessage("0", []byte("start setup win:"+game+" now..."), ws)
						//execBat("publish_android.bat", ws)
						//execBat("copy_apk.bat", ws)
						SendMessage("0", []byte(config.GetConfigStr("bat_apk_"+game)), ws)
						if execBat(config.GetConfigStr("bat_win_"+game), ws) == true {
							Broadcask("setup finish win", []byte(game), ws)
						}
						for k, v2 := range socketMap {
							if v2 == game {
								gamesMap[v2]--
								delete(socketMap, k)
								Broadcask("0", []byte("完成减少后当前用户数量:"+v2+":"+strconv.Itoa(gamesMap[v2])), ws)
							}
						}
						for _, v2 := range socketMap {
							if v2 == game {
								//k.Close()
							}
						}
					} else {
						SendMessage("0", []byte("wait setup win:"+game+" now..."), ws)
					}
				}
			} else {
				SendMessage("0", []byte("重复加入"), ws)
			}
		}
		if strings.Contains(message.Id, "reset huodong") {
			game := message.Data
			if _, ok := socketMap[ws]; !ok {
				if v, ok := gamesMap[game]; ok {
					gamesMap[game]++
					socketMap[ws] = game
					Broadcask("0", []byte("增加后当前用户数量:"+game+":"+strconv.Itoa(gamesMap[game])), ws)
					if v == 0 {
						SendMessage("0", []byte("start reset huodong:"+game+" now..."), ws)
						//execBat("publish_android.bat", ws)
						//execBat("copy_apk.bat", ws)
						SendMessage("0", []byte(config.GetConfigStr("bat_huodong_"+game)), ws)
						if execBat(config.GetConfigStr("bat_huodong_"+game), ws) == true {
							Broadcask("setup finish reset huodong", []byte(game), ws)
						}
						for k, v2 := range socketMap {
							if v2 == game {
								gamesMap[v2]--
								delete(socketMap, k)
								Broadcask("0", []byte("完成减少后当前用户数量:"+v2+":"+strconv.Itoa(gamesMap[v2])), ws)
							}
						}
						for _, v2 := range socketMap {
							if v2 == game {
								//k.Close()
							}
						}
					} else {
						SendMessage("0", []byte("wait reset huodong:"+game+" now..."), ws)
					}
				}
			} else {
				SendMessage("0", []byte("重复加入"), ws)
			}
		}
		if strings.Contains(message.Id, "update resource") {
			game := message.Data
			if _, ok := socketMap[ws]; !ok {
				if v, ok := gamesMap[game]; ok {
					gamesMap[game]++
					socketMap[ws] = game
					Broadcask("0", []byte("增加后当前用户数量:"+game+":"+strconv.Itoa(gamesMap[game])), ws)
					if v == 0 {
						SendMessage("0", []byte("start update resource:"+game+" now..."), ws)
						//execBat("publish_android.bat", ws)
						//execBat("copy_apk.bat", ws)
						SendMessage("0", []byte(config.GetConfigStr("bat_resource_"+game)), ws)
						if execBat(config.GetConfigStr("bat_resource_"+game), ws) == true {
							Broadcask("setup finish update resource", []byte(game), ws)
						}
						for k, v2 := range socketMap {
							if v2 == game {
								gamesMap[v2]--
								delete(socketMap, k)
								Broadcask("0", []byte("完成减少后当前用户数量:"+v2+":"+strconv.Itoa(gamesMap[v2])), ws)
							}
						}
						for _, v2 := range socketMap {
							if v2 == game {
								//k.Close()
							}
						}
					} else {
						SendMessage("0", []byte("wait update resource:"+game+" now..."), ws)
					}
				}
			} else {
				SendMessage("0", []byte("重复加入"), ws)
			}
		}
		if strings.Contains(message.Id, "publish apk") {
			game := message.Data
			if _, ok := socketMap[ws]; !ok {
				if v, ok := gamesMap[game]; ok {
					gamesMap[game]++
					socketMap[ws] = game
					Broadcask("0", []byte("增加后当前用户数量:"+game+":"+strconv.Itoa(gamesMap[game])), ws)
					if v == 0 {
						SendMessage("0", []byte("start publish apk:"+game+" now..."), ws)
						//execBat("publish_android.bat", ws)
						//execBat("copy_apk.bat", ws)
						SendMessage("0", []byte(config.GetConfigStr("bat_publish_apk_"+game)), ws)
						if execBat(config.GetConfigStr("bat_publish_apk_"+game), ws) == true {
							Broadcask("setup finish publish apk", []byte(game), ws)
						}
						for k, v2 := range socketMap {
							if v2 == game {
								gamesMap[v2]--
								delete(socketMap, k)
								Broadcask("0", []byte("完成减少后当前用户数量:"+v2+":"+strconv.Itoa(gamesMap[v2])), ws)
							}
						}
						for _, v2 := range socketMap {
							if v2 == game {
								//k.Close()
							}
						}
					} else {
						SendMessage("0", []byte("wait publish apk:"+game+" now..."), ws)
					}
				}
			} else {
				SendMessage("0", []byte("重复加入"), ws)
			}
		}
		if strings.Contains(message.Id, "publish patch") {
			game := message.Data
			if _, ok := socketMap[ws]; !ok {
				if v, ok := gamesMap[game]; ok {
					gamesMap[game]++
					socketMap[ws] = game
					Broadcask("0", []byte("增加后当前用户数量:"+game+":"+strconv.Itoa(gamesMap[game])), ws)
					if v == 0 {
						SendMessage("0", []byte("start publish apk:"+game+" now..."), ws)
						//execBat("publish_android.bat", ws)
						//execBat("copy_apk.bat", ws)
						SendMessage("0", []byte(config.GetConfigStr("bat_publish_patch_"+game)), ws)
						if execBat(config.GetConfigStr("bat_publish_patch_"+game), ws) == true {
							Broadcask("setup finish publish apk", []byte(game), ws)
						}
						for k, v2 := range socketMap {
							if v2 == game {
								gamesMap[v2]--
								delete(socketMap, k)
								Broadcask("0", []byte("完成减少后当前用户数量:"+v2+":"+strconv.Itoa(gamesMap[v2])), ws)
							}
						}
						for _, v2 := range socketMap {
							if v2 == game {
								//k.Close()
							}
						}
					} else {
						SendMessage("0", []byte("wait publish patch:"+game+" now..."), ws)
					}
				}
			} else {
				SendMessage("0", []byte("重复加入"), ws)
			}
		}
		if strings.Contains(message.Id, "restart server") {
			game := message.Data
			if _, ok := socketMap[ws]; !ok {
				if v, ok := gamesMap[game]; ok {
					gamesMap[game]++
					socketMap[ws] = game
					Broadcask("0", []byte("增加后当前用户数量:"+game+":"+strconv.Itoa(gamesMap[game])), ws)
					if v == 0 {
						SendMessage("0", []byte("start restart server:"+game+" now..."), ws)
						//execBat("publish_android.bat", ws)
						//execBat("copy_apk.bat", ws)
						SendMessage("0", []byte(config.GetConfigStr("bat_restart_"+game)), ws)
						if execBat(config.GetConfigStr("bat_restart_"+game), ws) == true {
							Broadcask("finish restart server", []byte(game), ws)
						}
						for k, v2 := range socketMap {
							if v2 == game {
								gamesMap[v2]--
								delete(socketMap, k)
								Broadcask("0", []byte("完成减少后当前用户数量:"+v2+":"+strconv.Itoa(gamesMap[v2])), ws)
							}
						}
						for _, v2 := range socketMap {
							if v2 == game {
								//k.Close()
							}
						}
					} else {
						SendMessage("0", []byte("wait restart server:"+game+" now..."), ws)
					}
				}
			} else {
				SendMessage("0", []byte("重复加入"), ws)
			}
		}

		//err = websocket.JSON.Send(ws, world.Update(message))
		//if err != nil {
		//	log.Print("Send error - stopping worker: ", err)
		//	break
		//}
	}
}
Example #7
0
func main() {
	flag.Parse()
	config.SetConfig("config", *flag.String("config", "config.xml", "config xml file for start"))
	config.SetConfig("logfilename", *flag.String("logfilename", "/log/logfilename.log", "log file name"))
	config.SetConfig("deamon", *flag.String("deamon", "false", "need run as demo"))
	config.SetConfig("port", *flag.String("port", "8000", "http port "))
	config.SetConfig("log", *flag.String("log", "debug", "logger level "))
	config.LoadFromFile(config.GetConfigStr("config"), "global")
	if err := config.LoadFromFile(config.GetConfigStr("config"), "SdoOnlineServer"); err != nil {
		fmt.Println(err)
		return
	}
	logger, err := logging.NewTimeRotationHandler(config.GetConfigStr("logfilename"), "060102-15")
	if err != nil {
		fmt.Println(err)
		return
	}
	logger.SetLevel(logging.DEBUG)
	logging.AddHandler("SDO", logger)
	mysqlurl := config.GetConfigStr("mysql")
	if ok, err := regexp.MatchString("^mysql://.*:.*@.*/.*$", mysqlurl); ok == false || err != nil {
		logging.Error("mysql config syntax err:%s", mysqlurl)
		return
	}
	logging.Info("server starting...")
	mysqlurl = strings.Replace(mysqlurl, "mysql://", "", 1)
	mysqlurl = strings.Replace(mysqlurl, "@", ":", 1)
	mysqlurl = strings.Replace(mysqlurl, "/", ":", 1)
	mysqlurls := strings.Split(mysqlurl, ":")
	config.SetConfig("dbname", mysqlurls[4])
	db_login = mysql.New("tcp", "", mysqlurls[2]+":"+mysqlurls[3], mysqlurls[0], mysqlurls[1], mysqlurls[4])
	if err != nil {
		logging.Error("db connect error:%s", err.Error())
		return
	}
	mysqlurl = config.GetConfigStr("mysql_monitor")
	if ok, err := regexp.MatchString("^mysql://.*:.*@.*/.*$", mysqlurl); ok == false || err != nil {
		logging.Error("mysql config syntax err:%s", mysqlurl)
		return
	}
	mysqlurl = strings.Replace(mysqlurl, "mysql://", "", 1)
	mysqlurl = strings.Replace(mysqlurl, "@", ":", 1)
	mysqlurl = strings.Replace(mysqlurl, "/", ":", 1)
	mysqlurls = strings.Split(mysqlurl, ":")
	config.SetConfig("dbname", mysqlurls[4])
	db_monitor = mysql.New("tcp", "", mysqlurls[2]+":"+mysqlurls[3], mysqlurls[0], mysqlurls[1], mysqlurls[4])
	if err != nil {
		logging.Error("db connect error:%s", err.Error())
		return
	}
	mysqlurl = config.GetConfigStr("mysql_account")
	if ok, err := regexp.MatchString("^mysql://.*:.*@.*/.*$", mysqlurl); ok == false || err != nil {
		logging.Error("mysql config syntax err:%s", mysqlurl)
		return
	}
	mysqlurl = strings.Replace(mysqlurl, "mysql://", "", 1)
	mysqlurl = strings.Replace(mysqlurl, "@", ":", 1)
	mysqlurl = strings.Replace(mysqlurl, "/", ":", 1)
	mysqlurls = strings.Split(mysqlurl, ":")
	config.SetConfig("dbname", mysqlurls[4])
	db_account = mysql.New("tcp", "", mysqlurls[2]+":"+mysqlurls[3], mysqlurls[0], mysqlurls[1], mysqlurls[4])
	if err != nil {
		logging.Error("db connect error:%s", err.Error())
		return
	}
	http.HandleFunc("/online", OnlineServer)
	http.HandleFunc("/online/country", OnlineCountryServer)
	http.HandleFunc("/gjia/maxlevel", HandleMaxlevel)
	http.HandleFunc("/gjia/isprecreate", HandleIsPrecreate)
	http.HandleFunc("/gjia/isonline", HandleIsOnline)
	http.HandleFunc("/card/gonghuiuser", HandleGonghuiUserCard)
	http.HandleFunc("/card/gonghuikey", HandleGonghuiKeyCard)
	err = http.ListenAndServe(":"+config.GetConfigStr("port"), nil)
	if err != nil {
		logging.Error("ListenAndServe:%s", err.Error())
	}
	logging.Info("server stop...")
}