Beispiel #1
0
func (self *FServerConnMgr) Init(connnector *server.Server, cfg config.SvrConfig) {

	fsCount := len(cfg.FsHost)
	self.Open(uint8(fsCount))

	for i := 0; i < fsCount; i++ {
		go func() {

			defer func() {
				if r := recover(); r != nil {
					fmt.Printf("FServerConnMgr runtime error:", r)

					debug.PrintStack()
				}
			}()

			connId := self.GetNewConnId()
			host := cfg.FsHost[connId]

			for {
				select {
				case <-self.quit:
					{
						logger.Info("FServerConnMgr Goroutine Quit ----->")
						return
					}
				default:
					{
						var err error
						var fsConn net.Conn

						for {
							fsConn, err = net.Dial("tcp", host)
							if err != nil {
								//logger.Fatal("Connect FightServer Error :%s", err.Error())
							} else {
								break
							}
						}

						logger.Info("Connect to FightServer : %s ok!!!!", host)

						fsRpcConn := server.NewTCPSocketConn(connnector, fsConn, 1000, 0, 1)
						fsRpcConn.SetResultServer("FightServer")
						self.NewConn(fsRpcConn, connId)

						connnector.ServeConn(fsRpcConn)
					}
				}
			}
		}()

	}
}
Beispiel #2
0
func StartServices(cfg *config.SvrConfig, id *uint64) *FightServer {
	fightServer = &FightServer{}

	fightServer = &FightServer{
		rpcServer: server.NewServer(),
		battles:   make(map[string]*Battle),
	}
	fightServer.rpcServer.Register(fightServer)

	fightServer.rpcServer.RegCallBackOnConn(
		func(conn server.RpcConn) {
			fightServer.onConn(conn)
		},
	)

	listener, err := net.Listen("tcp", cfg.FsHost[*id])
	if err != nil {
		logger.Fatal("net.Listen: %s", err.Error())
	}

	fightServer.id = *id

	go func() {
		for {
			//For Client/////////////////////////////
			time.Sleep(time.Millisecond * 5)
			conn, err := listener.Accept()
			if err != nil {
				logger.Error("fightserver StartServices %s", err.Error())
				break
			}
			go func() {
				rpcConn := server.NewTCPSocketConn(fightServer.rpcServer, conn, 1000, 0, 1)
				rpcConn.SetResultServer("Connector")

				defer func() {
					if r := recover(); r != nil {
						logger.Error("player rpc runtime error begin:", r)
						debug.PrintStack()
						rpcConn.Close()

						logger.Error("player rpc runtime error end ")
					}
				}()

				fightServer.rpcServer.ServeConn(rpcConn)
			}()
		}
	}()

	return fightServer
}
Beispiel #3
0
func wsServeConnHandler(w http.ResponseWriter, r *http.Request) {

	err := r.ParseForm()
	if err != nil {
		fmt.Fprintln(w, "rpc: error")
		return
	}

	logger.Debug("wsServeConnHandler : %v", r.FormValue("method"))

	conn, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		logger.Info("Upgrade:", err.Error())
		conn, bufrw, err := w.(http.Hijacker).Hijack()
		if err != nil {
			logger.Debug("rpc hijacking %v : %v", r.RemoteAddr, err.Error())
			return
		} else {

			httpConn := server.NewTCPSocketConn(pConnector.rpcServer, conn, 1, 1, 1)
			logger.Debug("rpc hijacking %v : %v", r.RemoteAddr, r.FormValue("method"))

			fmt.Fprintln(w, "rpc: hello")

			buf := make([]byte, 10)
			for i := 0; i < len(buf); i++ {
				buf[i] = byte(i)
			}

			_, err = bufrw.Write(buf)
			if err == nil {
				err = bufrw.Flush()
			}
			if err != nil {
				fmt.Printf("ResponseError: %s\\n", err)
			} else {
				fmt.Println("Bye, Jack!")
			}

			logger.Debug("httpConn WriteObj %v", httpConn.GetRemoteIp())
		}
		return
	}

	rpcConn := server.NewWebSocketConn(pConnector.rpcServer, *conn, 128, 45, 2)
	defer func() {
		rpcConn.Close() // 客户端退出减去计数
	}()

	pConnector.rpcServer.ServeConn(rpcConn)
}
Beispiel #4
0
func CreateConnectorServerForClient(cfg config.SvrConfig) *Connector {

	db.Init()

	var authCfg config.AuthConfig
	if err := config.ReadConfig("etc/authserver.json", &authCfg); err != nil {
		logger.Fatal("load config failed, error is: %v", err)
	}
	authConn, err := net.Dial("tcp", authCfg.AuthHost)
	if err != nil {
		logger.Fatal("connect logserver failed %s", err.Error())
	}

	var gsCfg config.LoginConfig
	if err = config.ReadConfig("etc/loginserver.json", &gsCfg); err != nil {
		logger.Fatal("load config failed, error is: %v", err)
	}
	gsConn, err := net.Dial("tcp", gsCfg.LoginHost)
	if err != nil {
		logger.Fatal("%s", err.Error())
	}

	pConnector = &Connector{
		m:           make(map[uint32]serverInfo),
		authserver:  rpc.NewClient(authConn),
		loginserver: rpc.NewClient(gsConn),
		rpcServer:   server.NewServer(),
		players:     make(map[uint64]*Player),
		playersbyid: make(map[string]*Player),
	}

	//初始化cache
	logger.Info("Init Cache %v", authCfg.MainCacheProfile)
	pConnector.maincache = db.NewCachePool(authCfg.MainCacheProfile)

	pConnector.rpcServer.ApplyProtocol(protobuf.CS_Protocol_value)
	pConnector.rpcServer.Register(pConnector)

	pConnector.rpcServer.RegCallBackOnConn(
		func(conn server.RpcConn) {
			pConnector.onConn(conn)
		},
	)

	pConnector.rpcServer.RegCallBackOnDisConn(
		func(conn server.RpcConn) {
			pConnector.onDisConn(conn)
		},
	)

	pConnector.rpcServer.RegCallBackOnCallBefore(
		func(conn server.RpcConn) {
			conn.Lock()
		},
	)

	pConnector.rpcServer.RegCallBackOnCallAfter(
		func(conn server.RpcConn) {
			conn.Unlock()
		},
	)

	//开始对fightserver的RPC服务
	pConnector.FsMgr.Init(pConnector.rpcServer, cfg)

	listener, err := net.Listen("tcp", cfg.TcpHost)
	if err != nil {
		logger.Fatal("net.Listen: %s", err.Error())
	}

	pConnector.id = cfg.ServerID
	pConnector.listenTcpIp = cfg.TcpHost
	pConnector.listenHttpIp = cfg.HttpHost

	pConnector.sendPlayerCountToGateServer()

	go func() {
		for {
			//For Client/////////////////////////////
			time.Sleep(time.Millisecond * 5)
			conn, err := listener.Accept()

			if err != nil {
				logger.Error("cns StartServices %s", err.Error())
				break
			}

			go func() {
				rpcConn := server.NewTCPSocketConn(pConnector.rpcServer, conn, 128, 45, 1)
				defer func() {
					if r := recover(); r != nil {
						logger.Error("player rpc runtime error begin:", r)
						debug.PrintStack()
						rpcConn.Close()

						logger.Error("player rpc runtime error end ")
					}
				}()

				pConnector.rpcServer.ServeConn(rpcConn)
			}()
		}
	}()

	http.HandleFunc("/", wsServeConnHandler)
	http.ListenAndServe(cfg.HttpHost, nil)

	return pConnector
}
Beispiel #5
0
func testLogin() {

	var cfg config.LoginConfig
	if err := config.ReadConfig(*gateConfigFile, &cfg); err != nil {
		logger.Fatal("load config failed, error is: %v", err)
		return
	}

	conn, err := net.Dial("tcp", cfg.TcpHostForClient)
	if err != nil {
		logger.Fatal("%s", err.Error())
	}

	rpcConn := server.NewTCPSocketConn(nil, conn, 4, 0, 1)

	login := &protobuf.CL_CheckAccount{}
	login.SetAccount("account")
	login.SetPassword("password")
	rpcConn.Call("LoginServer.CL_CheckAccount", login)

	rst := new(server.RequestWrap)
	err = rpcConn.ReadRequest(&rst.Request)

	// argv guaranteed to be a pointer now.
	argv := reflect.New(reflect.TypeOf(protobuf.LC_CheckAccountResult{}))
	rpcConn.GetRequestBody(&rst.Request, argv.Interface())

	info := argv.Interface().(*protobuf.LC_CheckAccountResult)

	logger.Info("LoginServer Info : %v", argv.Interface())
	logger.Info("                 %v", &rst.Request)
	logger.Info("                 %v", info.GetSessionKey())

	rpcConn.Close()

	time.Sleep(time.Millisecond * 1000)

	conn, err = net.Dial("tcp", info.GetGameServerIp())
	if err != nil {
		logger.Fatal("%s", err.Error())
	}

	rpcConn = server.NewTCPSocketConn(nil, conn, 4, 0, 1)

	check := &protobuf.CS_CheckSession{}
	check.SetUid(info.GetUid())
	check.SetSessionKey(info.GetSessionKey())
	check.SetTimestamp(uint32(time.Now().Unix()))
	rpcConn.Call("GameServer.CS_CheckSession", check)

	rst = new(server.RequestWrap)
	err = rpcConn.ReadRequest(&rst.Request)

	// argv guaranteed to be a pointer now.
	argv = reflect.New(reflect.TypeOf(protobuf.SC_CheckSessionResult{}))
	rpcConn.GetRequestBody(&rst.Request, argv.Interface())
	logger.Info("GameServer.CS_CheckSession : %v", argv.Interface())
	logger.Info("                 %v", &rst.Request)

	for i := 0; i < 100; i++ {
		time.Sleep(time.Millisecond * 1000)
		req := &protobuf.CS_Ping{}
		rpcConn.Call("GameServer.CS_Ping", req)

		rst = new(server.RequestWrap)
		err = rpcConn.ReadRequest(&rst.Request)

		// argv guaranteed to be a pointer now.
		argv = reflect.New(reflect.TypeOf(protobuf.SC_PingResult{}))
		rpcConn.GetRequestBody(&rst.Request, argv.Interface())
		logger.Info("GameServer.CS_Ping : %v", argv.Interface())
		logger.Info("                 %v", &rst.Request)
	}

	rpcConn.Close()
}