func main() { defer func() { if x := recover(); x != nil { ERR("caught panic in main()", x) } }() // 获取监听端口 getPort() //启动 global.Startup(global.ServerName, "world_log", nil) //连接Redis redisProxyErr := redisProxy.InitClient(cfg.GetValue("redis_ip"), cfg.GetValue("redis_port"), cfg.GetValue("redis_pwd")) checkError(redisProxyErr) //连接LogServer logProxyErr := logProxy.InitClient(cfg.GetValue("log_ip"), cfg.GetValue("log_port")) checkError(logProxyErr) //启动WorldServer worldProxyErr := worldProxy.InitServer(world_port) checkError(worldProxyErr) //开启下线用户处理 module.User.StartDealOfflineUser() //保持进程 global.Run() }
func startLocalServer() { //连接DB db.Init() //连接Redis redisProxyErr := redisProxy.InitClient(cfg.GetValue("redis_ip"), cfg.GetValue("redis_port")) checkError(redisProxyErr) listener, err := link.Serve("tcp", "0.0.0.0:"+local_port, packet.New( binary.SplitByUint32BE, 1024, 1024, 1024, )) checkError(err) listener.Serve(func(session *link.Session) { session.AddCloseCallback(session, func() { session.Close() }) global.AddSession(session) var msg packet.RAW for { if err := session.Receive(&msg); err != nil { break } module.ReceiveMessage(session, msg) } }) }
func getPort() { //端口号 gateway_ip = cfg.GetValue("gateway_ip") gateway_port = cfg.GetValue("gateway_port") global.ServerName = "GateServer[" + gateway_port + "]" transfer_ip = cfg.GetValue("transfer_ip") transfer_port = cfg.GetValue("transfer_port") }
func getPort() { var s int flag.IntVar(&s, "s", 0, "tcp listen port") flag.Parse() if s == 0 { ERR("please set gameserver port") os.Exit(-1) } game_ip = cfg.GetValue("game_ip_" + strconv.Itoa(s)) game_port = cfg.GetValue("game_port_" + strconv.Itoa(s)) global.ServerName = "GameServer[" + game_port + "]" }
func main() { defer func() { if x := recover(); x != nil { ERR("caught panic in main()", x) } }() // 获取监听端口 getPort() //启动 global.Startup(global.ServerName, "game_log", nil) //连接TransferServer err := transferProxy.InitClient(cfg.GetValue("transfer_ip"), cfg.GetValue("transfer_port")) checkError(err) //连接WorldServer worldProxyErr := worldProxy.InitClient(cfg.GetValue("world_ip"), cfg.GetValue("world_port")) checkError(worldProxyErr) //连接Redis redisProxyErr := redisProxy.InitClient(cfg.GetValue("redis_ip"), cfg.GetValue("redis_port"), cfg.GetValue("redis_pwd")) checkError(redisProxyErr) //连接LogServer logProxyErr := logProxy.InitClient(cfg.GetValue("log_ip"), cfg.GetValue("log_port")) checkError(logProxyErr) //保持进程 global.Run() }
func init() { if cfg.GetValue("debug") == "true" { debug_open = true } logs = make(map[string]int64) }
func startLocalServer() { //连接Redis redisProxyErr := redisProxy.InitClient(cfg.GetValue("redis_ip"), cfg.GetValue("redis_port"), cfg.GetValue("redis_pwd")) checkError(redisProxyErr) //开启DB db.Init() //开启同步DB数据到数据库 dbProxy.StartSysDB() //开启客户端监听 addr := "0.0.0.0:" + local_port err := global.Listener("tcp", addr, global.PackCodecType_UnSafe, func(session *link.Session) { global.AddSession(session) }, gameProxy.MsgDispatch, ) checkError(err) }
func main() { defer func() { if x := recover(); x != nil { ERR("caught panic in main()", x) } }() // 获取监听端口 getPort() //启动 global.Startup(global.ServerName, "login_log", nil) //连接TransferServer err := transferProxy.InitClient(cfg.GetValue("transfer_ip"), cfg.GetValue("transfer_port")) checkError(err) //连接DB dbProxyErr := dbProxy.InitClient(cfg.GetValue("db_ip"), cfg.GetValue("db_port")) checkError(dbProxyErr) //连接Redis redisProxyErr := redisProxy.InitClient(cfg.GetValue("redis_ip"), cfg.GetValue("redis_port")) checkError(redisProxyErr) //保持进程 global.Run() }
//数据库初始化 func Init() { hostname := cfg.GetValue("mysql_hostname") port := cfg.GetValue("mysql_port") username := cfg.GetValue("mysql_username") password := cfg.GetValue("mysql_password") database := cfg.GetValue("mysql_database") charset := cfg.GetValue("mysql_charset") sqlDb, err := sql.Open("mysql", username+":"+password+"@tcp("+hostname+":"+port+")/"+database+"?charset="+charset) if err != nil { ERR("Sql Open Error", err) } sqlDb.SetMaxOpenConns(5000) sqlDb.SetMaxIdleConns(1000) err = sqlDb.Ping() if err != nil { ERR("Sql Ping Error", err) } DBOrm = new(Model) DBOrm.db = sqlDb INFO("DB Open Success") }
func main() { defer func() { if x := recover(); x != nil { ERR("caught panic in main()", x) } }() // 获取端口号 getPort() //启动 global.Startup(global.ServerName, "gateway_log", nil) //连接TransferServer err := transferProxy.InitClient(cfg.GetValue("transfer_ip"), cfg.GetValue("transfer_port")) checkError(err) //开启GateServer监听 startGateway() //保持进程 global.Run() }
func main() { defer func() { if x := recover(); x != nil { ERR("caught panic in main()", x) } }() // 获取监听端口 getPort() //启动 global.Startup(global.ServerName, "db_log", stopDBServer) //连接Redis redisProxyErr := redisProxy.InitClient(cfg.GetValue("redis_ip"), cfg.GetValue("redis_port"), cfg.GetValue("redis_pwd")) checkError(redisProxyErr) //开启DBServer监听 err := dbProxy.InitServer(db_port) checkError(err) //保持进程 global.Run() }
func getPort() { db_ip = cfg.GetValue("db_ip") db_port = cfg.GetValue("db_port") global.ServerName = "DBServer[" + db_port + "]" }
func getPort() { //端口号 local_ip = cfg.GetValue("local_ip") local_port = cfg.GetValue("local_port") global.ServerName = "LocalServer[" + local_port + "]" }
func getPort() { login_ip = cfg.GetValue("login_ip") login_port = cfg.GetValue("login_port") global.ServerName = "LoginServer[" + login_port + "]" }
func getPort() { transfer_ip = cfg.GetValue("transfer_ip") transfer_port = cfg.GetValue("transfer_port") global.ServerName = "TransferServer[" + transfer_port + "]" }
func getPort() { world_ip = cfg.GetValue("world_ip") world_port = cfg.GetValue("world_port") global.ServerName = "WorldServer[" + world_port + "]" }
func init() { if cfg.GetValue("debug") == "true" { debug_open = true } }
func Test_gateway(t *testing.T) { DEBUG("消息通信测试") var wg sync.WaitGroup var successNum uint32 for i := 0; i < 3000; i++ { wg.Add(1) go func(flag int) { // if flag != 0 && RandomInt31n(100) < 50 { // flag -= 1 // } defer wg.Done() var count uint32 = 0 var userName string = "User" + strconv.Itoa(flag) //超时处理 timerFunc := func() { ERR("失败:", userName, count) } var timer *time.Timer = time.AfterFunc(10*time.Second, timerFunc) //连接服务器 client, err := link.ConnectTimeout("tcp", "0.0.0.0:"+cfg.GetValue("gateway_port"), time.Second*3, protocol) if !unitest.NotError(t, err) { return } defer client.Close() count += 1 //接收服务器连接成功消息 var revMsg packet.RAW // err = client.Receive(&revMsg) // if !unitest.NotError(t, err) { // return // } // msg := &gameProto.ConnectSuccessS2C{} // proto.Unmarshal(revMsg[2:], msg) // DEBUG(binary.GetUint16LE(revMsg[:2]), msg) count += 1 //发送登录消息 send_msg := createLoginBytes(userName) // DEBUG("发送数据:", flag, send_msg) err = client.Send(send_msg) if !unitest.NotError(t, err) { return } count += 1 //接受登录成功消息 err = client.Receive(&revMsg) if !unitest.NotError(t, err) { return } // DEBUG(binary.GetUint16LE(revMsg[:2])) msg1 := &gameProto.UserLoginS2C{} proto.Unmarshal(revMsg[2:], msg1) count += 1 if !unitest.Pass(t, msg1.GetUserID() > 0) { return } count += 1 //发送获取用户信息消息 if msg1.GetUserID() != 0 { err = client.Send(createGetUserInfoBytes(msg1.GetUserID())) if !unitest.NotError(t, err) { return } count += 1 //接受用户信息消息 err = client.Receive(&revMsg) if !unitest.NotError(t, err) { return } count += 1 if binary.GetUint16LE(revMsg[:2]) == gameProto.ID_ErrorMsgS2C { msg2 := &gameProto.ErrorMsgS2C{} proto.Unmarshal(revMsg[2:], msg2) // DEBUG(binary.GetUint16LE(revMsg[:2]), msg2) } else { msg2 := &gameProto.GetUserInfoS2C{} proto.Unmarshal(revMsg[2:], msg2) // DEBUG(binary.GetUint16LE(revMsg[:2]), msg2) successNum += 1 DEBUG("成功:", userName, msg1.GetUserID(), successNum) } } timer.Stop() }(i) } wg.Wait() }